forked from beef-broccoli/deebo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchem_arms.py
1665 lines (1425 loc) · 71.9 KB
/
chem_arms.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
import itertools
import pathlib
import pickle
import copy
import json
import pandas as pd
import numpy as np
import os
from tqdm.autonotebook import tqdm
# from tqdm import tqdm # if tqdm.autonotebook breaks
from sklearn.preprocessing import OneHotEncoder as OHE
from sklearn.ensemble import RandomForestRegressor as RFR
from sklearn.linear_model import LinearRegression as LR
from sklearn.model_selection import train_test_split
from sklearn.metrics import (r2_score,
mean_squared_error,
mean_absolute_error)
import algos_regret
import utils
# dev notes
# - propose multiple experiments for different arms; WAIT FOR BATCHED ALGORITHM
# - add round number; WAIT FOR BATCHED ALGORITHM
# - how to integrate stop conditions, and use arm selection algorithm
# - propose experiment in a non-random way
# - (maybe) prediction models with features
# - (maybe) better handle situation when no experiments are available. Set algo count very high to eliminate uncertainty?
class Scope:
def __init__(self):
self.data_dic = None # store a copy of the dictionary that's used to build scope
self.data = None # dataframe that holds all experiment and result
self.pre_accuracy = None # prediction accuracy
self.arms = None # a list of arms, e.g., [('a2', 'c1'), ('a2', 'c3'), ('a3', 'c1'), ('a3', 'c3')]
self.arm_labels = None # arm label names. e.g., ['component_a', 'component_b']
self.current_experiment_index = None # df index of the experiments currently running
self.current_arms = None # current arms being evaluated; corresponding to self.current_eperiment_index
return
def __str__(self):
return str(self.data_dic)
def reset(self, scope_dict):
"""
For simulations, reset the scope object. Keep all arm parameters, but remove all data.
This function is updated to use an externally supplied scope.
In case the scope is expanded, the parameters in the expansion need to be cleared also
Parameters
----------
scope_dict: dictionary used to build the scope
Returns
-------
"""
self.data_dic = None
self.data = None
self.pre_accuracy = None
self.current_experiment_index = None
self.current_arms = None
self.build_scope(scope_dict)
return
def build_scope(self, d):
"""
build a dataframe with reaction components populated, except yield
Parameters
----------
d: dict
dictionary of reaction components
e.g., d = {
'component_a': ['a1', 'a2', 'a3'],
'component_b': ['b1', 'b2'],
'component_c': ['c1', 'c2', 'c3', 'c4']
}
Returns
-------
None
"""
if self.data is not None:
exit('scope already exists, cannot build')
component_names = sorted(d)
combinations = itertools.product(*(d[c] for c in component_names))
self.data = pd.DataFrame(combinations, columns=component_names)
# check column dtypes for int or float. These cause issues for value matching when querying/updating
# add column name as prefix to force string
for col in self.data.columns:
if self.data[col].dtype == 'int64' or self.data[col].dtype == 'float64':
print(f'Warning: attempting to pass int/float as values for {col}.\n'
f'Renaming values with prefix, e.g., {self.data[col][0]} -> {col} {self.data[col][0]}\n'
f'Modifications to values needed when building arms')
new_val = [col+' '+str(v) for v in list(self.data[col])]
self.data[col] = new_val
d[col] = list(set(new_val))
self.data_dic = copy.deepcopy(d) # important that this is copy, otherwise will create reference to dict
self.data['yield'] = np.nan
self.data['prediction'] = np.ones(len(self.data))
return
# def build_incomplete_scope(self, d, pre_built_scope):
# """
# Same as build_scope, except in this case a dataframe with values are directly used to build a scope,
# rather than doing the full combinations of all components.
# This is helpful when simulating an incomplete dataset.
#
# d: dict
# dictionary of reaction components
# e.g., d = {
# 'component_a': ['a1', 'a2', 'a3'],
# 'component_b': ['b1', 'b2'],
# 'component_c': ['c1', 'c2', 'c3', 'c4']
# }
# pre_built_scope: pd.DataFrame
# a dataframe with combinations of reaction components pre-specified.
# This might be a partial scope with components in d
#
# """
# if self.data is not None:
# exit('scope already exists, cannot build')
#
# # sanity check for component labels, make sure everything is defined in scope dictionary
# component_names = sorted(d)
# for col in pre_built_scope.columns:
# if not col in component_names:
# exit(f'Column {col} in pre_built_scope is not in scope dictionary supplied')
#
# # sanity check for component values, make sure everything is defined in scope dictionary
# for c in component_names:
# if not np.isin(pre_built_scope[c].unique(), d[c]).all():
# exit(f'Error for {c}. Check pre_built_scope, '
# f'some components were not included in the scope dictionary supplied')
#
# pre_built_scope = pre_built_scope[component_names]
# self.data = copy.deepcopy(pre_built_scope)
# # check column dtypes for int or float. These cause issues for value matching when querying/updating
# # add column name as prefix to force string
# for col in self.data.columns:
# if self.data[col].dtype == 'int64' or self.data[col].dtype == 'float64':
# print(f'Warning: attempting to pass int/float as values for {col}.\n'
# f'Renaming values with prefix, e.g., {self.data[col][0]} -> {col} {self.data[col][0]}\n'
# f'Modifications to values needed when building arms')
# new_val = [col+' '+str(v) for v in list(self.data[col])]
# self.data[col] = new_val
# d[col] = list(set(new_val))
# self.data_dic = copy.deepcopy(d) # important that this is copy, otherwise will create reference to dict
# self.data['yield'] = np.nan
# self.data['prediction'] = np.ones(len(self.data))
#
# return
def expand_scope(self, expand_d):
"""
Parameters
----------
expand_d: dict
expansion dictionary
e.g.,
{component_a: [a_6, a_7, a_8],
component_b: [b_9]}
Returns
-------
"""
expand_components = sorted(expand_d) # sorted(dict) returns a list
# only support expanding existing dimensions
for e in expand_components:
if e not in self.data.columns:
exit(f'Cannot expand scope; requested dimension {e} does not exist')
if e in self.arm_labels:
exit(f'Cannot expand scope; requested dimension {e} will modify arms')
data_dic = self.data_dic.copy()
data_dic[e] = expand_d[e] # for the dimension getting expanded, change value to the ones in expansion
expand_combinations = itertools.product(*(data_dic[d] for d in data_dic.keys()))
expand_df = pd.DataFrame(expand_combinations, columns=data_dic.keys())
expand_df['yield'] = np.nan
expand_df['prediction'] = np.ones(len(expand_df))
self.data = pd.concat([self.data, expand_df]) # add the expansion df to self.data
self.data_dic[e] = list(self.data_dic[e]) + list(expand_d[e]) # update self.data_dic
return
def query(self, d):
"""
Queries the reaction scope for yield with a dictionary
Parameters
----------
d: dict
dictionary of reaction components and values to be queried for yield
e.g., d = {
'component_a': 'a2',
'component_b': 'b1',
'component_c': 'c3',
}
Returns
-------
{int, float, np.nan}
yield from query, or np.nan if no result is found
"""
assert self.data.shape[1] - 1 == len(d.items()), f'Missing reaction components when querying {d}'
names = set(list(self.data.columns))
names.remove('yield') # remove yield from column labels, then match
assert names == set(d.keys()), f'labels not fully matched for {d}, cannot query'
component_names = sorted(d) # sort query dictionary by component name
scope = self.data.sort_index(axis=1) # sort scope data by component name (column)
values = [d[c] for c in component_names] # get values for each component
# can directly match with a list of value, since both query dict and dataframe are sorted
y = scope[np.equal.outer(scope.to_numpy(copy=False), values).any(axis=1).all(axis=1)]['yield']
# WARNING: np.equal seems to cause very weird inequality problem if any dataframe element is int
if y.empty:
print(f'Query {d} does not exist in this scope')
return np.nan # no such result exists in the scope
elif np.isnan(list(y)[0]):
print(f'No result yet for {d}')
return np.nan
else:
assert len(list(y)) == 1, f'multiple result exist for query {d}'
return list(y)[0]
def update_with_dict(self, d):
"""
Update scope with reaction yield
Parameters
----------
d: dict
dictionary of reaction component (similar to d in query(), with yield).
e.g.,d = {
'component_a': 'a2',
'component_b': 'b1',
'component_c': 'c3',
'yield': 69,
}
Returns
-------
None
"""
assert self.data.shape[1]-1 == len(d.items()), 'missing reaction components or yield'
assert 'yield' in list(d.keys()), 'missing yield, cannot update'
assert type(d['yield']) in [int, float], 'yield value is not numerical'
cols = set(list(self.data.columns))
cols.remove('prediction')
assert cols == set(d.keys()), 'labels not fully matched, cannot update'
y = d.pop('yield')
component_names = sorted(d) # sort query dictionary by component name
#self.data = self.data.sort_index(axis=1) # sort scope data by component name (column)
values = [d[c] for c in component_names] # get values for each component
# match and update yield
boo = np.equal.outer(self.data.to_numpy(copy=False), values).any(axis=1).all(axis=1)
if not any(boo):
print(f'No update; requested components do not exist for {d}')
else:
self.data.loc[boo, 'yield'] = y
return
def update_with_index(self, index, y):
self.data.loc[index, 'yield'] = y
return
def predict(self, model='rf', encoding_dict=None):
"""
Build regression model to predict for all experiments in scope
Parameters
----------
model: str
what model to use. {'rf',}
encoding_dict: dict
nested dictionary for encodings.
First level of keys match column names in scope.
Second level of keys match individual values for each component.
If there are any column names missing, ohe will be used to encode those.
{'component_a': {
'a1': [1,2,3],
'a2': [4,5,6]},
'component_b': {
'b1': [7,8,9]
}}
Returns
-------
"""
ys = self.data['yield']
train_mask = ys.notna() # training data mask: index of experiments that have been run
ys_train = ys[train_mask].to_numpy()
df = self.data.drop(['yield', 'prediction'], axis=1)
if not encoding_dict: # user did not specify encoding, use OHE
Xs = OHE().fit_transform(df).toarray()
else:
Xs_list = []
for c in encoding_dict.keys():
if c not in self.data.columns:
exit('Error for encoding dictionary: dict keys must match column names in scope.')
try:
Xs_list.append(np.stack(df[c].apply(lambda x: encoding_dict[c][x]).values))
except KeyError:
exit('Error for encoding dictionary: some reaction components are missing encodings')
df = df.drop([c], axis=1)
Xs_list.append(OHE().fit_transform(df).toarray()) # the remaining columns are encoded by OHE
Xs = np.hstack(Xs_list)
# use data collected as train test, reactions without yield as test
Xs_train = Xs[train_mask, :]
Xs_test = Xs[~train_mask, :]
# np.save('xs_train_ohe.npy', Xs_train)
# np.save('ys_train_ohe.npy', ys_train)
# np.save('xs_ohe.npy', Xs)
# exit()
if model == 'rf':
model = RFR()
else:
exit()
model.fit(Xs_train, ys_train)
if Xs_train.shape[0] > 2: # to suppress sklearn warning when calculating R2 score with <2 samples
self.pre_accuracy = model.score(Xs_train, ys_train)
self.data['prediction'] = model.predict(Xs)
return
def recommend(self):
df = self.data.copy()
# supplement the experiments without yield with predicted yield
yields = np.array(df['yield'])
pres = np.array(df['prediction'])
mask = np.isnan(yields)
yields[mask] = 0
pres[~mask] = 0
df['yield'] = yields + pres
df['arm'] = df[self.arm_labels].apply(tuple, axis=1)
df = df.drop(self.arm_labels, axis=1)
columns_to_groupby = [c for c in df.columns if c not in ['yield', 'arm']]
recommendations = df[df.groupby(columns_to_groupby)['yield'].transform(max) == df['yield']]
return recommendations
def build_arms(self, d):
"""
Function to build arms with a dictionary
e.g., d = {'component_c': ['c1', 'c3'], 'component_a': ['a2', 'a3']}
arms =[('a2', 'c1'), ('a2', 'c3'), ('a3', 'c1'), ('a3', 'c3')]
Parameters
----------
d: dict
dictionary with component labels as keys, and components as values
Returns
-------
None
"""
if self.arms:
exit('Arms already exist, call clear_arms() first before building')
assert set(d.keys()).issubset(set(list(self.data.columns))), 'some requested component do not exist in this scope'
for component in d.keys():
if not set(d[component]).issubset(set(self.data_dic[component])):
exit(f'some values of requested component {component} not present in current scope.')
self.arm_labels = list(sorted(d.keys()))
self.arms = list(itertools.product(*(d[c] for c in self.arm_labels)))
return
def build_arm_dict(self, arm_index):
"""
Build a dictionary {(component_a, component_b): (a1, b2)}. For query purposes
Parameters
----------
arm_index: int
arm index
Returns
-------
dict
"""
arm = list(self.arms[int(arm_index)])
return dict(zip(self.arm_labels, arm))
def sort_results_with_arms(self):
"""
sort current results into lists based on current arms.
with all existing data, it queries with current arm values
all experimental results that match with current arm will be included into a list
if no result available for a certain arm, it's an empty list
Returns
-------
results: a nested list with all results sorted based on current arm values
"""
if self.arms is None:
exit('no arms in this scope. Create arm with scope.build_arms() first.')
existing_data = self.data[self.data['yield'].notna()]
results = []
for arm in self.arms:
query_dic = dict(zip(self.arm_labels, arm))
query = existing_data.loc[(existing_data[list(query_dic)] == pd.Series(query_dic)).all(axis=1)]
results.append(list(query['yield']))
assert len(results) == len(self.arms), 'something went wrong when sorting results based on arms'
return results
def clear_arms(self):
self.arms = None
self.arm_labels = None
def propose_experiment(self, arm_index, mode='random', num_exp=1):
"""
Propose an experiment for a specified arm.
Will return None when all experiments for a given arm are sampled
Parameters
----------
arm_index: int
index of the selected arm
mode: str
sampling method to select one experiment
choose from {'random', 'highest', 'random_highest'}
num_exp: int
the number of experiments to be proposed for this arm
Returns
-------
"""
candidates = self.data.loc[self.data['yield'].isnull()] # experiments without a yield
for ii in range(len(self.arm_labels)): # find available experiments for this arm
candidates = candidates.loc[candidates[self.arm_labels[ii]] == self.arms[arm_index][ii]]
if mode == 'random': # random sampling
try:
sample = candidates.sample(num_exp)
except ValueError: # not enough available reactions for this arm to be sampled
sample = None
n = num_exp
while n > 1: # keep reducing the number of sample until its sample-able
n = n-1
try:
sample = candidates.sample(n)
break
except ValueError:
continue
if sample is not None:
self.current_experiment_index = sample.index
self.current_arms = [arm_index]*len(self.current_experiment_index)
else:
self.current_experiment_index = None
self.current_arms = None
return sample
elif mode == 'highest': # choose the n highest predicted yield
sample = candidates.nlargest(num_exp, 'prediction')
if len(sample.index) != 0:
self.current_experiment_index = sample.index
self.current_arms = [arm_index] * len(self.current_experiment_index)
else:
sample = None # set empty dataframe to None; important for downstream function
self.current_experiment_index = None
self.current_arms = None
return sample
elif mode == 'random_highest': # randomly choose one from n highest yields; n default to 5
default_n_highest = 5 # get n experiments with highest predicted yield
if num_exp >= default_n_highest:
exit(f'requested # of experiments are too many for random highest mode. Default n_highest set to {default_n_highest}.')
nlargest = candidates.nlargest(default_n_highest, 'prediction')
try:
sample = nlargest.sample(num_exp)
except ValueError:
sample = None
n = num_exp
while n > 1: # keep reducing the number of sample until its sample-able
n = n - 1
try:
sample = nlargest.sample(n)
break
except ValueError:
continue
if sample is not None:
self.current_experiment_index = sample.index
self.current_arms = [arm_index] * len(self.current_experiment_index)
else:
self.current_experiment_index = None
self.current_arms = None
return sample
else: # other sampling modes
pass
def propose_initial_experiments(scope_dict, arms_dict, algo, dir='./test/', num_exp=1, propose_mode='random'):
"""
Build an initial scope, propose initial experiments and save required objects to be loaded later
This method also supports batch operations, but it does so by sampling multiple substrates of the chosen condition
Parameters
----------
scope_dict: dict
dictionary used to build scope.
e.g.,
x = {'component_a': ['a1', 'a2', 'a3'],
'component_b': ['b1', 'b2'],
'component_c': ['c1', 'c2', 'c3', 'c4']}
arms_dict: dict
dictionary used to build arms
e.g.,
y = {'component_a': ['a1', 'a3'],
'component_b': ['b1', 'b2']}
algo: deebo.algos_regret.RegretAlgorithm
implemented bandit algorithms
dir: str
directory for all the files to be saved into
num_exp: int
number of experiments requested. This will also enable batch operation, but do so by sampling multiple
substrates for the same condition selected
propose_mode: str
choose from {'random', 'highest', 'random_highest'}
See Scope.propose_experiment()
Returns
-------
None
"""
scope = Scope()
scope.build_scope(scope_dict)
scope.build_arms(arms_dict)
d = dict(zip(np.arange(len(scope.arms)), scope.arms))
with open(f'{dir}arms.pkl', 'wb') as f:
pickle.dump(d, f) # save arms dictionary {arm index: arm names}
chosen_arm = algo.select_next_arm()
proposed_experiments = scope.propose_experiment(chosen_arm, num_exp=num_exp, mode=propose_mode)
if os.path.exists(f'{dir}history.csv'):
os.remove(f'{dir}history.csv')
if os.path.exists(f'{dir}log.csv'):
os.remove(f'{dir}log.csv')
proposed_experiments.to_csv(f'{dir}proposed_experiments.csv') # save proposed experiments
with open(f'{dir}algo.pkl', 'wb') as f:
pickle.dump(algo, f) # save algo object
with open(f'{dir}scope.pkl', 'wb') as f:
pickle.dump(scope, f) # save scope object
return
def update_and_propose(dir='./test/', num_exp=1, propose_mode='random'):
"""
After user filled out experimental result, load the result and update scope and algoritm, propose next experiments
Parameters
----------
dir: str
directory where previous log files and results are stored
num_exp: int
number of experiments requested. This will also enable batch operation, but do so by sampling multiple
substrates for the same condition selected
propose_mode: str
choose from {'random', 'highest', 'random_highest'}
See Scope.propose_experiment()
Returns
-------
None
"""
THRESHOLD = 100 # threshold for how many experiments to wait for algo to output a different arm to evaluate
# load all files
with open(f'{dir}algo.pkl', 'rb') as f:
algo = pickle.load(f) # load algo object
with open(f'{dir}scope.pkl', 'rb') as f:
scope = pickle.load(f) # load scope object
exps = pd.read_csv(f'{dir}proposed_experiments.csv', index_col=0) # proposed experiments with results input from user
try:
log = pd.read_csv(f'{dir}log.csv') # acquisition log for algorithm
except FileNotFoundError:
log = None
try:
history = pd.read_csv(f'{dir}history.csv', index_col=0) # experiment history
except FileNotFoundError:
history = None
# get results from user for proposed experiments
rewards = np.array(list(exps['yield']))
if np.isnan(rewards).any():
exit('need to fill in yield')
if ((rewards > 1).any()) or ((rewards < 0).any()):
exit('adjust yield to be between 0 and 1')
# get some info from logs
if log is not None:
horizon = log['horizon'].iloc[-1] + 1
cumulative_reward = log['cumulative_reward'].iloc[-1]
else: # first time logging
horizon = 0
cumulative_reward = 0.0
# set up horizons, chosen_arms, reward and cumulative reward and update log
cumulative_rewards = []
current = cumulative_reward
for ii in range(len(rewards)):
current = current + rewards[ii]
cumulative_rewards.append(current)
horizons = list(np.arange(horizon, horizon+len(rewards)))
chosen_arms = scope.current_arms
new_log = pd.DataFrame(list(zip(horizons, chosen_arms, rewards, cumulative_rewards)),
columns=['horizon', 'chosen_arm', 'reward', 'cumulative_reward'])
log = pd.concat([log, new_log])
# update scope, algo, history
for ii in range(len(rewards)):
scope.update_with_index(scope.current_experiment_index[ii], rewards[ii])
algo.update(scope.current_arms[ii], rewards[ii])
scope.predict()
new_history = pd.concat([history, exps]).drop(columns=['prediction'])
# propose new experiments
chosen_arm = algo.select_next_arm()
proposed_experiments = scope.propose_experiment(chosen_arm, num_exp=num_exp, mode=propose_mode)
if proposed_experiments is None: # no experiments available for this arm
threshold = 0
print(f'No experiments available for arm {chosen_arm}: {scope.arms[chosen_arm]}. Trying to find new experiments')
while proposed_experiments is None:
if threshold > THRESHOLD:
print(f'No experiments available for arm {chosen_arm}: {scope.arms[chosen_arm]} after '
f'{THRESHOLD} attempts; it might be the best arm')
break
algo.update(chosen_arm, algo.emp_means[chosen_arm])
new_chosen_arm = algo.select_next_arm()
if new_chosen_arm == chosen_arm:
threshold = threshold + 1
continue
else:
chosen_arm = new_chosen_arm
proposed_experiments = scope.propose_experiment(new_chosen_arm, num_exp=num_exp, mode=propose_mode)
# save files and objects again
new_history.to_csv(f'{dir}history.csv')
if proposed_experiments is not None:
proposed_experiments.to_csv(f'{dir}proposed_experiments.csv') # save proposed experiments
log.to_csv(f'{dir}log.csv', index=False) # save acquisition log
with open(f'{dir}algo.pkl', 'wb') as f:
pickle.dump(algo, f) # save algo object
with open(f'{dir}scope.pkl', 'wb') as f:
pickle.dump(scope, f) # save scope object
return None
def propose_initial_experiments_interpolation(scope_dict, arms_dict, algo, dir='./test/', num_exp=2, propose_mode='random'):
"""
Build an initial scope, propose initial experiments and save required objects to be loaded later
This method uses interpolation to support batch operation. After one experiment is proposed, the prediction model
is used to supply a predicted yield to algorithm, which allows the algorithm to propose the next experiment.
After all required number of experiments are proposed, data are collected and used to update algorithm sequentially.
The prediction model is also updated, and the next round of experiments happens
NOT guaranteed to work with batch size of 1. Use propose_initial_experiments() for num_exp=1
Parameters
----------
scope_dict: dict
dictionary used to build scope.
e.g.,
x = {'component_a': ['a1', 'a2', 'a3'],
'component_b': ['b1', 'b2'],
'component_c': ['c1', 'c2', 'c3', 'c4']}
arms_dict: dict
dictionary used to build arms
e.g.,
y = {'component_a': ['a1', 'a3'],
'component_b': ['b1', 'b2']}
algo: deebo.algos_regret.RegretAlgorithm
implemented bandit algorithms
dir: str
directory for all the files to be saved into
num_exp: int
number of experiments requested. This will also enable batch operation, but do so by sampling multiple
substrates for the same condition selected
propose_mode: str
choose from {'random', 'highest', 'random_highest'}
See Scope.propose_experiment()
Returns
-------
None
"""
THRESHOLD = 100
scope = Scope()
scope.build_scope(scope_dict)
scope.build_arms(arms_dict)
d = dict(zip(np.arange(len(scope.arms)), scope.arms))
if not os.path.exists(f'{dir}cache/'):
os.makedirs(f'{dir}cache')
with open(f'{dir}cache/arms.pkl', 'wb') as f:
pickle.dump(d, f) # save arms dictionary {arm index: arm names}
chosen_arms = [] # all chosen arms for this round
to_queries = [] # all dicts representing experiments that need to be queried afterwards
scope_idxs = [] # keep track of all experiment idxs proposed. For ease of updating
all_proposed = pd.DataFrame() # all proposed experiments, this is needed for logging
algo_copy = copy.deepcopy(algo) # make a copy of algo for fake update with prediction
scope_copy = copy.deepcopy(scope) # make a copy of scope for fake update with prediction
for ii in range(num_exp):
chosen_arm = algo_copy.select_next_arm()
proposed_experiments = scope_copy.propose_experiment(chosen_arm, num_exp=1, mode=propose_mode) # propose exp
if proposed_experiments is None: # very unlikely here
print('What\'s going on? Scope is unable to propose initial experiments which should not happen')
threshold = 0
print(
f'No experiments available for arm {chosen_arm}: {scope_copy.arms[chosen_arm]}. '
f'Trying to find new experiments')
while proposed_experiments is None:
if threshold > THRESHOLD:
print(
f'No experiments available for arm {chosen_arm}: {scope_copy.arms[chosen_arm]} after '
f'{THRESHOLD} attempts; it might be the best arm')
break
algo_copy.update(chosen_arm, algo_copy.emp_means[chosen_arm])
new_chosen_arm = algo_copy.select_next_arm()
if new_chosen_arm == chosen_arm:
threshold = threshold + 1
continue
else:
chosen_arm = new_chosen_arm
proposed_experiments = scope_copy.propose_experiment(chosen_arm, num_exp=1, mode=propose_mode)
if proposed_experiments is not None:
# add some prefixes to proposed experiments; since this is initial propose method, can set round to 0.
# proposed_experiments_prefix = pd.DataFrame([0, ii, ii], columns=['round', 'experiment', 'horizon'])
# proposed_experiments = pd.concat([proposed_experiments_prefix, proposed_experiments], axis=1)
chosen_arms.append(chosen_arm) # proposed arm added to list
scope_idxs.append(scope_copy.current_experiment_index)
all_proposed = pd.concat([all_proposed, proposed_experiments])
else: # this is where no exp is available and algorithm refuses to choose any other arms
exit() # TODO: fix? maybe, problem when it comes to analysis
algo_copy.update(chosen_arm, proposed_experiments['prediction'].values[0]) # update with prediction for proposed experiment; only proposing one at a time
scope_copy.update_with_index(scope_copy.current_experiment_index[0], -1) # update scope with a fake yield just to block this experiemnt
assert len(chosen_arms) == len(scope_idxs), 'number of chosen arms and indexes to update in scope should be the same'
if os.path.exists(f'{dir}history.csv'):
os.remove(f'{dir}history.csv')
if os.path.exists(f'{dir}log.csv'):
os.remove(f'{dir}log.csv')
all_proposed.to_csv(f'{dir}proposed_experiments.csv') # save proposed experiments; will overwrite
# cache some variables that need to be used later
with open(f'{dir}cache/algo.pkl', 'wb') as f:
pickle.dump(algo, f) # save algo object
with open(f'{dir}cache/scope.pkl', 'wb') as f:
pickle.dump(scope, f) # save scope object
with open(f'{dir}cache/scope_idxs.pkl', 'wb') as f:
pickle.dump(scope_idxs, f) # save the indexes of current experiments
with open(f'{dir}cache/chosen_arms.pkl', 'wb') as f:
pickle.dump(chosen_arms, f)
return None
# TODO: a method to generate again if not happy with the proposal: verify if you can just call propose only
def update_and_propose_interpolation(dir='./test/', num_exp=2, propose_mode='random', encoding_dict=None, update_only=False, propose_only=False):
"""
After user filled out experimental result, load the result and update scope and algoritm, propose next experiments
This method uses interpolation to support batch operation. After one experiment is proposed, the prediction model
is used to supply a predicted yield to algorithm, which allows the algorithm to propose the next experiment.
After all required number of experiments are proposed, data are collected and used to update algorithm sequentially.
The prediction model is also updated, and the next round of experiments happens
NOT guaranteed to work with batch size of 1. Use propose_initial_experiments() for num_exp=1
Parameters
----------
dir: str
directory where previous log files and results are stored
num_exp: int
number of experiments requested. This will also enable batch operation, but do so by sampling multiple
substrates for the same condition selected
propose_mode: str
choose from {'random', 'highest', 'random_highest'}
See Scope.propose_experiment()
encoding_dict: dict
a two-level dictionary of encodings or features used to train prediction model
e.g., {'substrate': {'A': [1,2,3], 'B': [4,5,6]}
'solvent': {'C': [7,8,9], 'D': [10, 11, 12]}}
update_only: bool
if True, this method only updates the existing results, and not propose any new ones
propose_only: bool
if True, this method only proposes a new set of experiments, in case there's nothing to update.
Returns
-------
None
"""
THRESHOLD = 100 # threshold for how many experiments to wait for algo to output a different arm to evaluate
assert not (update_only and propose_only), 'only one of update_only and propose_only can be true at a time'
# load all cached variables
with open(f'{dir}cache/algo.pkl', 'rb') as f:
algo = pickle.load(f)
with open(f'{dir}cache/scope.pkl', 'rb') as f:
scope = pickle.load(f)
with open(f'{dir}cache/chosen_arms.pkl', 'rb') as f:
chosen_arms = pickle.load(f)
with open(f'{dir}cache/scope_idxs.pkl', 'rb') as f:
scope_idxs = pickle.load(f)
if not propose_only: # normal workflow; if propose_only=True, this will be skipped since no update is needed
try:
log = pd.read_csv(f'{dir}log.csv') # acquisition log for algorithm
except FileNotFoundError: # first time update
log = None
try:
history = pd.read_csv(f'{dir}history.csv', index_col=0) # experiment history
except FileNotFoundError: # first time update
history = None
# get results from user for proposed experiments
exps = pd.read_csv(f'{dir}proposed_experiments.csv', index_col=0)
rewards = np.array(list(exps['yield']))
if np.isnan(rewards).any():
exit('need to fill in yield')
if ((rewards > 1).any()) or ((rewards < 0).any()):
exit('adjust yield to be between 0 and 1')
# get current horizon, cumu reward from logs
if log is not None:
horizon = log['horizon'].iloc[-1] + 1
round = log['round'].iloc[-1] + 1
cumulative_reward = log['cumulative_reward'].iloc[-1]
else: # first time logging
horizon = 0
round = 0
cumulative_reward = 0.0
# set up horizons, chosen_arms, reward and cumulative reward and update log
cumulative_rewards = []
current = cumulative_reward
for ii in range(len(rewards)):
current = current + rewards[ii]
cumulative_rewards.append(current)
horizons = list(np.arange(horizon, horizon+len(rewards)))
rounds = list(np.repeat(round, len(rewards))) # basically just repeat the round number
experiments = list(np.arange(len(rewards)))
new_log = pd.DataFrame(list(zip(rounds, experiments, horizons, chosen_arms, rewards, cumulative_rewards)),
columns=['round', 'experiment', 'horizon', 'chosen_arm', 'reward', 'cumulative_reward']) # chosen arms directly loaded from cache
log = pd.concat([log, new_log])
# update scope, algo
for ii in range(len(rewards)):
scope.update_with_index(scope_idxs[ii], rewards[ii])
algo.update(chosen_arms[ii], rewards[ii])
scope.predict(encoding_dict=encoding_dict)
# update history
exps.insert(0, column='horizon', value=horizons)
new_history = pd.concat([history, exps])
# save history and log again
new_history.to_csv(f'{dir}history.csv')
log.to_csv(f'{dir}log.csv', index=False) # save acquisition log
if not update_only:
# now try to propose again
chosen_arms = [] # all chosen arms for this round
to_queries = [] # all dicts representing experiments that need to be queried afterwards
scope_idxs = [] # keep track of all experiment idxs proposed. For ease of updating
all_proposed = pd.DataFrame() # all proposed experiments, this is needed for logging
algo_copy = copy.deepcopy(algo) # make a copy of algo for fake update with prediction
scope_copy = copy.deepcopy(scope) # make a copy of scope for fake update with prediction
for _ in range(num_exp):
chosen_arm = algo_copy.select_next_arm()
proposed_experiments = scope_copy.propose_experiment(chosen_arm, num_exp=1, mode=propose_mode) # propose exp
if proposed_experiments is None:
threshold = 0
print(
f'No experiments available for arm {chosen_arm}: {scope_copy.arms[chosen_arm]}. '
f'Trying to find new experiments.')
while proposed_experiments is None:
if threshold > THRESHOLD:
print(
f'No experiments available for arm {chosen_arm}: {scope_copy.arms[chosen_arm]} after '
f'{THRESHOLD} attempts; it might be the best arm.')
break
algo_copy.update(chosen_arm, algo_copy.emp_means[chosen_arm])
new_chosen_arm = algo_copy.select_next_arm()
if new_chosen_arm == chosen_arm:
threshold = threshold + 1
continue
else:
chosen_arm = new_chosen_arm
proposed_experiments = scope_copy.propose_experiment(chosen_arm, num_exp=1, mode=propose_mode)
if proposed_experiments is not None:
chosen_arms.append(chosen_arm) # proposed arm added to list
scope_idxs.append(scope_copy.current_experiment_index)
all_proposed = pd.concat([all_proposed, proposed_experiments])
# to_dict() should generate a list
else: # this is where no exp is available and algorithm refuses to choose any other arms
exit() # fix? maybe, problem when it comes to analysis
algo_copy.update(chosen_arm, proposed_experiments['prediction'].values[0]) # update with prediction for proposed experiment; only proposing one at a time
scope_copy.update_with_index(scope_copy.current_experiment_index[0], -1) # update scope with a fake yield just to block this experiemnt
assert len(chosen_arms) == len(scope_idxs), 'number of chosen arms and indexes to update in scope should be the same'
all_proposed.to_csv(f'{dir}proposed_experiments.csv') # save proposed experiments
# cache some variables that need to be used later
with open(f'{dir}cache/algo.pkl', 'wb') as f:
pickle.dump(algo, f) # save algo object
with open(f'{dir}cache/scope.pkl', 'wb') as f:
pickle.dump(scope, f) # save scope object
with open(f'{dir}cache/scope_idxs.pkl', 'wb') as f:
pickle.dump(scope_idxs, f) # save the indexes of current experiments
with open(f'{dir}cache/chosen_arms.pkl', 'wb') as f:
pickle.dump(chosen_arms, f)
else: # update only
# ignore some of the warnings here. If the update part is skipped, this will not run.
pd.DataFrame().to_csv(f'{dir}proposed_experiments.csv')
new_history.to_csv(f'{dir}history.csv')
log.to_csv(f'{dir}log.csv', index=False) # save acquisition log
with open(f'{dir}cache/algo.pkl', 'wb') as f:
pickle.dump(algo, f) # save algo object
with open(f'{dir}cache/scope.pkl', 'wb') as f:
pickle.dump(scope, f) # save scope object
return None
def simulate_propose_and_update(scope_dict,
arms_dict,
ground_truth,
algo,
dir='./test/',
num_sims=10,
num_exp=1,
num_round=20,
propose_mode='random',
expansion_dict=None,
predict=False,
):
"""
Method for simulation; skipping the saving and loading by user, query dataset with full results instead
Parameters
----------
scope_dict: dict
dictionary used to build scope.
e.g.,
x = {'component_a': ['a1', 'a2', 'a3'],
'component_b': ['b1', 'b2'],