-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_ntrees.py
456 lines (350 loc) · 9.65 KB
/
test_ntrees.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import contextlib
import joblib
import numpy as np
import matplotlib.pyplot as plt
from joblib import Parallel, delayed
from sklearn.metrics import roc_curve
from tqdm.notebook import trange, tqdm
from sktree.ensemble import HonestForestClassifier
from sktree.stats import build_hyppo_oob_forest
# In[3]:
# @contextlib.contextmanager
# def tqdm_joblib(tqdm_object):
# """Context manager to patch joblib to report into tqdm progress bar given as argument"""
# class TqdmBatchCompletionCallback(joblib.parallel.BatchCompletionCallBack):
# def __call__(self, *args, **kwargs):
# tqdm_object.update(n=self.batch_size)
# return super().__call__(*args, **kwargs)
# old_batch_callback = joblib.parallel.BatchCompletionCallBack
# joblib.parallel.BatchCompletionCallBack = TqdmBatchCompletionCallback
# try:
# yield tqdm_object
# finally:
# joblib.parallel.BatchCompletionCallBack = old_batch_callback
# tqdm_object.close()
# In[4]:
FILE = "mendseq.featurematrix.csv.pkl"
N_ESTIMATORS = list(range(100, 4001, 100))
# N_ESTIMATORS[0] = 10
REPS = 5
# In[5]:
def sensitivity_at_specificity(y_true, y_score, target_specificity=0.98, pos_label=1):
n_trees, n_samples, n_classes = y_score.shape
# Compute nan-averaged y_score along the trees axis
y_score_avg = np.nanmean(y_score, axis=0)
# Extract true labels and nan-averaged predicted scores for the positive class
y_true = y_true.ravel()
y_score_binary = y_score_avg[:, 1]
# Identify rows with NaN values in y_score_binary
nan_rows = np.isnan(y_score_binary)
# Remove NaN rows from y_score_binary and y_true
y_score_binary = y_score_binary[~nan_rows]
y_true = y_true[~nan_rows]
# Compute ROC curve
fpr, tpr, thresholds = roc_curve(y_true, y_score_binary, pos_label=pos_label)
# Find the threshold corresponding to the target specificity
index = np.argmax(fpr >= (1 - target_specificity))
threshold_at_specificity = thresholds[index]
# Compute sensitivity at the chosen specificity
# sensitivity = tpr[index]
# return sensitivity
# Use the threshold to classify predictions
y_pred_at_specificity = (y_score_binary >= threshold_at_specificity).astype(int)
# Compute sensitivity at the chosen specificity
sensitivity = np.sum((y_pred_at_specificity == 1) & (y_true == 1)) / np.sum(
y_true == 1
)
return sensitivity
# In[6]:
data = np.load(FILE, allow_pickle=True)
columns_to_remove = [
"Run",
"Sample",
"Library",
"Cancer Status",
"Tumor type",
"MAF",
"Stage",
"P7",
"P7 primer",
"P7 Primer",
"Library volume",
"Library Volume",
"UIDs Used",
"Avg GC",
"Library volume (uL)",
"Total Reads",
"Total Alu",
]
y = data["Cancer Status"].to_numpy()
features = data.loc[:, ~data.columns.isin(columns_to_remove)].columns.tolist()
X = data.loc[:, features].to_numpy()
X = np.nan_to_num(X)
X.shape, y.shape
# In[6]:
# total_sas98s = []
# for n_estimators in tqdm(N_ESTIMATORS, desc="Estimators"):
# sas98s = []
# for reps in trange(REPS, desc="REPS"):
# est = HonestForestClassifier(
# n_estimators=n_estimators,
# # random_state=seed,
# honest_fraction=0.5,
# n_jobs=-1,
# bootstrap=True,
# stratify=True,
# max_samples=1.6,
# # permute_per_tree=True,
# )
# _, posterior_arr = build_hyppo_oob_forest(
# est,
# X,
# y,
# verbose=False,
# )
# sas98 = sensitivity_at_specificity(
# y, posterior_arr, target_specificity=0.98
# )
# sas98s.append(sas98)
# total_sas98s.append(sas98s)
# In[7]:
# np.array(total_sas98s)
# In[8]:
# fig, ax = plt.subplots()
# ax.plot(N_ESTIMATORS, np.var(total_sas98s, axis=1))
# ax.set(
# xlabel="Number of Trees",
# ylabel="Variance of S@98 Statistic",
# title="Variance of S@98 Stat vs. Number of Trees"
# )
# plt.show()
# In[9]:
# fig, ax = plt.subplots()
# ax.plot(N_ESTIMATORS, np.mean(total_sas98s, axis=1))
# ax.set(
# xlabel="Number of Trees",
# ylabel="Mean of S@98 Statistic",
# title="Mean of S@98 Stat vs. Number of Trees"
# )
# plt.show()
# In[10]:
# total_sas98s = []
# for seed in trange(10, desc="Reps S@98"):
# est = HonestForestClassifier(
# n_estimators=N_ESTIMATORS[-1],
# random_state=seed,
# honest_fraction=0.5,
# n_jobs=-1,
# bootstrap=True,
# stratify=True,
# max_samples=1.6,
# # permute_per_tree=True,
# )
# _, posterior_arr = build_hyppo_oob_forest(
# est,
# X,
# y,
# verbose=False
# )
# sas98s = []
# for tree in N_ESTIMATORS:
# sas98 = sensitivity_at_specificity(
# y, posterior_arr[:tree, :, :], target_specificity=0.98
# )
# sas98s.append(sas98)
# total_sas98s.append(sas98s)
# In[11]:
# fig, ax = plt.subplots()
# ax.plot(N_ESTIMATORS, np.var(total_sas98s, axis=0))
# ax.set(
# xlabel="Number of Trees",
# ylabel="Variance of S@98 Statistic",
# title="Variance of S@98 Stat vs. Number of Trees"
# )
# plt.show()
# In[12]:
# fig, ax = plt.subplots()
# ax.plot(N_ESTIMATORS, sas98s)
# ax.set(
# xlabel="Number of Trees",
# ylabel="Variance of S@98 Statistic",
# title="Variance of S@98 Stat vs. Number of Trees"
# )
# plt.show()
# In[14]:
# sas98s = []
# for seed in trange(100, desc="Reps Histogram S@98"):
# est = HonestForestClassifier(
# n_estimators=1600,
# random_state=seed,
# honest_fraction=0.5,
# n_jobs=-1,
# bootstrap=True,
# stratify=True,
# max_samples=1.6,
# # permute_per_tree=True,
# )
# _, posterior_arr = build_hyppo_oob_forest(
# est,
# X,
# y,
# verbose=False
# )
# sas98 = sensitivity_at_specificity(
# y, posterior_arr, target_specificity=0.98
# )
# sas98s.append(sas98)
# In[16]:
# fig, ax = plt.subplots()
# ax.hist(sas98s)
# ax.set(
# xlabel="S@98",
# ylabel="Counts",
# title="Histogram of S@98 for 1600 Trees"
# )
# plt.show()
# In[17]:
# sas98s = []
# for seed in trange(100, desc="Reps Histogram S@98"):
# est = HonestForestClassifier(
# n_estimators=4000,
# random_state=seed,
# honest_fraction=0.5,
# n_jobs=-1,
# bootstrap=True,
# stratify=True,
# max_samples=1.6,
# # permute_per_tree=True,
# )
# _, posterior_arr = build_hyppo_oob_forest(
# est,
# X,
# y,
# verbose=False
# )
# sas98 = sensitivity_at_specificity(
# y, posterior_arr, target_specificity=0.98
# )
# sas98s.append(sas98)
# In[19]:
# fig, ax = plt.subplots()
# ax.hist(sas98s)
# ax.set(
# xlabel="S@98",
# ylabel="Counts",
# title="Histogram of S@98 for 4000 Trees"
# )
# plt.show()
# In[7]:
# sas98s = []
# for seed in trange(100, desc="Reps Histogram S@98"):
# est = HonestForestClassifier(
# n_estimators=10000,
# random_state=seed,
# honest_fraction=0.5,
# n_jobs=-1,
# bootstrap=True,
# stratify=True,
# max_samples=1.6,
# # permute_per_tree=True,
# )
# _, posterior_arr = build_hyppo_oob_forest(
# est,
# X,
# y,
# verbose=False
# )
# sas98 = sensitivity_at_specificity(
# y, posterior_arr, target_specificity=0.98
# )
# sas98s.append(sas98)
# In[8]:
# fig, ax = plt.subplots()
# ax.hist(sas98s)
# ax.set(
# xlabel="S@98",
# ylabel="Counts",
# title="Histogram of S@98 for 10000 Trees"
# )
# plt.show()
# In[13]:
# def _parallel_counts(seed):
# est = HonestForestClassifier(
# n_estimators=100000,
# random_state=seed,
# honest_fraction=0.5,
# n_jobs=1,
# bootstrap=True,
# stratify=True,
# max_samples=1.6,
# # permute_per_tree=True,
# )
# _, posterior_arr = build_hyppo_oob_forest(
# est,
# X,
# y,
# verbose=False
# )
# sas98 = sensitivity_at_specificity(
# y, posterior_arr, target_specificity=0.98
# )
# return sas98
# In[15]:
# # with tqdm_joblib(tqdm(desc="Reps Histogram S@98", total=100)) as progress_bar:
# sas98s = Parallel(n_jobs=-2, verbose=100)(
# [
# delayed(_parallel_counts)(seed)
# for seed in range(100)
# ]
# )
# In[8]:
# fig, ax = plt.subplots()
# ax.hist(sas98s)
# ax.set(
# xlabel="S@98",
# ylabel="Counts",
# title="Histogram of S@98 for 10000 Trees"
# )
# plt.show()
# In[ ]:
from sklearn.ensemble import RandomForestClassifier
sas98s = []
for seed in range(100):
print("Create Object")
est = HonestForestClassifier(
n_estimators=100000,
random_state=seed,
# honest_fraction=0.5,
n_jobs=-1,
bootstrap=True,
# stratify=True,
# max_samples=1.6,
# permute_per_tree=True,
)
# print("Fit Estimator")
# est.fit(X, y)
print("Get Posteriors")
# posterior_arr = est.predict_proba(X)
_, posterior_arr = build_hyppo_oob_forest(
est,
X,
y,
verbose=False
)
print("Compute S@98")
sas98 = sensitivity_at_specificity(
y, posterior_arr, target_specificity=0.98
)
sas98s.append(sas98)
# In[ ]:
fig, ax = plt.subplots()
ax.hist(sas98s)
ax.set(
xlabel="S@98",
ylabel="Counts",
title="Histogram of S@98 for 100000 Trees"
)
plt.savefig('figs/histogram.png', bbox_inches='tight')