Skip to content

Commit 6b17fb5

Browse files
authored
Merge pull request #22 from nel-lab/caiman-tempdir-mcorr
fixes necessary for these changes have been merged upstream and have been released on `conda-forge` flatironinstitute/CaImAn#978
2 parents 584f167 + e1182ef commit 6b17fb5

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

mesmerize_core/algorithms/mcorr.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
from pathlib import Path
1111
import numpy as np
12+
from shutil import move as move_file
1213

1314

1415
# prevent circular import
@@ -29,7 +30,14 @@ def main(batch_path, uuid, data_path: str = None):
2930
set_parent_data_path(data_path)
3031
input_movie_path = str(get_full_data_path(input_movie_path))
3132

32-
params = item["params"]
33+
# because caiman doesn't let you specify filename to save memmap files
34+
# create dir with uuid as the dir name
35+
caiman_temp_dir = str(Path(input_movie_path).parent.joinpath(str(uuid)))
36+
os.makedirs(caiman_temp_dir, exist_ok=True)
37+
os.environ["CAIMAN_TEMP"] = caiman_temp_dir
38+
os.environ["CAIMAN_NEW_TEMPFILE"] = "True"
39+
40+
params = item['params']
3341

3442
# adapted from current demo notebook
3543
if "MESMERIZE_N_PROCESSES" in os.environ.keys():
@@ -52,8 +60,8 @@ def main(batch_path, uuid, data_path: str = None):
5260
try:
5361
# Run MC
5462
fnames = [input_movie_path]
55-
mc = MotionCorrect(fnames, dview=dview, **opts.get_group("motion"))
56-
mc.motion_correct(save_movie=True, base_name_prefix=uuid)
63+
mc = MotionCorrect(fnames, dview=dview, **opts.get_group('motion'))
64+
mc.motion_correct(save_movie=True)
5765

5866
# Find path to mmap file
5967
output_path = Path(mc.mmap_file[0])
@@ -106,22 +114,31 @@ def main(batch_path, uuid, data_path: str = None):
106114
shift_path = Path(input_movie_path).parent.joinpath(f"{uuid}_shifts.npy")
107115
np.save(str(shift_path), shifts)
108116

117+
# filename to move the output back to data dir
118+
mcorr_memmap = Path(input_movie_path).parent.joinpath(
119+
f"{uuid}_{output_path.stem}.mmap"
120+
)
121+
122+
# move the output file
123+
move_file(get_full_data_path(output_path), mcorr_memmap)
124+
os.removedirs(caiman_temp_dir)
125+
109126
if data_path is not None:
110127
cn_path = cn_path.relative_to(data_path)
111-
output_path = get_full_data_path(output_path).relative_to(data_path)
128+
mcorr_memmap = get_full_data_path(mcorr_memmap).relative_to(data_path)
112129
shift_path = shift_path.relative_to(data_path)
113130
for proj_type in proj_paths.keys():
114131
d[f"{proj_type}-projection-path"] = proj_paths[proj_type].relative_to(
115132
data_path
116133
)
117134
else:
118135
cn_path = cn_path
119-
output_path = get_full_data_path(output_path)
136+
mcorr_memmap = get_full_data_path(mcorr_memmap)
120137
shift_path = shift_path.resolve()
121138

122139
d.update(
123140
{
124-
"mcorr-output-path": output_path,
141+
"mcorr-output-path": mcorr_memmap,
125142
"corr-img-path": cn_path,
126143
"shifts": shift_path,
127144
"success": True,

0 commit comments

Comments
 (0)