Пример #1
0
def run_s2p(ops={}, db={}):
    """ run suite2p pipeline

        need to provide a 'data_path' or 'h5py'+'h5py_key' in db or ops

        Parameters
        ----------
        ops : :obj:`dict`, optional
            specify 'nplanes', 'nchannels', 'tau', 'fs'
        db : :obj:`dict`, optional
            specify 'data_path' or 'h5py'+'h5py_key' here or in ops

        Returns
        -------
            ops1 : list
                list of ops for each plane

    """

    t0 = time.time()
    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"
                    )
                    try:
                        # delete previous offsets
                        del op['yoff'], op['xoff'], op['corrXY']
                    except:
                        print('no offsets to delete')
            # 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 = h5.h5py_to_binary(ops)
            print('time %4.2f sec. Wrote h5py to binaries for %d planes' %
                  (time.time() - (t0), len(ops1)))
        else:
            if 'mesoscan' in ops and ops['mesoscan']:
                ops1 = tiff.mesoscan_to_binary(ops)
                print('time %4.2f sec. Wrote tifs to binaries for %d planes' %
                      (time.time() - (t0), len(ops1)))
            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' %
                      (time.time() - (t0), len(ops1)))
            else:
                ops1 = tiff.tiff_to_binary(ops)
                print('time %4.2f sec. Wrote tifs to binaries for %d planes' %
                      (time.time() - (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 = time.time()
        if not flag_binreg:
            ######### REGISTRATION #########
            t11 = time.time()
            print('----------- REGISTRATION')
            ops1[ipl] = register.register_binary(ops1[ipl])  # register binary
            np.save(fpathops1, ops1)  # save ops1
            print('----------- Total %0.2f sec' % (time.time() - t11))

            if ops['two_step_registration'] and ops['keep_movie_raw']:
                print('----------- REGISTRATION STEP 2')
                print('(making mean image (excluding bad frames)')
                refImg = reference.sampled_mean(ops1[ipl])
                ops1[ipl] = register.register_binary(ops1[ipl],
                                                     refImg,
                                                     raw=False)
                np.save(fpathops1, ops1)  # save ops1
                print('----------- Total %0.2f sec' % (time.time() - 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 = time.time()
                ops1[ipl] = metrics.get_pc_metrics(ops1[ipl])
                print('Registration metrics, %0.2f sec.' % (time.time() - 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 = time.time()
            print('----------- ROI DETECTION AND EXTRACTION')
            ops1[ipl] = extract.detect_and_extract(ops1[ipl])
            ops = ops1[ipl]
            fpath = ops['save_path']
            print('----------- Total %0.2f sec.' % (time.time() - t11))

            ######### SPIKE DECONVOLUTION ###############
            t11 = time.time()
            print('----------- SPIKE DECONVOLUTION')
            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('----------- Total %0.2f sec.' % (time.time() - 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')
                savemat(
                    matpath, {
                        'stat': stat,
                        'ops': ops,
                        'F': F,
                        'Fneu': Fneu,
                        'spks': spks,
                        'iscell': iscell
                    })
        else:
            print("WARNING: skipping cell detection (ops['roidetect']=False)")
        print('Plane %d processed in %0.2f sec (can open in GUI).' %
              (ipl, time.time() - t1))
        print('total = %0.2f sec.' % (time.time() - 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:
        save.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' % (time.time() - t0))
    return ops1
Пример #2
0
def run_s2p(ops={}, db={}):
    """ run suite2p pipeline

        need to provide a 'data_path' or 'h5py'+'h5py_key' in db or ops

        Parameters
        ----------
        ops : :obj:`dict`
            specify 'nplanes', 'nchannels', 'tau', 'fs'
        db : :obj:`dict`
            specify 'data_path' or 'h5py'+'h5py_key' here or in ops

        Returns
        -------
            ops : :obj:`dict`
                ops settings used to run suite2p

    """
    t0 = time.time()
    ops = {**default_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)
    if 'save_path0' not in ops or len(ops['save_path0']) == 0:
        ops['save_path0'] = os.path.split(
            ops['h5py'])[0] if ops.get('h5py') else ops['data_path'][0]

    # check if there are binaries already made
    if 'save_folder' not in ops or len(ops['save_folder']) == 0:
        ops['save_folder'] = 'suite2p'
    save_folder = os.path.join(ops['save_path0'], ops['save_folder'])
    os.makedirs(save_folder, exist_ok=True)
    plane_folders = natsorted([
        f.path for f in os.scandir(save_folder)
        if f.is_dir() and f.name[:5] == 'plane'
    ])
    if len(plane_folders) > 0:
        ops_paths = [os.path.join(f, 'ops.npy') for f in plane_folders]
        ops_found_flag = all(
            [os.path.isfile(ops_path) for ops_path in ops_paths])
        binaries_found_flag = all([
            os.path.isfile(os.path.join(f, 'data_raw.bin'))
            or os.path.isfile(os.path.join(f, 'data.bin'))
            for f in plane_folders
        ])
        files_found_flag = ops_found_flag and binaries_found_flag
    else:
        files_found_flag = False

    if files_found_flag:
        print(f'FOUND BINARIES AND OPS IN {ops_paths}')
    # if not set up files and copy tiffs/h5py to binary
    else:
        if len(ops['h5py']):
            ops['input_format'] = 'h5'
        elif ops.get('mesoscan'):
            ops['input_format'] = 'mesoscan'
        elif HAS_HAUS:
            ops['input_format'] = 'haus'
        elif not 'input_format' in ops:
            ops['input_format'] = 'tif'

        # 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.copy()),
            'bruker':
            io.ome_to_binary,
        }
        if ops['input_format'] in convert_funs:
            ops0 = convert_funs[ops['input_format']](ops.copy())
            if isinstance(ops, list):
                ops0 = ops0[0]
        else:
            ops0 = io.tiff_to_binary(ops.copy())
        plane_folders = natsorted([
            f.path for f in os.scandir(save_folder)
            if f.is_dir() and f.name[:5] == 'plane'
        ])
        ops_paths = [os.path.join(f, 'ops.npy') for f in plane_folders]
        print('time {:0.2f} sec. Wrote {} frames per binary for {} planes'.
              format(time.time() - t0, ops0['nframes'], len(plane_folders)))

    if ops.get('multiplane_parallel'):
        io.server.send_jobs(save_folder)
        return None
    else:
        for ipl, ops_path in enumerate(ops_paths):
            op = np.load(ops_path, allow_pickle=True).item()

            # make sure yrange and xrange are not overwritten
            for key in default_ops().keys():
                if key not in [
                        'data_path', 'save_path0', 'fast_disk', 'save_folder',
                        'subfolders'
                ]:
                    if key in op and key in ops:
                        op[key] = ops[key]

            print('>>>>>>>>>>>>>>>>>>>>> PLANE %d <<<<<<<<<<<<<<<<<<<<<<' %
                  ipl)
            op = run_plane(op, ops_path=ops_path)
            print('Plane %d processed in %0.2f sec (can open in GUI).' %
                  (ipl, op['timing']['total_plane_runtime']))
        run_time = time.time() - t0
        print('total = %0.2f sec.' % run_time)

        #### COMBINE PLANES or FIELDS OF VIEW ####
        if len(ops_paths) > 1 and ops['combined'] and ops.get(
                'roidetect', True):
            print('Creating combined view')
            io.combined(save_folder, save=True)

        # save to NWB
        if ops.get('save_NWB'):
            print('Saving in nwb format')
            io.save_nwb(save_folder)

        print('TOTAL RUNTIME %0.2f sec' % (time.time() - t0))
        return op
Пример #3
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 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 flag_binreg:
        print('SKIPPING REGISTRATION...')
    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 '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'))
            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,
                        'iscell': iscell
                    })
        else:
            print("WARNING: skipping cell detection (ops['roidetect']=False)")
        print(
            'Plane %d out of %d planes processed in %0.2f sec (can open in GUI).'
            % (ipl, len(ops1), 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
Пример #4
0
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
Пример #5
0
def run_s2p(ops={}, db={}):
    """ run suite2p pipeline

        need to provide a 'data_path' or 'h5py'+'h5py_key' in db or ops

        Parameters
        ----------
        ops : :obj:`dict`, optional
            specify 'nplanes', 'nchannels', 'tau', 'fs'
        db : :obj:`dict`, optional
            specify 'data_path' or 'h5py'+'h5py_key' here or in ops

        Returns
        -------
            ops1 : list
                list of ops for each plane

    """
    t0 = time.time()
    ops = {**default_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)
    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 binaries already made
    if 'save_folder' not in ops or len(ops['save_folder']) == 0:
        ops['save_folder'] = 'suite2p'
    save_folder = os.path.join(ops['save_path0'], ops['save_folder'])
    os.makedirs(save_folder, exist_ok=True)
    fpathops1 = os.path.join(save_folder, 'ops1.npy')
    plane_folders = natsorted([
        f.path for f in os.scandir(save_folder)
        if f.is_dir() and f.name[:5] == 'plane'
    ])
    if len(plane_folders) > 0:
        ops_paths = [os.path.join(f, 'ops.npy') for f in plane_folders]
        ops_found_flag = all(
            [os.path.isfile(ops_path) for ops_path in ops_paths])
        binaries_found_flag = all([
            os.path.isfile(os.path.join(f, 'data_raw.bin'))
            or os.path.isfile(os.path.join(f, 'data.bin'))
            for f in plane_folders
        ])
        files_found_flag = ops_found_flag and binaries_found_flag
    else:
        files_found_flag = False
    if files_found_flag:
        print(f'FOUND BINARIES AND OPS IN {ops_paths}')
    # if not set up files and copy tiffs/h5py to binary
    else:
        if not 'input_format' in ops.keys():
            ops['input_format'] = 'tif'
        if len(ops['h5py']):
            ops['input_format'] = 'h5'
        elif 'mesoscan' in ops and ops['mesoscan']:
            ops['input_format'] = 'mesoscan'
        elif HAS_HAUS:
            ops['input_format'] = 'haus'

        # 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.copy()),
            'bruker':
            io.ome_to_binary,
        }
        if ops['input_format'] in convert_funs:
            ops1 = convert_funs[ops['input_format']](ops.copy())
            print('time {:4.2f} sec. Wrote {} files to binaries for {} planes'.
                  format((time.time() - t0), ops['input_format'], len(ops1)))
        else:
            ops1 = io.tiff_to_binary(ops.copy())
            print(
                'time {:4.2f} sec. Wrote {} tiff frames to binaries for {} planes'
                .format(time.time() - t0, ops1[0]['nframes'], len(ops1)))
        plane_folders = natsorted([
            f.path for f in os.scandir(save_folder)
            if f.is_dir() and f.name[:5] == 'plane'
        ])
        ops_paths = [os.path.join(f, 'ops.npy') for f in plane_folders]

    ipl = 0

    ops1 = []
    for ipl, ops_path in enumerate(ops_paths):
        op = np.load(ops_path, allow_pickle=True).item()
        # make sure yrange and xrange are not overwritten
        if 'yrange' in ops: ops.pop('yrange')
        if 'xrange' in ops: ops.pop('xrange')
        op = {**op, **ops}
        print('>>>>>>>>>>>>>>>>>>>>> PLANE %d <<<<<<<<<<<<<<<<<<<<<<' % ipl)
        t1 = time.time()
        op = run_plane(op, ops_path=ops_path)
        print('Plane %d processed in %0.2f sec (can open in GUI).' %
              (ipl, time.time() - t1))
        ops1.append(op)
    print('total = %0.2f sec.' % (time.time() - t0))

    np.save(fpathops1, ops1)

    #### COMBINE PLANES or FIELDS OF VIEW ####
    if len(ops_paths) > 1 and ops['combined'] and (
        ('roidetect' in ops and ops['roidetect']) or 'roidetect' not in ops):
        print('Creating combined view')
        io.save_combined(save_folder)

    # save to NWB
    if 'save_NWB' in ops and ops['save_NWB']:
        print('Saving in nwb format')
        io.save_nwb(save_folder)

    print('TOTAL RUNTIME %0.2f sec' % (time.time() - t0))
    return ops1
Пример #6
0
def run_s2p(ops={}, db={}):
    i0 = tic()
    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}
            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 ops to list where each element is ops for each plane
        ops1 = utils.init_ops(ops)
        # copy tiff to a binary
        if len(ops['h5py']):
            ops1 = utils.h5py_to_binary(ops1)
            print('time %4.4f. Wrote h5py to binaries for %d planes' %
                  (toc(i0), len(ops1)))
        else:
            try:
                ops1 = utils.tiff_to_binary(ops1)
                print('time %4.4f. Wrote tifs to binaries for %d planes' %
                      (toc(i0), len(ops1)))
            except Exception as e:
                if HAS_HAUS:
                    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:
                    print('Unsupported file format: ' + str(e))
                    return
        # save ops1
        np.save(fpathops1, ops1)
    if not ops['do_registration']:
        flag_binreg = True
    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))
    elif files_found_flag:
        print('found ops1 and pre-registered binaries')
        print(ops1[0]['reg_file'])
        print('overwriting ops1 with new ops')
        print('skipping registration...')
    # registration is off, but previous binary was not found
    # need to initialize ops
    else:
        print('binary file created, but registration not performed')
    ######### 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')
            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