-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_recall.py
151 lines (124 loc) · 5.54 KB
/
eval_recall.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
import random
from pathlib import Path
import typer
import json
import numpy as np
from sklearn.metrics import roc_curve, roc_auc_score
import pandas as pd
from tqdm import tqdm
def test():
input_dir = Path("/mnt/storage/scratch/axel/cats/paper_debug_regularisation_36")
folders = list(input_dir.glob("**/fold_data"))
recall_data = list(input_dir.glob("**/recall_data.csv"))
dfs = []
for file in recall_data:
df = pd.read_csv(file)
df["dataset"] = file
dfs.append(df)
data = pd.concat(dfs)
data = data.sort_values(["optimal_sensitivity", "optimal_specificity"])
dir_list, auc_list, optimal_threshold_list, optimal_sensitivity_list, optimal_specificity_list = [], [], [], [], []
for folder in folders:
auc, optimal_threshold, optimal_sensitivity, optimal_specificity = eval_recall(folder)
auc_list.append(auc)
optimal_threshold_list.append(optimal_threshold)
optimal_sensitivity_list.append(optimal_sensitivity)
optimal_specificity_list.append(optimal_specificity)
dir_list.append(folder)
df = pd.DataFrame()
df["auc"] = auc_list
df["optimal_threshold"] = optimal_threshold_list
df["optimal_sensitivity"] = optimal_sensitivity_list
df["optimal_specificity"] = optimal_specificity_list
df["directory"] = dir_list
df = df.sort_values(["auc", "optimal_sensitivity", "optimal_specificity"])
print(df)
filepath = input_dir / "recall_test.csv"
print(filepath)
df.to_csv(filepath, index=False)
def get_cli(data, l="Sensitivity"):
print(f"DATA={data}")
# Convert the list to a NumPy array for convenience
data = np.array(data)
# Calculate the median
median = np.nanmedian(data)
# Calculate the 2.5th and 97.5th percentiles for the confidence interval
lower_bound = np.nanpercentile(data, 2.5)
upper_bound = np.nanpercentile(data, 97.5)
print(f"Median {l}: {median:.4f}")
print(f"95% Confidence Interval: ({lower_bound:.4f}, {upper_bound:.4f})")
def eval_recall(
input_folder: Path = typer.Option(
..., exists=False, file_okay=False, dir_okay=True, resolve_path=True
),
n_boots: int = 100
):
optimal_sensitivity_list, optimal_specificity_list, auc_list, optimal_threshold_list = [], [], [], []
for i in range(n_boots):
print("Find optimal Sensitivity/Specificity...")
print(input_folder)
fold_data_files = list(input_folder.glob("*.json"))
num_files_to_select = int(len(fold_data_files) * 0.95)
selected_files = random.sample(fold_data_files, num_files_to_select)
y_true_list = []
y_score_list = []
# optimal_sensitivity_list = []
# optimal_specificity_list = []
for file in tqdm(selected_files):
with open(file, "r") as fp:
fold_data = json.load(fp)
y_true = fold_data["y_test"]
y_true_list.extend(y_true)
y_score = fold_data["y_pred_proba_test"]
y_score_list.extend(y_score)
# print(y_true)
# print(y_score)
# fpr, tpr, thresholds = roc_curve(y_true, y_score)
# sensitivity = tpr
# specificity = 1 - fpr
# optimal_idx = np.argmax(sensitivity + specificity)
# optimal_threshold = thresholds[optimal_idx]
# optimal_sensitivity = sensitivity[optimal_idx]
# optimal_specificity = specificity[optimal_idx]
# optimal_sensitivity_list.append(optimal_sensitivity)
# optimal_specificity_list.append(optimal_specificity)
#
# get_cli(optimal_sensitivity_list, "optimal_sensitivity")
# get_cli(optimal_specificity_list, "optimal_specificity")
fpr, tpr, thresholds = roc_curve(y_true_list, y_score_list)
sensitivity = tpr
specificity = 1 - fpr
# # Find the index of the point on the ROC curve closest to the top-left corner
# optimal_idx = np.argmax(sensitivity + specificity)
# Calculate Youden's index
youden_index = sensitivity + specificity - 1
# Find the optimal threshold
optimal_idx = np.argmax(youden_index)
optimal_threshold = thresholds[optimal_idx]
optimal_sensitivity = sensitivity[optimal_idx]
optimal_specificity = specificity[optimal_idx]
print("Optimal Threshold:", optimal_threshold)
print("Optimal Sensitivity:", optimal_sensitivity)
print("Optimal Specificity:", optimal_specificity)
auc = roc_auc_score(y_true_list, y_score_list)
print("AUC:", auc)
df = pd.DataFrame()
df["auc"] = [auc]
df["optimal_threshold"] = [optimal_threshold]
df["optimal_sensitivity"] = [optimal_sensitivity]
df["optimal_specificity"] = [optimal_specificity]
filepath = input_folder.parent / f"{i}_recall_data.csv"
print(filepath)
df.to_csv(filepath, index=False)
optimal_sensitivity_list.append(optimal_sensitivity)
optimal_specificity_list.append(optimal_specificity)
auc_list.append(auc)
#return auc, optimal_threshold, optimal_sensitivity, optimal_specificity
get_cli(auc_list, "auc_list")
get_cli(optimal_sensitivity_list, "optimal_sensitivity")
get_cli(optimal_specificity_list, "optimal_specificity")
return auc_list, optimal_threshold_list, optimal_sensitivity_list, optimal_specificity_list
if __name__ == "__main__":
#test()
#"E:\Cats\paper_13\All_100_10_030_008\rbf\_LeaveOneOut\fold_data"
typer.run(eval_recall)