def sfig1(): df = pd.merge( add_net_meta(normalize(acw.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index().rename(columns={"metric": "acw"}), add_net_meta(normalize(acz.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index().rename(columns={"metric": "acz"}), on=["task", "subject", "region", "net_meta"], sort=False) fig, axs = plt.subplots(2, 2, figsize=(15, 10), sharex=True, sharey="col") ax = axs[0, 0] sns.distplot(df.groupby(["task", "subject"]).mean().reset_index().acw, ax=ax, rug=False, kde=True, hist=True) ax.set(xlabel=f"Normalized ACW-50 for subjects") ax.grid(False) ax = axs[1, 0] sns.distplot(df.groupby(["task", "region"]).mean().reset_index().acw, ax=ax, rug=False, kde=True, hist=True) ax.set(xlabel=f"Normalized ACW-50 for regions") ax.grid(False) ax = axs[0, 1] sns.distplot(df.groupby(["task", "subject"]).mean().reset_index().acz, ax=ax, rug=False, kde=True, hist=True) ax.grid(False) ax.set(xlabel=f"Normalized ACW-0 for subjects") ax = axs[1, 1] sns.distplot(df.groupby(["task", "region"]).mean().reset_index().acz, ax=ax, rug=False, kde=True, hist=True) ax.set(xlabel=f"Normalized ACW-0 for regions") ax.grid(False) fig.subplots_adjust(wspace=0.1, hspace=0.1) savefig(fig, "sfig1.dist.nolabel")
def corr(): df = add_net_meta(normalize(acw.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index() df1 = add_net_meta(normalize(acz.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index() correlations = [] for task in task_order(): dft = and_filter(df, task=task) subjects = dft.subject.unique() df_corr = np.zeros((len(subjects), len(subjects))) for i in range(len(subjects)): df_corr[i, i] = 1 x = and_filter(dft, subject=subjects[i]).metric for j in range(i + 1, len(subjects)): y = and_filter(dft, subject=subjects[j]).metric df_corr[i, j] = df_corr[j, i] = pearsonr(x, y)[0] correlations.append(df_corr) correlations1 = [] for task in task_order(): dft = and_filter(df1, task=task) subjects = dft.subject.unique() df_corr = np.zeros((len(subjects), len(subjects))) for i in range(len(subjects)): df_corr[i, i] = 1 x = and_filter(dft, subject=subjects[i]).metric for j in range(i + 1, len(subjects)): y = and_filter(dft, subject=subjects[j]).metric df_corr[i, j] = df_corr[j, i] = pearsonr(x, y)[0] correlations1.append(df_corr) min_val, max_val = 0, 1 ticks = np.arange(min_val, max_val, 0.1) cmap = cm.get_cmap("jet") fig, axs = plt.subplots(2, 4, figsize=(20, 10)) for i, task in enumerate(task_order()): ax = axs[0, i] isc = correlations[i] pp = ax.imshow(isc, interpolation="nearest", vmin=min_val, vmax=max_val, cmap=cmap) ax.xaxis.tick_top() down, up = sms.DescrStatsW(isc[np.triu_indices(len(isc), 1)]).tconfint_mean() ax.set_title(f"ACW-50 {task}: {down:.2f}:{up:.2f}") for i, task in enumerate(task_order()): ax = axs[1, i] isc = correlations1[i] pp = ax.imshow(isc, interpolation="nearest", vmin=min_val, vmax=max_val, cmap=cmap) ax.xaxis.tick_top() down, up = sms.DescrStatsW(isc[np.triu_indices(len(isc), 1)]).tconfint_mean() ax.set_title(f"ACW-0 {task}: {down:.2f}:{up:.2f}") cbar_ax = fig.add_axes([0.92, 0.125, 0.02, 0.755]) cbar = fig.colorbar(pp, cax=cbar_ax, ticks=ticks, orientation="vertical") savefig(fig, "isc", low=True)
def fig2(): unique_networks = net_order(tpt_name) palette = make_net_palette(unique_networks) dfs = [[], []] for i, lib in enumerate([acw, acz]): for avg in ["net_meta", "network"]: df = and_filter(add_net_meta(lib.gen_long_data(tpt_name), get_net("pmc", tpt_name)) .groupby(["task", "subject", avg]).mean().reset_index(), task="Rest") df.metric *= 1000 dfs[i].append(df) fig = plt.figure(figsize=(15, 15)) gs = fig.add_gridspec(2, 2, width_ratios=[0.6, 2], hspace=0.1, wspace=0.2) for row, ((df1, df2), label, (min_val, max_val)) in enumerate( zip(dfs, ["ACW-50", "ACW-0"], [(28, 50), (150, 500)])): ax = fig.add_subplot(gs[row, 0]) sns.barplot(data=df1, x="net_meta", y="metric", order=["P", "M", "C"], ax=ax) ax.set(xlabel="", ylabel=f"Mean \u00B1 %95 CI (ms)", ylim=[min_val, max_val]) ax.set_xticklabels(PMC_labels if row == 1 else [], rotation=45) ax = fig.add_subplot(gs[row, 1]) sns.barplot(data=df2, x="network", y="metric", order=unique_networks, palette=palette, ax=ax) ax.set(xlabel="", ylabel="", ylim=[min_val, max_val], yticklabels=[]) ax.set_xticklabels(ax.get_xticklabels() if row == 1 else [], rotation=45) ax.set_title(label, ha="center", loc="left", x=0.3) savefig(fig, "fig2.bar")
def fig7(): df = pd.merge( add_net_meta(normalize(acw.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index().rename(columns={"metric": "acw"}), add_net_meta(normalize(acz.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index().rename(columns={"metric": "acz"}), on=["task", "subject", "region", "net_meta"], sort=False) fig, axs = plt.subplots(1, 2, figsize=(15, 10), sharex=True, sharey="col") ax = axs[0, 0] sns.distplot(df.acw, ax=ax, rug=False, kde=True, hist=True, kde_kws={"bw": 0.02}) ax.set(xlabel=f"Normalized ACW-50") ax.grid(False) ax = axs[1, 0] for meta, label, color in zip(["P", "M", "C"], PMC_labels, PMC_colors): sns.distplot(and_filter(df, net_meta=meta).acw, ax=ax, rug=False, kde=True, hist=False, kde_kws={"bw": 0.02}, label=label, color=color) ax.set(xlabel=f"Normalized ACW-50") ax.grid(False) ax = axs[0, 1] sns.distplot(df.acz, ax=ax, rug=False, kde=True, hist=True, ) ax.grid(False) ax.set(xlabel=f"Normalized ACW-0") ax = axs[1, 1] for meta, label, color in zip(["P", "M", "C"], PMC_labels, PMC_colors): sns.distplot(and_filter(df, net_meta=meta).acz, ax=ax, rug=False, kde=True, hist=False, label=label, color=color) ax.set(xlabel=f"Normalized ACW-0") ax.grid(False) fig.subplots_adjust(wspace=0.1, hspace=0.1) savefig(fig, "fig7.dist.nolabel")
def fig4(): unique_networks = net_order(tpt_name) dfs = [[], []] for i, lib in enumerate([acw, acz]): for avg in ["net_meta", "network"]: df = add_net_meta( and_filter(lib.gen_long_data(tpt_name), subject=lib.find_shared_subjects(tpt_name, task_order())) \ .groupby(["task", "subject", "network", "region"]).mean().reset_index() \ .groupby(["subject", "network", "region"]).apply(calc_pchange).reset_index().drop("level_3", 1), get_net("pmc", tpt_name)).groupby(["task", "subject", avg]).mean().reset_index() df.pchange *= -1 dfs[i].append(df) fig = plt.figure(figsize=(20, 15)) gs = fig.add_gridspec(2, 2, width_ratios=[0.7, 1.9], hspace=0.1, wspace=0.2) for row, ((df1, df2), label, (min_val, max_val)) in enumerate( zip(dfs, ["ACW-50", "ACW-0"], [(None, None), (None, None)])): ax = fig.add_subplot(gs[row, 0]) sns.barplot(data=df1, x="task", y="pchange", hue="net_meta", order=task_order(False), hue_order=["P", "M", "C"], ax=ax) ax.set(xlabel="", ylabel=f"Mean \u00B1 %95 CI (% change)", ylim=[min_val, max_val]) if row == 0: h, l = ax.get_legend_handles_labels() ax.legend(h, PMC_labels, loc=3, ncol=3, mode="expand", borderaxespad=0, bbox_to_anchor=(0., 1.08, 1, 0.), handletextpad=0.1) else: ax.get_legend().remove() ax.set_xticklabels(ax.get_xticklabels() if row == 1 else [], rotation=45) ax = fig.add_subplot(gs[row, 1]) sns.barplot(data=df2, x="network", y="pchange", hue="task", palette=task_colors, hue_order=task_order(False), order=unique_networks, ax=ax) ax.set(xlabel="", ylabel="", ylim=[min_val, max_val], yticklabels=[]) if row == 0: lgn = ax.legend(loc=3, ncol=6, mode="expand", borderaxespad=0, bbox_to_anchor=(0., 1.08, 1, 0.)) else: ax.get_legend().remove() ax.set_xticklabels(ax.get_xticklabels() if row == 1 else [], rotation=45) ax.set_title(label, ha="center", loc="left", x=0.3) savefig(fig, "fig4.bar", extra_artists=(lgn,))
def kruskal(): for mes, mes_name in zip([acw, acz], ["acw", "acz"]): for task in task_order(True): for meta in ["net_meta", "network"]: df = mes.gen_long_data(tpt_name)\ .and_filter(task=task)\ .add_net_meta(get_net("pmc", tpt_name))\ .groupby(["subject", meta]).mean().reset_index()\ .convert_column(metric=lambda x: x * 1000) # feather.write_feather(df, f"r/{mes_name}.{task}.{meta}.feather") model = ols(f'metric ~ C({meta})', data=df) result = model.fit() result.summary() robust = None if result.diagn["omnipv"] > 0.05 else "hc3" aov_table = anova_table( sm.stats.anova_lm(result, typ=2, robust=robust)) print(aov_table.to_string()) if meta == "net_meta": mc = MultiComparison(df.metric, df[meta]) mc_results = mc.tukeyhsd() print(mc_results)
def regression(): df = pd.merge( normalize(add_net_meta(normalize(acw.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index().rename( columns={"metric": "acw"}), "acw"), normalize(add_net_meta(normalize(acz.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index().rename( columns={"metric": "acz"}), "acz"), on=["task", "subject", "region", "net_meta"], sort=False) df = and_filter(df, NOTnet_meta="M") X = df.iloc[:, -2:].values y = df.net_meta.map({"C": 0, "P": 1}).values X = sm.add_constant(X) model = sm.Logit(y, X) result = model.fit() print(result.summary()) mfx = result.get_margeff() print(mfx.summary())
def isc(): df = and_filter(add_net_meta(normalize(acw.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index(), NOTnet_meta="M") df1 = and_filter(add_net_meta(normalize(acz.gen_long_data(tpt_name), columns="metric"), get_net("pmc", tpt_name)) \ .groupby(["task", "subject", "region", "net_meta"]).mean().reset_index(), NOTnet_meta="M") correlations = [] for task in task_order(): temp = [] for meta in ["C", "P"]: dft = and_filter(df, task=task, net_meta=meta) subjects = dft.subject.unique() df_corr = np.zeros((len(subjects), len(subjects))) for i in range(len(subjects)): df_corr[i, i] = 1 x = and_filter(dft, subject=subjects[i]).metric for j in range(i + 1, len(subjects)): y = and_filter(dft, subject=subjects[j]).metric df_corr[i, j] = df_corr[j, i] = pearsonr(x, y)[0] temp.append(df_corr) correlations.append(temp) correlations1 = [] for task in task_order(): temp = [] for meta in ["C", "P"]: dft = and_filter(df1, task=task, net_meta=meta) subjects = dft.subject.unique() df_corr = np.zeros((len(subjects), len(subjects))) for i in range(len(subjects)): df_corr[i, i] = 1 x = and_filter(dft, subject=subjects[i]).metric for j in range(i + 1, len(subjects)): y = and_filter(dft, subject=subjects[j]).metric df_corr[i, j] = df_corr[j, i] = pearsonr(x, y)[0] temp.append(df_corr) correlations1.append(temp) min_val, max_val = 0, 1 ticks = np.arange(min_val, max_val + 0.01, 0.1) cmap = cm.get_cmap("jet") fig, axs = plt.subplots(4, 4, figsize=(20, 20)) cbar_ax = fig.add_axes([0.92, 0.125, 0.02, 0.755]) for i, task in enumerate(task_order()): for j, meta in enumerate(["Core", "Periphery"]): ax = axs[j, i] isc = correlations[i][j] xy_ticks = np.linspace(1, len(isc), 10, dtype=np.int) pp = ax.imshow(isc, interpolation="nearest", vmin=min_val, vmax=max_val, cmap=cmap) ax.set(xticks=xy_ticks, yticks=xy_ticks) ax.xaxis.tick_top() down, up = sms.DescrStatsW(isc[np.triu_indices(len(isc), 1)]).tconfint_mean() if j == 0: ax.set_title(task, fontsize=18) else: ax.set_xticks([]) if i == 0: ax.set_ylabel(meta, fontsize=18) else: ax.set_yticks([]) # ax.set_title(f"ACW-50: {down:.2f}:{up:.2f}") for i, task in enumerate(task_order()): for j, meta in enumerate(["Core", "Periphery"]): ax = axs[j + 2, i] isc = correlations1[i][j] xy_ticks = np.linspace(1, len(isc), 10, dtype=np.int) pp = ax.imshow(isc, interpolation="nearest", vmin=min_val, vmax=max_val, cmap=cmap) ax.set(xticks=[], yticks=xy_ticks) ax.xaxis.tick_top() down, up = sms.DescrStatsW(isc[np.triu_indices(len(isc), 1)]).tconfint_mean() if i == 0: ax.set_ylabel(meta, fontsize=18) else: ax.set_yticks([]) # ax.set_title(f"ACW-0: {down:.2f}:{up:.2f}") cbar = fig.colorbar(pp, cax=cbar_ax, ticks=ticks, orientation="vertical") txt1 = fig.text(0.06, 0.67, "ACW-50", rotation=90, fontsize=18) txt2 = fig.text(0.06, 0.27, "ACW-0", rotation=90, fontsize=18) savefig(fig, "isc1", extra_artists=(txt1, txt2))