-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat_project_graph.py
278 lines (240 loc) · 8.94 KB
/
cat_project_graph.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
from pathlib import Path, PurePath
import matplotlib
import pandas as pd
import json
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
import typer
from sklearn.metrics import roc_curve, auc
from tqdm import tqdm
import matplotlib.cm as cm
import matplotlib.colors as mcolors
from matplotlib.legend_handler import HandlerBase
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import precision_score
from sklearn.metrics import PrecisionRecallDisplay
DEFAULT_PLOTLY_COLORS = [
"rgb(31, 119, 180)",
"rgb(255, 127, 14)",
"rgb(44, 160, 44)",
"rgb(214, 39, 40)",
"rgb(148, 103, 189)",
"rgb(140, 86, 75)",
"rgb(227, 119, 194)",
"rgb(127, 127, 127)",
"rgb(188, 189, 34)",
"rgb(23, 190, 207)",
]
class AnyObjectHandler(HandlerBase):
def create_artists(self, legend, orig_handle,
x0, y0, width, height, fontsize, trans):
l1 = plt.Line2D([x0,y0+width], [0.7*height,0.7*height],
linestyle=orig_handle[1], color=orig_handle[0])
l2 = plt.Line2D([x0,y0+width], [0.3*height,0.3*height],
color=orig_handle[0])
return [l1, l2]
def mean_confidence_interval(x):
# boot_median = [np.median(np.random.choice(x, len(x))) for _ in range(iteration)]
x.values.sort()
lo_x_boot = np.percentile(x, 2.5)
hi_x_boot = np.percentile(x, 97.5)
# print(lo_x_boot, hi_x_boot)
return lo_x_boot, hi_x_boot
def local_run(
input_dir=Path("H:/Cats/article/ml_build_permutations_thesis"),
out_dir=Path("H:/Cats/article/ml_build_permutations_thesis"),
):
main(input_dir, out_dir)
def main(
input_dir: Path = typer.Option(
..., exists=True, file_okay=False, dir_okay=True, resolve_path=True
),
out_dir: Path = typer.Option(
..., exists=True, file_okay=False, dir_okay=True, resolve_path=True
),
):
data = []
for p in input_dir.rglob("*.json"):
if "proba" in str(p) or "fold_data" in str(p):
continue
split = list(PurePath(p).parts)
data.append(split + [p])
df = pd.DataFrame(data)
thresh_list = []
median_auc_test_list = []
median_auc_train_list = []
auc_test_list = []
auc_train_list = []
n_samples = []
window_size_list = []
p_steps_list = []
n_peaks = []
for index, row in df.iterrows():
res_file_path = row[8]
if "3000__001__0_00100__030" in str(res_file_path):
continue
if "2000__001__0_00100__030" in str(res_file_path):
continue
try:
results = json.load(open(res_file_path))
except Exception as e:
print(e)
continue
steps = row[5]
p_steps_list.append(steps)
windowsize = int(row[4].split("_")[-1])
window_size_list.append(windowsize)
thresh = row[4].split("__")[-2]
t_v = thresh.replace("_", ".")
thresh_float = float(t_v)
npeak = int(row[4].split("__")[1])
n_peaks.append(npeak)
print(res_file_path)
clf_res = results[list(results.keys())[0]]
aucs_test = []
aucs_train = []
precision_test = []
precision_train = []
all_y_test = []
all_probs_test = []
all_y_train = []
all_probs_train = []
for item in clf_res:
if "auc" not in item:
continue
# auc = item["auc"]
# aucs.append(auc)
all_y_test.extend(item["y_test"])
all_probs_test.extend(np.array(item["y_pred_proba_test"]))
all_y_train.extend(item["y_train"])
all_probs_train.extend(np.array(item["y_pred_proba_train"]))
training_shape = len(item["ids_train"])
testing_shape = len(item["ids_test"])
all_y_test = np.array(all_y_test)
all_probs_test = np.array(all_probs_test)
fpr, tpr, thresholds = roc_curve(all_y_test, all_probs_test)
roc_auc = auc(fpr, tpr)
aucs_test.append(roc_auc)
display = PrecisionRecallDisplay.from_predictions(all_y_test, all_probs_test, name="LinearSVC")
title = f"Precision-Recall curve |\n ws={windowsize} npeak={npeak} psteps={steps}"
_ = display.ax_.set_title(title)
filename = f"pr_{windowsize}_{npeak}_{steps}.png"
path = out_dir / "pr_curve_test"
path.mkdir(parents=True, exist_ok=True)
filepath = path / filename
print(filepath)
display.figure_.savefig(filepath)
precision_test.append(display.average_precision)
auc_test_list.append(aucs_test)
median_auc_test = np.median(aucs_test)
thresh_list.append(thresh_float)
median_auc_test_list.append(median_auc_test)
n_samples.append(training_shape + testing_shape)
all_y_train = np.array(all_y_train)
all_probs_train = np.array(all_probs_train)
fpr, tpr, thresholds = roc_curve(all_y_train, all_probs_train)
roc_auc = auc(fpr, tpr)
aucs_train.append(roc_auc)
display = PrecisionRecallDisplay.from_predictions(all_y_train, all_probs_train, name="LinearSVC")
title = f"Precision-Recall curve |\n ws={windowsize} npeak={npeak} psteps={steps}"
_ = display.ax_.set_title(title)
filename = f"pr_{windowsize}_{npeak}_{steps}.png"
path = out_dir / "pr_curve_train"
path.mkdir(parents=True, exist_ok=True)
filepath = path / filename
print(filepath)
display.figure_.savefig(filepath)
precision_train.append(display.average_precision)
auc_train_list.append(aucs_train)
median_auc_train = np.median(aucs_train)
median_auc_train_list.append(median_auc_train)
df_data = pd.DataFrame(
{
"auc_test_list": auc_test_list,
"median_auc_test": median_auc_test_list,
"auc_train_list": auc_train_list,
"median_auc_train": median_auc_train_list,
"thresh_list": thresh_list,
"n_samples": n_samples,
"p_steps_list": p_steps_list,
"window_size_list": window_size_list,
"n_peaks": n_peaks,
}
)
print(df_data)
fig, ax1 = plt.subplots(figsize=(10.80, 10.80))
ax2 = ax1.twinx()
dfs = [group for _, group in df_data.groupby(['p_steps_list'])]
ax2.bar(
[1, 2, 3, 4, 5, 6],
[520, 4680, 37440, 52000, 52000, 52000],
color="grey",
label="n samples",
alpha=0.4,
width=0.2
)
colors = list(mcolors.CSS4_COLORS.keys())
print(colors)
cpt = 0
colors_ = []
label_ = []
for i, df in enumerate(dfs):
dfs_ = [group for _, group in df.groupby(['window_size_list'])]
for df_ in dfs_:
# df_ = df_[df_['n_peaks'] <= 4]
# if 'linear' in df_['p_steps_list'].tolist()[0]:
# continue
# if 'linear' in df_['p_steps_list'].tolist()[0]:
# continue
# if 'cats_LeaveOneOut_-1_-1_STD_rbf' in df_['p_steps_list'].tolist()[0]:
# continue
#
# if 'cats_LeaveOneOut_-1_-1_STD_linear' in df_['p_steps_list'].tolist()[0]:
# continue
print(df_['p_steps_list'].tolist()[0])
#
# if len(df_["median_auc_test"]) != 4:
# continue
#print(df_["n_samples"])
label = f"Window size={df_['window_size_list'].tolist()[0]*2} sec | {'>'.join(df_['p_steps_list'].tolist()[0].split('_')[4:])}"
ax1.plot(
df_["n_peaks"],
df_["median_auc_test"],
label=label,
marker="x",
color=colors[cpt]
)
ax1.plot(
df_["n_peaks"],
df_["median_auc_train"],
#label=f"Train Window size={df_['window_size_list'].tolist()[0]*2} sec | {'>'.join(df_['p_steps_list'].tolist()[0].split('_')[4:])}",
marker=".",
linestyle='-.',
color=colors[cpt]
)
cpt +=1
colors_.append(colors[cpt])
label_.append(label)
ax1.axhline(y=0.5, color='black', linestyle='--')
fig.suptitle("Evolution of AUC(training and testing) with N peak increase")
ax1.set_xlabel("Number of peaks")
ax1.set_ylabel("Mean AUC")
ax2.set_ylabel("Number of samples(high activity peak window)")
#plt.legend()
#ax1.legend(loc="lower right").set_visible(True)
ax2.legend(loc="upper left").set_visible(True)
color_data = []
for item in colors_:
color_data.append((item, '--'))
ax1.legend(color_data, label_, loc="lower right",
handler_map={tuple: AnyObjectHandler()})
fig.tight_layout()
filename = f"auc_per_npeak.png"
out_dir.mkdir(parents=True, exist_ok=True)
filepath = out_dir / filename
print(filepath)
fig.savefig(filepath)
if __name__ == "__main__":
local_run()
# typer.run(main)