forked from kev98/Medical-Image-Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistical_test.py
More file actions
185 lines (140 loc) · 4.85 KB
/
Copy pathstatistical_test.py
File metadata and controls
185 lines (140 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import argparse
from pathlib import Path
import pandas as pd
import matplotlib.pyplot as plt
from autorank import autorank, plot_stats, create_report, latex_table
# =============================================
# Configuration — edit paths here
# =============================================
PLOT_OUTPUT_DIR = "/results/path"
def model_name_from_path(path):
"""
Example:
/path/to/TextBraTS_base_BioClinicalBERT.csv
-> TextBraTS_base_BioClinicalBERT
"""
return Path(path).stem
def read_metric_from_csv(csv_path, metric_column, id_column):
df = pd.read_csv(csv_path)
if id_column not in df.columns:
raise ValueError(f"Column '{id_column}' not found in {csv_path}")
if metric_column not in df.columns:
raise ValueError(f"Column '{metric_column}' not found in {csv_path}")
# Remove AVERAGE row
df = df[df[id_column].astype(str).str.upper() != "AVERAGE"].copy()
# Keep only subject ID and chosen metric
df = df[[id_column, metric_column]].copy()
# Convert metric to numeric
df[metric_column] = pd.to_numeric(df[metric_column], errors="coerce")
# Drop rows where the selected metric is NaN
df = df.dropna(subset=[metric_column])
# Check duplicated subject IDs
if df[id_column].duplicated().any():
duplicated = df.loc[df[id_column].duplicated(), id_column].tolist()
raise ValueError(
f"Duplicated subject IDs found in {csv_path}: {duplicated[:10]}"
)
return df
def build_paired_dataframe(csv_files, metric_column, id_column):
merged_df = None
for csv_path in csv_files:
model_name = model_name_from_path(csv_path)
model_df = read_metric_from_csv(
csv_path=csv_path,
metric_column=metric_column,
id_column=id_column,
)
model_df = model_df.rename(columns={metric_column: model_name})
if merged_df is None:
merged_df = model_df
else:
merged_df = pd.merge(
merged_df,
model_df,
on=id_column,
how="inner",
)
if merged_df is None:
raise ValueError("No CSV files were provided.")
if len(merged_df) == 0:
raise ValueError("No paired cases remain after merging by subject_id.")
# subject_id is only used for alignment, not for autorank
paired_data = merged_df.drop(columns=[id_column])
return merged_df, paired_data
def main():
parser = argparse.ArgumentParser(
description="Run autorank statistical comparison on paired model CSV files."
)
parser.add_argument(
"--csv-files",
nargs="+",
required=True,
help="CSV files, one per model.",
)
parser.add_argument(
"--metric-column",
required=True,
help="Metric column to compare, e.g. DSC_aggregated_mean or DSC_mean.",
)
parser.add_argument(
"--id-column",
default="subject_id",
help="Case ID column used to align paired cases. Default: subject_id.",
)
parser.add_argument(
"--alpha",
type=float,
default=0.05,
help="Significance level. Default: 0.05.",
)
parser.add_argument(
"--order",
choices=["ascending", "descending"],
default="descending",
help=(
"Use descending when higher metric is better, "
"ascending when lower metric is better. Default: descending."
),
)
parser.add_argument(
"--output-prefix",
default="autorank_results",
help="Prefix for output files.",
)
args = parser.parse_args()
merged_df, paired_data = build_paired_dataframe(
csv_files=args.csv_files,
metric_column=args.metric_column,
id_column=args.id_column,
)
print("\nNumber of paired cases used:", len(paired_data))
print("\nModels compared:")
for col in paired_data.columns:
print(f" - {col}")
print("\nMean performance per model:")
print(paired_data.mean().sort_values(ascending=False))
# Save the paired table used by autorank, useful for reproducibility
paired_output = f"{args.output_prefix}_paired_data.csv"
merged_df.to_csv(paired_output, index=False, float_format="%.4f")
print(f"\nSaved paired data to: {paired_output}")
# Run autorank
result = autorank(
paired_data,
alpha=args.alpha,
verbose=True,
order=args.order,
)
print("\nAutorank result:")
print(result)
print("\nAutorank textual report:")
create_report(result)
print("\nLaTeX table:")
latex_table(result)
# Plot statistical result
plot_stats(result)
plt.tight_layout()
plot_output = f"{PLOT_OUTPUT_DIR}/{args.output_prefix}_plot.png"
plt.savefig(plot_output, dpi=300)
print(f"\nSaved plot to: {plot_output}")
if __name__ == "__main__":
main()