88import traceback
99from pathlib import Path
1010
11- if __name__ == ' __main__' :
11+ if __name__ == " __main__" :
1212 from mesmerize_core import set_parent_data_path , get_full_data_path
1313
14+
1415@click .command ()
15- @click .option (' --batch-path' , type = str )
16- @click .option (' --uuid' , type = str )
17- @click .option (' --data-path' )
16+ @click .option (" --batch-path" , type = str )
17+ @click .option (" --uuid" , type = str )
18+ @click .option (" --data-path" )
1819def main (batch_path , uuid , data_path : str = None ):
1920 df = pd .read_pickle (batch_path )
20- item = df [df [' uuid' ] == uuid ].squeeze ()
21+ item = df [df [" uuid" ] == uuid ].squeeze ()
2122
22- input_movie_path = item [' input_movie_path' ]
23+ input_movie_path = item [" input_movie_path" ]
2324 set_parent_data_path (data_path )
2425 input_movie_path = str (get_full_data_path (input_movie_path ))
2526
26- params = item [' params' ]
27+ params = item [" params" ]
2728 print ("cnmfe params:" , params )
2829
29- #adapted from current demo notebook
30+ # adapted from current demo notebook
3031 n_processes = psutil .cpu_count () - 1
3132 # Start cluster for parallel processing
3233 c , dview , n_processes = cm .cluster .setup_cluster (
33- backend = 'local' ,
34- n_processes = n_processes ,
35- single_thread = False
36- )
34+ backend = "local" , n_processes = n_processes , single_thread = False
35+ )
3736
3837 try :
3938 fname_new = cm .save_memmap (
40- [input_movie_path ],
41- base_name = f'{ uuid } _cnmf-memmap_' ,
42- order = 'C' ,
43- dview = dview
39+ [input_movie_path ], base_name = f"{ uuid } _cnmf-memmap_" , order = "C" , dview = dview
4440 )
4541
46- print (' making memmap' )
47- gSig = params [' cnmfe_kwargs' ][ ' gSig' ][0 ]
42+ print (" making memmap" )
43+ gSig = params [" cnmfe_kwargs" ][ " gSig" ][0 ]
4844
4945 Yr , dims , T = cm .load_memmap (fname_new )
50- images = np .reshape (Yr .T , [T ] + list (dims ), order = 'F' )
46+ images = np .reshape (Yr .T , [T ] + list (dims ), order = "F" )
5147
5248 proj_paths = dict ()
53- for proj_type in ['mean' , 'std' , 'max' ]:
54- p_img = getattr (np , f'nan{ proj_type } ' )(images , axis = 0 )
55- proj_paths [proj_type ] = Path (input_movie_path ).parent .joinpath (f'{ uuid } _{ proj_type } _projection.npy' )
49+ for proj_type in ["mean" , "std" , "max" ]:
50+ p_img = getattr (np , f"nan{ proj_type } " )(images , axis = 0 )
51+ proj_paths [proj_type ] = Path (input_movie_path ).parent .joinpath (
52+ f"{ uuid } _{ proj_type } _projection.npy"
53+ )
5654 np .save (str (proj_paths [proj_type ]), p_img )
5755
58- downsample_ratio = params [' downsample_ratio' ]
56+ downsample_ratio = params [" downsample_ratio" ]
5957 # in fname new load in memmap order C
6058
6159 cn_filter , pnr = cm .summary_images .correlation_pnr (
6260 images [::downsample_ratio ], swap_dim = False , gSig = gSig
6361 )
6462
65- pnr_output_path = Path (input_movie_path ).parent .joinpath (f"{ uuid } _pn.npy" ).resolve ()
66- cn_output_path = Path (input_movie_path ).parent .joinpath (f"{ uuid } _cn.npy" ).resolve ()
63+ pnr_output_path = (
64+ Path (input_movie_path ).parent .joinpath (f"{ uuid } _pn.npy" ).resolve ()
65+ )
66+ cn_output_path = (
67+ Path (input_movie_path ).parent .joinpath (f"{ uuid } _cn.npy" ).resolve ()
68+ )
6769
6870 np .save (str (pnr_output_path ), pnr , allow_pickle = False )
6971 np .save (str (cn_output_path ), cn_filter , allow_pickle = False )
7072
7173 d = dict () # for output
7274
73- if params ['do_cnmfe' ]:
74- cnmfe_params_dict = \
75- {
76- "method_init" : 'corr_pnr' ,
77- "n_processes" : n_processes ,
78- "only_init" : True , # for 1p
79- "center_psf" : True , # for 1p
80- "normalize_init" : False # for 1p
81- }
82- tot = {** cnmfe_params_dict , ** params ['cnmfe_kwargs' ]}
75+ if params ["do_cnmfe" ]:
76+ cnmfe_params_dict = {
77+ "method_init" : "corr_pnr" ,
78+ "n_processes" : n_processes ,
79+ "only_init" : True , # for 1p
80+ "center_psf" : True , # for 1p
81+ "normalize_init" : False , # for 1p
82+ }
83+ tot = {** cnmfe_params_dict , ** params ["cnmfe_kwargs" ]}
8384 cnmfe_params_dict = CNMFParams (params_dict = tot )
8485 cnm = cnmf .CNMF (
85- n_processes = n_processes ,
86- dview = dview ,
87- params = cnmfe_params_dict
86+ n_processes = n_processes , dview = dview , params = cnmfe_params_dict
8887 )
8988 print ("Performing CNMFE" )
9089 cnm = cnm .fit (images )
9190 print ("evaluating components" )
9291 cnm .estimates .evaluate_components (images , cnm .params , dview = dview )
9392
94- output_path = Path (input_movie_path ).parent .joinpath (f"{ uuid } .hdf5" ).resolve ()
93+ output_path = (
94+ Path (input_movie_path ).parent .joinpath (f"{ uuid } .hdf5" ).resolve ()
95+ )
9596 cnm .save (str (output_path ))
9697
9798 if data_path is not None :
9899 cnmf_hdf5_path = Path (output_path ).relative_to (data_path )
99100 for proj_type in proj_paths .keys ():
100- d [f"{ proj_type } -projection-path" ] = proj_paths [proj_type ].relative_to (data_path )
101+ d [f"{ proj_type } -projection-path" ] = proj_paths [
102+ proj_type
103+ ].relative_to (data_path )
101104 else :
102105 cnmf_hdf5_path = output_path
103106 for proj_type in proj_paths .keys ():
@@ -122,7 +125,7 @@ def main(batch_path, uuid, data_path: str = None):
122125 "corr-img-path" : cn_output_path ,
123126 "pnr-image-path" : pnr_output_path ,
124127 "success" : True ,
125- "traceback" : None
128+ "traceback" : None ,
126129 }
127130 )
128131
@@ -132,10 +135,10 @@ def main(batch_path, uuid, data_path: str = None):
132135 d = {"success" : False , "traceback" : traceback .format_exc ()}
133136
134137 # Add dictionary to output column of series
135- df .loc [df [' uuid' ] == uuid , ' outputs' ] = [d ]
138+ df .loc [df [" uuid" ] == uuid , " outputs" ] = [d ]
136139 # save dataframe to disc
137140 df .to_pickle (batch_path )
138141
139142
140143if __name__ == "__main__" :
141- main ()
144+ main ()
0 commit comments