Skip to content

Commit e40a104

Browse files
committed
Update emd vertical when it cannot be calculated
1 parent dbaeb0f commit e40a104

2 files changed

Lines changed: 58 additions & 36 deletions

File tree

src/metrics/emd/helper.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,48 @@ def calculate_vertical_emd(
5050
i_adata=i_split2_adata, markers_to_assess=markers_to_assess
5151
)
5252

53-
emd_long = pd.concat([emd_split1_long, emd_split2_long])
54-
55-
# mean global emd across all sample combinations, markers, and splits
56-
mean_emd_global = np.nanmean(
57-
emd_long[emd_long["cell_type"] == "global"]
58-
.drop(columns=["cell_type", "first_sample", "second_sample"])
59-
.to_numpy()
60-
.flatten()
61-
)
62-
max_emd_global = np.nanmax(
63-
emd_long[emd_long["cell_type"] == "global"]
64-
.drop(columns=["cell_type", "first_sample", "second_sample"])
65-
.to_numpy()
66-
.flatten()
67-
)
53+
# safeguard
54+
mean_emd_global = np.nan
55+
max_emd_global = np.nan
56+
mean_emd_ct = np.nan
57+
max_emd_ct = np.nan
58+
59+
# compute these only if we can.
60+
emd_long = []
61+
for df in [emd_split1_long, emd_split2_long]:
62+
if isinstance(df, pd.DataFrame):
63+
emd_long.append(df)
64+
65+
if len(emd_long) > 0:
66+
emd_long = pd.concat(emd_long)
67+
68+
# mean global emd across all sample combinations, markers, and splits
69+
mean_emd_global = np.nanmean(
70+
emd_long[emd_long["cell_type"] == "global"]
71+
.drop(columns=["cell_type", "first_sample", "second_sample"])
72+
.to_numpy()
73+
.flatten()
74+
)
75+
max_emd_global = np.nanmax(
76+
emd_long[emd_long["cell_type"] == "global"]
77+
.drop(columns=["cell_type", "first_sample", "second_sample"])
78+
.to_numpy()
79+
.flatten()
80+
)
6881

69-
# mean cell type emd across all sample combinations, markers, and splits
70-
mean_emd_ct = np.nanmean(
71-
emd_long[emd_long["cell_type"] != "global"]
72-
.drop(columns=["cell_type", "first_sample", "second_sample"])
73-
.to_numpy()
74-
.flatten()
75-
)
76-
max_emd_ct = np.nanmax(
77-
emd_long[emd_long["cell_type"] != "global"]
78-
.drop(columns=["cell_type", "first_sample", "second_sample"])
79-
.to_numpy()
80-
.flatten()
81-
)
82+
# mean cell type emd across all sample combinations, markers, and splits
83+
mean_emd_ct = np.nanmean(
84+
emd_long[emd_long["cell_type"] != "global"]
85+
.drop(columns=["cell_type", "first_sample", "second_sample"])
86+
.to_numpy()
87+
.flatten()
88+
)
89+
max_emd_ct = np.nanmax(
90+
emd_long[emd_long["cell_type"] != "global"]
91+
.drop(columns=["cell_type", "first_sample", "second_sample"])
92+
.to_numpy()
93+
.flatten()
94+
)
8295

8396
return {
8497
KEY_MEAN_EMD_GLOBAL: mean_emd_global,
@@ -122,10 +135,10 @@ def get_vert_emd_for_integrated_adata(i_adata: ad.AnnData, markers_to_assess: li
122135

123136
print(
124137
f"{i_adata.uns['dataset_id']} from {i_adata.uns['method_id']} does not have"
125-
f"at least 2 samples per group. Skipping EMD vertical calculation."
138+
f" at least 2 samples per group. Skipping EMD vertical calculation."
126139
)
127140

128-
return np.nan, np.nan, np.nan
141+
return np.nan, np.nan
129142

130143
cell_types = i_adata.obs["cell_type"].unique()
131144

src/metrics/emd/script.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@
99
"input_integrated_split1": "resources_test/task_cyto_batch_integration/mouse_spleen_flow_cytometry_subset/integrated_split1.h5ad",
1010
"input_integrated_split2": "resources_test/task_cyto_batch_integration/mouse_spleen_flow_cytometry_subset/integrated_split2.h5ad",
1111
"input_unintegrated": "resources_test/task_cyto_batch_integration/mouse_spleen_flow_cytometry_subset/unintegrated.h5ad",
12+
# "input_integrated_split1": "resources_test/task_cyto_batch_integration/cytonorm_data_full/cycombine_mid_out.h5ad",
13+
# "input_integrated_split2": "resources_test/task_cyto_batch_integration/cytonorm_data_full/cycombine_mid_out.h5ad",
14+
# "input_unintegrated": "resources_test/task_cyto_batch_integration/cytonorm_data_full/unintegrated.h5ad",
1215
"output": "resources_test/task_cyto_batch_integration/mouse_spleen_flow_cytometry_subset/emd_out.h5ad",
1316
}
14-
meta = {"name": "emd", "resources_dir": "src/utils/helper_functions.py"}
17+
meta = {"name": "emd", "resources_dir": "src/utils/"}
1518
## VIASH END
1619

1720
sys.path.append(meta["resources_dir"])
1821

1922
import helper as emd_helper
2023
import helper_functions as global_helper
2124

25+
# import src.metrics.emd.helper as emd_helper
26+
2227
print("Reading input files", flush=True)
2328

2429
input_integrated_split1 = ad.read_h5ad(par["input_integrated_split1"])
@@ -37,11 +42,15 @@
3742
)
3843

3944
# more preprocessing
40-
input_integrated_split1 = global_helper.subset_markers_tocorrect(input_integrated_split1)
45+
input_integrated_split1 = global_helper.subset_markers_tocorrect(
46+
input_integrated_split1
47+
)
4148
input_integrated_split1 = global_helper.subset_nocontrols(input_integrated_split1)
4249
input_integrated_split1 = global_helper.remove_unlabelled(input_integrated_split1)
4350

44-
input_integrated_split2 = global_helper.subset_markers_tocorrect(input_integrated_split2)
51+
input_integrated_split2 = global_helper.subset_markers_tocorrect(
52+
input_integrated_split2
53+
)
4554
input_integrated_split2 = global_helper.subset_nocontrols(input_integrated_split2)
4655
input_integrated_split2 = global_helper.remove_unlabelled(input_integrated_split2)
4756

@@ -63,9 +72,9 @@
6372
# check that the data for each donor in integrated left and right are actually from two different batches!
6473
for donor in donor_list:
6574
# donor = donor_list[0]
66-
batch_split1 = input_integrated_split1.obs[input_integrated_split1.obs["donor"] == donor][
67-
"batch"
68-
].unique()
75+
batch_split1 = input_integrated_split1.obs[
76+
input_integrated_split1.obs["donor"] == donor
77+
]["batch"].unique()
6978
batch_split2 = input_integrated_split2.obs[
7079
input_integrated_split2.obs["donor"] == donor
7180
]["batch"].unique()

0 commit comments

Comments
 (0)