Ejemplo n.º 1
0
def run_s2p(ops={},db={}):
    t0 = tic()
    ops0 = default_ops()
    ops = {**ops0, **ops}
    ops = {**ops, **db}
    print(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, allow_pickle=True)
        print('FOUND OPS IN %s'%ops1[0]['save_path'])
        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 or op['do_registration']>1:
                flag_binreg = False
                if i==len(ops1)-1:
                    print("NOTE: not registered / registration forced with ops['do_registration']>1")
            # use the new False
            ops1[i] = {**op, **ops}.copy()
            # for mesoscope tiffs, preserve original lines, etc
            if 'lines' in op:
                ops1[i]['nrois'] = op['nrois']
                ops1[i]['nplanes'] = op['nplanes']
                ops1[i]['lines'] = op['lines']
                ops1[i]['dy'] = op['dy']
                ops1[i]['dx'] = op['dx']
                ops1[i]['iplane'] = op['iplane']

            #ops1[i] = ops1[i].copy()
            # except for registration results
            ops1[i]['xrange'] = op['xrange']
            ops1[i]['yrange'] = op['yrange']

    else:
        files_found_flag = False
        flag_binreg = False

    # if not set up files and copy tiffs/h5py to binary
    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.2f sec. Wrote h5py to binaries for %d planes'%(toc(t0), len(ops1)))
        else:
            if 'mesoscan' in ops and ops['mesoscan']:
                ops1 = utils.mesoscan_to_binary(ops)
                print('time %4.2f sec. Wrote tifs to binaries for %d planes'%(toc(t0), len(ops1)))
            elif 'bruker' in ops and ops['bruker']:
                ops1 = utils.ome_to_binary(ops)
            elif HAS_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'%(toc(t0), len(ops1)))
            else:
                ops1 = utils.tiff_to_binary(ops)
                print('time %4.2f sec. Wrote tifs to binaries for %d planes'%(toc(t0), len(ops1)))

        np.save(fpathops1, ops1) # save ops1
    else:
        print('FOUND BINARIES: %s'%ops1[0]['reg_file'])

    ops1 = np.array(ops1)
    #ops1 = utils.split_multiops(ops1)
    if not ops['do_registration']:
        flag_binreg = True

    if ops['do_registration']>1:
        flag_binreg = False
        print('do_registration>1 => forcing registration')

    if flag_binreg:
        print('SKIPPING REGISTRATION FOR ALL PLANES...')
    if flag_binreg and not files_found_flag:
        print('NOTE: binary file created, but registration not performed')

    # set up number of CPU workers for registration and cell detection
    ipl = 0

    while ipl<len(ops1):
        print('>>>>>>>>>>>>>>>>>>>>> PLANE %d <<<<<<<<<<<<<<<<<<<<<<'%ipl)
        t1 = tic()
        if not flag_binreg:
            ######### REGISTRATION #########
            t11=tic()
            print('----------- REGISTRATION')
            ops1[ipl] = register.register_binary(ops1[ipl]) # register binary
            np.save(fpathops1, ops1) # save ops1
            print('----------- Total %0.2f sec'%(toc(t11)))
        if not files_found_flag or not flag_binreg:
            # compute metrics for registration
            if 'do_regmetrics' in ops:
                do_regmetrics = ops['do_regmetrics']
            else:
                do_regmetrics = True
            if do_regmetrics and ops1[ipl]['nframes']>=1500:
                t0=tic()
                ops1[ipl] = regmetrics.get_pc_metrics(ops1[ipl])
                print('Registration metrics, %0.2f sec.'%(toc(t0)))
                np.save(os.path.join(ops1[ipl]['save_path'],'ops.npy'), ops1[ipl])
        if 'roidetect' in ops1[ipl]:
            roidetect = ops['roidetect']
        else:
            roidetect = True
        if roidetect:
            ######## CELL DETECTION AND ROI EXTRACTION ##############
            t11=tic()
            print('----------- ROI DETECTION AND EXTRACTION')
            ops1[ipl] = roiextract.roi_detect_and_extract(ops1[ipl])
            ops = ops1[ipl]
            fpath = ops['save_path']
            print('----------- Total %0.2f sec.'%(toc(t11)))

            ######### SPIKE DECONVOLUTION ###############
            t11=tic()
            print('----------- SPIKE DECONVOLUTION')
            F = np.load(os.path.join(fpath,'F.npy'))
            Fneu = np.load(os.path.join(fpath,'Fneu.npy'))
            redcell = np.load(os.path.join(fpath,'redcell.npy'))
            dF = F - ops['neucoeff']*Fneu
            spks = dcnv.oasis(dF, ops)
            np.save(os.path.join(ops['save_path'],'spks.npy'), spks)
            print('----------- Total %0.2f sec.'%(toc(t11)))

            # save as matlab file
            if ('save_mat' in ops) and ops['save_mat']:
                stat = np.load(os.path.join(fpath,'stat.npy'), allow_pickle=True)
                iscell = np.load(os.path.join(fpath,'iscell.npy'))
                matpath = os.path.join(ops['save_path'],'Fall.mat')
                io.savemat(matpath, {'stat': stat,
                                     'ops': ops,
                                     'F': F,
                                     'Fneu': Fneu,
                                     'spks': spks,
                                     'redcell': redcell,
                                     'iscell': iscell})
        else:
            print("WARNING: skipping cell detection (ops['roidetect']=False)")
        print('Plane %d processed in %0.2f sec (can open in GUI).'%(ipl,toc(t1)))
        print('total = %0.2f sec.'%(toc(t0)))
        ipl += 1 #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'] and roidetect:
        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('TOTAL RUNTIME %0.2f sec'%toc(t0))
    return ops1
Ejemplo n.º 2
0
def register_binary(ops, refImg=None):
    ''' registration of binary files '''
    # if ops is a list of dictionaries, each will be registered separately
    if (type(ops) is list) or (type(ops) is np.ndarray):
        for op in ops:
            op = register_binary(op)
        return ops

    # make blocks for nonrigid
    if ops['nonrigid']:
        ops = utils.make_blocks(ops)

    ops['nframes'] = get_nFrames(ops)

    # check number of frames and print warnings
    if ops['nframes']<50:
        raise Exception('ERROR: the total number of frames should be at least 50 ')
    if ops['nframes']<200:
        print('WARNING: number of frames is below 200, unpredictable behaviors may occur')

    if 'do_regmetrics' in ops:
        do_regmetrics = ops['do_regmetrics']
    else:
        do_regmetrics = True


    # compute reference image
    if refImg is not None:
        print('WARNING: using reference frame given, will not compute registration metrics')
        do_regmetrics = False
    else:
        t0 = time.time()
        refImg = pick_init(ops)
        print('Reference frame, %0.2f sec.'%(toc(t0)))
    ops['refImg'] = refImg

    # get binary file paths
    raw = 'keep_movie_raw' in ops and ops['keep_movie_raw'] and 'raw_file' in ops and os.path.isfile(ops['raw_file'])
    reg_file_align, reg_file_alt, raw_file_align, raw_file_alt = bin_paths(ops, raw)

    k = 0
    nfr = 0

    # register binary to reference image
    ops, offsets = register_binary_to_ref(ops, refImg, reg_file_align, raw_file_align)

    if ops['nchannels']>1:
        ops = apply_shifts_to_binary(ops, offsets, reg_file_alt, raw_file_alt)

    ops['yoff'] = offsets[0]
    ops['xoff'] = offsets[1]
    ops['corrXY'] = offsets[2]
    if ops['nonrigid']:
        ops['yoff1'] = offsets[3]
        ops['xoff1'] = offsets[4]
        ops['corrXY1'] = offsets[5]

    # compute valid region
    # ignore user-specified bad_frames.npy
    ops['badframes'] = np.zeros((ops['nframes'],), np.bool)
    if os.path.isfile(os.path.join(ops['data_path'][0], 'bad_frames.npy')):
        badframes = np.load(os.path.join(ops['data_path'][0], 'bad_frames.npy'))
        badframes = badframes.flatten().astype(int)
        ops['badframes'][badframes] = True
        print(ops['badframes'].sum())
    # return frames which fall outside range
    ops = compute_crop(ops)

    if 'ops_path' in ops:
        np.save(ops['ops_path'], ops)

    # compute metrics for registration
    if do_regmetrics and ops['nframes']>=2000:
        t0=tic()
        ops = regmetrics.get_pc_metrics(ops)
        print('Registration metrics, %0.2f sec.'%(toc(t0)))

    if 'ops_path' in ops:
        np.save(ops['ops_path'], ops)
    return ops