-
Notifications
You must be signed in to change notification settings - Fork 0
/
cal_metrics.py
executable file
·293 lines (265 loc) · 9.29 KB
/
cal_metrics.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import argparse
import numpy as np
from calzone.metrics import CalibrationMetrics, get_CI
from calzone.utils import *
from calzone.vis import plot_reliability_diagram
def perform_calculation(probs, labels, args, suffix=""):
"""
Calculate calibration metrics and visualize reliability diagram.
Args:
probs (numpy.ndarray): Predicted probabilities for each class.
labels (numpy.ndarray): True labels.
args (argparse.Namespace): Command-line arguments.
suffix (str, optional): Suffix for output files. Defaults to "".
Returns:
numpy.ndarray: Calculated metrics and confidence intervals (if bootstrapping is used).
"""
cal_metrics = CalibrationMetrics(
class_to_calculate=args.class_to_calculate, num_bins=args.num_bins
)
if args.hl_test_validation:
df = args.num_bins
else:
df = args.num_bins - 2
metrics_to_calculate = args.metrics.split(",") if args.metrics else ["all"]
if metrics_to_calculate == ["all"]:
metrics_to_calculate = "all"
result = cal_metrics.calculate_metrics(
y_true=labels,
y_proba=probs,
metrics=metrics_to_calculate,
perform_pervalance_adjustment=args.prevalence_adjustment,
df = df
)
keys = list(result.keys())
result = np.array(list(result.values())).reshape(1, -1)
if args.n_bootstrap > 0:
bootstrap_results = cal_metrics.bootstrap(
y_true=labels,
y_proba=probs,
n_samples=args.n_bootstrap,
metrics=metrics_to_calculate,
perform_pervalance_adjustment=args.prevalence_adjustment,
df = df
)
CI = get_CI(bootstrap_results)
result = np.vstack((result, np.array(list(CI.values())).T))
if args.verbose:
print_metrics(result, keys, args.n_bootstrap, suffix)
if args.save_metrics:
save_metrics_to_csv(result, keys, args.save_metrics, suffix)
if args.plot:
plot_reliability(labels, probs, args, suffix)
return result
def print_metrics(result, keys, n_bootstrap, suffix):
"""
Print calculated metrics.
Args:
result (numpy.ndarray): Calculated metrics and confidence intervals.
keys (list): Names of the calculated metrics.
n_bootstrap (int): Number of bootstrap samples.
suffix (str): Suffix for output files.
"""
if n_bootstrap > 0:
print_header = (
"Metrics with bootstrap confidence intervals:"
if suffix == ""
else f"Metrics for {suffix} with bootstrap confidence intervals:"
)
print(print_header)
for i, num in enumerate(keys):
print(
f"{num}: {np.format_float_positional(result[0][i], 3)}",
f"({np.format_float_positional(result[1][i], 3)}, "
f"{np.format_float_positional(result[2][i], 3)})",
)
else:
print_header = "Metrics:" if suffix == "" else f"Metrics for subgroup {suffix}:"
print(print_header)
for i, num in enumerate(keys):
print(f"{num}: {np.format_float_positional(result[0][i], 3)}")
def save_metrics_to_csv(result, keys, save_metrics, suffix):
"""
Save calculated metrics to a CSV file.
Args:
result (numpy.ndarray): Calculated metrics and confidence intervals.
keys (list): Names of the calculated metrics.
save_metrics (str): Path to save the CSV file.
suffix (str): Suffix for output files.
"""
if suffix == "":
filename = save_metrics
else:
split_filename = save_metrics.split(".")
pathwithoutextension = ".".join(split_filename[:-1])
filename = pathwithoutextension + "_" + suffix + ".csv"
np.savetxt(
filename,
np.array(result),
delimiter=",",
header=",".join(keys),
comments="",
fmt="%s",
)
print("Result saved to", filename)
def plot_reliability(labels, probs, args, suffix):
"""
Plot and save reliability diagram.
Args:
labels (numpy.ndarray): True labels.
probs (numpy.ndarray): Predicted probabilities for each class.
args (argparse.Namespace): Command-line arguments.
suffix (str): Suffix for output files.
"""
if suffix == "":
filename = args.save_plot
diagram_filename = args.save_diagram_output or None
else:
split_filename = args.save_plot.split(".")
pathwithoutextension = ".".join(split_filename[:-1])
filename = pathwithoutextension + "_" + suffix + "." + split_filename[-1]
if args.save_diagram_output:
split_filename = args.save_diagram_output.split(".")
pathwithoutextension = ".".join(split_filename[:-1])
diagram_filename = pathwithoutextension + "_" + suffix + ".csv"
else:
diagram_filename = None
valid_image_formats = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.pdf']
if not any(filename.lower().endswith(fmt) for fmt in valid_image_formats):
print(filename)
raise ValueError("Invalid file format. Please provide a valid image format.")
reliability, confidence, bin_edge, bin_count = reliability_diagram(
y_true=labels,
y_proba=probs,
num_bins=args.plot_bins,
class_to_plot=args.class_to_calculate,
save_path=diagram_filename,
)
plot_reliability_diagram(
reliability,
confidence,
bin_count,
save_path=filename,
title=suffix,
error_bar=True,
return_fig=True
)
print("Plot saved to", filename)
def main():
"""
Main function to parse arguments and perform calibration calculations.
"""
parser = argparse.ArgumentParser(
description="Calculate calibration metrics and visualize reliability diagram."
)
parser.add_argument(
"--csv_file",
type=str,
help="Path to the input CSV file. (If there is header,it must be in: "
"proba_0,proba_1,...,subgroup_1(optional),subgroup_2(optional),...label. "
"If no header, then the columns must be in the order of "
"proba_0,proba_1,...,label)",
)
parser.add_argument(
"--metrics",
type=str,
help="Comma-separated list of specific metrics to calculate "
"(SpiegelhalterZ,ECE-H,MCE-H,HL-H,ECE-C,MCE-C,HL-C,COX,Loess,all). "
"Default: all",
)
parser.add_argument(
"--prevalence_adjustment",
default=False,
action="store_true",
help="Perform prevalence adjustment (default: False)",
)
parser.add_argument(
"--n_bootstrap",
type=int,
default=0,
help="Number of bootstrap samples (default: 0)",
)
parser.add_argument(
"--bootstrap_ci",
type=float,
default=0.95,
help="Bootstrap confidence interval (default: 0.95)",
)
parser.add_argument(
"--class_to_calculate",
type=int,
default=1,
help="Class to calculate metrics for (default: 1)",
)
parser.add_argument(
"--num_bins",
type=int,
default=10,
help="Number of bins for ECE/MCE/HL calculations (default: 10)",
)
parser.add_argument(
"--hl_test_validation",
default=False,
action="store_true",
help="Using nbin instead of nbin-2 as HL test DOF. Use it if the dataset is validation set.",
)
parser.add_argument(
"--topclass",
default=False,
action="store_true",
help="Whether to transform the problem to top-class problem.",
)
parser.add_argument(
"--save_metrics", type=str, help="Save the metrics to a csv file"
)
parser.add_argument(
"--plot",
default=False,
action="store_true",
help="Plot reliability diagram (default: False)",
)
parser.add_argument(
"--plot_bins",
type=int,
default=10,
help="Number of bins for reliability diagram",
)
parser.add_argument(
"--save_plot",
default="",
type=str,
help="Save the plot to a file. Must end with valid image formats.",
)
parser.add_argument(
"--save_diagram_output",
default="",
type=str,
help="Save the reliability diagram output to a file",
)
parser.add_argument(
"--verbose", default=True, action="store_true", help="Print verbose output"
)
args = parser.parse_args()
loader = data_loader(args.csv_file)
if args.topclass:
loader = loader.transform_topclass()
if not loader.have_subgroup:
perform_calculation(
probs=loader.probs, labels=loader.labels, args=args, suffix=""
)
else:
perform_calculation(
probs=loader.probs, labels=loader.labels, args=args, suffix=""
)
for i, subgroup_column in enumerate(loader.subgroup_indices):
for j, subgroup_class in enumerate(loader.subgroups_class[i]):
proba = loader.probs[loader.subgroups_index[i][j], :]
label = loader.labels[loader.subgroups_index[i][j]]
perform_calculation(
probs=proba,
labels=label,
args=args,
suffix=f"subgroup_{i+1}_group_{subgroup_class}",
)
if __name__ == "__main__":
main()