def run_s2p(ops={}, db={}): i0 = tic() ops0 = default_ops() ops = {**ops0, **ops} ops = {**ops, **db} if 'save_path0' not in ops or len(ops['save_path0']) == 0: if ('h5py' in ops) and len(ops['h5py']) > 0: ops['save_path0'], tail = os.path.split(ops['h5py']) else: ops['save_path0'] = ops['data_path'][0] # check if there are files already registered fpathops1 = os.path.join(ops['save_path0'], 'suite2p', 'ops1.npy') if os.path.isfile(fpathops1): files_found_flag = True flag_binreg = True ops1 = np.load(fpathops1) for i, op in enumerate(ops1): # default behavior is to look in the ops flag_reg = os.path.isfile(op['reg_file']) if not flag_reg: # otherwise look in the user defined save_path0 op['save_path'] = os.path.join(ops['save_path0'], 'suite2p', 'plane%d' % i) op['ops_path'] = os.path.join(op['save_path'], 'ops.npy') op['reg_file'] = os.path.join(op['save_path'], 'data.bin') flag_reg = os.path.isfile(op['reg_file']) files_found_flag &= flag_reg if 'refImg' not in op: flag_binreg = False # use the new options ops1[i] = {**op, **ops}.copy() #ops1[i] = ops1[i].copy() print(ops1[i]['save_path']) # except for registration results ops1[i]['xrange'] = op['xrange'] ops1[i]['yrange'] = op['yrange'] else: files_found_flag = False flag_binreg = False ######### REGISTRATION ######### if not files_found_flag: # get default options ops0 = default_ops() # combine with user options ops = {**ops0, **ops} # copy tiff to a binary if len(ops['h5py']): ops1 = utils.h5py_to_binary(ops) print('time %4.4f. Wrote h5py to binaries for %d planes' % (toc(i0), len(ops1))) else: if 'mesoscan' in ops and ops['mesoscan']: ops1 = utils.mesoscan_to_binary(ops) print('time %4.4f. Wrote tifs to binaries for %d planes' % (toc(i0), len(ops1))) elif HAS_HAUS: print('time %4.4f. Using HAUSIO') dataset = haussio.load_haussio(ops['data_path'][0]) ops1 = dataset.tosuite2p(ops) print('time %4.4f. Wrote data to binaries for %d planes' % (toc(i0), len(ops1))) else: ops1 = utils.tiff_to_binary(ops) print('time %4.4f. Wrote tifs to binaries for %d planes' % (toc(i0), len(ops1))) np.save(fpathops1, ops1) # save ops1 ops1 = np.array(ops1) ops1 = utils.split_multiops(ops1) if not ops['do_registration']: flag_binreg = True if files_found_flag: print('found ops1 and binaries') print(ops1[0]['reg_file']) if flag_binreg: print('foundpre-registered binaries') print('skipping registration...') if flag_binreg and not files_found_flag: print('binary file created, but registration not performed') if len(ops1) > 1 and ops['num_workers_roi'] >= 0: if ops['num_workers_roi'] == 0: ops['num_workers_roi'] = len(ops1) ni = ops['num_workers_roi'] else: ni = 1 ik = 0 while ik < len(ops1): ipl = ik + np.arange(0, min(ni, len(ops1) - ik)) if not flag_binreg: ops1[ipl] = register.register_binary(ops1[ipl]) # register binary np.save(fpathops1, ops1) # save ops1 print('time %4.4f. Registration complete for %d planes' % (toc(i0), ni)) if ni > 1: with Pool(len(ipl)) as p: ops1[ipl] = p.map(utils.get_cells, ops1[ipl]) else: ops1[ipl[0]] = utils.get_cells(ops1[ipl[0]]) for ops in ops1[ipl]: fpath = ops['save_path'] F = np.load(os.path.join(fpath, 'F.npy')) Fneu = np.load(os.path.join(fpath, 'Fneu.npy')) dF = F - ops['neucoeff'] * Fneu spks = dcnv.oasis(dF, ops) np.save(os.path.join(ops['save_path'], 'spks.npy'), spks) print('time %4.4f. Detected spikes in %d ROIs' % (toc(i0), F.shape[0])) stat = np.load(os.path.join(fpath, 'stat.npy')) # apply default classifier classfile = os.path.join( os.path.abspath(os.path.dirname(__file__)), 'classifiers/classifier_user.npy') print(classfile) iscell = classifier.run(classfile, stat) np.save(os.path.join(ops['save_path'], 'iscell.npy'), iscell) # save as matlab file if ('save_mat' in ops) and ops['save_mat']: matpath = os.path.join(ops['save_path'], 'Fall.mat') io.savemat( matpath, { 'stat': stat, 'ops': ops, 'F': F, 'Fneu': Fneu, 'spks': spks, 'iscell': iscell }) ik += len(ipl) # save final ops1 with all planes np.save(fpathops1, ops1) #### COMBINE PLANES or FIELDS OF VIEW #### if len(ops1) > 1 and ops1[0]['combined']: utils.combined(ops1) # running a clean up script if 'clean_script' in ops1[0]: print('running clean-up script') os.system('python ' + ops['clean_script'] + ' ' + fpathops1) for ops in ops1: if ('delete_bin' in ops) and ops['delete_bin']: os.remove(ops['reg_file']) if ops['nchannels'] > 1: os.remove(ops['reg_file_chan2']) print('finished all tasks in total time %4.4f sec' % toc(i0)) return ops1
def roi_detect_and_extract(ops): t0 = time.time() if ops['sparse_mode']: ops, stat = sparsedetect.sparsery(ops) else: ops, stat = sourcery.sourcery(ops) print('Found %d ROIs, %0.2f sec' % (len(stat), toc(t0))) ### apply default classifier ### if len(stat) > 0: classfile = os.path.join( os.path.abspath(os.path.dirname(__file__)), "classifiers/classifier_user.npy", ) if not os.path.isfile(classfile): classorig = os.path.join( os.path.abspath(os.path.dirname(__file__)), "classifiers/classifier.npy") shutil.copy(classorig, classfile) print('NOTE: applying classifier %s' % classfile) iscell = classifier.run(classfile, stat, keys=['npix_norm', 'compact', 'skew']) if 'preclassify' in ops and ops['preclassify'] > 0.0: ic = (iscell[:, 0] > ops['preclassify']).flatten().astype(np.bool) stat = stat[ic] iscell = iscell[ic] print('After classification with threshold %0.2f, %d ROIs remain' % (ops['preclassify'], len(stat))) else: iscell = np.zeros((0, 2)) stat = sparsedetect.get_overlaps(stat, ops) stat, ix = sparsedetect.remove_overlaps(stat, ops, ops['Ly'], ops['Lx']) iscell = iscell[ix, :] print('After removing overlaps, %d ROIs remain' % (len(stat))) np.save(os.path.join(ops['save_path'], 'iscell.npy'), iscell) # extract fluorescence and neuropil F, Fneu, F_chan2, Fneu_chan2, ops, stat = masks_and_traces(ops, stat) # subtract neuropil dF = F - ops['neucoeff'] * Fneu # compute activity statistics for classifier sk = stats.skew(dF, axis=1) sd = np.std(dF, axis=1) for k in range(F.shape[0]): stat[k]['skew'] = sk[k] stat[k]['std'] = sd[k] # if second channel, detect bright cells in second channel fpath = ops['save_path'] if 'meanImg_chan2' in ops: if 'chan2_thres' not in ops: ops['chan2_thres'] = 0.65 ops, redcell = chan2detect.detect(ops, stat) np.save(os.path.join(fpath, 'redcell.npy'), redcell) np.save(os.path.join(fpath, 'F_chan2.npy'), F_chan2) np.save(os.path.join(fpath, 'Fneu_chan2.npy'), Fneu_chan2) # add enhanced mean image ops = utils.enhanced_mean_image(ops) # save ops np.save(ops['ops_path'], ops) # save results np.save(os.path.join(fpath, 'F.npy'), F) np.save(os.path.join(fpath, 'Fneu.npy'), Fneu) np.save(os.path.join(fpath, 'stat.npy'), stat) return ops
def run_s2p(ops={}, db={}): i0 = tic() ops = {**ops, **db} if 'save_path0' not in ops or len(ops['save_path0']) == 0: ops['save_path0'] = ops['data_path'][0] # check if there are files already registered fpathops1 = os.path.join(ops['save_path0'], 'suite2p', 'ops1.npy') if os.path.isfile(fpathops1): files_found_flag = True flag_binreg = True ops1 = np.load(fpathops1) for i, op in enumerate(ops1): # default behavior is to look in the ops flag_reg = os.path.isfile(op['reg_file']) if not flag_reg: # otherwise look in the user defined save_path0 op['save_path'] = os.path.join(ops['save_path0'], 'suite2p', 'plane%d' % i) op['ops_path'] = os.path.join(op['save_path'], 'ops.npy') op['reg_file'] = os.path.join(op['save_path'], 'data.bin') flag_reg = os.path.isfile(op['reg_file']) files_found_flag &= flag_reg if 'refImg' not in op: flag_binreg = False # use the new options ops1[i] = {**op, **ops} ops1[i] = ops1[i].copy() print(ops1[i]['save_path']) # except for registration results ops1[i]['xrange'] = op['xrange'] ops1[i]['yrange'] = op['yrange'] else: files_found_flag = False flag_binreg = False ######### REGISTRATION ######### if not files_found_flag: # get default options ops0 = default_ops() # combine with user options ops = {**ops0, **ops} # copy tiff to a binary if len(ops['h5py']): ops1 = utils.h5py_to_binary(ops) print('time %4.4f. Wrote h5py to binaries for %d planes' % (toc(i0), len(ops1))) else: ops1 = utils.tiff_to_binary(ops) print('time %4.4f. Wrote tifs to binaries for %d planes' % (toc(i0), len(ops1))) # save ops1 np.save(fpathops1, ops1) if not flag_binreg: ops1 = register.register_binary(ops1) # register tiff np.save(fpathops1, ops1) # save ops1 print('time %4.4f. Registration complete' % toc(i0)) else: print('found ops1 and pre-registered binaries') print(ops1[0]['reg_file']) print('overwriting ops1 with new ops') print('skipping registration...') ######### CELL DETECTION ######### if len(ops1) > 1 and ops['num_workers_roi'] >= 0: if ops['num_workers_roi'] == 0: ops['num_workers_roi'] = len(ops1) with Pool(ops['num_workers_roi']) as p: ops1 = p.map(utils.get_cells, ops1) else: for k in range(len(ops1)): ops1[k] = utils.get_cells(ops1[k]) ######### SPIKE DECONVOLUTION AND CLASSIFIER ######### for ops in ops1: fpath = ops['save_path'] F = np.load(os.path.join(fpath, 'F.npy')) Fneu = np.load(os.path.join(fpath, 'Fneu.npy')) dF = F - ops['neucoeff'] * Fneu spks = dcnv.oasis(dF, ops) np.save(os.path.join(ops['save_path'], 'spks.npy'), spks) print('time %4.4f. Detected spikes in %d ROIs' % (toc(i0), F.shape[0])) stat = np.load(os.path.join(fpath, 'stat.npy')) # apply default classifier classfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'classifiers/classifier_user.npy') print(classfile) iscell = classifier.run(classfile, stat) np.save(os.path.join(ops['save_path'], 'iscell.npy'), iscell) # save as matlab file if ('save_mat' in ops) and ops['save_mat']: matpath = os.path.join(ops['save_path'], 'Fall.mat') scipy.io.savemat( matpath, { 'stat': stat, 'ops': ops, 'F': F, 'Fneu': Fneu, 'spks': spks, 'iscell': iscell }) # save final ops1 with all planes np.save(fpathops1, ops1) #### COMBINE PLANES or FIELDS OF VIEW #### if len(ops1) > 1 and ops1[0]['combined']: utils.combined(ops1) for ops in ops1: if ('delete_bin' in ops) and ops['delete_bin']: os.remove(ops['reg_file']) if ops['nchannels'] > 1: os.remove(ops['reg_file_chan2']) print('finished all tasks in total time %4.4f sec' % toc(i0)) return ops1