def load_and_plot_scores_regressions(
        sub_path="Intercept_surprise_100_Surprisenp1_RepeatAlter_RepeatAlternp1",
        filter_name='Stand',
        scores_name='scores--remapped_gtmbaselined_clean.npy'):

    from jr.plot import pretty_decod

    results_path = op.join(config.result_path, 'linear_models', filter_name,
                           sub_path)

    scores_all = []
    for subject in config.subjects_list:
        subject_path = op.join(results_path, subject)
        score = np.load(op.join(subject_path, scores_name))
        scores_all.append(score)

    scores_all = np.asarray(scores_all)

    pretty_decod(np.mean(scores_all, 1),
                 np.linspace(-0.048, 0.65, scores_all.shape[-1]),
                 chance=0)
    plt.title("Score " + filter_name)
    plt.show()
    gat.estimators_ = [gat.estimators_[t] for t in toi_]
    gat.train_times_['times'] = [gat.train_times_['times'][t] for t in toi_]
    gat.train_times_['slices'] = [gat.train_times_['slices'][t] for t in toi_]
    # predict all trials, including absent to keep cv scheme
    gat.predict(epochs[sel_gat])

    # subscore on present only
    gat.scores_ = subscore(gat, sel, y[sel])
    scores.append(gat.scores_)

    # Save classifier results
    save([gat, analysis, sel_gat, events],
         'decod',
         subject=subject,
         analysis=analysis['name'] + '_probe_to_target')

times = epochs.times[:-5]
score_diag = np.array([np.diag(np.array(score)[4:, :]) for score in scores])
p_val = stats(score_diag)
pretty_decod(score_diag,
             times=times,
             sig=p_val < .05,
             color=analysis['color'],
             fill=True)
# toi_ = np.where((times >= toi[0]) & (times < toi[1]))[0]
toi_ = np.where(p_val < .05)[0]
score = np.mean(score_diag[:, toi_], axis=1)
print(np.mean(score, axis=0),
      np.std(score, axis=0) / np.sqrt(len(score_diag)), np.min(p_val[toi_]),
      times[toi_])
        scores = -np.array(results['bias'][:, ii, jj, ...])
        p_val = results['bias_pval'][ii, jj, :, :]
        pretty_gat(scores.mean(0), times=times, ax=axes[ii, jj],
                   colorbar=False, clim=[-.1, .1], sig=p_val < .05)
        axes[ii, jj].axvline(.800, color='k')
        axes[ii, jj].axhline(.800, color='k')
pretty_axes(axes, ylabelpad=-15, xticks=np.linspace(-.100, 1.400, 13),
            xticklabels=['', 0] + [''] * 13 + [1400, ''])
pretty_colorbar(cax=fig.add_axes([.88, .25, .02, .55]), ax=axes[0, 0])
report.add_figs_to_section(fig, 'gat', 'bias')

# Plot bias of TG diagonal (i.e. decoding bias)
fig, ax = plt.subplots(1, figsize=[7., 2.])
scores = np.array([np.diag(s) for s in results['bias'][:, 0, 1, ...]])
p_val = np.diag(results['bias_pval'][0, 1, :, :])
pretty_decod(-scores, ax=ax, times=times, color=cmap(1.), sig=p_val < .05,
             fill=True)
ax.axvline(.800, color='k')
ax.set_xlabel('Times', labelpad=-10)
fig.tight_layout()
report.add_figs_to_section(fig, 'diagonal', 'bias')


def quick_stats(x, ax=None):
    """Report significant bias in each toi for unseen and seen"""
    pvals = [wilcoxon(ii)[1] for ii in x.T]
    sig = [['', '*'][p < .05] for p in pvals]
    m = np.nanmean(x, axis=0)
    s = np.nanstd(x, axis=0)
    print(m, s, pvals)
    if ax is not None:
        for x_, (y_, sig_) in enumerate(zip(m / 2., sig)):
                  fill_color=analysis['color'])
    for ax in axs:
        ax.axvline(.800, color='k')
        if analysis['typ'] == 'regress':
            ax.set_ylabel('R', labelpad=-15)
        elif analysis['typ'] == 'categorize':
            ax.set_ylabel('AUC', labelpad=-15)
        else:
            ax.set_ylabel('rad.', labelpad=-15)
        ax.set_yticklabels(['', '', '%.2f' % ax.get_yticks()[2]])
    ax.set_xlabel('Times', labelpad=-10)
    report.add_figs_to_section(fig_offdiag, 'slices', analysis['name'])

    # Decod
    ax_diag = fig_alldiag.add_subplot(ax_diag)
    pretty_decod(scores_diag, times=times, sig=p_values_diag < alpha,
                 chance=chance, color=analysis['color'], fill=True, ax=ax_diag)
    xlim, ylim = ax_diag.get_xlim(), np.array(ax_diag.get_ylim())
    # ylim[1] = np.ceil(ylim[1] * 10) / 10.
    sem = scores_diag.std(0) / np.sqrt(len(scores_diag))
    ylim = [np.min(scores_diag.mean(0) - sem),
            np.max(scores_diag.mean(0) + sem)]
    ax_diag.set_ylim(ylim)
    ax_diag.axvline(.800, color='k')
    ax_diag.set_xticklabels([int(x) if x in np.linspace(0., 1000., 11) else ''
                             for x in np.round(1e3 * ax_diag.get_xticks())])
    if ax_diag != axes_alldiag[-1]:
        # bottom suplot
        ax_diag.set_xlabel('')
        ax_diag.set_xticklabels([])
        ax_diag.xaxis.set_visible(False)
    elif ax_diag == axes_alldiag[0]:
    for roi in rois:
        idx = np.where([roi in this_roi for this_roi in labels])[0]
        scores.append(np.mean(data[:, idx, :], axis=1))
    p_values = stats(np.transpose(scores, [1, 2, 0]) - chance, n_jobs=-1)

    fig, axes = plt.subplots(len(rois), 1, sharex=True, sharey=True,
                             figsize=[10, 40])
    cmap = plt.get_cmap('rainbow')
    colors = cmap(np.linspace(0., 1., len(rois)))
    ylim = [2, -2]

    # Plot
    details_toi, details_roi = list(), list()
    for ii, (score, p_val, ax, color, roi) in enumerate(zip(
            scores, p_values.T, axes, colors, rois)):
        pretty_decod(score, sig=p_val < .05, times=times-np.min(times),
                     color=color, ax=ax, fill=True, chance=chance)

        xlim = ax.get_xlim()
        ylim[0] = min([ylim[0], ax.get_ylim()[0]])
        ylim[1] = max([ylim[1], ax.get_ylim()[1]])
        ax.set_ylim(ylim)
        ax.set_yticks(ylim)
        ax.set_yticklabels(['%.3f' % y for y in ylim])
        ax.set_xticklabels([int(x) if x in np.linspace(0., 1200., 11) else ''
                            for x in np.round(1e3 * ax.get_xticks())])
        if ax != axes[-1]:
            ax.set_xlabel('')
            ax.set_xticklabels([])
        elif ax == axes[0]:
            ax.text(0, ylim[1], 'Target',  backgroundcolor='w', ha='center',
                    va='top')
            all_pval_tois[vis, t, :] = stats(score_toi - analysis['chance'])
    save([all_score_tois, all_pval_tois, times], 'score', analysis=ana_name)
    return [all_score_tois, all_pval_tois, times]


# Main plotting
colors = dict(visibility=plt.get_cmap('bwr')(np.linspace(0, 1, 4.)),
              contrast=plt.get_cmap('hot_r')([.5, .75, 1.]))

# Loop across visibility and orientation analyses
for analysis in analyses:
    # Plot correlation of decoding score with visibility and contrast
    scores, R, times = _analyze_continuous(analysis)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=[20, 10])
    sig = stats(R['visibility']) < .05
    pretty_decod(-R['visibility'], times=times, sig=sig, ax=ax1,
                 color='purple', fill=True)
    sig = stats(R['contrast']) < .05
    pretty_decod(-R['contrast'], times=times, sig=sig, ax=ax2,
                 color='orange', fill=True)
    report.add_figs_to_section([fig], ['continuous regress'], analysis['name'])

    # Plot decoding score for each visibility level
    all_scores, score_pvals, times = _subscore_pipeline(analysis)
    if 'circAngle' in analysis['name']:
        all_scores /= 2.  # from circle to half circle
    figs, axes = list(), list()
    for vis in range(4):
        fig, ax = plt.subplots(1, figsize=[14, 11])
        scores = all_scores[:, vis, ...]
        p_val = score_pvals[vis]
        pretty_gat(np.nanmean(scores, axis=0), times=times,
    gat, _, sel_gat, _ = load('decod', subject=subject, analysis='probe_phase')
    # only keep estimators after probe onset
    times = gat.train_times_['times']
    toi_ = np.where((times >= (.800 + toi[0])) & (times < (.800 + toi[1])))[0]
    gat.estimators_ = [gat.estimators_[t] for t in toi_]
    gat.train_times_['times'] = [gat.train_times_['times'][t] for t in toi_]
    gat.train_times_['slices'] = [gat.train_times_['slices'][t] for t in toi_]
    # predict all trials, including absent to keep cv scheme
    gat.predict(epochs[sel_gat])

    # subscore on present only
    gat.scores_ = subscore(gat, sel, y[sel])
    scores.append(gat.scores_)

    # Save classifier results
    save([gat, analysis, sel_gat, events], 'decod', subject=subject,
         analysis=analysis['name'] + '_probe_to_target')

times = epochs.times[:-5]
score_diag = np.array([np.diag(np.array(score)[4:, :]) for score in scores])
p_val = stats(score_diag)
pretty_decod(score_diag, times=times, sig=p_val < .05, color=analysis['color'],
             fill=True)
# toi_ = np.where((times >= toi[0]) & (times < toi[1]))[0]
toi_ = np.where(p_val < .05)[0]
score = np.mean(score_diag[:, toi_], axis=1)
print(np.mean(score, axis=0),
      np.std(score, axis=0) / np.sqrt(len(score_diag)),
      np.min(p_val[toi_]),
      times[toi_])
        axes[ii, jj].axvline(.800, color='k')
        axes[ii, jj].axhline(.800, color='k')
pretty_axes(axes,
            ylabelpad=-15,
            xticks=np.linspace(-.100, 1.400, 13),
            xticklabels=['', 0] + [''] * 13 + [1400, ''])
pretty_colorbar(cax=fig.add_axes([.88, .25, .02, .55]), ax=axes[0, 0])
report.add_figs_to_section(fig, 'gat', 'bias')

# Plot bias of TG diagonal (i.e. decoding bias)
fig, ax = plt.subplots(1, figsize=[7., 2.])
scores = np.array([np.diag(s) for s in results['bias'][:, 0, 1, ...]])
p_val = np.diag(results['bias_pval'][0, 1, :, :])
pretty_decod(-scores,
             ax=ax,
             times=times,
             color=cmap(1.),
             sig=p_val < .05,
             fill=True)
ax.axvline(.800, color='k')
ax.set_xlabel('Times', labelpad=-10)
fig.tight_layout()
report.add_figs_to_section(fig, 'diagonal', 'bias')


def quick_stats(x, ax=None):
    """Report significant bias in each toi for unseen and seen"""
    pvals = [wilcoxon(ii)[1] for ii in x.T]
    sig = [['', '*'][p < .05] for p in pvals]
    m = np.nanmean(x, axis=0)
    s = np.nanstd(x, axis=0)
    print(m, s, pvals)
Ejemplo n.º 9
0
        0.176,  0.18 ,  0.184,  0.188,  0.192,  0.196,  0.2  ,  0.204,
        0.208,  0.212,  0.216,  0.22 ,  0.224,  0.228,  0.232,  0.236,
        0.24 ,  0.244,  0.248,  0.252,  0.256,  0.26 ,  0.264,  0.268,
        0.272,  0.276,  0.28 ,  0.284,  0.288,  0.292,  0.296,  0.3  ,
        0.304,  0.308,  0.312,  0.316,  0.32 ,  0.324,  0.328,  0.332,
        0.336,  0.34 ,  0.344,  0.348,  0.352,  0.356,  0.36 ,  0.364,
        0.368,  0.372,  0.376,  0.38 ,  0.384,  0.388,  0.392,  0.396,
        0.4  ,  0.404,  0.408,  0.412,  0.416,  0.42 ,  0.424,  0.428,
        0.432,  0.436,  0.44 ,  0.444,  0.448,  0.452,  0.456,  0.46 ,
        0.464,  0.468,  0.472,  0.476,  0.48 ,  0.484,  0.488,  0.492,
        0.496,  0.5  ,  0.504,  0.508,  0.512,  0.516,  0.52 ,  0.524,
        0.528,  0.532,  0.536,  0.54 ,  0.544,  0.548,  0.552,  0.556,
        0.56 ,  0.564,  0.568,  0.572,  0.576,  0.58 ,  0.584,  0.588,
        0.592,  0.596,  0.6  ]

pretty_decod(scores_regression_surprise,times)
plt.plot(times,scores_regression_surprise.T)
plt.title('r2 score CV 4 surprise')
plt.xlabel('time (s)')
plt.xticks([0,0.25,0.5])
plt.axvline([0])
plt.axvline([0.25])
plt.axvline([0.5])
plt.show()

betas_dict = {'intercept':[],'RepeatAlter':[],'RepeatAlternp1':[],'surpriseN':[],'surpriseNp1':[]}
mean_evoked = {'intercept':[],'RepeatAlter':[],'RepeatAlternp1':[],'surpriseN':[],'surpriseNp1':[]}
for key in betas_dict:
    for subj in config.subjects_list:
        base_path = config.result_path + '/linear_models/reg_repeataltern_surpriseOmegainfinity/' + subj +'/_cvbeta_'+key+'-ave.fif'
        evo = mne.read_evokeds(base_path)
        if analysis['typ'] == 'regress':
            ax.set_ylabel('R', labelpad=-15)
        elif analysis['typ'] == 'categorize':
            ax.set_ylabel('AUC', labelpad=-15)
        else:
            ax.set_ylabel('rad.', labelpad=-15)
        ax.set_yticklabels(['', '', '%.2f' % ax.get_yticks()[2]])
    ax.set_xlabel('Times', labelpad=-10)
    report.add_figs_to_section(fig_offdiag, 'slices', analysis['name'])

    # Decod
    ax_diag = fig_alldiag.add_subplot(ax_diag)
    pretty_decod(scores_diag,
                 times=times,
                 sig=p_values_diag < alpha,
                 chance=chance,
                 color=analysis['color'],
                 fill=True,
                 ax=ax_diag)
    xlim, ylim = ax_diag.get_xlim(), np.array(ax_diag.get_ylim())
    # ylim[1] = np.ceil(ylim[1] * 10) / 10.
    sem = scores_diag.std(0) / np.sqrt(len(scores_diag))
    ylim = [
        np.min(scores_diag.mean(0) - sem),
        np.max(scores_diag.mean(0) + sem)
    ]
    ax_diag.set_ylim(ylim)
    ax_diag.axvline(.800, color='k')
    ax_diag.set_xticklabels([
        int(x) if x in np.linspace(0., 1000., 11) else ''
        for x in np.round(1e3 * ax_diag.get_xticks())
Ejemplo n.º 11
0
        print('There is something wrong with the filename')

    inds = np.where(filter_times)
    scores_to_avg_filter = scores_to_avg[:, inds[0], inds[0]]

    signif = np.zeros((scores_to_avg.shape[1], 1))
    p_vals = stats.stats_cluster_based_permutation_test(scores_to_avg_filter -
                                                        chance[k])
    sig = (p_vals < 0.05)

    for l, ll in enumerate(inds[0]):
        signif[ll] = sig[l]

    pretty_decod(np.mean(scores_to_avg, axis=0),
                 times=time_list,
                 chance=chance[k],
                 sfreq=100,
                 sig=signif,
                 fill=True)
    fig = plt.gcf()
    fig.savefig(fig_path + 'diagonal_' + names[k])
    plt.close('all')

# ============= fancy plot ============

# COMP
# hemifield, location-within-hemifield, retinotopic, decade symbolic, unit symbolic, quantity

conditions = [
    ['/hemifield/scores_*_standard_-400_1000_comp.pkl'],
    [
        '/location/scores_*_standard_-400_1000_comp_left_hemifield.pkl',
    return [all_score_tois, all_pval_tois, times]


# Main plotting
colors = dict(visibility=plt.get_cmap('bwr')(np.linspace(0, 1, 4.)),
              contrast=plt.get_cmap('hot_r')([.5, .75, 1.]))

# Loop across visibility and orientation analyses
for analysis in analyses:
    # Plot correlation of decoding score with visibility and contrast
    scores, R, times = _analyze_continuous(analysis)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=[20, 10])
    sig = stats(R['visibility']) < .05
    pretty_decod(-R['visibility'],
                 times=times,
                 sig=sig,
                 ax=ax1,
                 color='purple',
                 fill=True)
    sig = stats(R['contrast']) < .05
    pretty_decod(-R['contrast'],
                 times=times,
                 sig=sig,
                 ax=ax2,
                 color='orange',
                 fill=True)
    report.add_figs_to_section([fig], ['continuous regress'], analysis['name'])

    # Plot decoding score for each visibility level
    all_scores, score_pvals, times = _subscore_pipeline(analysis)
    if 'circAngle' in analysis['name']:
        all_scores /= 2.  # from circle to half circle
Ejemplo n.º 13
0
# get some time informations (TODO stats)
fname = op.join(data_path, 's%i_%s_fit.pkl' % (subject, contrast))
with open(fname, 'rb') as f:
    gat = pickle.load(f)
times = gat.train_times_['times']

# plot scores
fig_diag, axes_diag = plt.subplots(len(contrasts), 1, figsize=[2, 5])
cmap = plt.get_cmap('rainbow')
colors = cmap(np.linspace(0., 1., len(contrasts)))
for contrast, color, ax in zip(contrasts, colors, axes_diag):
    scores = all_scores[contrast]
    # plot diagonal
    scores_diag = np.array([np.diag(x) for x in scores])
    pretty_decod(scores_diag, times=times, color=color, ax=ax, chance=.5)
    ax.set_title(contrast)
    if ax != axes_diag[-1]:
        ax.set_xticklabels([])
        ax.set_xlabel('')
    # plot gat
    fig, ax = plt.subplots(1, figsize=[5, 5])
    pretty_gat(np.mean(scores, axis=0), times=times, chance=.5)
    report.add_figs_to_section(fig, 'GAT %s' % contrast, contrast)
    # plot slices
    tois = np.arange(0., .600, .100)
    fig, axes = plt.subplots(len(tois), 1, figsize=[5, 5])
    pretty_slices(scores, tois=tois, times=times, chance=.5, axes=axes)
    report.add_figs_to_section(fig, 'Slices %s' % contrast, contrast)
fig_diag.tight_layout()
report.add_figs_to_section(fig_diag, 'Diagonal', 'All')