Example #1
0
def main(directory):
    fictrac_folder = os.path.join(directory, 'fictrac')
    fictrac_raw = bbb.load_fictrac(fictrac_folder)

    fly = os.path.split(os.path.split(directory)[0])[1]
    expt = os.path.split(directory)[1]
    full_id = fly + ', ' + expt

    resolution = 10  #desired resolution in ms
    expt_len = 1000 * 30 * 60
    fps = 50  #of fictrac camera
    behaviors = ['dRotLabY', 'dRotLabZ']
    fictrac = {}
    for behavior in behaviors:
        if behavior == 'dRotLabY': short = 'Y'
        elif behavior == 'dRotLabZ': short = 'Z'
        fictrac[short] = bbb.smooth_and_interp_fictrac(fictrac_raw, fps,
                                                       resolution, expt_len,
                                                       behavior)
    xnew = np.arange(0, expt_len, resolution)

    make_2d_hist(fictrac, fictrac_folder, full_id, save=True, fixed_crop=True)
    make_2d_hist(fictrac, fictrac_folder, full_id, save=True, fixed_crop=False)
    make_velocity_trace(fictrac, fictrac_folder, full_id, xnew, save=True)

    return None
Example #2
0
def main(args):

    logfile = args['logfile']
    directory = args[
        'directory']  # directory will be a full path to a func/fictrac folder
    width = 120
    printlog = getattr(flow.Printlog(logfile=logfile), 'print_to_log')

    fictrac_raw = bbb.load_fictrac(directory)

    #fly = os.path.split(os.path.split(directory)[0])[1]
    #expt = os.path.split(directory)[1]
    full_id = ', '.join(directory.split('/')[-3:-1])

    resolution = 10  #desired resolution in ms
    expt_len = 1000 * 30 * 60
    fps = 50  #of fictrac camera
    behaviors = ['dRotLabY', 'dRotLabZ']
    fictrac = {}
    for behavior in behaviors:
        if behavior == 'dRotLabY': short = 'Y'
        elif behavior == 'dRotLabZ': short = 'Z'
        fictrac[short] = bbb.smooth_and_interp_fictrac(fictrac_raw, fps,
                                                       resolution, expt_len,
                                                       behavior)
    xnew = np.arange(0, expt_len, resolution)

    make_2d_hist(fictrac, directory, full_id, save=True, fixed_crop=True)
    make_2d_hist(fictrac, directory, full_id, save=True, fixed_crop=False)
    make_velocity_trace(fictrac, directory, full_id, xnew, save=True)
Example #3
0
def main(args):

    logfile = args['logfile']
    directory = args['directory']  # full fly func path
    behavior = args['behavior']
    printlog = getattr(flow.Printlog(logfile=logfile), 'print_to_log')

    #brain_path = os.path.join(directory, 'brain_zscored_green.nii')
    brain_path = os.path.join(directory,
                              'brain_zscored_green_high_pass_masked.nii')
    brain = np.asarray(nib.load(brain_path).get_data(), dtype='float32')

    timestamps = bbb.load_timestamps(os.path.join(directory, 'imaging'))
    fictrac_raw = bbb.load_fictrac(os.path.join(directory, 'fictrac'))

    resolution = 10  #desired resolution in ms
    expt_len = 1000 * 30 * 60
    fps = 50  #of fictrac camera

    fictrac_interp = interp_fictrac(fictrac_raw, fps, resolution, expt_len,
                                    timestamps, behavior)
    xnew = np.arange(0, expt_len, resolution)

    printlog("Performing Correlation on {}; behavior: {}".format(
        brain_path, behavior))
    corr_brain = np.zeros((256, 128, 49))
    for z in range(49):
        for i in range(256):
            for j in range(128):
                corr_brain[i, j,
                           z] = scipy.stats.pearsonr(fictrac_interp,
                                                     brain[i, j, z, :])[0]

    corr_directory = os.path.join(directory, 'corr')
    if not os.path.exists(corr_directory):
        os.mkdir(corr_directory)

    save_file = os.path.join(corr_directory,
                             '20201020_corr_{}.nii'.format(behavior))
    nib.Nifti1Image(corr_brain, np.eye(4)).to_filename(save_file)
    printlog("Saved {}".format(save_file))
Example #4
0
def main(directory):

    ### Load PCA
    save_file = os.path.join(directory, 'pca', 'scores_(spatial).npy')
    pca_spatial = np.load(save_file)
    save_file = os.path.join(directory, 'pca', 'loadings_(temporal).npy')
    pca_loadings = np.load(save_file)
    print('pca_loadings_shape: {}'.format(pca_loadings.shape))

    ### Load timestamps
    timestamps = bbb.load_timestamps(os.path.join(directory, 'imaging'))

    ### Load fictrac
    fictrac_raw = bbb.load_fictrac(os.path.join(directory, 'fictrac'))
    fictrac = bbb.smooth_and_interp_fictrac(fictrac_raw,
                                            fps=50,
                                            resolution=10,
                                            expt_len=1000 * 30 * 60,
                                            behavior='dRotLabY',
                                            timestamps=timestamps)

    ### Fit model
    num_pcs = 100
    Y_glm = fictrac
    X_glm = pca_loadings[:, :num_pcs]

    model = LassoCV().fit(X_glm, Y_glm)
    score = model.score(X_glm, Y_glm)

    brain_map = np.tensordot(model.coef_,
                             pca_spatial[:num_pcs, :, :, :],
                             axes=1)

    pca_glm_directory = os.path.join(directory, 'pca_glm')
    if not os.path.exists(pca_glm_directory):
        os.mkdir(pca_glm_directory)

    save_file = os.path.join(pca_glm_directory, 'forward.nii')
    bbb.save_brain(save_file, brain_map)
Example #5
0
 def __init__(self, fly_dir, timestamps):
     self.fictrac_raw = bbb.load_fictrac(
         os.path.join(fly_dir, 'fictrac'))
     self.timestamps = timestamps
Example #6
0
def main(args):

    logfile = args['logfile']
    directory = args['directory']  # full fly func path
    pca_subfolder = args['pca_subfolder']
    glm_date = args['glm_date']
    printlog = getattr(flow.Printlog(logfile=logfile), 'print_to_log')

    ### Load PCA ###
    pca_directory = os.path.join(directory, 'pca', pca_subfolder)
    printlog("performing glm on {}".format(pca_directory))
    file = os.path.join(pca_directory, 'scores_(spatial).npy')
    pca_spatial = np.load(file)
    file = os.path.join(pca_directory, 'loadings_(temporal).npy')
    pca_loadings = np.load(file)

    ### Load Fictrac and Timstamps###
    timestamps = bbb.load_timestamps(os.path.join(directory, 'imaging'))
    fictrac_raw = bbb.load_fictrac(os.path.join(directory, 'fictrac'))

    ### Prepare Fictrac ###
    resolution = 100  #desired resolution in ms
    expt_len = 1000 * 30 * 60
    fps = 50  #of fictrac camera
    behaviors = ['dRotLabY', 'dRotLabZ']
    fictrac = {}
    for behavior in behaviors:
        if behavior == 'dRotLabY': short = 'Y'
        elif behavior == 'dRotLabZ': short = 'Z'
        fictrac[short] = bbb.smooth_and_interp_fictrac(fictrac_raw,
                                                       fps,
                                                       resolution,
                                                       expt_len,
                                                       behavior,
                                                       timestamps=timestamps,
                                                       smoothing=51)
        fictrac[short] = fictrac[short] / np.std(fictrac[short])
    xnew = np.arange(0, expt_len, resolution)

    ### Fit GLM ###
    Y_glm = {}
    Y_glm['Y'] = fictrac['Y'].copy()
    Y_glm['Z'] = np.abs(fictrac['Z'].copy())

    models = {}
    num_pcs = 1000
    behaviors = ['Y', 'Z']
    for behavior in behaviors:
        t0 = time.time()
        models[behavior] = {'num_pcs': num_pcs, 'model': LassoCV()}
        X_glm = pca_loadings[:, :num_pcs]
        models[behavior]['model'].fit(X_glm, Y_glm[behavior])
        models[behavior]['score'] = models[behavior]['model'].score(
            X_glm, Y_glm[behavior])

        ### Construct Spatial Map ###
        coef = models[behavior]['model'].coef_
        spatial_map = np.tensordot(coef, pca_spatial[:1000, :, :, :], axes=1)

        ### Save map ###
        glm_directory = os.path.join(directory, 'glm')
        if not os.path.exists(glm_directory):
            os.mkdir(glm_directory)

        save_file = os.path.join(glm_directory,
                                 '{}_{}.nii'.format(glm_date, behavior))
        nib.Nifti1Image(spatial_map, np.eye(4)).to_filename(save_file)

        ### Save scores ###
        score_file = os.path.join(glm_directory,
                                  '{}_score_{}.txt'.format(glm_date, behavior))
        with open(score_file, "a") as f:
            f.write("{}:{}".format(behavior, models[behavior]['score']))