66from numba .typed import List
77from scipy import stats , signal
88from .masks import create_masks
9+ from ..io import BinaryFile
910
1011def extract_traces (ops , cell_masks , neuropil_masks , reg_file ):
1112 """ extracts activity from reg_file using masks in stat and neuropil_masks
@@ -33,8 +34,9 @@ def extract_traces(ops, cell_masks, neuropil_masks, reg_file):
3334 each element is neuropil pixels in (Ly*Lx) coordinates
3435 GOING TO BE DEPRECATED: size [ncells x npixels] where weights of each mask are elements
3536
36- reg_file : string
37- path to registered binary file
37+ reg_file : io.BinaryFile object
38+ io.BinaryFile object that has iter_frames(batch_size=ops['batch_size']) method
39+
3840
3941 Returns
4042 ----------------
@@ -58,36 +60,30 @@ def extract_traces(ops, cell_masks, neuropil_masks, reg_file):
5860 F = np .zeros ((ncells , nframes ),np .float32 )
5961 Fneu = np .zeros ((ncells , nframes ),np .float32 )
6062
61- reg_file = open (reg_file , 'rb' )
6263 nimgbatch = int (nimgbatch )
63- block_size = Ly * Lx * nimgbatch * 2
64- ix = 0
65- data = 1
66- neuropil_ipix = None
64+
65+ cell_ipix = [cell_mask [0 ].astype (np .int64 ) for cell_mask in cell_masks ]
66+ cell_lam = [cell_mask [1 ].astype (np .float32 ) for cell_mask in cell_masks ]
6767
68- cell_ipix = List ()
69- [cell_ipix .append (cell_mask [0 ].astype (np .int64 )) for cell_mask in cell_masks ]
70- cell_lam = List ()
71- [cell_lam .append (cell_mask [1 ].astype (np .float32 )) for cell_mask in cell_masks ]
7268 if neuropil_masks is not None :
7369 if isinstance (neuropil_masks , np .ndarray ) and neuropil_masks .shape [1 ] == Ly * Lx :
7470 neuropil_masks = [np .nonzero (neuropil_mask )[0 ] for neuropil_mask in neuropil_masks ]
7571 else :
7672 neuropil_masks = [neuropil_mask .astype (np .int64 ) for neuropil_mask in neuropil_masks ]
77- neuropil_ipix = List ()
78- [neuropil_ipix .append (neuropil_mask ) for neuropil_mask in neuropil_masks ]
73+ neuropil_ipix = neuropil_masks
7974 neuropil_npix = np .array ([len (neuropil_ipixi ) for neuropil_ipixi in neuropil_ipix ]).astype (np .float32 )
75+ else :
76+ neuropil_ipix = None
8077
81- while data is not None :
82- buff = reg_file .read (block_size )
83- data = np .frombuffer (buff , dtype = np .int16 , offset = 0 )
84- nimg = int (np .floor (data .size / (Ly * Lx )))
78+ ix = 0
79+ for k , (_ , data ) in enumerate (reg_file .iter_frames (batch_size = ops ['batch_size' ])):
80+ nimg = data .shape [0 ]
8581 if nimg == 0 :
8682 break
87- data = np .reshape (data , (- 1 , Ly , Lx ))
8883 inds = ix + np .arange (0 ,nimg ,1 ,int )
8984 data = np .reshape (data , (nimg ,- 1 )).astype (np .float32 )
9085 Fi = np .zeros ((ncells , data .shape [0 ]), np .float32 )
86+
9187 # extract traces and neuropil
9288
9389 # (WITHOUT NUMBA)
@@ -127,9 +123,13 @@ def extract_traces_from_masks(ops, cell_masks, neuropil_masks):
127123
128124 """
129125 F_chan2 , Fneu_chan2 = [], []
130- F , Fneu , ops = extract_traces (ops , cell_masks , neuropil_masks , ops ['reg_file' ])
126+ with BinaryFile (Ly = ops ['Ly' ], Lx = ops ['Lx' ],
127+ read_filename = ops ['reg_file' ]) as f :
128+ F , Fneu , ops = extract_traces (ops , cell_masks , neuropil_masks , f )
131129 if 'reg_file_chan2' in ops :
132- F_chan2 , Fneu_chan2 , _ = extract_traces (ops .copy (), cell_masks , neuropil_masks , ops ['reg_file_chan2' ])
130+ with BinaryFile (Ly = ops ['Ly' ], Lx = ops ['Lx' ],
131+ read_filename = ops ['reg_file_chan2' ]) as f :
132+ F_chan2 , Fneu_chan2 , _ = extract_traces (ops .copy (), cell_masks , neuropil_masks , f )
133133 return F , Fneu , F_chan2 , Fneu_chan2 , ops
134134
135135def create_masks_and_extract (ops , stat , cell_masks = None , neuropil_masks = None ):
0 commit comments