-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew_evaluator.py
1248 lines (1166 loc) · 48.4 KB
/
new_evaluator.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from copy import deepcopy
from scipy.stats import kendalltau
import numpy as np
import dataloader
from dataloader import yield_to_ranking
from abc import ABC, abstractmethod
from label_ranking import *
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV, PredefinedSplit
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from scipy.stats.mstats import rankdata
PERFORMANCE_DICT = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
class Evaluator(ABC):
"""Base class for evaluators for various types of algorithsm.
Parameters
----------
dataset : Dataset object from Dataloader.py
Dataset to utilize.
n_rxns : int
Number of reactions that is simulated to be selected and conducted.
outer_cv : sklearn split object or list of them
Cross-validation scheme to 'evaluate' the algorithms.
n_rxns_to_erase : int, default=0
Number of reaction conditions to erase from each substrate pair.
n_evaluations : int, default=1
Number of evaluations to conduct.
"""
def __init__(self, dataset, n_rxns, outer_cv, n_rxns_to_erase=0, n_evaluations=1, use_all_conditions=False):
self.dataset = dataset
self.n_rxns = n_rxns
self.outer_cv = outer_cv
self.n_rxns_to_erase = n_rxns_to_erase
self.n_evaluations = n_evaluations
self.use_all_conditions = use_all_conditions # only when n_rxns_to_erase > 0 and specified.
if self.n_rxns_to_erase >= 1 :
np.random.seed(42)
def _update_perf_dict(self, perf_dict, kt, rr, mrr, regret, comp, model, eval_loop):
"""Updates the dictionary keeping track of performances.
Parameters
----------
perf_dict : dictionary
Has 6 keys as you can see below.
kt : list or float
Kendall-tau ranking correlation scores.
This being a list comes from the case when multiple rows are evaluated simultaneously.
rr : list or float
Reciprocal (ground-truth) rank of the best selection by the model.
mrr : list or float
Mean reciprocal (ground-truth) rank of all selections by the model.
regret : list or float
Ground-truth best yield - ground-truth yield of best selection by model.
comp : int
Compound index of the test compound.
model : str
Name of the model
eval_loop : int
Evaluation loop index.
Returns
-------
None
The perf_dict input is updated in-place.
"""
if type(kt) != list:
perf_dict["kendall_tau"].append(kt)
perf_dict["reciprocal_rank"].append(rr)
perf_dict["mean_reciprocal_rank"].append(mrr)
perf_dict["regret"].append(regret)
perf_dict["test_compound"].append(comp)
perf_dict["model"].append(model)
perf_dict["evaluation_loop"].append(eval_loop)
elif type(kt) == list:
assert len(kt) == len(rr)
assert len(rr) == len(mrr)
perf_dict["kendall_tau"].extend(kt)
perf_dict["reciprocal_rank"].extend(rr)
perf_dict["mean_reciprocal_rank"].extend(mrr)
perf_dict["regret"].extend(regret)
if type(comp) == list:
perf_dict["test_compound"].extend(comp)
else:
perf_dict["test_compound"].extend([comp] * len(kt))
if type(model) == "list":
perf_dict["model"].extend(model)
elif type(model) == str:
perf_dict["model"].extend([model] * len(kt))
perf_dict["evaluation_loop"].extend([eval_loop] * len(kt))
def _evaluate_alg(self, perf_dict, test_yield, pred_rank, comp, model, eval_loop=0):
"""Evaluates algorithms on the following metrics
• 'reciprocal rank' : of the selected n_rxns, the reciprocal of the best ground-truth-rank.
• 'mean reciprocal rank' : mean of the reciprocal of ground-truth-ranks of all rxns selected by model.
• 'regret' : the numerical difference between the ground-truth highest yield and
the best yield retrieved by model.
and updates the performance dictionary.
If the yield array from which predicted_rank is derived from has ties at the top values, that means
under the constraint that we can choose only one reaction we randomly choose one of them.
For convenience we select the one that shows up the earliest in the vector.
Parameters
----------
test_yield : np.ndarray of shape (n_test_rxns, ) or (n_test_substrates, n_rxn_conditions)
Array of numerical yields from the test set.
pred_rank : np.ndarray of shape (n_test_substrates, n_rxn_conditions)
Array of predicted ranks under each reaction condition
comp : int
Index of test compound.
model : str
Name of the algorithm that is evaluated.
eval_loop : int
Index of evaluation loop.
Returns
-------
self (with updated perf_dict)
"""
def _yield_to_rank_with_ties(yield_array):
copy = deepcopy(yield_array)
if copy.ndim == 1 :
copy = np.expand_dims(copy, 0)
raw_rank = copy.shape[1] + 1 - rankdata(copy, axis=1)
for i, row in enumerate(copy):
raw_rank[i, np.where(row == 0)[0]] = 25 # penalize for picking 0% yielding reaction
return raw_rank
test_rank = _yield_to_rank_with_ties(test_yield)
if np.ndim(pred_rank) == 1:
test_rank = test_rank.flatten()
kt = kendalltau(test_rank, pred_rank).statistic
predicted_highest_yield_inds = np.argpartition(
pred_rank.flatten(), self.n_rxns
)[: self.n_rxns]
best_retrieved_yield = np.max(test_yield[predicted_highest_yield_inds])
actual_inds_with_that_yield = np.where(test_yield == best_retrieved_yield)[
0
]
rr = 1 / np.min(test_rank[actual_inds_with_that_yield])
mrr = np.mean(
np.reciprocal(test_rank[predicted_highest_yield_inds], dtype=np.float32)
)
regret = max(test_yield) - max(test_yield[predicted_highest_yield_inds])
elif np.ndim(pred_rank) == 2:
kt = [
kendalltau(test_rank[i, :], pred_rank[i, :]).statistic
for i in range(pred_rank.shape[0])
]
predicted_highest_yield_inds = np.argpartition(
pred_rank, self.n_rxns, axis=1
)[:, : self.n_rxns]
best_retrieved_yield = [
np.max(test_yield[i, row])
for i, row in enumerate(predicted_highest_yield_inds)
]
actual_inds_with_that_yield = [
np.where(test_yield[i, :] == best_y)[0]
for i, best_y in enumerate(best_retrieved_yield)
]
rr = [
1 / np.min(test_rank[a, x])
for a, x in enumerate(actual_inds_with_that_yield)
]
mrr = [
np.mean(np.reciprocal(test_rank[a, row]))
for a, row in enumerate(predicted_highest_yield_inds)
]
raw_regret = np.max(test_yield, axis=1) - np.max(
np.vstack(
tuple(
[
test_yield[i, row]
for i, row in enumerate(predicted_highest_yield_inds)
]
)
),
axis=1,
)
regret = list(raw_regret.flatten())
print("RECIPROCAL RANK", rr)
print("REGRET", regret)
self._update_perf_dict(perf_dict, kt, rr, mrr, regret, comp, model, eval_loop)
def _load_X(self):
"""Prepares the input features based on the specified type."""
if self.feature_type == "desc":
X = self.dataset.X_desc
elif self.feature_type == "fp":
X = self.dataset.X_fp
elif self.feature_type == "onehot":
X = self.dataset.X_onehot
elif self.feature_type == "random":
X = self.dataset.X_random
return X
def _dist_array_train_test_split(self, dist_array, test_ind):
"""
Splits a precomputed distance array into those only with training compounds and test compounds.
We assume that only one test compound is left out.
Parameters
----------
dist_array : np.ndarray of shape (n_substrates, n_substrates)
Precomputed Tanimoto distance array.
test_ind : int [0, dist_array.shape[0]-1]
Index of the test compound to separate out from the dist_array.
Returns
-------
train_dists : np.ndarray of shape (n_substrates-1, n_substrates-1)
Distances between the training compounds.
test_dists : np.ndarray of shape (n_substrates-1)
Distance of test compound to all other training compounds.
"""
train_dists = np.vstack(
tuple(
[row for ind, row in enumerate(dist_array) if ind not in test_ind]
) # used to be != instead of not in
)
train_dists = train_dists[
:, [x for x in range(train_dists.shape[1]) if x not in test_ind]
]
print("DIST ARRAY SHAPE", dist_array.shape)
if len(test_ind) == 1:
test_dists = dist_array[
test_ind, [x for x in range(dist_array.shape[1]) if x != test_ind]
]
else:
test_dists = np.vstack(
tuple(
[
dist_array[
x,
[
a
for a in range(dist_array.shape[1])
if a not in test_ind
],
]
for x in test_ind
]
)
)
print("DIST SHAPES", train_dists.shape, test_dists.shape)
return train_dists, test_dists
@abstractmethod
def train_and_evaluate_models(self):
pass
def _CV_loops(
self, perf_dict, cv, X, y, further_action_before_logging, y_yield=None
):
"""
Implements multiple CV loops that are utilized across all algorithms.
Parameters
----------
perf_dict : dict
Dictionary to keep track of performance measurements.
cv : sklearn split object
Cross-validation scheme to execute.
X : np.ndarray of shape (n_reactions, n_features)
Input features.
y : np.ndarray of shape (n_reactions, ) or (n_substrates, n_reaction_conditions)
Array the model needs to train upon!
For label rankers or classifiers, this should not be yields!
further_action_before_logging : function
Parameters: trained model, X_test, test_YIELD_ARRAY
y_yield : np.ndarray of shape (n_reactions, )
For label rankers and classifiers that does not use raw yields.
Returns
-------
self
perf_dict is updated in-place.
"""
for a, (train_ind, test_ind) in enumerate(cv.split()):
y_train, y_test = y[train_ind], y[test_ind]
print("Y TRAIN, TEST SHAPE", y.shape, y_train.shape, y_test.shape)
if X is not None:
X_train, X_test = X[train_ind, :], X[test_ind]
std = StandardScaler()
X_train = std.fit_transform(X_train)
X_test = std.transform(X_test)
else:
X_test = y_train
if y_yield is not None:
y_yield_test = y_yield[test_ind]
y_yield_train = y_yield[train_ind]
print()
print("CV FOLD", a)
for eval_loop_num in range(self.n_evaluations):
print("EVALUATION #", eval_loop_num)
print("USE ALL CONDS?", self.use_all_conditions)
if self.n_rxns_to_erase >= 1:
if not self.use_all_conditions :
### Deciding which reactions to mask out for this evaluation loop in the format of ranking arrays.
if self.dataset.for_regressor :
shape = (len(train_ind)//self.dataset.n_rank_component, self.dataset.n_rank_component)
else:
shape = (len(train_ind), self.dataset.n_rank_component)
random_numbers = np.random.rand(shape[0], shape[1])
inds_to_erase = (
np.repeat(np.arange(shape[0]), self.n_rxns_to_erase).flatten(),
np.argpartition(random_numbers, kth=self.n_rxns_to_erase, axis=1)[
:, : self.n_rxns_to_erase
].flatten(),
)
print("INDS TO ERASE", inds_to_erase)
# Need this block again so that we use the original arrays for different evaluations.
if X is not None:
X_train = std.transform(X[train_ind, :])
y_train, y_test = y[train_ind], y[test_ind]
# For regressors
if type(self) == RegressorEvaluator:
inds_to_remove = []
inds_to_remove.extend(
[
self.dataset.n_rank_component * row_num + col_num
for row_num, col_num in zip(
inds_to_erase[0], inds_to_erase[1]
)
]
)
inds_to_keep = [
x for x in range(len(y_train)) if x not in inds_to_remove
]
y_train = y_train[inds_to_keep]
X_train = X_train[inds_to_keep]
elif type(self) == MulticlassEvaluator:
y_train_missing = deepcopy(y_yield_train).astype(float)
y_train_missing[inds_to_erase] = np.nan
y_train = np.nanargmax(y_train_missing, axis=1)
# For label ranking
else:
y_train_missing = deepcopy(y_train).astype(float)
y_train_missing[inds_to_erase] = np.nan
if type(self) == LabelRankingEvaluator:
y_train = mstats.rankdata(
np.ma.masked_invalid(y_train_missing), axis=1
)
y_train[y_train == 0] = np.nan
# print("YTRAIN", y_train_missing)
elif type(self) == BaselineEvaluator:
y_train = np.nanmean(y_train_missing, axis=0)
X_train = y_train
X_test = y_train_missing
elif type(self) == MultilabelEvaluator:
y_yield_train = y_yield[train_ind]
y_train_missing = deepcopy(y_yield_train).astype(float)
y_train_missing[inds_to_erase] = np.nan
# print("YTRAIN", y_train_missing)
nonzero_inds = np.argpartition(
-1 * y_train_missing, self.n_rxns, axis=1
)[:, : self.n_rxns]
y_train = np.zeros_like(y_train_missing)
for row_num, col_nums in enumerate(nonzero_inds):
y_train[np.array([row_num] * self.n_rxns), col_nums] = 1
# print("MULTILABEL YTRAIN", y_train)
else :
print("COMES IN HERE")
if self.dataset.for_regressor :
shape = (len(train_ind)//self.dataset.n_rank_component, self.dataset.n_rank_component)
else:
shape = (len(train_ind), self.dataset.n_rank_component)
n_subs_to_choose = int(shape[0]*(1-(self.n_rxns_to_erase/shape[1])))
inds_chosen = np.random.choice(shape[0], n_subs_to_choose, replace=False)
if X is not None:
X_train = std.transform(X[train_ind, :])
y_train, y_test = y[train_ind], y[test_ind]
print("ORIGINAL ARRAY SHAPE", y_train.shape)
print("INDS CHOSEN", inds_chosen)
if type(self) == RegressorEvaluator:
inds_to_keep = []
for start_row in inds_chosen :
inds_to_keep.extend(
[
shape[1] * start_row + col_num
for col_num in range(shape[1])
]
)
y_train = y_train[inds_to_keep]
X_train = X_train[inds_to_keep]
else :
y_train = y_train[inds_chosen]
if type(self) == BaselineEvaluator:
y_train = np.nanmean(y_train, axis=0)
X_train = y_train
else :
X_train = X_train[inds_chosen]
print("FULL CONDITION, NUM SUBS:", X_train.shape, y_train.shape)
for b, (model, model_name) in enumerate(
zip(self.list_of_algorithms, self.list_of_names)
):
###### Model training phase ######
if X is not None:
# For multilabel LogReg
if (
type(model) == GridSearchCV
and type(model.estimator) == LogisticRegression
and type(self) == MultilabelEvaluator
):
trained_models = []
if len(y_train.shape) == 1:
y_train = y_train.reshape(-1, 1)
for i in range(y_train.shape[1]):
if np.sum(y_train[:, i]) in [0, y_train.shape[0]]:
trained_models.append(
float(np.sum(y_train[:, i]) / y_train.shape[0])
)
elif np.sum(y_train[:, i]) > 3:
model.fit(X_train, y_train[:, i])
trained_models.append(model)
else:
lr = LogisticRegression(
penalty="l1",
solver="liblinear", # lbfgs doesn't converge
random_state=42,
)
lr.fit(X_train, y_train[:, i])
trained_models.append(lr)
model = trained_models
# For nearest neighbor based models, if the substrate features are the only inputs
# to the model, use Tanimoto distances.
elif (
type(model) == GridSearchCV
and type(model.estimator)
in [KNeighborsClassifier, IBLR_M, IBLR_PL]
) and not self.dataset.train_together:
dist_array = self.dataset.X_dist
train_dists, test_dists = self._dist_array_train_test_split(
dist_array, test_ind
)
model.fit(train_dists, y_train)
else:
model.fit(X_train, y_train)
###### EVALUATION PHASE ######
# For baseline
if y_yield is None:
(
processed_y_test,
processed_pred_rank,
) = further_action_before_logging(model, X_test, y_test)
# Nearest neighbors based models require different input from other algorithms.
elif (
type(model) == GridSearchCV
and type(model.estimator)
in [KNeighborsClassifier, IBLR_M, IBLR_PL]
# and not self.dataset.train_together
):
(
processed_y_test,
processed_pred_rank,
) = further_action_before_logging(
model, test_dists, y_yield_test
)
else:
(
processed_y_test,
processed_pred_rank,
) = further_action_before_logging(model, X_test, y_yield_test)
print("PROCESSED Y TEST", processed_y_test)
print("PROCESSED PREDICTED RANK", processed_pred_rank)
self._evaluate_alg(
perf_dict,
processed_y_test,
processed_pred_rank,
a,
model_name,
eval_loop_num
)
return self
class BaselineEvaluator(Evaluator):
"""Evaluates the baseline model of selecting based on average yield in the training dataset.
Parameters
----------
dataset : Dataset object as in Dataloader.py
Dataset to utilize.
n_rxns : int
Number of reactions that is simulated to be conducted.
Attributes
----------
perf_dict : dict or list of dicts
Records of model performances, measured by rr, mrr, regret and kendall tau, over each test compound.
"""
def __init__(self, dataset, n_rxns, outer_cv, n_rxns_to_erase=0, n_evaluations=1, use_all_conditions=False):
super().__init__(dataset, n_rxns, outer_cv, n_rxns_to_erase, n_evaluations, use_all_conditions)
self.list_of_algorithms = ["Baseline"]
self.list_of_names = ["Baseline"]
def _processing_before_logging(self, model, y_train, y_test):
processed_pred_rank = np.tile(
yield_to_ranking(np.nanmean(y_train, axis=0)),
(y_test.shape[0], 1),
)
return y_test, processed_pred_rank
def train_and_evaluate_models(self):
y = self.dataset.y_yield
if type(self.outer_cv) == list:
self.perf_dict = []
for i, (array, cv) in enumerate(zip(y, self.outer_cv)):
perf_dict = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
self._CV_loops(
perf_dict, cv, None, array, self._processing_before_logging
)
self.perf_dict.append(perf_dict)
else:
self.perf_dict = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
self._CV_loops(
self.perf_dict, self.outer_cv, None, y, self._processing_before_logging
)
return self
def external_validation(self):
self.valid_dict = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
y_train = self.dataset.y_yield
y_valid = self.dataset.y_valid
self._evaluate_alg(
self.valid_dict,
y_valid,
np.tile(yield_to_ranking(np.mean(y_train, axis=0)), (y_valid.shape[0], 1)),
"Validation",
"Baseline",
)
return self
class MulticlassEvaluator(Evaluator):
"""Evaluates multiclass classifiers.
Note that this doesn't process informer, ullmann and borylation datasets.
Parameters
----------
dataset : Dataset object as in Dataloader.py
Dataset to utilize.
feature_type : str {'desc', 'fp', 'onehot', 'random'}
Which representation to use as inputs.
n_rxns : int
Number of reactions that is simulated to be conducted.
outer_cv : sklearn split object
Cross-validation scheme to 'evaluate' the algorithms.
Attributes
----------
list_of_algorithms : list
GridSearchCV objects of algorithms specified by the user.
perf_dict : dict
Logging of performances of each test substrate.
"""
def __init__(
self,
dataset,
feature_type,
n_rxns,
list_of_names,
outer_cv,
n_rxns_to_erase=0,
n_evaluations=1,
use_all_conditions=False
):
super().__init__(dataset, n_rxns, outer_cv, n_rxns_to_erase, n_evaluations, use_all_conditions)
self.feature_type = feature_type
self.list_of_names = list_of_names
self.list_of_algorithms = []
if type(self.dataset) in [
dataloader.DeoxyDataset,
dataloader.InformerDataset,
dataloader.ScienceDataset,
dataloader.UllmannDataset,
dataloader.BorylationDataset,
dataloader.ArylBorylationDataset
]:
cv = 4
elif type(self.dataset) == dataloader.NatureDataset:
cv = 3
for name in self.list_of_names:
if name == "RFC":
self.list_of_algorithms.append(
GridSearchCV(
RandomForestClassifier(random_state=42),
param_grid={
"n_estimators": [25, 50, 100],
"max_depth": [3, 5, None],
},
scoring="balanced_accuracy",
n_jobs=-1,
cv=cv,
)
)
elif name == "LR":
self.list_of_algorithms.append(
GridSearchCV(
LogisticRegression(
solver="liblinear", # lbfgs doesn't converge
multi_class="auto",
random_state=42,
),
param_grid={"penalty": ["l1", "l2"], "C": [0.1, 0.3, 1, 3, 10]},
scoring="balanced_accuracy",
n_jobs=-1,
cv=cv,
)
)
elif name == "KNN":
if self.dataset.train_together:
metric = "euclidean"
else:
metric = "precomputed"
self.list_of_algorithms.append(
GridSearchCV(
KNeighborsClassifier(metric=metric),
param_grid={"n_neighbors": [3,5,10]},
scoring="balanced_accuracy",
n_jobs=-1,
cv=cv,
)
)
def _get_full_class_proba(self, pred_proba, model):
""" " When the training set is only exposed to a subset of target classes,
the predicted probability for those classes with the multiclass classifier is 0.
To measure metrics, this needs to be accounted for.
This function fills up the classes not in the training set.
Parameters
----------
pred_proba : list of n_classes number of np.ndarrays of shape (n_samples, )
Predicted positive probability values.
model : sklearn classifier object
Trained model that gives the pred_proba array.
Returns
-------
new_pred_proba :
Updated pred_proba array with all classes.
"""
if pred_proba.shape[0] == 1 :
new_pred_proba_list = []
for i in range(self.dataset.n_rank_component):
if i not in model.classes_:
new_pred_proba.append(0)
else:
new_pred_proba.append(pred_proba[0][list(model.classes_).index(i)])
new_pred_proba = np.array(new_pred_proba_list)
else :
allzero_column_idx = []
for i in range(self.dataset.n_rank_component):
if i not in model.classes_:
allzero_column_idx.append(i)
new_pred_proba = np.insert(pred_proba, tuple(allzero_column_idx), 0, axis=1)
return new_pred_proba
def _processing_before_logging(self, model, X_test, y_yield_test):
pred_proba = model.predict_proba(X_test)
print("PRED PROBA", pred_proba)
if len(pred_proba[0]) < self.dataset.n_rank_component:
pred_proba = self._get_full_class_proba(pred_proba, model)
pred_rank_reshape = yield_to_ranking(pred_proba)
return y_yield_test, pred_rank_reshape
def train_and_evaluate_models(self):
X = self._load_X()
y_rank = self.dataset.y_ranking
y_yield = self.dataset.y_yield
if (
type(self.outer_cv) == list
): # When one component is ranked but separating the datasets
y = [
np.argmin(y_sub_rank, axis=1) for y_sub_rank in y_rank
] # transforming ranks into multiclass outputs
self.perf_dict = []
for i, (X_array, array, yield_array, cv) in enumerate(
zip(X, y, y_yield, self.outer_cv)
): # X is not included as it remains the same across different reagents
perf_dict = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
self._CV_loops(
perf_dict,
cv,
X_array,
array,
self._processing_before_logging,
y_yield=yield_array,
)
self.perf_dict.append(perf_dict)
else:
y = np.argmin(y_rank, axis=1)
perf_dict = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
self._CV_loops(
perf_dict,
self.outer_cv,
X,
y,
self._processing_before_logging,
y_yield=y_yield,
)
self.perf_dict = perf_dict
return self
def external_validation(self):
self.valid_dict = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
y_rank_train = self.dataset.y_label
y_rank_valid = self.dataset.y_valid
y_yield_train = self.dataset.y_yield
y_yield_valid = self.dataset.y_valid
for b, (model, model_name) in enumerate(
zip(self.list_of_algorithms, self.list_of_names)
):
if type(model.estimator) != KNeighborsClassifier:
X_train = self._load_X()
X_valid = self.dataset.X_valid
model.fit(X_train, y_rank_train)
else:
X_train = self.dataset.X_dist
X_valid = self.dataset.X_valid
model.fit(X_train, y_rank_train)
pred_proba = model.predict_proba(X_valid)
if len(pred_proba[0]) < self.dataset.n_rank_component:
pred_proba = self._get_full_class_proba(pred_proba, model)
self._evaluate_alg(
self.valid_dict,
y_yield_valid,
yield_to_ranking(pred_proba),
"Validation",
model_name,
)
return self
class MultilabelEvaluator(MulticlassEvaluator):
"""Evaluates multilabel classifiers.
Parameters
----------
dataset : Dataset object as in Dataloader.py
Dataset to utilize.
feature_type : str {'desc', 'fp', 'onehot', 'random'}
Which representation to use as inputs.
n_rxns : int
Number of reactions that is simulated to be conducted.
outer_cv : sklearn split object
Cross-validation scheme to 'evaluate' the algorithms.
"""
def __init__(
self,
dataset,
feature_type,
n_rxns,
list_of_names,
outer_cv,
n_rxns_to_erase=0,
n_evaluations=1,
use_all_conditions=False
):
super().__init__(
dataset,
feature_type,
n_rxns,
list_of_names,
outer_cv,
n_rxns_to_erase,
n_evaluations,
use_all_conditions
)
self.list_of_algorithms = []
for name in self.list_of_names:
if name == "RFC":
self.list_of_algorithms.append(
GridSearchCV(
RandomForestClassifier(random_state=42),
param_grid={
"n_estimators": [25, 50, 100],
"max_depth": [3, 5, None],
},
scoring="roc_auc",
n_jobs=-1,
cv=3,
)
)
elif name == "LR":
self.list_of_algorithms.append(
GridSearchCV(
LogisticRegression(
solver="liblinear", # lbfgs doesn't converge
random_state=42,
),
param_grid={
"penalty": ["l1", "l2"],
"C": [0.1, 0.3, 1, 3, 10],
},
scoring="roc_auc",
n_jobs=-1,
cv=3,
)
)
elif name == "KNN":
if self.dataset.train_together:
metric = "euclidean"
else:
metric = "precomputed"
self.list_of_algorithms.append(
GridSearchCV(
KNeighborsClassifier(metric=metric),
param_grid={"n_neighbors": [2, 4, 6]},
scoring="roc_auc",
n_jobs=-1,
cv=4,
)
)
def _processing_before_logging(self, model, X_test, y_yield_test):
if type(model) == list: # Only for multilabel Logistic Regressions
pred_proba = []
for lr in model:
if type(lr) == float:
if not self.dataset.train_together:
pred_proba.append(np.array([[lr]]))
else:
pred_proba.append(lr * np.ones((X_test.shape[0], 2)))
else:
pred_proba.append(lr.predict_proba(X_test))
# print(pred_proba)
else:
# print(X_test)
if X_test.ndim == 1 :
pred_proba = model.predict_proba(X_test.reshape(1,-1))
else :
pred_proba = model.predict_proba(X_test)
# print("PRED PROBA SHAPE", pred_proba)
if self.dataset.train_together or type(self.dataset) in [
dataloader.UllmannDataset,
dataloader.BorylationDataset,
dataloader.ArylBorylationDataset
]:
arrays_to_stack = []
predict_array = model.predict(X_test)
for i, proba_array in enumerate(pred_proba):
if proba_array.shape[1] > 1:
arrays_to_stack.append(proba_array[:, 1].reshape(-1, 1))
elif proba_array.shape[0] > 1:
arrays_to_stack.append(predict_array[:, i].reshape(-1,1))
# arrays_to_stack.append(proba_array.flatten().reshape(-1, 1))
else:
assert len(proba_array.flatten()) == 1
arrays_to_stack.append(
np.repeat(proba_array, X_test.shape[0], axis=0)
)
pred_proba = np.hstack(tuple(arrays_to_stack))
y_test_reshape = y_yield_test
else:
pred_proba = np.array(
[x[0][1] if len(x[0]) == 2 else 1 - x[0][0] for x in pred_proba]
)
y_test_reshape = y_yield_test.flatten()
pred_rank_reshape = yield_to_ranking(pred_proba)
return y_test_reshape, pred_rank_reshape
def train_and_evaluate_models(self):
X = self._load_X()
y_label = self.dataset.y_label
y_yield = self.dataset.y_yield
if (
type(self.outer_cv) == list
): # When one component is ranked but separating the datasets
self.perf_dict = []
for i, (X_array, array, yield_array, cv) in enumerate(
zip(X, y_label, y_yield, self.outer_cv)
): # X is not included as it remains the same across different reagents
perf_dict = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
self._CV_loops(
perf_dict,
cv,
X_array,
array,
self._processing_before_logging,
y_yield=yield_array,
)
self.perf_dict.append(perf_dict)
else:
perf_dict = {
"kendall_tau": [],
"reciprocal_rank": [],
"mean_reciprocal_rank": [],
"regret": [],
"test_compound": [],
"model": [],
"evaluation_loop":[]
}
self._CV_loops(
perf_dict,
self.outer_cv,
X,
y_label,
self._processing_before_logging,
y_yield=y_yield,
)
self.perf_dict = perf_dict
return self
class LabelRankingEvaluator(Evaluator):
"""Evaluates multiple label ranking algorithms.
Parameters
----------
dataset : Dataset object as in Dataloader.py