-
Notifications
You must be signed in to change notification settings - Fork 1
/
ga_lib_3.py
1310 lines (1124 loc) · 43.3 KB
/
ga_lib_3.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
''' Library of code used to develop, validate, and run the SIMPD algorithm
Author: Greg Landrum ([email protected])
'''
import sys
from rdkit import RDPaths
sys.path.append(RDPaths.RDContribDir)
from rdkit import Chem
from rdkit.Chem import rdMolDescriptors
from rdkit.Chem import Descriptors
from rdkit.Chem.MolStandardize import rdMolStandardize
from rdkit.Chem import Crippen
from SA_Score import sascorer
# The GA scenario targets
# scatter((74,80,72,78),(61,66,51,54),color='r')
target_FG_vals = [(61, 74), (66, 80), (51, 72), (54, 78)]
delta_test_active_frac_vals = [.11, .30]
from multiprocessing.pool import ThreadPool
def run_GA_old(df,
strategy="CLUSTERS_SPLIT",
pop_size=500,
ngens=100,
verbose=False,
numThreads=1,
seed_input=0xf00d,
return_random_result=True,
smilesCol='Smiles',
actCol='Property_1',
targetTrainFracActive=-1,
targetTestFracActive=-1,
targetDeltaTestFracActive=None,
targetFval=None,
targetGval=None,
skipDescriptors=False):
''' Runs the GA using descriptors + activity distribution + F + G
This is not the form used for SIMPD
'''
from pymoo.core.problem import starmap_parallelized_eval
from pymoo.core.problem import elementwise_eval, looped_eval
if numThreads > 1:
pool = ThreadPool(numThreads)
runner = pool.starmap
func_eval = starmap_parallelized_eval
else:
runner = None
func_eval = looped_eval
random.seed(seed_input)
np.random.seed(seed_input)
sel_strategy = getattr(SelectionStrategy, strategy)
df["mol"] = [
Chem.MolFromSmiles(tmp_smi)
for tmp_smi in df[smilesCol].to_numpy(dtype=str)
]
# generate the descriptors we're using for the molecules:
dvals = np.array([calc_descrs(m) for m in df.mol])
dtgts = get_descr_targets()
# generate the FPS we're using for the molecules:
fps = get_fps(df.mol)
# generate the distance matrix based on the fingerprints:
dmat = np.zeros((len(fps), len(fps)), float)
for i, fp in enumerate(fps):
if i == len(fps) - 1:
break
ds = np.array(
DataStructs.BulkTanimotoSimilarity(fp,
fps[i + 1:],
returnDistance=1))
dmat[i, i + 1:] = ds
dmat[i + 1:, i] = ds
# generate the binned activity values
#binned = [map_activity_to_idg_val((""), x*1000)[1] for x in df["Property_1"]];
binned = df[actCol].to_numpy()
binned[binned == "active"] = 1
binned[binned == "inactive"] = 0
binned = list(binned)
if verbose:
print('------------\nRunning GA')
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.optimize import minimize
max_pts = len(binned)
#keep = 1000
keep = max_pts
n_max = int(0.2 * keep)
if verbose:
print(f'working with {keep} points and picking {n_max}')
# do the clustering for the starting points
distThreshold = 0.65
clusterSizeThreshold = max(5, len(dmat) / 50)
clusters = clusterData(dmat,
distThreshold,
clusterSizeThreshold=clusterSizeThreshold)
if not skipDescriptors:
problem = SplitProblem_NoR_TargetDescriptorDeltas(
binned[:keep],
fps[:keep],
dmat[:keep, :keep],
dvals[:keep],
dtgts,
n_max,
runner=runner,
func_eval=func_eval,
clusters=clusters,
targetTrainFracActive=targetTrainFracActive,
targetTestFracActive=targetTestFracActive,
targetDeltaTestFracActive=targetDeltaTestFracActive,
targetFval=targetFval,
targetGval=targetGval)
else:
problem = SplitProblem_JustFracActive(
binned[:keep],
fps[:keep],
dmat[:keep, :keep],
dvals[:keep],
dtgts,
n_max,
runner=runner,
func_eval=func_eval,
clusters=clusters,
targetTrainFracActive=targetTrainFracActive,
targetTestFracActive=targetTestFracActive,
targetDeltaTestFracActive=targetDeltaTestFracActive,
targetFval=targetFval,
targetGval=targetGval)
algorithm = NSGA2(pop_size=pop_size,
sampling=ClusterSampling(selectionStrategy=sel_strategy,
clusters=clusters),
crossover=BinaryCrossover2(),
mutation=MyMutation2(),
eliminate_duplicates=True)
res = minimize(problem,
algorithm, ('n_gen', ngens),
seed=seed_input,
verbose=True)
if verbose:
print(f"{len(res.F)} solutions")
print("Function value: %s" % res.F[0])
#return res;
#now bring the solutions in a nice format
tests_inds = []
train_inds = []
for tmp_sol in range(len(res.F)):
tests_inds.append(np.arange(len(res.X[tmp_sol]))[(res.X[tmp_sol])])
train_inds.append(np.arange(len(res.X[tmp_sol]))[~(res.X[tmp_sol])])
if return_random_result:
sample_ind_returned = np.random.choice(np.arange(len(train_inds)))
return train_inds[sample_ind_returned], tests_inds[
sample_ind_returned], res
else:
return train_inds, tests_inds, res
def run_GA_SIMPD(df,
strategy="CLUSTERS_SPLIT",
pop_size=500,
ngens=100,
verbose=False,
numThreads=1,
seed_input=0xf00d,
return_random_result=True,
smilesCol='Smiles',
actCol='Property_1',
targetTrainFracActive=-1,
targetTestFracActive=-1,
targetDeltaTestFracActive=None):
''' Runs the GA using descriptors + activity distribution + (G-F) + G
This is the form used for SIMPD
'''
from pymoo.core.problem import starmap_parallelized_eval
from pymoo.core.problem import elementwise_eval, looped_eval
if numThreads > 1:
pool = ThreadPool(numThreads)
runner = pool.starmap
func_eval = starmap_parallelized_eval
else:
runner = None
func_eval = looped_eval
random.seed(seed_input)
np.random.seed(seed_input)
sel_strategy = getattr(SelectionStrategy, strategy)
df["mol"] = [
rdMolStandardize.ChargeParent(Chem.MolFromSmiles(tmp_smi))
for tmp_smi in df[smilesCol].to_numpy(dtype=str)
]
# generate the descriptors we're using for the molecules:
dvals = np.array([calc_descrs(m) for m in df.mol])
dtgts = get_descr_targets()
# generate the FPS we're using for the molecules:
fps = get_fps(df.mol)
# generate the distance matrix based on the fingerprints:
dmat = np.zeros((len(fps), len(fps)), float)
for i, fp in enumerate(fps):
if i == len(fps) - 1:
break
ds = np.array(
DataStructs.BulkTanimotoSimilarity(fp,
fps[i + 1:],
returnDistance=1))
dmat[i, i + 1:] = ds
dmat[i + 1:, i] = ds
# generate the binned activity values
#binned = [map_activity_to_idg_val((""), x*1000)[1] for x in df["Property_1"]];
binned = df[actCol].to_numpy()
binned[binned == "active"] = 1
binned[binned == "inactive"] = 0
binned = list(binned)
if verbose:
print('------------\nRunning GA')
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.optimize import minimize
max_pts = len(binned)
#keep = 1000
keep = max_pts
n_max = int(0.2 * keep)
if verbose:
print(f'working with {keep} points and picking {n_max}')
# do the clustering for the starting points
distThreshold = 0.65
clusterSizeThreshold = max(5, len(dmat) / 50)
clusters = clusterData(dmat,
distThreshold,
clusterSizeThreshold=clusterSizeThreshold)
problem = SplitProblem_DescriptorAndFGDeltas(
binned[:keep],
fps[:keep],
dmat[:keep, :keep],
dvals[:keep],
dtgts,
n_max,
runner=runner,
func_eval=func_eval,
clusters=clusters,
targetTrainFracActive=targetTrainFracActive,
targetTestFracActive=targetTestFracActive,
targetDeltaTestFracActive=targetDeltaTestFracActive)
algorithm = NSGA2(pop_size=pop_size,
sampling=ClusterSampling(selectionStrategy=sel_strategy,
clusters=clusters),
crossover=BinaryCrossover2(),
mutation=MyMutation2(),
eliminate_duplicates=True)
res = minimize(problem,
algorithm, ('n_gen', ngens),
seed=seed_input,
verbose=True)
if verbose:
print(f"{len(res.F)} solutions")
print("Function value: %s" % res.F[0])
#return res;
#now bring the solutions in a nice format
tests_inds = []
train_inds = []
for tmp_sol in range(len(res.F)):
tests_inds.append(np.arange(len(res.X[tmp_sol]))[(res.X[tmp_sol])])
train_inds.append(np.arange(len(res.X[tmp_sol]))[~(res.X[tmp_sol])])
if return_random_result:
sample_ind_returned = np.random.choice(np.arange(len(train_inds)))
return train_inds[sample_ind_returned], tests_inds[
sample_ind_returned], res
else:
return train_inds, tests_inds, res
#*************************************************************************************
#****************************** calculation of descriptors **************************
#*************************************************************************************
from collections import namedtuple
DescrTuple = namedtuple(
'DescrTuple',
('descriptor', 'function', 'direction', 'target_fraction', 'target_value'))
descrs = [
DescrTuple('SA_Score', sascorer.calculateScore, 1, 0.10, 0.10 * 2.8),
DescrTuple('HeavyAtomCount', lambda x: x.GetNumHeavyAtoms(), 1, 0.1,
0.1 * 31),
DescrTuple('TPSA', Descriptors.TPSA, 1, 0.15, 0.15 * 88.),
DescrTuple(
'fr_benzene/1000 HeavyAtoms',
lambda x: 1000 * Descriptors.fr_benzene(x) / x.GetNumHeavyAtoms(), -1,
-0.2, -0.2 * 44),
]
def calc_descrs(mol):
res = []
for itm in descrs:
res.append(itm.function(mol))
return res
def get_descr_targets():
res = []
for itm in descrs:
res.append(itm.target_value)
return res
#************************************************************************************
#******************************* GA utilities ***************************************
#************************************************************************************
import numpy as np
import random
from rdkit.Chem import rdFingerprintGenerator
from rdkit import DataStructs
import bisect
def get_fps(ms, generator=None):
" generate fingerprints for a set of molecules "
if generator is None:
#generator = rdFingerprintGenerator.GetRDKitFPGenerator(maxPath=6)
generator = rdFingerprintGenerator.GetMorganGenerator(radius=3)
fps = [generator.GetFingerprint(x) for x in ms]
return fps
def get_distance_cdf(fps1,
fps2,
removeSelf=False,
vals=np.arange(0, 1.01, 0.01)):
' finds the CDF for the closest member of fps2 to each element of fps1 '
nbrds = []
for i, fp in enumerate(fps1):
ds = DataStructs.BulkTanimotoSimilarity(fp, fps2, returnDistance=1)
if removeSelf:
ds.pop(i)
nbrds.append(min(ds))
nPts = len(nbrds)
nbrds = np.array(nbrds)
res = []
for v in vals:
res.append(np.sum(nbrds <= v) / nPts)
return res
def get_distance_cdf_dmat(dmat,
idx1,
idx2,
removeSelf=False,
vals=np.arange(0, 1.01, 0.01)):
''' CDF of number of points in fps2 which are less than distThresh
from each point in fps1
'''
dmat = dmat[idx1][:, idx2]
nPts = len(idx1)
if removeSelf:
for i in range(nPts):
dmat[i, i] = 10
dmat = np.min(dmat, axis=1)
res = []
for v in vals:
res.append(np.sum(dmat <= v) / nPts)
return res
def get_dissim_cdf(fps1,
fps2,
frac=0.2,
removeSelf=False,
vals=np.arange(0, 1.01, 0.01)):
' finds the CDF for the X percentile most-distant member of fps2 to each element of fps1 '
nbrds = []
refPos = int(frac * len(fps2))
for i, fp in enumerate(fps1):
ds = DataStructs.BulkTanimotoSimilarity(fp, fps2, returnDistance=1)
if removeSelf:
ds.pop(i)
ds = sorted(ds)
nbrds.append(ds[refPos])
nPts = len(nbrds)
nbrds = np.array(nbrds)
res = []
for v in vals:
res.append(np.sum(nbrds <= v) / nPts)
return res
def get_randomdist_cdf(fps1,
fps2,
distThresh=0.8,
removeSelf=False,
vals=np.arange(0, 1.01, 0.01)):
''' CDF of number of points in fps2 which are greater than distThresh
from each point in fps1
'''
nbrds = []
nfps2 = len(fps2)
for i, fp in enumerate(fps1):
ds = DataStructs.BulkTanimotoSimilarity(fp, fps2, returnDistance=1)
if removeSelf:
ds.pop(i)
ds = sorted(ds)
pos = bisect.bisect_left(ds, distThresh)
nbrds.append((nfps2 - pos) / nfps2)
nPts = len(nbrds)
nbrds = np.array(nbrds)
res = []
for v in vals:
res.append(np.sum(nbrds <= v) / nPts)
return res
def get_randomdist_cdf_dmat(dmat,
idx1,
idx2,
distThresh=0.8,
removeSelf=False,
vals=np.arange(0, 1.01, 0.01)):
''' CDF of number of points in fps2 which are greater than distThresh
from each point in fps1
'''
dmat = dmat[idx1][:, idx2]
nbrds = []
nfps2 = dmat.shape[1]
for i in range(dmat.shape[0]):
pos = sum(dmat[i] < distThresh)
if removeSelf:
# the zero distance is always at the beginning
pos += 1
nbrds.append((nfps2 - pos) / nfps2)
nPts = dmat.shape[0]
if removeSelf:
nPts -= 1
nbrds = np.array(nbrds)
res = []
for v in vals:
res.append(np.sum(nbrds <= v) / nPts)
return res
def get_relateddist_cdf(fps1,
fps2,
distThresh=0.65,
removeSelf=False,
vals=np.arange(0, 1.01, 0.01)):
''' CDF of number of points in fps2 which are less than distThresh
from each point in fps1
'''
nbrds = []
nfps2 = len(fps2)
for i, fp in enumerate(fps1):
ds = DataStructs.BulkTanimotoSimilarity(fp, fps2, returnDistance=1)
if removeSelf:
ds.pop(i)
ds = sorted(ds)
nbrds.append(bisect.bisect_left(ds, distThresh) / nfps2)
nPts = len(nbrds)
nbrds = np.array(nbrds)
res = []
for v in vals:
res.append(np.sum(nbrds <= v) / nPts)
return res
def get_relateddist_cdf_dmat(dmat,
idx1,
idx2,
distThresh=0.65,
removeSelf=False,
vals=np.arange(0, 1.01, 0.01)):
''' CDF of number of points in fps2 which are less than distThresh
from each point in fps1
'''
dmat = dmat[idx1][:, idx2]
nbrds = []
nfps2 = dmat.shape[1]
for i in range(dmat.shape[0]):
pos = sum(dmat[i] < distThresh)
if removeSelf:
# the zero distance is always at the beginning
pos -= 1
nbrds.append(pos / nfps2)
nPts = dmat.shape[0]
nbrds = np.array(nbrds)
res = []
for v in vals:
res.append(np.sum(nbrds <= v) / nPts)
return res
def calc_spatial_stats(testfps,
trainfps,
vals=np.arange(0, 1.01, 0.01),
includeTestInBootstrap=True):
g_vals = get_distance_cdf(testfps, testfps, removeSelf=True, vals=vals)
tfps = trainfps
if includeTestInBootstrap:
tfps = tfps + testfps
bootstrap = [
tfps[x]
for x in [random.randint(0,
len(tfps) - 1) for x in range(len(tfps))]
]
f_vals = get_distance_cdf(bootstrap, testfps, vals=vals)
s_vals = [f - g for f, g in zip(f_vals, g_vals)]
return vals, g_vals, f_vals, s_vals
def modified_spatial_stats(testfps,
trainfps,
vals=np.arange(0, 1.01, 0.01),
includeTestInBootstrap=True,
justTheBasics=False):
' calculates F using closest member of train to test instead of vice-versa '
g_vals = get_distance_cdf(testfps, testfps, removeSelf=True, vals=vals)
tfps = trainfps
if includeTestInBootstrap:
tfps = tfps + testfps
bootstrap = [
tfps[x]
for x in [random.randint(0,
len(tfps) - 1) for x in range(len(tfps))]
]
f_vals = get_distance_cdf(testfps, bootstrap, vals=vals)
s_vals = [f - g for f, g in zip(f_vals, g_vals)]
if not justTheBasics:
h_vals = get_dissim_cdf(testfps, bootstrap, vals=vals)
r1_vals = get_randomdist_cdf(testfps, bootstrap, vals=vals)
r2_vals = get_relateddist_cdf(testfps,
testfps,
vals=vals,
removeSelf=True)
r3_vals = get_relateddist_cdf(testfps, bootstrap, vals=vals)
return vals, g_vals, f_vals, s_vals, h_vals, r1_vals, r2_vals, r3_vals
else:
return vals, g_vals, f_vals, s_vals
def modified_spatial_stats_dmat(dmat,
testIdx,
trainIdx,
vals=np.arange(0, 1.01, 0.01),
includeTestInBootstrap=True):
' calculates F using closest member of train to test instead of vice-versa '
g_vals = get_distance_cdf_dmat(dmat,
testIdx,
testIdx,
removeSelf=True,
vals=vals)
tidx = list(trainIdx)
if includeTestInBootstrap:
tidx += list(testIdx)
bootstrap = [
tidx[x]
for x in [random.randint(0,
len(tidx) - 1) for x in range(len(tidx))]
]
bootstrap = np.array(bootstrap)
f_vals = get_distance_cdf_dmat(dmat,
testIdx,
bootstrap,
removeSelf=False,
vals=vals)
s_vals = [f - g for f, g in zip(f_vals, g_vals)]
return vals, g_vals, f_vals, s_vals
import math
def map_activity_to_idg_val(target_desc, standard_activity):
''' target-class based assignment of active/inactive labels from https://link.springer.com/article/10.1186/s13321-018-0325-4'''
if 'protein kinase' in target_desc:
thresh = 7.5
idg_class = "Protein Kinase"
elif 'enzyme protease' in target_desc:
thresh = 7.0
idg_class = "Protease" # Note: this is an addition to the original scheme
elif 'membrane receptor 7tm1' in target_desc:
thresh = 7.0
idg_class = "GPCR"
elif 'nuclear receptor' in target_desc:
thresh = 7.0
idg_class = "Nuclear Receptor"
elif 'ion channel' in target_desc:
thresh = 6.5
idg_class = "Ion Channel"
else:
thresh = 6.0
idg_class = "Other"
if standard_activity <= 0:
return idg_class, 0
pact = -1 * math.log10(standard_activity * 1e-9)
if pact >= thresh:
return idg_class, 1
else:
return idg_class, 0
def get_imbalanced_bins_orig(data,
tgt_frac=0.2,
step_size=0.5,
active_inactive_offset=0.5):
vs = list(sorted(data, reverse=True))
tgt = int(tgt_frac * len(vs))
act = vs[tgt]
# "round"
binAct = int(act / step_size) * step_size
while sum([1 for x in vs if x >= binAct]) < tgt:
binAct -= step_size
lowerAct = binAct - active_inactive_offset
return binAct, lowerAct
def get_imbalanced_bins(data,
tgt_frac=0.2,
step_size=0.1,
active_inactive_offset=0.5,
tol=0.01):
vs = list(sorted(data, reverse=True))
tgt = int(tgt_frac * len(vs))
act = vs[tgt]
# "round"
binAct = int(act / step_size) * step_size
remain = [
x for x in vs if x >= binAct or x <= (binAct - active_inactive_offset)
]
fRemain = sum([1 for x in remain if x >= binAct]) / len(remain)
d = abs(fRemain - tgt_frac)
lastD = 1e8
while d < lastD and d > tol:
if fRemain < tgt_frac:
binAct -= step_size
else:
binAct += step_size
lastD = d
remain = [
x for x in vs
if x >= binAct or x <= (binAct - active_inactive_offset)
]
lowerAct = binAct - active_inactive_offset
return binAct, lowerAct
def score_pareto_solutions(Fs, weights):
Fs = np.copy(Fs)
qs = np.quantile(Fs, 0.9, axis=0)
maxv = np.max(np.abs(Fs), axis=0)
for i, q in enumerate(qs):
if q == 0:
qs[i] = maxv[i]
if qs[i] == 0:
qs[i] = 1
Fs /= qs
Fs = np.exp(Fs * -1)
weights = np.array(weights, float)
# normalize:
weights /= np.sum(weights)
Fs *= weights
return np.sum(Fs, axis=1)
# ---------------------------------
# Asymmetric Validation Embedding (AVE) implementation
# code adapted from the supplementary material from https://doi.org/10.1021/acs.jcim.7b00403
# ---------------------------------
def calc_AVE(actfps,
inactfps,
at_indices,
av_indices,
it_indices,
iv_indices,
calcVE=False,
offsetDiagonal=False):
# approach from https://doi.org/10.1007/978-3-030-50420-5_44
activesTrainFPs = [actfps[x] for x in at_indices]
activesTestFPs = [actfps[x] for x in av_indices]
inactivesTrainFPs = [inactfps[x] for x in it_indices]
inactivesTestFPs = [inactfps[x] for x in iv_indices]
av_at_D = calcDistMat(activesTestFPs, activesTrainFPs)
iv_at_D = calcDistMat(inactivesTestFPs, activesTrainFPs)
av_it_D = calcDistMat(activesTestFPs, inactivesTrainFPs)
iv_it_D = calcDistMat(inactivesTestFPs, inactivesTrainFPs)
if offsetDiagonal:
av_at_D += np.identity(len(at_indices))
iv_it_D += np.identity(len(it_indices))
av_it_d = np.min(av_it_D, axis=1)
av_at_d = np.min(av_at_D, axis=1)
iv_at_d = np.min(iv_at_D, axis=1)
iv_it_d = np.min(iv_it_D, axis=1)
av_term = np.mean(av_it_d - av_at_d)
iv_term = np.mean(iv_at_d - iv_it_d)
if not calcVE:
return av_term + iv_term
else:
return np.sqrt(av_term * av_term + iv_term * iv_term)
def calc_AVE_from_dists(actives_D,
inactives_D,
actives_inactives_D,
at_indices,
av_indices,
it_indices,
iv_indices,
calcVE=False,
offsetDiagonal=False):
# approach from https://doi.org/10.1007/978-3-030-50420-5_44
av_it_D = actives_inactives_D[av_indices, :][:, it_indices]
av_at_D = actives_D[av_indices, :][:, at_indices]
iv_at_D = actives_inactives_D.transpose()[iv_indices, :][:, at_indices]
iv_it_D = inactives_D[iv_indices, :][:, it_indices]
if offsetDiagonal:
av_at_D += np.identity(len(at_indices))
iv_it_D += np.identity(len(it_indices))
av_term = np.mean(np.min(av_it_D, axis=1) - np.min(av_at_D, axis=1))
iv_term = np.mean(np.min(iv_at_D, axis=1) - np.min(iv_it_D, axis=1))
if not calcVE:
return av_term + iv_term
else:
return np.sqrt(av_term * av_term + iv_term * iv_term)
from rdkit.ML.InfoTheory import rdInfoTheory
def population_cluster_entropy(X, clusters):
if len(clusters) <= 1:
return 0
ccounts = np.zeros(len(clusters), int)
for i, clust in enumerate(clusters):
for entry in clust:
if X[entry]:
ccounts[i] += 1
return rdInfoTheory.InfoEntropy(ccounts) / rdInfoTheory.InfoEntropy(
np.ones(len(clusters), int))
def population_tanimoto(pop1, pop2):
denom = sum(pop1 | pop2)
if not denom:
return 0.0
return sum(pop1 & pop2) / denom
#************************************************************************************
#******************************* GA utilities ***************************************
#************************************************************************************
try:
from pymoo.core.problem import ElementwiseProblem, elementwise_eval, looped_eval
except ImportError:
ElementWiseProblem = None
class SplitProblem_NoR_TargetDescriptorDeltas(ElementwiseProblem):
''' pymoo problem form not used in SIMPD '''
def __init__(self,
binned_acts,
fps,
dmat,
dvals,
descriptor_delta_targets,
n_max,
clusters=None,
targetTrainFracActive=-1,
targetTestFracActive=-1,
targetDeltaTestFracActive=None,
targetFval=None,
targetGval=None,
**kwargs):
assert len(binned_acts) == len(fps)
assert len(fps) == len(dvals)
self.acts = np.array(binned_acts)
self.dvals = np.array(dvals)
self.dtargets = np.array(descriptor_delta_targets)
assert self.dvals.shape[1] == self.dtargets.shape[0]
self.fps = np.zeros((len(fps), ), object)
for i, fp in enumerate(fps):
self.fps[i] = fp
self.dmat = dmat
self._nObjs = len(dvals[0])
if targetTestFracActive > 0 or targetTrainFracActive > 0:
self.tgtTestFrac = targetTestFracActive
self.tgtTrainFrac = targetTrainFracActive
self._nObjs += int(targetTestFracActive > 0) + int(
targetTrainFracActive > 0)
self.tgtFrac = None
self.deltaTestFracActive = None
elif targetDeltaTestFracActive is not None:
self.deltaTestFracActive = targetDeltaTestFracActive
self.tgtFrac = None
else:
self._nObjs += 1
self.tgtFrac = binned_acts.count(1) / len(binned_acts)
self.deltaTestFracActive = None
self.tgtFval = targetFval
if targetFval is not None:
self._nObjs += 1
self.tgtGval = targetGval
if targetGval is not None:
self._nObjs += 1
super().__init__(n_var=len(dvals),
n_obj=self._nObjs,
n_constr=1,
**kwargs)
self.n_max = n_max
self.clusters = clusters
def _evaluate(self, x, out, *args, **kwargs):
train = np.median(self.dvals[~x], axis=0)
test = np.median(self.dvals[x], axis=0)
descr_deltas = test - train
descr_objects = abs(descr_deltas - self.dtargets)
objectives = list(descr_objects)
train_acts = self.acts[~x]
train_frac = np.sum(train_acts, axis=0) / len(train_acts)
test_acts = self.acts[x]
test_frac = np.sum(test_acts, axis=0) / len(test_acts)
if self.tgtFrac is not None:
objectives.append(abs(test_frac - self.tgtFrac))
elif self.deltaTestFracActive is not None:
dTestFracActive = test_frac - np.sum(self.acts, axis=0) / len(
self.acts)
# print(f' {test_frac:.2f} {np.sum(self.acts, axis=0)/len(self.acts):.2f} {dTestFracActive:.2f}')
objectives.append(abs(self.deltaTestFracActive - dTestFracActive))
else:
if self.tgtTrainFrac > 0:
objectives.append(abs(train_frac - self.tgtTrainFrac))
if self.tgtTestFrac > 0:
objectives.append(abs(test_frac - self.tgtTestFrac))
if self.tgtFval is not None or self.tgtGval is not None:
allIdx = np.arange(0, len(x), dtype=int)
testIdx = allIdx[x]
trainIdx = allIdx[~x]
vals, g_vals, f_vals, s_vals = modified_spatial_stats_dmat(
self.dmat, testIdx, trainIdx, includeTestInBootstrap=False)
if self.tgtFval is not None:
sum_F = np.sum(f_vals)
objectives.append(abs(sum_F - self.tgtFval))
if self.tgtGval is not None:
sum_G = np.sum(g_vals)
objectives.append(abs(sum_G - self.tgtGval))
# objectives:
out["F"] = objectives
# constraints:
out["G"] = [(self.n_max - np.sum(x))**2]
if self.clusters:
# keep the entropy below 0.9
out["G"].append(population_cluster_entropy(x, self.clusters) - 0.9)
class SplitProblem_JustFracActive(ElementwiseProblem):
''' pymoo problem form not used in SIMPD '''
def __init__(self,
binned_acts,
fps,
dmat,
dvals,
descriptor_delta_targets,
n_max,
clusters=None,
targetTrainFracActive=-1,
targetTestFracActive=-1,
targetDeltaTestFracActive=None,
targetFval=None,
targetGval=None,
**kwargs):
assert len(binned_acts) == len(fps)
assert len(fps) == len(dvals)
self.acts = np.array(binned_acts)
self.dmat = dmat
self._nObjs = 0
if targetTestFracActive > 0 or targetTrainFracActive > 0:
self.tgtTestFrac = targetTestFracActive
self.tgtTrainFrac = targetTrainFracActive
self._nObjs += int(targetTestFracActive > 0) + int(
targetTrainFracActive > 0)
self.tgtFrac = None
self.deltaTestFracActive = None
elif targetDeltaTestFracActive is not None:
self.deltaTestFracActive = targetDeltaTestFracActive
self.tgtFrac = None
else:
self._nObjs += 1
self.tgtFrac = binned_acts.count(1) / len(binned_acts)
self.deltaTestFracActive = None
super().__init__(n_var=len(dvals),
n_obj=self._nObjs,
n_constr=1,
**kwargs)
self.n_max = n_max
self.clusters = clusters
def _evaluate(self, x, out, *args, **kwargs):
objectives = []
train_acts = self.acts[~x]
train_frac = np.sum(train_acts, axis=0) / len(train_acts)
test_acts = self.acts[x]
test_frac = np.sum(test_acts, axis=0) / len(test_acts)
if self.tgtFrac is not None:
objectives.append(abs(test_frac - self.tgtFrac))
elif self.deltaTestFracActive is not None:
dTestFracActive = test_frac - np.sum(self.acts, axis=0) / len(
self.acts)
# print(f' {test_frac:.2f} {np.sum(self.acts, axis=0)/len(self.acts):.2f} {dTestFracActive:.2f}')
objectives.append(abs(self.deltaTestFracActive - dTestFracActive))
else:
if self.tgtTrainFrac > 0:
objectives.append(abs(train_frac - self.tgtTrainFrac))
if self.tgtTestFrac > 0:
objectives.append(abs(test_frac - self.tgtTestFrac))
# objectives:
out["F"] = objectives
# constraints:
out["G"] = [(self.n_max - np.sum(x))**2]
if self.clusters:
# keep the entropy below 0.9
out["G"].append(population_cluster_entropy(x, self.clusters) - 0.9)
class SplitProblem_DescriptorAndFGDeltas(ElementwiseProblem):
''' This is the pymoo Problem form used for SIMPD '''
def __init__(self,
binned_acts,
fps,
dmat,
dvals,
descriptor_delta_targets,
n_max,
clusters=None,
targetTrainFracActive=-1,
targetTestFracActive=-1,
targetDeltaTestFracActive=None,
targetGFDeltaWindow=(10, 30),
targetGval=70,
**kwargs):
assert len(binned_acts) == len(fps)
assert len(fps) == len(dvals)
self.acts = np.array(binned_acts)
self.dvals = np.array(dvals)