Example #1
0
def plot_tuning_split_by_run_state(df, savepath):

    running_threshold = 1.0  # cm/s
    directions, contrasts = grating_params()

    MIN_SESSIONS = 3
    MIN_CELLS = 3  #per session

    areas, cres = dataset_params()
    for area in areas:
        for cre in cres:

            session_IDs = get_sessions(df, area, cre)
            num_sessions = len(session_IDs)

            if num_sessions >= MIN_SESSIONS:

                curve_dict = {}
                num_sessions_included = 0
                for i_session, session_ID in enumerate(session_IDs):

                    sweep_table = load_sweep_table(savepath, session_ID)
                    mse = load_mean_sweep_events(savepath, session_ID)
                    condition_responses, blank_responses = compute_mean_condition_responses(
                        sweep_table, mse)

                    p_all = chi_square_all_conditions(sweep_table, mse,
                                                      session_ID, savepath)
                    all_idx = np.argwhere(p_all < SIG_THRESH)[:, 0]

                    mean_sweep_running = load_mean_sweep_running(
                        session_ID, savepath)
                    is_run = mean_sweep_running >= running_threshold

                    run_responses, stat_responses, run_blank, stat_blank = condition_response_running(
                        sweep_table, mse, is_run)

                    condition_responses = center_direction_zero(
                        condition_responses)
                    run_responses = center_direction_zero(run_responses)
                    stat_responses = center_direction_zero(stat_responses)

                    peak_dir, __ = get_peak_conditions(condition_responses)

                    if len(all_idx) >= MIN_CELLS:
                        curve_dict = populate_curve_dict(
                            curve_dict, run_responses, run_blank, all_idx,
                            'all_run', peak_dir)
                        curve_dict = populate_curve_dict(
                            curve_dict, stat_responses, stat_blank, all_idx,
                            'all_stat', peak_dir)
                        num_sessions_included += 1

                if num_sessions_included >= MIN_SESSIONS:
                    plot_from_curve_dict(curve_dict, 'all', area, cre,
                                         num_sessions_included, savepath)
Example #2
0
def plot_all_waterfalls(df, savepath, scale='blank_subtracted_NLL'):

    areas, cres = dataset_params()
    for area in areas:
        for cre in cres:
            session_IDs = get_sessions(df, area, cre)

            if len(session_IDs) > 0:

                #sort by direction using event magnitude
                direction_order = get_cell_order_direction_sorted(
                    df, area, cre, savepath)

                #display response significance
                resp, blank, p_all = pool_sessions(session_IDs,
                                                   area + '_' + cre,
                                                   savepath,
                                                   scale=scale)
                resp = center_direction_zero(resp)
                condition_responses = resp[p_all < SIG_THRESH]

                dirXcon_mat = concatenate_contrasts(condition_responses)
                dirXcon_mat = dirXcon_mat[direction_order]
                dirXcon_mat = move_all_negative_to_bottom(dirXcon_mat)

                plot_full_waterfall(
                    dirXcon_mat, cre,
                    shorthand(area) + '_' + shorthand(cre) + '_full', scale,
                    savepath)
def plot_y(X, y, area, cre, savename, savepath):

    (num_conditions, num_params) = np.shape(X)

    directions, contrasts = grating_params()

    stat_resp, run_resp, stat_blank_resp, run_blank_resp = extract_tuning_curves(
        y)

    stat_resp = center_direction_zero(
        stat_resp.reshape(1, len(directions), len(contrasts)))[0]
    run_resp = center_direction_zero(
        run_resp.reshape(1, len(directions), len(contrasts)))[0]

    plot_pooled_mat(run_resp - run_blank_resp, area, cre,
                    'GLM_run_' + savename, savepath)
    plot_pooled_mat(stat_resp - stat_blank_resp, area, cre,
                    'GLM_stat_' + savename, savepath)
Example #4
0
def get_cell_order_direction_sorted(df, area, cre, savepath):

    session_IDs = get_sessions(df, area, cre)

    resp, blank, p_all = pool_sessions(session_IDs,
                                       area + '_' + cre,
                                       savepath,
                                       scale='event')

    resp = center_direction_zero(resp)
    condition_responses = resp[p_all < SIG_THRESH]

    peak_dir, peak_con = get_peak_conditions(condition_responses)
    direction_mat = select_peak_contrast(condition_responses, peak_con)
    direction_order = sort_by_weighted_peak_direction(direction_mat)

    return direction_order
Example #5
0
def plot_single_cell_example(df,
                             savepath,
                             cre,
                             example_cell,
                             example_session_idx=0):

    directions, contrasts = grating_params()

    session_IDs = get_sessions(df, 'VISp', cre)
    session_ID = session_IDs[example_session_idx]

    mse = load_mean_sweep_events(savepath, session_ID)
    sweep_table = load_sweep_table(savepath, session_ID)

    condition_responses, __ = compute_mean_condition_responses(
        sweep_table, mse)
    condition_SEM, __ = compute_SEM_condition_responses(sweep_table, mse)
    p_all = chi_square_all_conditions(sweep_table, mse, session_ID, savepath)

    sig_resp = condition_responses[p_all < SIG_THRESH]
    sig_SEM = condition_SEM[p_all < SIG_THRESH]

    #shift zero to center:
    directions = [-135, -90, -45, 0, 45, 90, 135, 180]
    sig_resp = center_direction_zero(sig_resp)
    sig_SEM = center_direction_zero(sig_SEM)

    #full direction by contrast response heatmap
    plt.figure(figsize=(6, 4))
    ax = plt.subplot2grid((5, 5), (0, 0), rowspan=5, colspan=2)
    ax.imshow(sig_resp[example_cell],
              vmin=0.0,
              interpolation='nearest',
              aspect='auto',
              cmap='plasma')
    ax.set_ylabel('Direction (deg)', fontsize=14)
    ax.set_xlabel('Contrast (%)', fontsize=14)
    ax.set_xticks(np.arange(len(contrasts)))
    ax.set_xticklabels([str(int(100 * x)) for x in contrasts], fontsize=10)
    ax.set_yticks(np.arange(len(directions)))
    ax.set_yticklabels([str(x) for x in directions], fontsize=10)

    peak_dir_idx, peak_con_idx = get_peak_conditions(sig_resp)

    #contrast tuning at peak direction
    contrast_means = sig_resp[example_cell, peak_dir_idx[example_cell], :]
    contrast_SEMs = sig_SEM[example_cell, peak_dir_idx[example_cell], :]
    ax = plt.subplot2grid((5, 5), (0, 3), rowspan=2, colspan=2)
    ax.errorbar(np.log(contrasts), contrast_means, contrast_SEMs)
    ax.set_xticks(np.log(contrasts))
    ax.set_xticklabels([str(int(100 * x)) for x in contrasts], fontsize=10)
    ax.set_xlabel('Contrast (%)', fontsize=14)
    ax.set_ylabel('Response', fontsize=14)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

    #direction tuning at peak contrast
    direction_means = sig_resp[example_cell, :, peak_con_idx[example_cell]]
    direction_SEMs = sig_SEM[example_cell, :, peak_con_idx[example_cell]]
    ax = plt.subplot2grid((5, 5), (3, 3), rowspan=2, colspan=2)
    ax.errorbar(np.arange(len(directions)), direction_means, direction_SEMs)
    ax.set_xlim(-0.07, 7.07)
    ax.set_xticks(np.arange(len(directions)))
    ax.set_xticklabels([str(x) for x in directions], fontsize=10)
    ax.set_xlabel('Direction (deg)', fontsize=14)
    ax.set_ylabel('Response', fontsize=14)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

    plt.tight_layout(w_pad=-5.5, h_pad=0.1)
    plt.savefig(savepath + shorthand(cre) + '_example_cell.svg', format='svg')
    plt.close()
def plot_single_cell_tuning_curves(session_ID, savepath, cre, example_cell,
                                   plot_path, figure_format):

    directions, contrasts = cu.grating_params()

    mse = 3000.0 * cu.load_mean_sweep_events(savepath, session_ID)
    sweep_table = cu.load_sweep_table(savepath, session_ID)

    condition_responses, blank_responses = cm.compute_mean_condition_responses(
        sweep_table, mse)
    condition_SEM, __ = cm.compute_SEM_condition_responses(sweep_table, mse)

    #shift zero to center:
    directions = [-135, -90, -45, 0, 45, 90, 135, 180]
    condition_resp = cu.center_direction_zero(condition_responses)
    condition_SEM = cu.center_direction_zero(condition_SEM)

    #full direction by contrast response heatmap
    plt.figure(figsize=(7, 4))
    ax = plt.subplot2grid((5, 5), (0, 3), rowspan=5, colspan=2)
    im = ax.imshow(condition_resp[example_cell],
                   vmin=0.0,
                   interpolation='nearest',
                   aspect='auto',
                   cmap='plasma')
    ax.set_ylabel('Direction (deg)', fontsize=12)
    ax.set_xlabel('Contrast (%)', fontsize=12)
    ax.set_xticks(np.arange(len(contrasts)))
    ax.set_xticklabels([str(int(100 * x)) for x in contrasts], fontsize=12)
    ax.set_yticks(np.arange(len(directions)))
    ax.set_yticklabels([str(x) for x in directions], fontsize=12)
    cbar = plt.colorbar(im, ax=ax)
    cbar.set_label('Event magnitude per second (%)', fontsize=12)

    peak_dir_idx, peak_con_idx = cm.get_peak_conditions(condition_resp)

    #contrast tuning at peak direction
    contrast_means = condition_resp[example_cell,
                                    peak_dir_idx[example_cell], :]
    contrast_SEMs = condition_SEM[example_cell, peak_dir_idx[example_cell], :]

    y_max = 1.1 * np.max(contrast_means + contrast_SEMs)

    ax = plt.subplot2grid((5, 5), (0, 0), rowspan=2, colspan=2)
    ax.errorbar(np.log(contrasts),
                contrast_means,
                contrast_SEMs,
                linewidth=0.7,
                color='b')
    ax.plot([np.log(contrasts[0]), np.log(contrasts[-1])],
            [blank_responses[example_cell], blank_responses[example_cell]],
            linewidth=0.7,
            linestyle='--',
            color='b')
    ax.set_xticks(np.log(contrasts))
    ax.set_xticklabels([str(int(100 * x)) for x in contrasts], fontsize=12)
    ax.tick_params(axis='y', labelsize=12)
    ax.set_xlabel('Contrast (%)', fontsize=12)
    ax.set_ylabel('Event magnitude per second (%)  ', fontsize=12)
    ax.set_ylim([0, y_max])
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.set_title('@ ' + str(directions[peak_dir_idx[example_cell]]) +
                 ' degrees',
                 fontsize=12)

    #direction tuning at peak contrast
    direction_means = condition_resp[example_cell, :,
                                     peak_con_idx[example_cell]]
    direction_SEMs = condition_SEM[example_cell, :, peak_con_idx[example_cell]]
    ax = plt.subplot2grid((5, 5), (3, 0), rowspan=2, colspan=2)
    ax.errorbar(np.arange(len(directions)),
                direction_means,
                direction_SEMs,
                linewidth=0.7,
                color='b')
    ax.plot([0, len(directions) - 1],
            [blank_responses[example_cell], blank_responses[example_cell]],
            linestyle='--',
            color='b',
            linewidth=0.7)
    ax.set_xlim(-0.07, 7.07)
    ax.set_xticks(np.arange(len(directions)))
    ax.set_xticklabels([str(x) for x in directions], fontsize=12)
    ax.tick_params(axis='y', labelsize=12)

    ax.set_xlabel('Direction (deg)', fontsize=12)
    #ax.set_ylabel('Response',fontsize=14)
    ax.set_ylim([0, y_max])
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.set_title('@ ' + str(int(100 * contrasts[peak_con_idx[example_cell]])) +
                 '% contrast',
                 fontsize=12)

    plt.tight_layout(w_pad=-5.5, h_pad=0.1)

    if figure_format == 'svg':
        plt.savefig(plot_path + cre + '_' + str(session_ID) + '_cell_' +
                    str(example_cell) + '_tuning_curves.svg',
                    format='svg')
    else:
        plt.savefig(plot_path + cre + '_' + str(session_ID) + '_cell_' +
                    str(example_cell) + '_tuning_curves.png',
                    dpi=300)
    plt.close()