1818from ..detection .stats import roi_stats
1919from ..extraction import preprocess
2020from ..extraction .dcnv import oasis
21+ from ..extraction .extract import extract_traces
22+ from ..io .binary import BinaryFile
23+ from ..parameters import default_settings
24+ from ..run_s2p import _assign_torch_device
2125
2226
2327def masks_and_traces (settings , stat_manual , stat_orig ):
@@ -27,6 +31,8 @@ def masks_and_traces(settings, stat_manual, stat_orig):
2731 returns: F (ROIs x time), Fneu (ROIs x time), F_chan2, Fneu_chan2, settings, stat
2832 F_chan2 and Fneu_chan2 will be empty if no second channel
2933 """
34+ # Merge with defaults to ensure all required keys are present
35+ settings = {** default_settings (), ** settings }
3036
3137 t0 = time .time ()
3238
@@ -35,11 +41,12 @@ def masks_and_traces(settings, stat_manual, stat_orig):
3541 for n in range (len (stat_orig )):
3642 stat_all .append (stat_orig [n ])
3743
38- stat_all = roi_stats (stat_all , settings ["Ly" ], settings ["Lx" ], aspect = settings .get ("aspect" , None ),
44+ stat_all = np .array (stat_all )
45+ stat_all = roi_stats (stat_all , settings ["Ly" ], settings ["Lx" ],
3946 diameter = settings ["diameter" ])
4047 cell_masks = [
4148 masks .create_cell_mask (stat , Ly = settings ["Ly" ], Lx = settings ["Lx" ],
42- allow_overlap = settings ["allow_overlap" ]) for stat in stat_all
49+ allow_overlap = settings ["extraction" ][ " allow_overlap" ]) for stat in stat_all
4350 ]
4451 cell_pix = masks .create_cell_pix (stat_all , Ly = settings ["Ly" ], Lx = settings ["Lx" ])
4552 manual_roi_stats = stat_all [:len (stat_manual )]
@@ -48,13 +55,26 @@ def masks_and_traces(settings, stat_manual, stat_orig):
4855 ypixs = [stat ["ypix" ] for stat in manual_roi_stats ],
4956 xpixs = [stat ["xpix" ] for stat in manual_roi_stats ],
5057 cell_pix = cell_pix ,
51- inner_neuropil_radius = settings ["inner_neuropil_radius" ],
52- min_neuropil_pixels = settings ["min_neuropil_pixels" ],
58+ inner_neuropil_radius = settings ["extraction" ][ " inner_neuropil_radius" ],
59+ min_neuropil_pixels = settings ["extraction" ][ " min_neuropil_pixels" ],
5360 )
5461 print ("Masks made in %0.2f sec." % (time .time () - t0 ))
5562
56- F , Fneu , F_chan2 , Fneu_chan2 = extract_traces_from_masks (settings , manual_cell_masks ,
57- manual_neuropil_masks )
63+ # Extract traces from binary file
64+ Ly , Lx = settings ["Ly" ], settings ["Lx" ]
65+ batch_size = settings ["extraction" ]["batch_size" ]
66+ device = _assign_torch_device (settings ["torch_device" ])
67+ f_reg = BinaryFile (Ly , Lx , settings ["reg_file" ])
68+ F , Fneu = extract_traces (f_reg , manual_cell_masks , manual_neuropil_masks , batch_size = batch_size , device = device )
69+ f_reg .close ()
70+
71+ # Handle chan2 if present
72+ if "reg_file_chan2" in settings and settings ["reg_file_chan2" ]:
73+ f_reg_chan2 = BinaryFile (Ly , Lx , settings ["reg_file_chan2" ])
74+ F_chan2 , Fneu_chan2 = extract_traces (f_reg_chan2 , manual_cell_masks , manual_neuropil_masks , batch_size = batch_size , device = device )
75+ f_reg_chan2 .close ()
76+ else :
77+ F_chan2 , Fneu_chan2 = None , None
5878
5979 # compute activity statistics for classifier
6080 npix = np .array ([stat_orig [n ]["npix" ] for n in range (len (stat_orig ))
@@ -69,7 +89,7 @@ def masks_and_traces(settings, stat_manual, stat_orig):
6989 manual_roi_stats [n ]["iplane" ] = stat_orig [0 ]["iplane" ]
7090
7191 # subtract neuropil and compute skew, std from F
72- dF = F - settings ["neucoeff " ] * Fneu
92+ dF = F - settings ["extraction" ][ "neuropil_coefficient " ] * Fneu
7393 sk = stats .skew (dF , axis = 1 )
7494 sd = np .std (dF , axis = 1 )
7595
@@ -81,10 +101,10 @@ def masks_and_traces(settings, stat_manual, stat_orig):
81101 np .mean (manual_roi_stats [n ]["xpix" ])
82102 ]
83103
84- dF = preprocess (F = dF , baseline = settings ["baseline" ], win_baseline = settings ["win_baseline" ],
85- sig_baseline = settings ["sig_baseline" ], fs = settings ["fs" ],
86- prctile_baseline = settings ["prctile_baseline" ])
87- spks = oasis (F = dF , batch_size = settings ["batch_size" ], tau = settings ["tau" ], fs = settings ["fs" ])
104+ dF = preprocess (F = dF , baseline = settings ["dcnv_preprocess" ][ " baseline" ], win_baseline = settings [ "dcnv_preprocess" ] ["win_baseline" ],
105+ sig_baseline = settings ["dcnv_preprocess" ][ " sig_baseline" ], fs = settings ["fs" ],
106+ prctile_baseline = settings ["dcnv_preprocess" ][ " prctile_baseline" ], device = device )
107+ spks = oasis (F = dF , batch_size = settings ["extraction" ][ " batch_size" ], tau = settings ["tau" ], fs = settings ["fs" ])
88108
89109 return F , Fneu , F_chan2 , Fneu_chan2 , spks , settings , manual_roi_stats
90110
@@ -187,7 +207,7 @@ def __init__(self, parent):
187207 self .saveGUI = False
188208 self .closeGUI = QPushButton ("Save and Quit" )
189209 self .closeGUI .setFont (QtGui .QFont ("Arial" , 8 , QtGui .QFont .Bold ))
190- self .closeGUI .clicked .connect (self .close_GUI )
210+ self .closeGUI .clicked .connect (lambda : self .close_GUI () )
191211 self .closeGUI .setEnabled (False )
192212 self .closeGUI .setFixedWidth (100 )
193213 self .closeGUI .setStyleSheet (self .styleUnpressed )
@@ -247,9 +267,7 @@ def close_GUI(self):
247267
248268 # Append new stat file with old and save
249269 print ("Saving new stat" )
250- stat_all = self .new_stat .copy ()
251- for n in range (len (self .parent .stat )):
252- stat_all .append (self .parent .stat [n ])
270+ stat_all = np .concatenate ((self .new_stat , self .parent .stat ))
253271 np .save (os .path .join (self .parent .basename , "stat.npy" ), stat_all )
254272 iscell_prob = np .concatenate (
255273 (self .parent .iscell [:, np .newaxis ], self .parent .probcell [:, np .newaxis ]),
@@ -297,30 +315,26 @@ def normalize_img_add_masks(self):
297315 if i == 0 :
298316 mimg = np .zeros ((self .Ly , self .Lx ), np .float32 )
299317 mimg [self .parent .ops ["yrange" ][0 ]:self .parent .ops ["yrange" ][1 ],
300- self .parent .ops ["xrange" ][0 ]:self .parent .
301- settings ["xrange" ][1 ]] = self .parent .ops ["meanImg" ][
318+ self .parent .ops ["xrange" ][0 ]:self .parent .ops ["xrange" ][1 ]] = self .parent .ops ["meanImg" ][
302319 self .parent .ops ["yrange" ][0 ]:self .parent .ops ["yrange" ][1 ],
303320 self .parent .ops ["xrange" ][0 ]:self .parent .ops ["xrange" ][1 ]]
304321
305322 elif i == 1 :
306323 mimg = np .zeros ((self .Ly , self .Lx ), np .float32 )
307324 mimg [self .parent .ops ["yrange" ][0 ]:self .parent .ops ["yrange" ][1 ],
308- self .parent .ops ["xrange" ][0 ]:self .parent .
309- settings ["xrange" ][1 ]] = self .parent .ops ["meanImgE" ][
325+ self .parent .ops ["xrange" ][0 ]:self .parent .ops ["xrange" ][1 ]] = self .parent .ops ["meanImgE" ][
310326 self .parent .ops ["yrange" ][0 ]:self .parent .ops ["yrange" ][1 ],
311327 self .parent .ops ["xrange" ][0 ]:self .parent .ops ["xrange" ][1 ]]
312328 elif i == 2 :
313329 mimg = np .zeros ((self .Ly , self .Lx ), np .float32 )
314330 mimg [self .parent .ops ["yrange" ][0 ]:self .parent .ops ["yrange" ][1 ],
315- self .parent .ops ["xrange" ][0 ]:self .parent .
316- settings ["xrange" ][1 ]] = self .parent .ops ["Vcorr" ]
331+ self .parent .ops ["xrange" ][0 ]:self .parent .ops ["xrange" ][1 ]] = self .parent .ops ["Vcorr" ]
317332
318333 else :
319334 mimg = np .zeros ((self .Ly , self .Lx ), np .float32 )
320335 if "max_proj" in self .parent .ops :
321336 mimg [self .parent .ops ["yrange" ][0 ]:self .parent .ops ["yrange" ][1 ],
322- self .parent .ops ["xrange" ][0 ]:self .parent .
323- settings ["xrange" ][1 ]] = self .parent .ops ["max_proj" ]
337+ self .parent .ops ["xrange" ][0 ]:self .parent .ops ["xrange" ][1 ]] = self .parent .ops ["max_proj" ]
324338
325339 mimg1 = np .percentile (mimg , 1 )
326340 mimg99 = np .percentile (mimg , 99 )
0 commit comments