Skip to content

Commit

Permalink
Run rye fmt formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Ramirez committed Sep 18, 2024
1 parent 15b7d76 commit e40fb37
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 337 deletions.
6 changes: 3 additions & 3 deletions pf2/data_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def import_data(small=False) -> anndata.AnnData:
_, data.obs["condition_unique_idxs"] = np.unique(
data.obs_vector("sample_id"), return_inverse=True
)
data.obs["condition_unique_idxs"] = data.obs[
"condition_unique_idxs"
].astype("category")
data.obs["condition_unique_idxs"] = data.obs["condition_unique_idxs"].astype(
"category"
)

return data

Expand Down
2 changes: 1 addition & 1 deletion pf2/figures/commonFuncs/plotFactors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def plot_condition_factors(
pd.set_option("display.max_rows", None)
yt = pd.Series(np.unique(data.obs[cond]))
X = np.array(data.uns["Pf2_A"])

XX = X
X -= np.median(XX, axis=0)
X /= np.std(XX, axis=0)
Expand Down
27 changes: 17 additions & 10 deletions pf2/figures/commonFuncs/plotGeneral.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def plot_avegene_per_status(
condition="sample_id",
cellType="cell_type",
status1="binary_outcome",
status2="patient_category"
status2="patient_category",
):
"""Plots average gene expression across cell types for a category of drugs"""
genesV = X[:, gene]
Expand All @@ -21,7 +21,7 @@ def plot_avegene_per_status(
dataDF[status2] = genesV.obs[status2].values
dataDF["Condition"] = genesV.obs[condition].values
dataDF["Cell Type"] = genesV.obs[cellType].values

df = bal_combine_bo_covid(dataDF, status1, status2)

df = pd.melt(
Expand All @@ -30,7 +30,6 @@ def plot_avegene_per_status(

df = df.groupby(["Status", "Cell Type", "Gene", "Condition"], observed=False).mean()
df = df.rename(columns={"Value": "Average Gene Expression"}).reset_index()


sns.boxplot(
data=df.loc[df["Gene"] == gene],
Expand All @@ -44,15 +43,23 @@ def plot_avegene_per_status(

return df

def bal_combine_bo_covid(df, status1: str = "binary_outcome", status2: str = "patient_category"):

def bal_combine_bo_covid(
df, status1: str = "binary_outcome", status2: str = "patient_category"
):
"""Combines binary outcome and covid status columns"""
df = df.replace({status1: {0: "L-",
1: "D-"}})
df = df.replace({status1: {0: "L-", 1: "D-"}})

df = df.replace({status2: {"COVID-19": "C19",
"Non-Pneumonia Control": "nC19",
"Other Pneumonia": "nC19",
"Other Viral Pneumonia": "nC19"}})
df = df.replace(
{
status2: {
"COVID-19": "C19",
"Non-Pneumonia Control": "nC19",
"Other Pneumonia": "nC19",
"Other Viral Pneumonia": "nC19",
}
}
)
df["Status"] = df[status1] + df[status2]

return df
Expand Down
15 changes: 7 additions & 8 deletions pf2/figures/commonFuncs/plotPaCMAP.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def plot_labels_pacmap(
ds_show(result, ax)
ax.legend(handles=legend_elements)
ax = assign_labels(ax)


def plot_pair_wp_pacmap(
X: anndata.AnnData,
Expand All @@ -130,16 +130,18 @@ def plot_pair_wp_pacmap(
):
"""Scatterplot of UMAP visualization weighted by condition or cell type"""


points = np.concatenate(
([X.obsm["weighted_projections"][:, cmp1 - 1]], [X.obsm["weighted_projections"][:, cmp2 - 1]])
(
[X.obsm["weighted_projections"][:, cmp1 - 1]],
[X.obsm["weighted_projections"][:, cmp2 - 1]],
)
).transpose()

canvas = _get_canvas(points)
data = pd.DataFrame(points, columns=("x", "y"))

aggregation = canvas.points(data, "x", "y")

result = tf.shade(
aggregation,
how="eq_hist",
Expand All @@ -150,7 +152,6 @@ def plot_pair_wp_pacmap(
# ax = assign_labels(ax)



def plot_wp_per_celltype(
X: anndata.AnnData, cmp: int, ax: Axes, outliers: bool = False, cellType="cell_type"
):
Expand All @@ -168,9 +169,7 @@ def plot_wp_per_celltype(
ax=ax,
)
maxvalue = np.max(np.abs(ax.get_xticks()))
ax.set(
xticks=np.linspace(-maxvalue, maxvalue, num=5), xlabel="WP Weight"
)
ax.set(xticks=np.linspace(-maxvalue, maxvalue, num=5), xlabel="WP Weight")
ax.set_title(cmpName)


Expand Down
66 changes: 33 additions & 33 deletions pf2/figures/figure2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,49 @@


def makeFigure():
# meta = import_meta()
# data = import_data()
# conversions = convert_to_patients(data)
# meta = import_meta()
# data = import_data()
# conversions = convert_to_patients(data)

axs, fig = getSetup((6, 6), (2, 1))

# ranks = np.arange(5, 65, 5)
# r2xs = pd.Series(0, dtype=float, index=ranks)
# accuracies = pd.Series(0, dtype=float, index=ranks)
# for rank in ranks:
# fac, r2x = pf2(data, rank, do_embedding=False)
# patient_factor = pd.DataFrame(
# fac.uns["Pf2_A"],
# index=conversions,
# columns=np.arange(fac.uns["Pf2_A"].shape[1]) + 1,
# )
# if meta.shape[0] != patient_factor.shape[0]:
# meta = meta.loc[patient_factor.index, :]
# ranks = np.arange(5, 65, 5)
# r2xs = pd.Series(0, dtype=float, index=ranks)
# accuracies = pd.Series(0, dtype=float, index=ranks)
# for rank in ranks:
# fac, r2x = pf2(data, rank, do_embedding=False)
# patient_factor = pd.DataFrame(
# fac.uns["Pf2_A"],
# index=conversions,
# columns=np.arange(fac.uns["Pf2_A"].shape[1]) + 1,
# )
# if meta.shape[0] != patient_factor.shape[0]:
# meta = meta.loc[patient_factor.index, :]

# acc, _ = predict_mortality(patient_factor, meta)
# r2xs.loc[rank] = r2x
# accuracies.loc[rank] = acc
# r2xs.to_csv("/home/jchin/BAL-Pf2/output/r2x_v_rank_no_ig.csv")
# accuracies.to_csv("/home/jchin/BAL-Pf2/output/acc_v_rank_no_ig.csv")
# acc, _ = predict_mortality(patient_factor, meta)
# r2xs.loc[rank] = r2x
# accuracies.loc[rank] = acc
# r2xs.to_csv("/home/jchin/BAL-Pf2/output/r2x_v_rank_no_ig.csv")
# accuracies.to_csv("/home/jchin/BAL-Pf2/output/acc_v_rank_no_ig.csv")

# gc.collect()
# gc.collect()

# # R2X Plots
# # R2X Plots

# axs[0].plot(r2xs.index.astype(int), r2xs)
# axs[0].set_xticks(r2xs.index.astype(int))
# axs[0].grid(True)
# axs[0].plot(r2xs.index.astype(int), r2xs)
# axs[0].set_xticks(r2xs.index.astype(int))
# axs[0].grid(True)

# axs[0].set_ylabel("R2X")
# axs[0].set_xlabel("Rank")
# axs[0].set_ylabel("R2X")
# axs[0].set_xlabel("Rank")

# # Accuracy Plots
# # Accuracy Plots

# axs[1].plot(accuracies.index, accuracies)
# axs[1].set_xticks(accuracies.index.astype(int))
# axs[1].grid(True)
# axs[1].plot(accuracies.index, accuracies)
# axs[1].set_xticks(accuracies.index.astype(int))
# axs[1].grid(True)

# axs[1].set_ylabel("Accuracy")
# axs[1].set_xlabel("Rank")
# axs[1].set_ylabel("Accuracy")
# axs[1].set_xlabel("Rank")

return fig
6 changes: 2 additions & 4 deletions pf2/figures/figure4.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ def makeFigure():
boot_meta = meta.iloc[boot_index, :]
_, (covid_plsr, nc_plsr) = predict_mortality(boot_factor, boot_meta)

covid_coefficients.loc[trial + 1, covid_plsr.coef_.index] = \
covid_plsr.coef_
nc_coefficients.loc[trial + 1, nc_plsr.coef_.index] = \
nc_plsr.coef_
covid_coefficients.loc[trial + 1, covid_plsr.coef_.index] = covid_plsr.coef_
nc_coefficients.loc[trial + 1, nc_plsr.coef_.index] = nc_plsr.coef_

axs, fig = getSetup((8, 4), (1, 1))
ax = axs[0]
Expand Down
25 changes: 11 additions & 14 deletions pf2/figures/figureA1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Figure A1: Condition, eigen-state, and gene factors,
"""Figure A1: Condition, eigen-state, and gene factors,
along with PaCMAP labeled by cell type"""

import anndata
Expand All @@ -23,12 +23,14 @@ def makeFigure():
X.uns["Pf2_A"] = correct_conditions(X)
add_obs(X, "patient_category")
add_obs(X, "binary_outcome")
plot_condition_factors(X, ax[0], cond="sample_id", cond_group_labels=pd.Series(label_all_samples(X)))
plot_condition_factors(
X, ax[0], cond="sample_id", cond_group_labels=pd.Series(label_all_samples(X))
)
ax[0].yaxis.set_ticklabels([])
plot_eigenstate_factors(X, ax[1])
plot_gene_factors(X, ax[2])
ax[2].yaxis.set_ticklabels([])

df = X.obs[["patient_category", "binary_outcome"]].reset_index(drop=True)
df = bal_combine_bo_covid(df)
X.obs["Status"] = df["Status"].to_numpy()
Expand All @@ -50,24 +52,19 @@ def label_all_samples(X: anndata.AnnData):
for i, sample in enumerate(pd.unique(X.obs["sample_id"])):
bo = pd.unique(X[X.obs.sample_id.isin([sample])].obs["binary_outcome"])
if bo == 0:
bo = "L-"
bo = "L-"
else:
bo = "D-"
bo_only[i] = bo

pc = pd.unique(X[X.obs.sample_id.isin([sample])].obs["patient_category"])
pc = pd.unique(X[X.obs.sample_id.isin([sample])].obs["patient_category"])
if pc == "COVID-19":
pc = "C19"
else:
else:
pc = "nC19"
pc_only[i] = pc

for i in range(len(labels_samples)):
labels_samples[i] = bo_only[i]+pc_only[i]


return labels_samples


for i in range(len(labels_samples)):
labels_samples[i] = bo_only[i] + pc_only[i]


return labels_samples
Loading

0 comments on commit e40fb37

Please sign in to comment.