Skip to content

Commit

Permalink
update figure styles (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaan-abraham authored Apr 1, 2024
1 parent a2fa9a5 commit ef070d1
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 83 deletions.
1 change: 1 addition & 0 deletions maserol/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import xarray as xr
import tensordata

from tensordata.zohar import data as zohar
from tensordata.kaplonek import MGH4D, load_file as load_file_kaplonek
from tensordata.alter import data as alter, load_file as load_file_alter
Expand Down
11 changes: 7 additions & 4 deletions maserol/figures/figure_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from maserol.figures.common import CACHE_DIR, Multiplot
from maserol.forward_backward import forward_backward


N_ITER_2D = 3
STEPS_2D = 4
N_ITER_2E = 3
Expand All @@ -19,7 +20,7 @@
def makeFigure():
plot = Multiplot(
(3, 2),
(3, 2.5),
fig_size=(7.5, 7.5 * 2 / 3),
subplot_specs=[
(0, 2, 0, 1),
(2, 1, 0, 1),
Expand All @@ -28,13 +29,13 @@ def makeFigure():
(2, 1, 1, 1),
],
)
plot.add_subplot_labels()
plot.add_subplot_labels(ax_relative=True)
axes, fig = plot.axes, plot.fig
figure_2b(axes[1])
figure_2c(axes[2])
figure_2d(axes[3])
figure_2e(axes[4])
fig.tight_layout()
fig.tight_layout(pad=0.01, w_pad=0, h_pad=0.2)
return fig


Expand Down Expand Up @@ -66,7 +67,7 @@ def figure_2c(ax):
sns.scatterplot(
x=np.log10(Rtot_inferred[:, 0]), y=np.log10(Rtot[:, 0]), alpha=0.6, ax=ax
)
ax.set_title("Actual vs predicted abundance (30% noise)")
ax.set_title("Actual vs predicted (30% noise)")
ax.set_xlabel("$\mathrm{log_{10}}$ Inferred IgG1")
ax.set_ylabel("$\mathrm{log_{10}}$ Actual IgG1")

Expand Down Expand Up @@ -104,6 +105,7 @@ def figure_2d(ax):
ax.set_title("Prediction performance vs detection noise")
ax.set_xlabel("Noise σ")
ax.set_ylabel("$R^2$")
ax.set_ylim(-0.01, 1.01)


def figure_2e(ax):
Expand Down Expand Up @@ -141,3 +143,4 @@ def figure_2e(ax):
ax.set_title("Prediction performance vs $K_{a}$ noise")
ax.set_xlabel("Noise σ")
ax.set_ylabel("$R^2$")
ax.set_ylim(-0.01, 1.01)
4 changes: 2 additions & 2 deletions maserol/figures/figure_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
def makeFigure():
plot = Multiplot(
(3, 2),
(3, 2.5),
fig_size=(7.5, 7.5 * 2 / 3),
subplot_specs=[
(0, 2, 0, 1),
(2, 1, 0, 1),
Expand All @@ -34,7 +34,7 @@ def makeFigure():
figure_3b(plot.axes[1])
figure_3cd(plot.axes[2], plot.axes[3])
figure_3e(plot.axes[4])
plot.fig.tight_layout()
plot.fig.tight_layout(pad=0.01, w_pad=0, h_pad=0.2)
plot.add_subplot_labels(ax_relative=True)
return plot.fig

Expand Down
54 changes: 27 additions & 27 deletions maserol/figures/figure_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
def makeFigure():
plot = Multiplot(
(3, 2),
(3, 2.5),
fig_size=(7.5, 7.5 * 2 / 3 + 0.1),
subplot_specs=[
(0, 1, 0, 2),
(0, 1, 0, 1),
(1, 1, 0, 1),
(2, 1, 0, 1),
(1, 1, 1, 1),
(0, 2, 1, 1),
(2, 1, 1, 1),
],
)
figure_4b(plot.axes[1])
figure_4cde(plot.axes[2], plot.axes[3], plot.axes[4])
plot.add_subplot_labels()
plot.fig.tight_layout()
figure_mechanistic_relate(plot.axes[0], plot.axes[1], plot.axes[2])
figure_CE(plot.axes[4])
plot.add_subplot_labels(ax_relative=True)
plot.fig.tight_layout(pad=0, w_pad=0.2, h_pad=1)
return plot.fig


def figure_4b(ax):
def figure_CE(ax):
fucose_ce = Alter().get_fucose_data()
if UPDATE_CACHE:
detection_signal = Alter().get_detection_signal()
Expand All @@ -56,15 +56,15 @@ def figure_4b(ax):
r, p = pearsonr(fucose_compare["fucose_ce"], fucose_compare["fucose_inferred"])
ax.set_title("Model Inferences vs CE Measurements")
ax.text(
0.7,
0.6,
0.06,
r"r = " + str(round(r, 2)),
verticalalignment="bottom",
horizontalalignment="left",
transform=ax.transAxes,
)
ax.text(
0.7,
0.6,
0.01,
r"p = " + "{:.2e}".format(p),
verticalalignment="bottom",
Expand All @@ -82,7 +82,7 @@ def figure_4b(ax):
ax.plot(lims, lims, linestyle="--", color="gray", alpha=0.75)


def figure_4cde(ax_c, ax_d, ax_e):
def figure_mechanistic_relate(ax_0, ax_1, ax_2):
zohar = Zohar()
if UPDATE_CACHE:
detection_signal = zohar.get_detection_signal()
Expand All @@ -104,50 +104,50 @@ def figure_4cde(ax_c, ax_d, ax_e):
sns.scatterplot(
y=y,
x=df["fucose_inferred"],
ax=ax_c,
ax=ax_0,
alpha=ALPHA,
)
ax_c.set_xlabel("Inferred IgG Fucosylation (%)")
ax_c.set_ylabel(
ax_0.set_xlabel("Inferred IgG Fucosylation (%)")
ax_0.set_ylabel(
r"$\mathrm{log_{10}}$"
+ f"({DETECTION_DISPLAY_NAMES['FcR3A']} / {DETECTION_DISPLAY_NAMES['FcR2A']})"
)
r, p = pearsonr(df["fucose_inferred"], y)
ax_c.text(
0.75,
ax_0.text(
0.05,
0.06,
r"r = " + str(round(r, 2)),
verticalalignment="bottom",
horizontalalignment="left",
transform=ax_c.transAxes,
transform=ax_0.transAxes,
)

sns.scatterplot(
data=df, y="ADNKA_CD107a_S", x="fucose_inferred", ax=ax_d, alpha=ALPHA
data=df, y="ADNKA_CD107a_S", x="fucose_inferred", ax=ax_1, alpha=ALPHA
)
ax_d.set_xlabel("Inferred IgG Fucosylation (%)")
ax_d.set_ylabel(r"ADCC (CD107a)")
ax_1.set_xlabel("Inferred IgG Fucosylation (%)")
ax_1.set_ylabel(r"ADCC (CD107a)")
r, p = pearsonr(df["fucose_inferred"], df["ADNKA_CD107a_S"])
ax_d.text(
ax_1.text(
0.05,
0.86,
r"r = " + str(round(r, 2)),
verticalalignment="bottom",
horizontalalignment="left",
transform=ax_d.transAxes,
transform=ax_1.transAxes,
)

sns.scatterplot(
data=df, y="ADNKA_MIP1b_S", x="fucose_inferred", ax=ax_e, alpha=ALPHA
data=df, y="ADNKA_MIP1b_S", x="fucose_inferred", ax=ax_2, alpha=ALPHA
)
ax_e.set_xlabel("Inferred IgG Fucosylation (%)")
ax_e.set_ylabel(r"ADCC (MIP1b)")
ax_2.set_xlabel("Inferred IgG Fucosylation (%)")
ax_2.set_ylabel(r"ADCC (MIP1b)")
r, p = pearsonr(df["fucose_inferred"], df["ADNKA_MIP1b_S"])
ax_e.text(
ax_2.text(
0.05,
0.86,
r"r = " + str(round(r, 2)),
verticalalignment="bottom",
horizontalalignment="left",
transform=ax_e.transAxes,
transform=ax_2.transAxes,
)
65 changes: 47 additions & 18 deletions maserol/figures/figure_5.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pingouin as pg
import seaborn as sns
from scipy.stats import pearsonr
from statannotations.Annotator import Annotator

from maserol.core import optimize_loss
Expand All @@ -22,19 +24,19 @@

def makeFigure():
plot = Multiplot(
(3, 2),
(3, 2.5),
(10, 2),
fig_size=(7.5, 7.5 * 2 / 3),
subplot_specs=[
(0, 1, 0, 1),
(1, 1, 0, 1),
(2, 1, 0, 1),
(0, 3, 1, 1),
(0, 3, 0, 1),
(3, 3, 0, 1),
(6, 4, 0, 1),
(0, 10, 1, 1),
],
)
figure_5abc(plot.axes[0], plot.axes[1], plot.axes[2])
figure_5d(plot.axes[3])
plot.add_subplot_labels()
plot.fig.tight_layout()
plot.fig.tight_layout(pad=0, w_pad=0.2, h_pad=1)
return plot.fig


Expand Down Expand Up @@ -68,7 +70,6 @@ def figure_5abc(ax_a, ax_b, ax_c):
showfliers=False,
)
ax.set_ylabel("anti-S IgG Fucosylation (%)")
ax.set_ylim(30, 100)
pairs = (("Yes", "No"),)
annotator = Annotator(ax, pairs, data=df_merged, x="ARDS", y="fucose_inferred")
annotate_mann_whitney(annotator)
Expand All @@ -82,25 +83,54 @@ def figure_5abc(ax_a, ax_b, ax_c):
order=[0, 1],
showfliers=False,
)
ax.set_ylim(30, 100)
ax.set_ylabel("anti-S IgG Fucosylation (%)")
# ax.set_ylabel("anti-S IgG Fucosylation (%)")
ax.set_ylabel(None)
ax.set_xlabel("Immunosuppressed")
ax.set_xticklabels(["No", "Yes"])
pairs = ((1, 0),)
annotator = Annotator(ax, pairs, data=df_merged, x="immunosup", y="fucose_inferred")
annotate_mann_whitney(annotator)

ax = ax_c
fucose_over_time = pd.merge(
fucose_inferred, metadata[["days", "patient_ID"]], how="inner", on="Sample"
)
fucose_over_time.dropna(inplace=True)
fucose_over_time = fucose_over_time.groupby("patient_ID").filter(
lambda x: x["days"].nunique() >= 2
)
rmcorr_result = pg.rm_corr(
data=fucose_over_time, x="days", y="fucose_inferred", subject="patient_ID"
)
r, p = rmcorr_result.r.iloc[0], rmcorr_result.pval.iloc[0]
fucose_over_time_binned = pd.merge(
fucose_inferred, zohar.get_days_binned(), how="inner", on="Sample"
)
sns.lineplot(
data=pd.merge(
fucose_inferred, zohar.get_days_binned(), how="inner", on="Sample"
),
data=fucose_over_time_binned,
x="days",
y="fucose_inferred",
ax=ax,
)
ax.text(
0.7,
0.8,
f"r = " + str(round(r, 2)),
verticalalignment="bottom",
horizontalalignment="left",
transform=ax.transAxes,
)
ax.text(
0.7,
0.72,
r"p = " + "{:.2e}".format(p),
verticalalignment="bottom",
horizontalalignment="left",
transform=ax.transAxes,
)
ax.set_xlabel("Days following symptom onset")
ax.set_ylabel("anti-S IgG Fucosylation (%)")
# ax.set_ylabel("anti-S IgG Fucosylation (%)")
ax.set_ylabel(None)
ax.set_xlim(0, 30)


Expand Down Expand Up @@ -151,7 +181,7 @@ def figure_5d(ax):
)
ax.set_xticklabels(ax.get_xticklabels(), rotation=30, fontsize="small")
ax.set_ylabel("IgG Fucosylation (%)")
ax.set_ylim(0, 100)
ax.set_ylim(-2, 102)
ax.set_xlabel("Antigen")
pairs = [((ag, "control"), (ag, "case")) for ag in df_compare.Antigen.unique()]
annotator = Annotator(
Expand All @@ -166,6 +196,5 @@ def figure_5d(ax):

handles, labels = ax.get_legend_handles_labels()
new_labels = ["Avoided infection", "Infected"]
ax.legend(handles, new_labels, framealpha=1, title=None)

sns.move_legend(ax, "lower right")
legend = ax.legend(handles, new_labels, title=None, loc="lower right")
legend.get_frame().set_alpha(1)
16 changes: 9 additions & 7 deletions maserol/figures/figure_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def makeFigure():
df_compare.replace("IIIb.pr55.Gag", "pr55.Gag.IIIb", inplace=True)

plot = Multiplot(
(5, 2),
fig_size=(9, 5),
(7, 2),
fig_size=(7.5, 5),
subplot_specs=[
(0, 5, 0, 1),
(0, 4, 1, 1),
(4, 1, 1, 1),
(0, 7, 0, 1),
(0, 6, 1, 1),
(6, 1, 1, 1),
],
)

Expand Down Expand Up @@ -67,8 +67,8 @@ def makeFigure():
"Treated progressor",
"Untreated progressor",
]
ax.legend(handles, new_labels)
sns.move_legend(ax, "lower right")
legend = ax.legend(handles, new_labels, loc="lower right")
legend.get_frame().set_alpha(1)

# b
ax = plot.axes[1]
Expand Down Expand Up @@ -128,6 +128,7 @@ def makeFigure():
ax.set_xticklabels(
ax.get_xticklabels(), rotation=X_LABEL_ROTATION, fontsize="small"
)
ax.set_yticklabels([])

pairs = [("Env trimer", "p24"), ("Env trimer", "pr55.Gag")]
annotator = Annotator(
Expand All @@ -136,5 +137,6 @@ def makeFigure():
annotate_mann_whitney(annotator)

plot.add_subplot_labels()
plot.fig.tight_layout(pad=0, w_pad=0.2, h_pad=1)

return plot.fig
6 changes: 4 additions & 2 deletions maserol/figures/figure_7.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
}


TITLE_FONT_SIZE = 13
TITLE_FONT_SIZE = 12.5


def makeFigure():
plot = Multiplot((2, 2), (4.5, 3))
plot = Multiplot((2, 2), fig_size=(7.5, 5))

if UPDATE_CACHE:
update_cache(2)
Expand All @@ -45,8 +45,10 @@ def makeFigure():
plot_combinations(df_3, "r2", plot.axes[3], ylim=(-1, 1))
plot.axes[0].set_title("2 Missing Detections", fontsize=TITLE_FONT_SIZE)
plot.axes[1].set_title("3 Missing Detections", fontsize=TITLE_FONT_SIZE)
plot.axes[1].set_yticklabels([])
plot.axes[1].set_ylabel(None)
plot.axes[3].set_ylabel(None)
plot.axes[3].set_yticklabels([])

plot.axes[0].set_ylabel(
f"Imputation Accuracy ({METRIC_LABEL_MAPPINGS['r']})", fontsize=TITLE_FONT_SIZE
Expand Down
Loading

0 comments on commit ef070d1

Please sign in to comment.