Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix subidx to compute motion correction template from a movie slice #1

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions caiman/motion_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(self, fname, min_mov=None, dview=None, max_shifts=(6, 6), niter_rig
strides=(96, 96), overlaps=(32, 32), splits_els=14, num_splits_to_process_els=None,
upsample_factor_grid=4, max_deviation_rigid=3, shifts_opencv=True, nonneg_movie=True, gSig_filt=None,
use_cuda=False, border_nan=True, pw_rigid=False, num_frames_split=80, var_name_hdf5='mov',is3D=False,
indices=(slice(None), slice(None))):
indices=(slice(None), slice(None)), subidx=slice(None, None, 1)):
"""
Constructor class for motion correction operations

Expand Down Expand Up @@ -213,6 +213,7 @@ def __init__(self, fname, min_mov=None, dview=None, max_shifts=(6, 6), niter_rig
self.var_name_hdf5 = var_name_hdf5
self.is3D = bool(is3D)
self.indices = indices
self.subidx = subidx
if self.use_cuda and not HAS_CUDA:
logging.debug("pycuda is unavailable. Falling back to default FFT.")

Expand Down Expand Up @@ -317,7 +318,8 @@ def motion_correct_rigid(self, template=None, save_movie=False) -> None:
border_nan=self.border_nan,
var_name_hdf5=self.var_name_hdf5,
is3D=self.is3D,
indices=self.indices)
indices=self.indices,
subidx=self.subidx)
if template is None:
self.total_template_rig = _total_template_rig

Expand Down Expand Up @@ -2830,7 +2832,9 @@ def motion_correct_batch_rigid(fname, max_shifts, dview=None, splits=56, num_spl
Exception 'The movie contains nans. Nans are not allowed!'

"""

if subidx.start is not None:
print(f"Computing template from frames {subidx}.")

dims, T = cm.source_extraction.cnmf.utilities.get_file_size(fname, var_name_hdf5=var_name_hdf5)
Ts = np.arange(T)[subidx].shape[0]
step = Ts // 10 if is3D else Ts // 50
Expand Down Expand Up @@ -2863,6 +2867,8 @@ def motion_correct_batch_rigid(fname, max_shifts, dview=None, splits=56, num_spl
m = m.copy()
template = caiman.motion_correction.bin_median(
m.motion_correct(max_shifts[1], max_shifts[0], template=None)[0])
else:
template = high_pass_filter_space(template, gSig_filt)

new_templ = template
if add_to_movie is None:
Expand Down Expand Up @@ -2892,10 +2898,10 @@ def motion_correct_batch_rigid(fname, max_shifts, dview=None, splits=56, num_spl

fname_tot_rig, res_rig = motion_correction_piecewise(fname, splits, strides=None, overlaps=None,
add_to_movie=add_to_movie, template=old_templ, max_shifts=max_shifts, max_deviation_rigid=0,
dview=dview, save_movie=save_movie, base_name=base_name, subidx = subidx,
dview=dview, save_movie=save_movie, base_name=base_name,
num_splits=num_splits_to_process, shifts_opencv=shifts_opencv, nonneg_movie=nonneg_movie, gSig_filt=gSig_filt,
use_cuda=use_cuda, border_nan=border_nan, var_name_hdf5=var_name_hdf5, is3D=is3D,
indices=indices)
indices=indices, subidx=None)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI subidx=None by default in motion_correction_piecewise, but no harm in stating that explicitly here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and I don't know why they had subidx=subidx in the function call, as it tells motion correction to run just on the specified slice (that's what was making subidx dual function, one the one hand to constrict frames from which to compute the template, and on the other hand by computing motion correction on this only). Writing this I think they maybe never had the use case of wanting to apply a template on a superset of frames, if it makes sense! So maybe they just put that here to say "if I have a movie and just want to do MC from frame A to frame B, I can do so" and it impacted both template computation and MC itself.

if is3D:
new_templ = np.nanmedian(np.stack([r[-1] for r in res_rig]), 0)
else:
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
channels:
- conda-forge
dependencies:
- python >=3.10
- python =3.9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great! we should add specific versions for all dependencies as we make improvements (e.g. I know some older versions of numpy don't work)

- bokeh
- coverage
- cython
Expand Down