Skip to content

Commit

Permalink
Fix motion correction and CNMF for 3D movies (#300)
Browse files Browse the repository at this point in the history
* Fix failure to compute correlation image for 3D data (pending caiman update)

* Fix by passing file as string and re-run without baseline if that fails

* Make local_correlations in cnmf compatible with 3D data

* Add z shifts for pw_rigid case

* Pin caiman version instead of catching error from old version
  • Loading branch information
ethanbb authored Aug 22, 2024
1 parent 6f46b98 commit c42c035
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
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:
- caiman >= 1.9.10
- caiman >= 1.11.2
- pandas >= 1.5.0
- requests
- click
Expand Down
2 changes: 1 addition & 1 deletion environment_rtd.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
channels:
- conda-forge
dependencies:
- caiman >= 1.9.10
- caiman >= 1.11.2
- pandas >= 1.5.0
- requests
- click
Expand Down
2 changes: 1 addition & 1 deletion mesmerize_core/algorithms/cnmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def run_algo(batch_path, uuid, data_path: str = None):

cnm.save(str(output_path))

Cn = cm.local_correlations(images.transpose(1, 2, 0))
Cn = cm.local_correlations(images, swap_dim=False)
Cn[np.isnan(Cn)] = 0

corr_img_path = output_dir.joinpath(f"{uuid}_cn.npy").resolve()
Expand Down
13 changes: 8 additions & 5 deletions mesmerize_core/algorithms/mcorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def run_algo(batch_path, uuid, data_path: str = None):

print("Computing correlation image")
Cns = local_correlations_movie_offline(
[str(mcorr_memmap_path)],
str(mcorr_memmap_path),
remove_baseline=True,
window=1000,
stride=1000,
Expand All @@ -102,24 +102,27 @@ def run_algo(batch_path, uuid, data_path: str = None):
Cn[np.isnan(Cn)] = 0
cn_path = output_dir.joinpath(f"{uuid}_cn.npy")
np.save(str(cn_path), Cn, allow_pickle=False)

# output dict for pandas series for dataframe row
d = dict()


print("finished computing correlation image")


# Compute shifts
if opts.motion["pw_rigid"] == True:
x_shifts = mc.x_shifts_els
y_shifts = mc.y_shifts_els
shifts = [x_shifts, y_shifts]
if hasattr(mc, 'z_shifts_els'):
shifts += mc.z_shifts_els
shift_path = output_dir.joinpath(f"{uuid}_shifts.npy")
np.save(str(shift_path), shifts)
else:
shifts = mc.shifts_rig
shift_path = output_dir.joinpath(f"{uuid}_shifts.npy")
np.save(str(shift_path), shifts)

# output dict for pandas series for dataframe row
d = dict()

# save paths as relative path strings with forward slashes
cn_path = str(PurePosixPath(cn_path.relative_to(output_dir.parent)))
mcorr_memmap_path = str(PurePosixPath(mcorr_memmap_path.relative_to(output_dir.parent)))
Expand Down

0 comments on commit c42c035

Please sign in to comment.