|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import seaborn as sns |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from scipy.stats import shapiro, wilcoxon |
| 6 | + |
| 7 | + |
| 8 | +METRIC = "msa" |
| 9 | +CRITERION = 0.05 |
| 10 | + |
| 11 | + |
| 12 | +def statistical_analysis_dataset(dataset, method1_path, method2_path, verbose=True): |
| 13 | + res1 = pd.read_csv(f"./results/{method1_path}/{dataset}.csv")[METRIC].values |
| 14 | + res2 = pd.read_csv(f"./results/{method2_path}/{dataset}.csv")[METRIC].values |
| 15 | + assert res1.shape == res2.shape |
| 16 | + |
| 17 | + diff = res1 - res2 |
| 18 | + |
| 19 | + _, p_gauss = shapiro(diff) |
| 20 | + if verbose: |
| 21 | + print("P-value for gaussian distribution:", p_gauss) |
| 22 | + |
| 23 | + is_better = diff.sum() > 0 |
| 24 | + _, p = wilcoxon(diff, alternative="greater" if is_better else "less") |
| 25 | + is_significant = p < CRITERION |
| 26 | + |
| 27 | + if verbose: |
| 28 | + print( |
| 29 | + "Hypothesis:", method1_path if is_better else method2_path, "is better than", |
| 30 | + method2_path if is_better else method1_path |
| 31 | + ) |
| 32 | + print("Result:", "True" if is_significant else "False", f"(p = {p:.4f})") |
| 33 | + |
| 34 | + return is_better, is_significant |
| 35 | + |
| 36 | + |
| 37 | +def statistical_analysis_pair(datasets, method1_path, method2_path, verbose=False): |
| 38 | + better1 = 0 |
| 39 | + better2 = 0 |
| 40 | + neutral = 0 |
| 41 | + |
| 42 | + for ds in datasets: |
| 43 | + is_better, is_significant = statistical_analysis_dataset(ds, method1_path, method2_path, verbose=verbose) |
| 44 | + if is_significant and is_better: |
| 45 | + better1 += 1 |
| 46 | + elif is_significant: |
| 47 | + better2 += 1 |
| 48 | + else: |
| 49 | + neutral += 1 |
| 50 | + |
| 51 | + assert better1 + better2 + neutral == len(datasets) |
| 52 | + if verbose: |
| 53 | + print(method1_path, "better than", method2_path, ":", better1) |
| 54 | + print(method2_path, "better than", method1_path, ":", better2) |
| 55 | + print("No difference:", neutral) |
| 56 | + return better1, better2, neutral |
| 57 | + |
| 58 | + |
| 59 | +def get_datasets(domain): |
| 60 | + domain_to_ds = { |
| 61 | + "fluo_cells": [ |
| 62 | + "cellpose", |
| 63 | + "covid_if", |
| 64 | + "hpa", |
| 65 | + "plantseg_root", |
| 66 | + "plantseg_ovules", |
| 67 | + "pnas_arabidopsis", |
| 68 | + "tissuenet", |
| 69 | + "cellbindb", |
| 70 | + "mouse_embryo", |
| 71 | + ], |
| 72 | + "fluo_nuclei": [ |
| 73 | + "arvidsson", |
| 74 | + "bitdepth_nucseg", |
| 75 | + "dsb", |
| 76 | + "dynamicnuclearnet", |
| 77 | + "gonuclear", |
| 78 | + "ifnuclei", |
| 79 | + "nis3d", |
| 80 | + "parhyale_regen", |
| 81 | + "u20s", |
| 82 | + ], |
| 83 | + "label_free": [ |
| 84 | + "deepbacs", |
| 85 | + "deepseas", |
| 86 | + "livecell", |
| 87 | + "omnipose", |
| 88 | + "usiigaci", |
| 89 | + "vicar", |
| 90 | + "toiam", |
| 91 | + "yeaz", |
| 92 | + "segpc", |
| 93 | + ], |
| 94 | + "histopatho": [ |
| 95 | + "cytodark0", |
| 96 | + "ihc_tma", |
| 97 | + "monuseg", |
| 98 | + "lynsec", |
| 99 | + "nuinsseg", |
| 100 | + "pannuke", |
| 101 | + "puma", |
| 102 | + "tnbc", |
| 103 | + "cryonuseg", |
| 104 | + ], |
| 105 | + } |
| 106 | + datasets = domain_to_ds[domain] |
| 107 | + assert len(datasets) == 9 |
| 108 | + return datasets |
| 109 | + |
| 110 | + |
| 111 | +def _plot_comparison_heatmap(domain, comparison_df, title=None): |
| 112 | + # Extract wins for method in row vs method in column |
| 113 | + n = len(comparison_df) |
| 114 | + win_matrix = np.zeros((n, n)) |
| 115 | + |
| 116 | + for i in range(n): |
| 117 | + for j in range(n): |
| 118 | + if i != j: |
| 119 | + parts = comparison_df.iloc[i, j].split(' / ') |
| 120 | + win_matrix[i, j] = int(parts[0]) # wins for row method |
| 121 | + |
| 122 | + # Masking the diagonal to exclude it from coloring. |
| 123 | + mask = np.eye(n, dtype=bool) |
| 124 | + |
| 125 | + fig, ax = plt.subplots(figsize=(10, 8)) |
| 126 | + sns.heatmap( |
| 127 | + win_matrix, annot=comparison_df.values, fmt='', |
| 128 | + cmap='RdYlGn', center=len(get_datasets(domain))/2, |
| 129 | + xticklabels=comparison_df.columns, |
| 130 | + yticklabels=comparison_df.index, |
| 131 | + cbar_kws={'label': 'Wins'}, ax=ax, |
| 132 | + mask=mask, linewidths=0.5, linecolor='#A9A9A9' |
| 133 | + ) |
| 134 | + |
| 135 | + plt.title(title) |
| 136 | + plt.tight_layout() |
| 137 | + plt.savefig(f'comparison_heatmap_{domain}.png', dpi=400, bbox_inches='tight') |
| 138 | + plt.savefig(f'comparison_heatmap_{domain}.svg', dpi=400, bbox_inches='tight') |
| 139 | + plt.close() |
| 140 | + |
| 141 | + |
| 142 | +def compare_all(): |
| 143 | + # Sorting out the paths where the methods' results exist. |
| 144 | + method_configs = { |
| 145 | + "amg": "amg/vit_b", |
| 146 | + "ais_lm": "ais/vit_b_lm", |
| 147 | + "ais_histo": "ais/vit_b_histopathology", |
| 148 | + "cellpose3": "cellpose/cyto3", |
| 149 | + "cellpose4": "cellpose/cpsam", |
| 150 | + "cellsam": "cellsam/cellsam", |
| 151 | + "sam3": "sam3/cell", |
| 152 | + "apg_lm": "apg/vit_b_lm", |
| 153 | + "apg_histo": "apg/vit_b_histopathology", |
| 154 | + } |
| 155 | + |
| 156 | + # Sorting the methods we would like to compare stuff with. |
| 157 | + domain_methods = { |
| 158 | + "fluo_cells": ["amg", "ais_lm", "cellpose3", "cellpose4", "cellsam", "sam3", "apg_lm"], |
| 159 | + "fluo_nuclei": ["amg", "ais_lm", "cellpose3", "cellpose4", "cellsam", "sam3", "apg_lm"], |
| 160 | + "label_free": ["amg", "ais_lm", "cellpose3", "cellpose4", "cellsam", "sam3", "apg_lm"], |
| 161 | + "histopatho": ["amg", "ais_histo", "cellpose3", "cellpose4", "cellsam", "sam3", "apg_histo"], |
| 162 | + } |
| 163 | + |
| 164 | + # Let's map the keys to expected names. |
| 165 | + display_names = { |
| 166 | + "amg": "AMG (SAM)", |
| 167 | + "ais_lm": "AIS (μSAM)", |
| 168 | + "ais_histo": "AIS\n(PathoSAM)", |
| 169 | + "cellsam": "CellSAM", |
| 170 | + "cellpose3": "Cellpose 3", |
| 171 | + "cellpose4": "CellposeSAM", |
| 172 | + "sam3": "SAM3", |
| 173 | + "apg_lm": r"$\mathbf{APG}$" + r" $\mathbf{(μSAM)}$", |
| 174 | + "apg_histo": r"$\mathbf{APG}$" + "\n" + r"$\mathbf{(PathoSAM)}$", |
| 175 | + } |
| 176 | + |
| 177 | + # Choosing custom plot titles. |
| 178 | + custom_titles = { |
| 179 | + "fluo_cells": "Fluorescence Microscopy (Cell Segmentation)", |
| 180 | + "fluo_nuclei": "Fluorescence Microscopy (Nucleus Segmentation)", |
| 181 | + "label_free": "Label-Free Microscopy (Cell Segmentation)", |
| 182 | + "histopatho": "Histopathology (Nucleus Segmentation)", |
| 183 | + } |
| 184 | + |
| 185 | + for domain in ["fluo_cells", "fluo_nuclei", "label_free", "histopatho"]: |
| 186 | + datasets = get_datasets(domain) |
| 187 | + methods = domain_methods[domain] |
| 188 | + n_methods = len(methods) |
| 189 | + |
| 190 | + comparison = np.empty((n_methods, n_methods), dtype="U15") |
| 191 | + |
| 192 | + for i in range(n_methods): |
| 193 | + for j in range(n_methods): |
| 194 | + if i == j: |
| 195 | + comparison[i, j] = "-" |
| 196 | + continue |
| 197 | + |
| 198 | + method_row = methods[i] |
| 199 | + method_col = methods[j] |
| 200 | + method_row_path = method_configs[method_row] |
| 201 | + method_col_path = method_configs[method_col] |
| 202 | + |
| 203 | + better_row, better_col, neutral = statistical_analysis_pair( |
| 204 | + datasets, method_row_path, method_col_path |
| 205 | + ) |
| 206 | + comparison[i, j] = f"{better_row} / {better_col} / {neutral}" |
| 207 | + |
| 208 | + # Let's use expected display names. |
| 209 | + display_method_names = [display_names[m] for m in methods] |
| 210 | + comparison = pd.DataFrame(comparison, index=display_method_names, columns=display_method_names) |
| 211 | + |
| 212 | + # Let's visualize the results |
| 213 | + _plot_comparison_heatmap(domain, comparison, title=custom_titles[domain]) |
| 214 | + print(f"Generated heatmap for {domain}: comparison_heatmap_{domain}.png") |
| 215 | + |
| 216 | + |
| 217 | +def main(): |
| 218 | + compare_all() |
| 219 | + |
| 220 | + |
| 221 | +if __name__ == "__main__": |
| 222 | + main() |
0 commit comments