-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_samples.py
206 lines (162 loc) · 7.59 KB
/
run_samples.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
import os
import argparse
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import datetime
import importlib.util
import pandas as pd
import torch
from moses.models_storage import ModelsStorage
from moses.vae import VAE
def load_module(name, path):
dirname = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(dirname, path)
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
MODELS = ModelsStorage()
split_dataset = load_module('split_dataset', 'split_dataset.py')
sampler_script = load_module('sample', 'sample.py')
def get_model_path(config, model, model_starttime):
if len(config.experiment_suff) > 0:
unique_folder = f'{config.data}_{model}_{config.experiment_suff}_{model_starttime}'
else:
unique_folder = f'{config.data}_{model}_{model_starttime}'
unique_folder_path = os.path.join(config.checkpoint_dir, unique_folder)
return os.path.join(
unique_folder_path, model + config.experiment_suff + f'_model_{config.load_epoch}.pt'
)
def get_log_path(config, model, model_starttime):
if len(config.experiment_suff) > 0:
unique_folder = f'{config.data}_{model}_{config.experiment_suff}_{model_starttime}'
else:
unique_folder = f'{config.data}_{model}_{model_starttime}'
unique_folder_path = os.path.join(config.checkpoint_dir, unique_folder)
return os.path.join(
unique_folder_path, model + config.experiment_suff + '_log.txt'
)
def get_config_path(config, model,model_starttime):
if len(config.experiment_suff) > 0:
unique_folder = f'{config.data}_{model}_{config.experiment_suff}_{model_starttime}'
else:
unique_folder = f'{config.data}_{model}_{model_starttime}'
unique_folder_path = os.path.join(config.checkpoint_dir, unique_folder)
return os.path.join(
unique_folder_path, model + config.experiment_suff + '_config.pt'
)
def get_vocab_path(config, model, model_starttime):
if len(config.experiment_suff) > 0:
unique_folder = f'{config.data}_{model}_{config.experiment_suff}_{model_starttime}'
else:
unique_folder = f'{config.data}_{model}_{model_starttime}'
unique_folder_path = os.path.join(config.checkpoint_dir, unique_folder)
return os.path.join(
unique_folder_path, model + config.experiment_suff + '_vocab.pt'
)
def get_generation_path(config, model, model_starttime):
if len(config.experiment_suff) > 0:
unique_folder = f'{config.data}_{model}_{config.experiment_suff}_{model_starttime}'
else:
unique_folder = f'{config.data}_{model}_{model_starttime}'
unique_folder_path = os.path.join(config.checkpoint_dir, unique_folder)
return os.path.join(
unique_folder_path,
model + config.experiment_suff + '_generated.csv'
)
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='all',
choices=['all'] + MODELS.get_model_names(),
help='Which model to run')
parser.add_argument('--test_path',
type=str, required=False,
help='Path to test molecules csv')
parser.add_argument('--test_scaffolds_path',
type=str, required=False,
help='Path to scaffold test molecules csv')
parser.add_argument('--train_path',
type=str, required=False,
help='Path to train molecules csv')
parser.add_argument('--ptest_path',
type=str, required=False,
help='Path to precalculated test npz')
parser.add_argument('--ptest_scaffolds_path',
type=str, required=False,
help='Path to precalculated scaffold test npz')
parser.add_argument('--checkpoint_dir', type=str, default='./checkpoints',
help='Directory for checkpoints')
parser.add_argument('--n_samples', type=int, default=30000,
help='Number of samples to sample')
parser.add_argument('--n_jobs', type=int, default=4,
help='Number of threads')
parser.add_argument('--device', type=str, default='cpu',
help='GPU device index in form `cuda:N` (or `cpu`)')
parser.add_argument('--metrics', type=str, default='metrics.csv',
help='Path to output file with metrics')
parser.add_argument('--train_size', type=int, default=None,
help='Size of training dataset')
parser.add_argument('--test_size', type=int, default=None,
help='Size of testing dataset')
parser.add_argument('--experiment_suff', type=str, default='',
help='Experiment suffix to break ambiguity')
parser.add_argument('--data', type=str, default='ZINC',
choices=['ZINC', 'QM9', 'ZINC250K'], help='Dataset to use')
parser.add_argument('--use_selfies', type=int, default=0,
choices=[0, 1], help='Use selfies format')
parser.add_argument('--wandb_entity', type=str,
help='Wandb entity name')
parser.add_argument('--wandb_project', type=str, default='Moses',
help='Wandb project name')
parser.add_argument('--nowandb', type=int, default=1,
choices=[0, 1], help='Disable wandb')
parser.add_argument('--debug_mode', type=int, default=0,
choices=[0, 1], help='Debug mode')
parser.add_argument('--model_save_time', type=str,
help='Model save time')
parser.add_argument('--load_epoch', type=str,
help='Model epoch to load')
return parser
def sample_from_model(config, model, model_starttime):
print('Sampling...')
model_path = get_model_path(config, model, model_starttime)
config_path = get_config_path(config, model, model_starttime)
vocab_path = get_vocab_path(config, model, model_starttime)
assert os.path.exists(model_path), (
"Can't find model path for sampling: '{}'".format(model_path)
)
assert os.path.exists(config_path), (
"Can't find config path for sampling: '{}'".format(config_path)
)
assert os.path.exists(vocab_path), (
"Can't find vocab path for sampling: '{}'".format(vocab_path)
)
sampler_parser = sampler_script.get_parser()
sampler_config = sampler_parser.parse_known_args(
[model] + sys.argv[1:] +
['--device', config.device,
'--model_load', model_path,
'--config_load', config_path,
'--vocab_load', vocab_path,
'--gen_save', get_generation_path(config, model, model_starttime, ),
'--n_samples', str(config.n_samples)]
)[0]
sampler_config.data = config.data
if config.use_selfies:
sampler_config.use_selfies = True
else:
sampler_config.use_selfies = False
dict1 = vars(config)
dict2 = vars(sampler_config)
whole_config = dict1.copy()
whole_config.update(dict2)
whole_config = argparse.Namespace(**whole_config)
sampler_script.main(model, whole_config)
def main(config):
save_time = config.model_save_time
model = config.model
sample_from_model(config, model, save_time)
if __name__ == '__main__':
parser = get_parser()
config = parser.parse_known_args()[0]
main(config)