@@ -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
0 commit comments