-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathtest_runset.py
More file actions
356 lines (314 loc) · 11.8 KB
/
test_runset.py
File metadata and controls
356 lines (314 loc) · 11.8 KB
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
"""RunSet tests"""
import os
from cmdstanpy import _TMPDIR
from cmdstanpy.cmdstan_args import CmdStanArgs, PathfinderArgs, SamplerArgs
from cmdstanpy.stanfit import RunSet
from cmdstanpy.utils import EXTENSION
HERE = os.path.dirname(os.path.abspath(__file__))
DATAFILES_PATH = os.path.join(HERE, 'data')
def test_check_repr() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = SamplerArgs()
chain_ids = [1, 2, 3, 4] # default
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args, chains=4)
assert 'RunSet: chains=4' in repr(runset)
assert 'method=sample' in repr(runset)
assert 'retcodes=[-1, -1, -1, -1]' in repr(runset)
assert 'csv_file' in repr(runset)
assert 'console_msgs' in repr(runset)
assert 'diagnostics_file' not in repr(runset)
assert 'config_file' in repr(runset)
def test_check_retcodes() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = SamplerArgs()
chain_ids = [1, 2, 3, 4] # default
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args, chains=4)
retcodes = runset._retcodes
assert 4 == len(retcodes)
for i in range(len(retcodes)):
assert -1 == runset._retcode(i)
runset._set_retcode(0, 0)
assert 0 == runset._retcode(0)
for i in range(1, len(retcodes)):
assert -1 == runset._retcode(i)
assert not runset._check_retcodes()
for i in range(1, len(retcodes)):
runset._set_retcode(i, 0)
assert runset._check_retcodes()
def test_get_err_msgs() -> None:
exe = os.path.join(DATAFILES_PATH, 'logistic' + EXTENSION)
rdata = os.path.join(DATAFILES_PATH, 'logistic.missing_data.R')
sampler_args = SamplerArgs()
chain_ids = [1, 2, 3]
cmdstan_args = CmdStanArgs(
model_name='logistic',
model_exe=exe,
chain_ids=chain_ids,
data=rdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args, chains=3, chain_ids=chain_ids)
for i in range(3):
runset._set_retcode(i, 70)
stdout_file = 'chain-' + str(i + 1) + '-missing-data-stdout.txt'
path = os.path.join(DATAFILES_PATH, stdout_file)
runset._stdout_files[i] = path
errs = runset.get_err_msgs()
assert 'Exception: variable does not exist' in errs
def test_output_filenames_one_proc_per_chain() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = SamplerArgs()
chain_ids = [1, 2, 3, 4]
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args, chains=4, one_process_per_chain=True)
assert all("bernoulli-" in csv_file for csv_file in runset.csv_files)
assert all(
csv_file.endswith(f"_{id}.csv")
for id, csv_file in zip(chain_ids, runset.csv_files)
)
assert len(runset.stdout_files) == len(chain_ids)
assert all(
stdout_file.endswith(f"_stdout_{id}.txt")
for id, stdout_file in zip(chain_ids, runset.stdout_files)
)
assert len(runset.config_files) == len(chain_ids)
assert all(
config_file.endswith(f"_{id}_config.json")
for id, config_file in zip(chain_ids, runset.config_files)
)
cmdstan_args_other_files = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
save_latent_dynamics=True,
save_profile=True,
)
runset_other_files = RunSet(
args=cmdstan_args_other_files, chains=4, one_process_per_chain=True
)
assert len(runset_other_files.diagnostic_files) == len(chain_ids)
assert all(
diag_file.endswith(f"_diagnostic_{id}.csv")
for id, diag_file in zip(chain_ids, runset_other_files.diagnostic_files)
)
assert len(runset_other_files.profile_files) == len(chain_ids)
assert all(
prof_file.endswith(f"_profile_{id}.csv")
for id, prof_file in zip(chain_ids, runset_other_files.profile_files)
)
def test_output_filenames_threading() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = SamplerArgs()
chain_ids = [1, 2, 3, 4]
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args, chains=4, one_process_per_chain=False)
assert all("bernoulli-" in csv_file for csv_file in runset.csv_files)
assert all(
csv_file.endswith(f"_{id}.csv")
for id, csv_file in zip(chain_ids, runset.csv_files)
)
assert len(runset.stdout_files) == 1
assert runset.stdout_files[0].endswith("_stdout.txt")
assert len(runset.config_files) == 1
assert runset.config_files[0].endswith("_config.json")
cmdstan_args_other_files = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
save_latent_dynamics=True,
save_profile=True,
)
runset_other_files = RunSet(
args=cmdstan_args_other_files, chains=4, one_process_per_chain=False
)
assert len(runset_other_files.diagnostic_files) == len(chain_ids)
assert all(
diag_file.endswith(f"_diagnostic_{id}.csv")
for id, diag_file in zip(chain_ids, runset_other_files.diagnostic_files)
)
assert len(runset_other_files.profile_files) == 1
assert runset_other_files.profile_files[0].endswith("_profile.csv")
def test_output_filenames_single_chain() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = SamplerArgs()
chain_ids = [1]
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args, chains=1, one_process_per_chain=False)
base_file = runset._base_outfile
assert len(runset.csv_files) == 1
assert len(runset.stdout_files) == 1
assert runset.csv_files[0].endswith(f"{base_file}.csv")
assert runset.stdout_files[0].endswith(f"{base_file}_stdout.txt")
runset = RunSet(args=cmdstan_args, chains=1, one_process_per_chain=True)
base_file = runset._base_outfile
assert runset.stdout_files[0].endswith(f"{base_file}_stdout.txt")
assert runset.config_files[0].endswith(f"{base_file}_config.json")
cmdstan_args_other_files = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
save_latent_dynamics=True,
save_profile=True,
)
runset_other_files = RunSet(
args=cmdstan_args_other_files, chains=1, one_process_per_chain=False
)
assert len(runset_other_files.diagnostic_files) == 1
assert runset_other_files.diagnostic_files[0].endswith("_diagnostic.csv")
assert len(runset_other_files.profile_files) == 1
assert runset_other_files.profile_files[0].endswith("_profile.csv")
runset_other_files = RunSet(
args=cmdstan_args_other_files, chains=1, one_process_per_chain=True
)
assert len(runset_other_files.diagnostic_files) == 1
assert runset_other_files.diagnostic_files[0].endswith("_diagnostic.csv")
assert len(runset_other_files.profile_files) == 1
assert runset_other_files.profile_files[0].endswith("_profile.csv")
def test_commands() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = SamplerArgs()
chain_ids = [1, 2, 3, 4]
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args, chains=4)
assert 'id=1' in runset.cmd(0)
assert 'id=4' in runset.cmd(3)
def test_save_latent_dynamics() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = SamplerArgs()
chain_ids = [1, 2, 3, 4]
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
save_latent_dynamics=True,
)
runset = RunSet(args=cmdstan_args, chains=4)
assert _TMPDIR in runset.diagnostic_files[0]
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
save_latent_dynamics=True,
output_dir=os.path.abspath('.'),
)
runset = RunSet(args=cmdstan_args, chains=4)
assert os.path.abspath('.') in runset.diagnostic_files[0]
def test_chain_ids() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = SamplerArgs()
chain_ids = [11, 12, 13, 14]
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args, chains=4, chain_ids=chain_ids)
assert 'id=11' in runset.cmd(0)
assert '_11.csv' in runset._csv_files[0]
assert 'id=14' in runset.cmd(3)
assert '_14.csv' in runset._csv_files[3]
def test_output_filenames_pathfinder_single_paths() -> None:
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
sampler_args = PathfinderArgs(num_paths=4, save_single_paths=True)
chain_ids = [1]
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args)
assert len(runset.single_path_csv_files) == 4
assert len(runset.single_path_json_files) == 4
assert all(
csv_file.endswith(f"_path_{id}.csv")
for id, csv_file in zip(range(1, 5), runset.single_path_csv_files)
)
assert all(
json_file.endswith(f"_path_{id}.json")
for id, json_file in zip(range(1, 5), runset.single_path_json_files)
)
sampler_args = PathfinderArgs(num_paths=1, save_single_paths=True)
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args)
assert len(runset.single_path_csv_files) == 1
assert len(runset.single_path_json_files) == 1
assert runset.single_path_csv_files[0].endswith(".csv")
assert runset.single_path_json_files[0].endswith(".json")
sampler_args = PathfinderArgs(num_paths=1, save_single_paths=False)
cmdstan_args = CmdStanArgs(
model_name='bernoulli',
model_exe=exe,
chain_ids=chain_ids,
data=jdata,
method_args=sampler_args,
)
runset = RunSet(args=cmdstan_args)
assert len(runset.single_path_csv_files) == 0
assert len(runset.single_path_json_files) == 0