Beispiel #1
0
def run_normcorr(folder_name, files):

    parameters = yaml.load(open(folder_name + '/parameters.yaml', 'r'),
                           Loader=PrettySafeLoader)

    # ## STARTING THE CLUSTER
    # For now, pool mapping is used only for the motion correction algorithm. Instantiating "procs = None" works as well, it all depends on your local configuration.

    c, procs, n_processes = setup_cluster(backend='local',
                                          n_processes=8,
                                          single_thread=False)
    print('started cluster')

    # ## MOTION CORRECTION
    # This function implements Normcorre (see...). All the outputs and associated information are stored in one HDF5 per session that will eventually be used for detection of calcium transients.

    files_to_load = [str(file) for file in files[:]]

    print('processing files')
    print(files_to_load)

    data, video_info = normcorre(files_to_load, procs,
                                 parameters['motion_correction'])

    data.close()

    print('finished motion correction')
    video_info  # some information about the dataset

    return ()
def run_CNMFE(path_to_hdf5_file, folder_name):
    # ## WORKING WITH HDF5
    #
    # The HDF5 file (called data in this example) contains a Numpy array called 'movie' of size (number of frames, number of pixels). The cool thing about HDF5 format is that you can attach attributes (i.e. extra information) to every dataset or group it contains. Here, we attached the attributes of the frame dimension and the recording duration to the dataset 'movie'.
    # load hdf5 file with motion corrected data
    parameters = yaml.load(open(folder_name + '/parameters.yaml', 'r'),
                           Loader=PrettySafeLoader)

    c, procs, n_processes = setup_cluster(backend='local',
                                          n_processes=8,
                                          single_thread=False)
    print('started cluster')

    data = hd.File(path_to_hdf5_file, 'r+')

    #print(data['movie'].attrs['dims']) # width and height of the frame
    #print(data['movie'].attrs['duration']) # number of frames
    #print(data.attrs['folder'])
    #print(data.attrs['filename'])

    # In the following example, the datasets contains additional attributes such as the animal's identity and frame rate. Both are located at the root of the HDF5 file.
    # Check the first frame of the movie to see if it's in correct order.

    #dims = data['movie'].attrs['dims']
    #frame_0 = data['movie'][0].reshape(dims)
    #figure()
    #imshow(frame_0)
    #show()

    # ## RUNNING CNMF-E

    parameters['cnmfe']['init_params']['thresh_init'] = 1.2
    parameters['cnmfe']['init_params']['min_corr'] = 0.8
    parameters['cnmfe']['init_params']['min_pnr'] = 1.5

    print('starting cnmfe')
    print('file: ' + path_to_hdf5_file)
    cnm = CNMFE(data, parameters['cnmfe'])
    cnm.fit(procs)

    # # VISUALIZATION
    ##Save A and C output before visualization
    dims = cnm.dims
    C = cnm.C.value.copy()
    A = cnm.A.value.copy()

    # A is normalized to 1 for display
    A -= np.vstack(A.min(1))
    A /= np.vstack(A.max(1))
    Atotal = A.sum(0).reshape(dims)
    out_path = folder_name + '/'
    pd.DataFrame(C).to_hdf(out_path + 'C', key='df')
    pd.DataFrame(A).to_hdf(out_path + 'A', key='df')

    procs.terminate()

    cn, pnr = cnm.get_correlation_info()
            out_file.write(data)
        files = glob.glob(folder_name + '/*.avi')
        if len(files) == 0:
            print("No avi files found, please provide one at least")

    #############################################################################################################
    # LOADING PARAMETERS
    #############################################################################################################
    parameters = yaml.load(
        open(os.path.join(folder_name, 'parameters.yaml'), 'r'))

    #############################################################################################################
    # start a cluster for parallel processing
    #############################################################################################################
    c, procs, n_processes = setup_cluster(backend='local',
                                          n_processes=8,
                                          single_thread=False)

    #############################################################################################################
    # MOTION CORRECTION | create the motion_corrected.hdf5 file
    #############################################################################################################
    # Here we update the parameters to
    parameters_glob = yaml.load(open(folder_name + '/parameters.yaml', 'r'))
    parameters = parameters_glob['motion_correction']

    # This parameter controls the number of times the whole movie is corrected
    parameters['nb_round'] = 1

    # This parameter allows us to save the original movie to compare with the corrected one
    parameters['save_original'] = True