def get_r_scaled(x, scale="log2"):
x = np.asarray(x, dtype=float)
powers = np.arange(np.ceil(np.log2(x.min())), np.floor(np.log2(x.max())) + 1)
xlabels = 2 ** powers
if scale == "log10":
xscale = np.log10(x)
xticks = np.log10(xlabels)
elif scale == "log2":
xscale = np.log2(x)
xticks = np.log2(xlabels)
else:
xscale = x
xticks = xlabels
xlabel_str = [f"{r:g}" for r in xlabels]
return xscale, xticks, xlabel_str
plot_variants = {
'identity': {
'label': "Isotropic",
'tag_locs': [256, 512, 8192],
'tag_dx': -0.012,
'tag_dy': 0.0,
'tag_ha': "right",
},
'intercepts': {
'label': "LDSC Intercepts (Full)",
'tag_locs': [512, 2580, 6502],
'tag_dx': 0.012,
'tag_dy': 0.0,
'tag_ha': "left",
},
'shielded': {
'label': "LDSC Intercepts (Shielded)",
'tag_locs': [512],
'tag_dx': -0.012,
'tag_dy': 0.0,
'tag_ha': "right",
},
'shrinked': {
'label': "LDSC Intercepts (GLEANR pre-processing)",
'tag_locs': [512],
'tag_dx': -0.012,
'tag_dy': 0.0,
'tag_ha': "right",
},
}
fig = plt.figure(figsize = (20,16))
gs = fig.add_gridspec(nrows=2, ncols=2, wspace=0.2, hspace=0.4)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, 0])
ax4 = fig.add_subplot(gs[1, 1])
def make_spearman_plot(
ax, xcolname, ycolname,
xlabel, ylabel,
show_raw_ref=False,
line_colors=None,
):
# plot_variants
# recovery_df
# recovery_df_raw
if show_raw_ref:
ax.scatter(recovery_df_raw[xcolname], recovery_df_raw[ycolname],
marker="*", s=100, label="Raw data")
lines = {}
for vname, vparams in plot_variants.items():
df = recovery_df[vname]
lines[vname], = ax.plot(df[xcolname], df[ycolname], 'o-', label=vparams['label'])
if line_colors is None:
line_colors = {vname: lines[vname].get_color() for vname in plot_variants.keys()}
def make_tag_on_lines(df, rlist, dx, dy, color, ha='left'):
for r in rlist:
x, y = df.loc[r, xcolname], df.loc[r, ycolname]
ax.annotate(f'r={r}', (x,y), (x+dx,y+dy), color=color, ha=ha, va='center')
ax.axvline(0, color='gray', lw=1, ls='dotted')
for vname, vparams in plot_variants.items():
make_tag_on_lines(
recovery_df[vname], vparams['tag_locs'],
vparams['tag_dx'], vparams['tag_dy'],
line_colors[vname],
ha=vparams['tag_ha'],
)
ax.legend(handlelength=2)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
return line_colors
line_colors = make_spearman_plot(
ax1,
"spearman_vs_noise",
"spearman_vs_rg",
r"Noise contamination (Spearman vs $S$)",
r"Genetic recovery (Spearman vs $R_g$)",
show_raw_ref=True,
line_colors=None,
)
_ = make_spearman_plot(
ax2,
"partial_noise_given_rg",
"partial_rg_given_noise",
r"Partial Spearman vs $S$ given $R_g$",
r"Partial Spearman vs $R_g$ given $S$",
show_raw_ref=True,
line_colors=line_colors,
)
_ = make_spearman_plot(
ax3,
"spearman_vs_noise",
"spearman_vs_rg_clean",
r"Noise contamination (Spearman vs $S$)",
r"Spearman vs $R_g$ where $|G| < 0.05$",
show_raw_ref=True,
line_colors=line_colors,
)
ycolname = "spearman_vs_noise"
x, xticks, xlabels = get_r_scaled(rank_list, scale="log2")
for vname, vparams in plot_variants.items():
df = recovery_df[vname]
ax4.plot(x, df[ycolname], 'o-', label=vparams['label'], color=line_colors[vname])
ax4.axhline(0, color='gray', lw=1, ls='dotted')
ax4.axhspan(0.26, 0.59, color=line_colors['identity'], alpha=0.07)
ax4.set_xticks(xticks)
ax4.set_xticklabels(xlabels, rotation=90)
ax4.set_xlabel("Nuclear norm constraint r")
ax4.set_ylabel(r"Noise contamination (Spearman vs $S$)")
outfile_name=f"figures/pgc_recovery_contamination_tradeoff_78_traits"
plt.savefig(f"{outfile_name}.pdf", bbox_inches='tight')
plt.savefig(f"{outfile_name}.png", bbox_inches='tight')
plt.show()