Skip to content

Commit 3488651

Browse files
committed
return nan instead of crashing when every emd stratum is too small
* Guard the pd.concat() calls in both the horizontal and vertical path * Warn as soon as one cell type is missing from a split, not two * Fix the newline in that warning
1 parent 79a262f commit 3488651

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

src/metrics/emd/helper.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ def get_vert_emd_for_integrated_adata(i_adata: ad.AnnData, markers_to_assess: li
175175

176176
emd_vals.append(emd_df)
177177

178+
if len(emd_vals) == 0:
179+
# every sample combination and cell type fell below the 50 cell floor,
180+
# so there is nothing to concatenate.
181+
182+
print(
183+
f"{i_adata.uns['dataset_id']} from {i_adata.uns['method_id']} does not have"
184+
f" at least 50 cells for any sample combination and cell type."
185+
f" Skipping EMD vertical calculation."
186+
)
187+
188+
return np.nan
189+
178190
# concatenate EMD values
179191
emd_vals = pd.concat(emd_vals)
180192
# remove unparsable characters like "/"
@@ -224,11 +236,11 @@ def calculate_horizontal_emd(
224236
i_split1_donor.obs["cell_type"].unique(),
225237
i_split2_donor.obs["cell_type"].unique(),
226238
)
227-
if len(cell_type_not_in_both) > 1:
239+
if len(cell_type_not_in_both) > 0:
228240
print(
229241
f"In donor {donor}: some cell types are in split 1"
230242
f" but not in split 2.\n"
231-
f"Cell types missing: {''.join(cell_type_not_in_both)}]n"
243+
f"Cell types missing: {', '.join(cell_type_not_in_both)}\n"
232244
f"Computing cell type EMD using just cell types common in both."
233245
)
234246

@@ -265,6 +277,21 @@ def calculate_horizontal_emd(
265277

266278
emd_per_donor_per_ct.append(emd_df)
267279

280+
if len(emd_per_donor_per_ct) == 0:
281+
# every donor and cell type fell below the 50 cell floor,
282+
# so there is nothing to concatenate.
283+
284+
print(
285+
f"{i_split1_adata.uns['dataset_id']} from {i_split1_adata.uns['method_id']}"
286+
f" does not have at least 50 cells for any donor and cell type."
287+
f" Skipping EMD horizontal calculation."
288+
)
289+
290+
return {
291+
KEY_MEAN_EMD_CT: np.nan,
292+
KEY_EMD_HORZ_PER_DONOR: np.nan,
293+
}
294+
268295
emd_per_donor_per_ct = pd.concat(emd_per_donor_per_ct)
269296

270297
# compute the mean and max per ct and for global.

0 commit comments

Comments
 (0)