Exemple #1
0
def parse_data(res, excitatory=True):
    list_odor_on = res['DAQ_O_ON_F']
    list_odor_off = res['DAQ_O_OFF_F']
    list_water_on = res['DAQ_W_ON_F']
    list_data = res['data']

    for i, data in enumerate(list_data):
        start_time = time.time()
        p_list = []
        dff_list = []
        for cell_data in data:
            baseline = cell_data[:, config.baseline_start:list_odor_on[i] -
                                 config.baseline_end]
            baseline = baseline.flatten()
            data_trial_time_window = _rolling_window(
                cell_data, window=condition_config.p_window)
            data_time_trial_window = np.transpose(data_trial_time_window,
                                                  [1, 0, 2])
            data_time_pixels = data_time_trial_window.reshape(
                data_time_trial_window.shape[0], -1)

            #statistical significance
            if excitatory:
                f = lambda y: mannwhitneyu(
                    baseline, y, use_continuity=True, alternative='less')[-1]
            else:
                f = lambda y: mannwhitneyu(
                    baseline, y, use_continuity=True, alternative='greater')[-1
                                                                             ]
            p = []
            for y in data_time_pixels:
                try:
                    out = f(y)
                except:
                    out = 1
                p.append(out)
            while (len(p) < cell_data.shape[-1]):
                p.append(p[-1])

            #magnitude
            dff = psth.psth_helper.subtract_baseline(
                cell_data, config.baseline_start,
                list_odor_on[i] - config.baseline_end)
            dff = np.mean(dff, axis=0)

            p_list.append(np.array(p))
            dff_list.append(np.array(dff))
        res['dff'].append(np.array(dff_list))
        res['p'].append(np.array(p_list))
        elapsed = time.time() - start_time
        print('analyzed mouse {}, day {}, odor {}, in {} seconds'.format(
            res['mouse'][i], res['day'][i], res['odor'][i], elapsed))
    for key, val in res.items():
        res[key] = np.array(val)
    # res.pop('data')
    name_str = 'dict'
    if not excitatory:
        name_str += '_I'
    fio.save_pickle(save_path=save_path, save_name=name_str, data=res)
Exemple #2
0
def do_PCA(condition, PCAConfig, data_path, save_path):
    '''
    Run PCA experiment. For each mouse, data should be in format of (trials * time) X neurons,
    stacked for all odor conditions

    :param condition:
    :param PCAConfig:
    :param data_path:
    :param save_path:
    :return:
    '''
    data_pathnames = sorted(
        glob.glob(os.path.join(data_path, '*' + Config.mat_ext)))
    config_pathnames = sorted(
        glob.glob(os.path.join(data_path, '*' + Config.cons_ext)))

    list_of_all_data = np.array([Config.load_mat_f(d) for d in data_pathnames])
    list_of_all_cons = np.array(
        [Config.load_cons_f(d) for d in config_pathnames])
    mouse_names_per_file = np.array(
        [cons.NAME_MOUSE for cons in list_of_all_cons])
    mouse_names, list_of_mouse_ix = np.unique(mouse_names_per_file,
                                              return_inverse=True)

    if mouse_names.size != len(condition.paths):
        raise ValueError(
            "res has {0:d} mice, but filter has {1:d} mice".format(
                mouse_names.size, len(condition.paths)))

    for i, mouse_name in enumerate(mouse_names):
        ix = mouse_name == mouse_names_per_file

        start_day = 0
        list_of_cons = list_of_all_cons[ix][start_day:]
        list_of_data = list_of_all_data[ix][start_day:]
        for cons in list_of_cons:
            assert cons.NAME_MOUSE == mouse_name, 'Wrong mouse file!'

        if hasattr(condition, 'odors'):
            odor = condition.odors[i]
            csp = condition.csp[i]
        else:
            odor = condition.dt_odors[i] + condition.pt_csp[i]
            csp = condition.dt_csp[i] + condition.pt_csp[i]

        name = list_of_cons[0].NAME_MOUSE
        out, variance_explained = PCA(list_of_cons, list_of_data, odor, csp,
                                      PCAConfig)
        fio.save_pickle(save_path=save_path, save_name=name, data=out)
        print("Analyzed: {}. Variance explained: {}".format(
            name, variance_explained))
Exemple #3
0
def organizer_test_split(condition, decodeConfig, data_path, save_path):
    '''

    :param condition:
    :param decodeConfig:
    :param data_path:
    :param save_path:
    :return:
    '''
    data_pathnames = sorted(
        glob.glob(os.path.join(data_path, '*' + Config.mat_ext)))
    config_pathnames = sorted(
        glob.glob(os.path.join(data_path, '*' + Config.cons_ext)))

    list_of_all_data = np.array([Config.load_mat_f(d) for d in data_pathnames])
    list_of_all_cons = np.array(
        [Config.load_cons_f(d) for d in config_pathnames])
    mouse_names_per_file = np.array(
        [cons.NAME_MOUSE for cons in list_of_all_cons])
    mouse_names, list_of_mouse_ix = np.unique(mouse_names_per_file,
                                              return_inverse=True)

    if mouse_names.size != len(condition.paths):
        raise ValueError(
            "res has {0:d} mice, but filter has {1:d} mice".format(
                mouse_names.size, len(condition.paths)))

    start_days = decodeConfig.start_day
    end_days = decodeConfig.end_day
    days_per_mouse = []
    for x, y in zip(start_days, end_days):
        days_per_mouse.append(np.arange(x, y + 1))
    print(days_per_mouse)

    for i, mouse_name in enumerate(mouse_names):
        start_time = time.time()
        ix = mouse_name == mouse_names_per_file
        list_of_cons_ = list_of_all_cons[ix]
        list_of_data_ = list_of_all_data[ix]
        for cons in list_of_cons_:
            assert cons.NAME_MOUSE == mouse_name, 'Wrong mouse file!'

        if len(days_per_mouse[i]):
            list_of_cons = list_of_cons_[days_per_mouse[i]]
            list_of_data = list_of_data_[days_per_mouse[i]]

            cons = list_of_cons[0]
            cons_dict = cons.__dict__
            for key, value in cons_dict.items():
                if isinstance(value, list) or isinstance(value, np.ndarray):
                    pass
                else:
                    setattr(decodeConfig, key, value)

            if hasattr(condition, 'odors'):
                odor = condition.odors[i]
                csp = condition.csp[i]
            else:
                odor = condition.dt_odors[i]
                csp = condition.dt_csp[i]
                cons_odors = [cons.ODOR_UNIQUE for cons in list_of_cons]
                ix = [
                    i for i, unique_odors in enumerate(cons_odors)
                    if odor[0] in unique_odors
                ]
                list_of_cons = [list_of_cons[i] for i in ix]
                list_of_data = [list_of_data[i] for i in ix]

            scores_res = decoding.test_split(list_of_cons, list_of_data, odor,
                                             csp, decodeConfig)
            name = cons.NAME_MOUSE
            fio.save_json(save_path=save_path,
                          save_name=name,
                          config=decodeConfig)
            fio.save_pickle(save_path=save_path,
                            save_name=name,
                            data=scores_res)
            print("Analyzed: {0:s} in {1:.2f} seconds".format(
                name,
                time.time() - start_time))
    imdir = os.path.join(d, 'python_reg.tif')
    fi.imsave(imdir, mean_ims.astype('uint16'), photometric='minisblack')
    for i, im in enumerate(mean_ims):
        plt.imshow(im, cmap='gray')
        plt.xticks([])
        plt.yticks([])
        plot._easy_save(d, str(i))

    #saving
    p, experiment = os.path.split(d)
    p, mouse = os.path.split(p)
    save_dir = os.path.join(data_directory, condition.name)
    res = defaultdict(list)

    res['within_day_corrs'].append(within_day_corrs)
    res['within_day_corrs_average'].append(within_day_corr_average)
    res['within_day_crisp'].append(within_day_crisp)
    res['within_day_crisp_average'].append(np.mean(within_day_crisp))
    res['across_day_mean_corrs'].append(across_day_mean_corrs)
    res['across_day_mean_corrs_average'].append(across_day_mean_corrs_average)
    # res['within_day_mean_corrs'].append(within_day_mean_corrs)
    res['mouse'].append(mouse)
    res['experiment'].append(condition.name)

    for k, v in res.items():
        res[k] = np.array(v)
        if k in ['mouse', 'within_day_crisp', 'across_day_mean_corrs_average']:
            print('{}: {}'.format(k, v))

    file_io.save_pickle(save_path=save_dir, save_name=mouse, data=res)