Example #1
0
def statistical_test(df, score, fdr_corr=True, test_type='nonparametric'):
    def cohen_d_2samp(x, y):
        nx = len(x)
        ny = len(y)
        dof = nx + ny - 2

        # 2 independent sample t test
        return (np.mean(x) - np.mean(y)) / np.sqrt(
            ((nx - 1) * np.std(x, ddof=1)**2 +
             (ny - 1) * np.std(y, ddof=1)**2) / dof)

    class_labels = plot_tasks.sort_class_labels(np.unique(df['class']))

    pval_rewir = []
    effs_rewir = []
    for clase in class_labels:

        scores = df.loc[df['class'] == clase, :]

        # reliability scores
        brain = scores.loc[scores['analysis'] == 'reliability', score].values
        rewire = scores.loc[scores['analysis'] == 'significance', score].values

        # ----------------------------------------------------------------------------
        # nonparametric Mann-Whitney U test
        if test_type == 'nonparametric':
            # rewired null model
            u, pval = stats.mannwhitneyu(brain,
                                         rewire,
                                         alternative='two-sided')
            eff_size = u / (len(brain) * len(rewire))
            if fdr_corr:
                pval = multipletests(pval, 0.05, 'bonferroni')[1].squeeze()

            pval_rewir.append(pval)
            effs_rewir.append(eff_size)

        # ----------------------------------------------------------------------------
        # parametric t-test
        if test_type == 'parametric':

            # rewired null model
            _, pval = stats.ttest_ind(brain, rewire, equal_var=False)
            eff_size = cohen_d_2samp(brain, rewire)
            if fdr_corr:
                pval = multipletests(pval, 0.05, 'bonferroni')[1].squeeze()

            pval_rewir.append(pval)
            effs_rewir.append(eff_size)

    pval_rewir = [float(p) for p in pval_rewir]

    return pval_rewir, effs_rewir
def scatterplot_net_prop_vs_scores_group(dynamics, coding, x, y):

    sns.set(style="ticks", font_scale=2.0)
    fig = plt.figure(figsize=(24, 8))
    COLORS = sns.color_palette("husl", 8)[:-1]

    for i, dyn_regime in enumerate(dynamics):

        ax = plt.subplot(1, len(dynamics), i + 1)

        df, _ = merge_net_props_n_scores(dyn_regime, coding)
        class_labels = plot_tasks.sort_class_labels(np.unique(df['class']))

        # scale values
        df[x] = (df[x] - min(df[x])) / (max(df[x]) - min(df[x]))
        df[y] = (df[y] - min(df[y])) / (max(df[y]) - min(df[y]))

        # average values for markers
        av_x = [
            df.query("`class` == @clase")[x].mean() for clase in class_labels
        ]
        av_y = [
            df.query("`class` == @clase")[y].mean() for clase in class_labels
        ]

        # standard deviation values for error bars
        sd_x = [
            df.query("`class` == @clase")[x].std() for clase in class_labels
        ]
        sd_y = [
            df.query("`class` == @clase")[y].std() for clase in class_labels
        ]

        tmp_df = pd.DataFrame(
            data=np.column_stack((av_x, sd_x, av_y, sd_y)),
            columns=[f'avg {x}', f'sd {x}', f'avg {y}', f'sd {y}'],
        )

        tmp_df['class'] = class_labels

        # scatter plot
        for j, clase in enumerate(class_labels):

            plt.errorbar(x=tmp_df.query("`class` == @clase")[f'avg {x}'],
                         y=tmp_df.query("`class` == @clase")[f'avg {y}'],
                         xerr=tmp_df.query("`class` == @clase")[f'sd {x}'],
                         yerr=tmp_df.query("`class` == @clase")[f'sd {y}'],
                         ecolor=COLORS[j])

            plt.scatter(x=tmp_df.query("`class` == @clase")[f'avg {x}'],
                        y=tmp_df.query("`class` == @clase")[f'avg {y}'],
                        s=200,
                        marker='D',
                        edgecolor=COLORS[j],
                        color=COLORS[j],
                        alpha=0.5)

        if i == 1: plt.xlabel(f'avg {x}')
        ax.xaxis.set_major_locator(MultipleLocator(0.2))
        plt.xlim(0, 1)

        if i == 0: plt.ylabel(f'avg {y}')
        if i == 1 or i == 2: ax.get_yaxis().set_ticklabels([])
        plt.ylim(0, 1)

        plt.title(dyn_regime)

    sns.despine(offset=10, trim=False)
    #    fig.savefig(fname=os.path.join('C:/Users/User/Dropbox/figures_RC/eps', f'{x}_{ANALYSIS}_scttplt.eps'), transparent=True, bbox_inches='tight', dpi=300)
    plt.show()
    plt.close()
df_rsn_scores = pd.concat(df_rsn_scores)
df_rsn_scores = df_rsn_scores.query("sample_id <= 999").reset_index(drop=True)

# scale avg encoding scores
score = 'performance'
min_score = np.min(df_rsn_scores[score].values)
max_score = np.max(df_rsn_scores[score].values)
df_rsn_scores[score] = (df_rsn_scores[score] - min_score) / (max_score -
                                                             min_score)

#%%
# boxplot
score = 'performance'
DYNAMICS = ['stable', 'edge_chaos', 'chaos']
class_labels = plot_tasks.sort_class_labels(np.unique(df_rsn_scores['class']))

for dyn_regime in DYNAMICS:

    print(
        f'---------------------------------------  {dyn_regime.upper()}  ---------------------------------------'
    )

    df = df_rsn_scores.loc[df_rsn_scores.dyn_regime == dyn_regime, :]

    plotting.boxplot(
        x='class',
        y=f'{score}',
        df=df,
        palette=sns.color_palette('husl', 5),
        suptitle=f'encoding - {dyn_regime}',
Example #4
0
    df_encoding = load_avg_scores_per_class(dyn_regime, 'encoding')
    df_decoding = load_avg_scores_per_class(dyn_regime, 'decoding')
    df_scores = pd.concat((df_encoding, df_decoding))

    # encoding vs decoding - jointplot
    plot_tasks.jointplot_enc_vs_dec(df_scores.copy(),
                                    score,
                                    scale=True,
#                                    minmax=(1,16),
                                    xlim=(0,1),
                                    ylim=(0,1),
                                    title=dyn_regime,
                                    )

    # information transfer
    class_labels = np.array(plot_tasks.sort_class_labels(np.unique(df_scores['class'])))

    # statistical tests
    t, pval = plot_tasks.ttest(df_scores.copy(),
                               score,
                               )

    eff_size = plot_tasks.effect_size(df_scores.copy(),
                                      score,
                                      )

    print("Intrinsic networks:")
    print(np.array(class_labels)[np.argsort(eff_size)])
    print(f'p-vals: {np.round(np.array(pval)[np.argsort(eff_size)],4)}')
    print(f'Effect sizes: {np.round(eff_size[np.argsort(eff_size)],3)}')