Skip to content

Commit

Permalink
Revert "Minor refactor in run_s2p.py"
Browse files Browse the repository at this point in the history
  • Loading branch information
chriski777 committed Jul 20, 2020
1 parent bda5cf6 commit 502e6ce
Showing 1 changed file with 88 additions and 86 deletions.
174 changes: 88 additions & 86 deletions suite2p/run_s2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,101 +20,92 @@

def default_ops():
""" default options to run pipeline """
return {
ops = {
# file paths
'look_one_level_down': False, # whether to look in all subfolders when searching for tiffs
'fast_disk': [], # used to store temporary binary file, defaults to save_path0
'delete_bin': False, # whether to delete binary file after processing
'mesoscan': False, # for reading in scanimage mesoscope files
'bruker': False, # whether or not single page BRUKER tiffs!
'h5py': [], # take h5py as input (deactivates data_path)
'h5py_key': 'data', #key in h5py where data array is stored
'save_path0': [], # stores results, defaults to first item in data_path
'look_one_level_down': False, # whether to look in all subfolders when searching for tiffs
'fast_disk': [], # used to store temporary binary file, defaults to save_path0
'delete_bin': False, # whether to delete binary file after processing
'mesoscan': False, # for reading in scanimage mesoscope files
'bruker': False, # whether or not single page BRUKER tiffs!
'h5py': [], # take h5py as input (deactivates data_path)
'h5py_key': 'data', #key in h5py where data array is stored
'save_path0': [], # stores results, defaults to first item in data_path
'save_folder': [],
'subfolders': [],
'move_bin': False, # if 1, and fast_disk is different than save_disk, binary file is moved to save_disk

'move_bin': False, # if 1, and fast_disk is different than save_disk, binary file is moved to save_disk
# main settings
'nplanes' : 1, # each tiff has these many planes in sequence
'nchannels' : 1, # each tiff has these many channels per plane
'functional_chan' : 1, # this channel is used to extract functional ROIs (1-based)
'tau': 1., # this is the main parameter for deconvolution
'nplanes' : 1, # each tiff has these many planes in sequence
'nchannels' : 1, # each tiff has these many channels per plane
'functional_chan' : 1, # this channel is used to extract functional ROIs (1-based)
'tau': 1., # this is the main parameter for deconvolution
'fs': 10., # sampling rate (PER PLANE e.g. for 12 plane recordings it will be around 2.5)
'force_sktiff': False, # whether or not to use scikit-image for tiff reading
'frames_include': -1,

# output settings
'preclassify': 0., # apply classifier before signal extraction with probability 0.3
'save_mat': False, # whether to save output as matlab files
'save_NWB': False, # whether to save output as NWB file
'combined': True, # combine multiple planes into a single result /single canvas for GUI
'aspect': 1.0, # um/pixels in X / um/pixels in Y (for correct aspect ratio in GUI)

'preclassify': 0., # apply classifier before signal extraction with probability 0.3
'save_mat': False, # whether to save output as matlab files
'save_NWB': False, # whether to save output as NWB file
'combined': True, # combine multiple planes into a single result /single canvas for GUI
'aspect': 1.0, # um/pixels in X / um/pixels in Y (for correct aspect ratio in GUI)
# bidirectional phase offset
'do_bidiphase': False,
'bidiphase': 0,
'bidi_corrected': False,

# registration settings
'do_registration': 1, # whether to register data (2 forces re-registration)
'do_registration': 1, # whether to register data (2 forces re-registration)
'two_step_registration': False,
'keep_movie_raw': False,
'nimg_init': 300, # subsampled frames for finding reference image
'batch_size': 500, # number of frames per batch
'maxregshift': 0.1, # max allowed registration shift, as a fraction of frame max(width and height)
'align_by_chan' : 1, # when multi-channel, you can align by non-functional channel (1-based)
'reg_tif': False, # whether to save registered tiffs
'reg_tif_chan2': False, # whether to save channel 2 registered tiffs
'subpixel' : 10, # precision of subpixel registration (1/subpixel steps)
'smooth_sigma_time': 0, # gaussian smoothing in time
'smooth_sigma': 1.15, # ~1 good for 2P recordings, recommend >5 for 1P recordings
'th_badframes': 1.0, # this parameter determines which frames to exclude when determining cropping - set it smaller to exclude more frames
'nimg_init': 300, # subsampled frames for finding reference image
'batch_size': 500, # number of frames per batch
'maxregshift': 0.1, # max allowed registration shift, as a fraction of frame max(width and height)
'align_by_chan' : 1, # when multi-channel, you can align by non-functional channel (1-based)
'reg_tif': False, # whether to save registered tiffs
'reg_tif_chan2': False, # whether to save channel 2 registered tiffs
'subpixel' : 10, # precision of subpixel registration (1/subpixel steps)
'smooth_sigma_time' : 0, # gaussian smoothing in time
'smooth_sigma': 1.15, # ~1 good for 2P recordings, recommend >5 for 1P recordings
'th_badframes': 1.0, # this parameter determines which frames to exclude when determining cropping - set it smaller to exclude more frames
'pad_fft': False,

# non rigid registration settings
'nonrigid': True, # whether to use nonrigid registration
'block_size': [128, 128], # block size to register (** keep this a multiple of 2 **)
'snr_thresh': 1.2, # if any nonrigid block is below this threshold, it gets smoothed until above this threshold. 1.0 results in no smoothing
'maxregshiftNR': 5, # maximum pixel shift allowed for nonrigid, relative to rigid

'nonrigid': True, # whether to use nonrigid registration
'block_size': [128, 128], # block size to register (** keep this a multiple of 2 **)
'snr_thresh': 1.2, # if any nonrigid block is below this threshold, it gets smoothed until above this threshold. 1.0 results in no smoothing
'maxregshiftNR': 5, # maximum pixel shift allowed for nonrigid, relative to rigid
# 1P settings
'1Preg': False, # whether to perform high-pass filtering and tapering
'spatial_hp': 25, # window for spatial high-pass filtering before registration
'spatial_hp_reg': 26, # window for spatial high-pass filtering before registration
'1Preg': False, # whether to perform high-pass filtering and tapering
'spatial_hp': 25, # window for spatial high-pass filtering before registration
'spatial_hp_reg': 26, # window for spatial high-pass filtering before registration
'spatial_hp_detect': 25, # window for spatial high-pass filtering before registration
'pre_smooth': 2, # whether to smooth before high-pass filtering before registration
'spatial_taper': 50, # how much to ignore on edges (important for vignetted windows, for FFT padding do not set BELOW 3*ops['smooth_sigma'])

'pre_smooth': 2, # whether to smooth before high-pass filtering before registration
'spatial_taper': 50, # how much to ignore on edges (important for vignetted windows, for FFT padding do not set BELOW 3*ops['smooth_sigma'])
# cell detection settings
'roidetect': True, # whether or not to run ROI extraction
'spikedetect': True, # whether or not to run spike deconvolution
'sparse_mode': True, # whether or not to run sparse_mode
'diameter': 12, # if not sparse_mode, use diameter for filtering and extracting
'spatial_scale': 0, # 0: multi-scale; 1: 6 pixels, 2: 12 pixels, 3: 24 pixels, 4: 48 pixels
'connected': True, # whether or not to keep ROIs fully connected (set to 0 for dendrites)
'nbinned': 5000, # max number of binned frames for cell detection
'max_iterations': 20, # maximum number of iterations to do cell detection
'roidetect': True, # whether or not to run ROI extraction
'spikedetect': True, # whether or not to run spike deconvolution
'sparse_mode': True, # whether or not to run sparse_mode
'diameter': 12, # if not sparse_mode, use diameter for filtering and extracting
'spatial_scale': 0, # 0: multi-scale; 1: 6 pixels, 2: 12 pixels, 3: 24 pixels, 4: 48 pixels
'connected': True, # whether or not to keep ROIs fully connected (set to 0 for dendrites)
'nbinned': 5000, # max number of binned frames for cell detection
'max_iterations': 20, # maximum number of iterations to do cell detection
'threshold_scaling': 1.0, # adjust the automatically determined threshold by this scalar multiplier
'max_overlap': 0.75, # cells with more overlap than this get removed during triage, before refinement
'high_pass': 100, # running mean subtraction with window of size 'high_pass' (use low values for 1P)

'max_overlap': 0.75, # cells with more overlap than this get removed during triage, before refinement
'high_pass': 100, # running mean subtraction with window of size 'high_pass' (use low values for 1P)
# ROI extraction parameters
'inner_neuropil_radius': 2, # number of pixels to keep between ROI and neuropil donut
'min_neuropil_pixels': 350, # minimum number of pixels in the neuropil
'allow_overlap': False, # pixels that are overlapping are thrown out (False) or added to both ROIs (True)

'inner_neuropil_radius': 2, # number of pixels to keep between ROI and neuropil donut
'min_neuropil_pixels': 350, # minimum number of pixels in the neuropil
'allow_overlap': False, # pixels that are overlapping are thrown out (False) or added to both ROIs (True)
# channel 2 detection settings (stat[n]['chan2'], stat[n]['not_chan2'])
'chan2_thres': 0.65, # minimum for detection of brightness on channel 2

'chan2_thres': 0.65, # minimum for detection of brightness on channel 2
# deconvolution settings
'baseline': 'maximin', # baselining mode (can also choose 'prctile')
'win_baseline': 60., # window for maximin
'sig_baseline': 10., # smoothing constant for gaussian filter
'prctile_baseline': 8., # optional (whether to use a percentile baseline)
'baseline': 'maximin', # baselining mode (can also choose 'prctile')
'win_baseline': 60., # window for maximin
'sig_baseline': 10., # smoothing constant for gaussian filter
'prctile_baseline': 8.,# optional (whether to use a percentile baseline)
'neucoeff': .7, # neuropil coefficient
'xrange': np.array([0, 0]),
'yrange': np.array([0, 0]),
}
return ops

def run_plane(ops, flag_binreg=False):
""" run suite2p processing on a single binary file
Expand Down Expand Up @@ -236,7 +227,9 @@ def run_s2p(ops={},db={}):
"""
t0 = time.time()
ops = {**default_ops(), **ops, **db}
ops0 = default_ops()
ops = {**ops0, **ops}
ops = {**ops, **db}
if isinstance(ops['diameter'], list) and len(ops['diameter'])>1 and ops['aspect']==1.0:
ops['aspect'] = ops['diameter'][0] / ops['diameter'][1]
print(db)
Expand Down Expand Up @@ -306,25 +299,34 @@ def run_s2p(ops={},db={}):
ops['input_format'] = 'haus'
# if not set up files and copy tiffs/h5py to binary
if not files_found_flag:
# combine default options with user options
ops = {**default_ops(), **ops}

# copy file format to a binary file
convert_funs = {
'h5': io.h5py_to_binary,
'sbx': io.sbx_to_binary,
'mesoscan': io.mesoscan_to_binary,
'haus': lambda ops: haussio.load_haussio(ops['data_path'][0]).tosuite2p(ops),
'bruker': io.ome_to_binary,
}
if ops['input_format'] in convert_funs:
ops1 = convert_funs[ops['input_format']](ops)
print('time %4.2f sec. Wrote h5py to binaries for %d planes' % (time.time() - (t0), len(ops1)))
# get default options
ops0 = default_ops()
# combine with user options
ops = {**ops0, **ops}
# copy tiff to a binary
if ops['input_format'] == 'h5':
ops1 = io.h5py_to_binary(ops)
print('time %4.2f sec. Wrote h5py to binaries for %d planes'%(time.time()-(t0), len(ops1)))
elif ops['input_format'] == 'sbx':
ops1 = io.sbx_to_binary(ops)
print('time %4.2f sec. Wrote sbx to binaries for %d planes'%(time.time()-(t0), len(ops1)))
else:
ops1 = io.tiff_to_binary(ops)
print('time %4.2f sec. Wrote %d tiff frames to binaries for %d planes' %
(time.time() - (t0), ops1[0]['nframes'], len(ops1)))

if ops['input_format'] == 'mesoscan':
ops1 = io.mesoscan_to_binary(ops)
print('time %4.2f sec. Wrote mesoscope tifs to binaries for %d planes'%(time.time()-(t0), len(ops1)))
elif ops['input_format'] == 'haus':
print('time %4.2f sec. Using HAUSIO')
dataset = haussio.load_haussio(ops['data_path'][0])
ops1 = dataset.tosuite2p(ops)
print('time %4.2f sec. Wrote data to binaries for %d planes'%(time.time()-(t0), len(ops1)))
elif ops['input_format'] == 'bruker':
ops['bruker'] = True
ops1 = io.ome_to_binary(ops)
print('time %4.2f sec. Wrote bruker tifs to binaries for %d planes'%(time.time()-(t0), len(ops1)))
else:
ops1 = io.tiff_to_binary(ops)
print('time %4.2f sec. Wrote %d tiff frames to binaries for %d planes'%
(time.time()-(t0), ops1[0]['nframes'], len(ops1)))
np.save(fpathops1, ops1) # save ops1
else:
print('FOUND BINARIES: %s'%ops1[0]['reg_file'])
Expand Down

0 comments on commit 502e6ce

Please sign in to comment.