-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathcommand_test.cpp
More file actions
574 lines (493 loc) · 19.8 KB
/
Copy pathcommand_test.cpp
File metadata and controls
574 lines (493 loc) · 19.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
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
#include <test/utility.hpp>
#include <cmdstan/return_codes.hpp>
#include <stan/callbacks/stream_writer.hpp>
#include <stan/services/error_codes.hpp>
#include <stan/io/string_utils.hpp>
#include <boost/math/policies/error_handling.hpp>
#include <gtest/gtest.h>
#include <stdexcept>
#include <string>
using cmdstan::test::convert_model_path;
using cmdstan::test::count_matches;
using cmdstan::test::multiple_command_separator;
using cmdstan::test::run_command;
using cmdstan::test::run_command_output;
TEST(StanUiCommand, countMatches) {
EXPECT_EQ(-1, count_matches("", ""));
EXPECT_EQ(-1, count_matches("", "abc"));
EXPECT_EQ(0, count_matches("abc", ""));
EXPECT_EQ(0, count_matches("abc", "ab"));
EXPECT_EQ(0, count_matches("abc", "dab"));
EXPECT_EQ(0, count_matches("abc", "abde"));
EXPECT_EQ(0, count_matches("aa", "a"));
EXPECT_EQ(1, count_matches("aa", "aa"));
EXPECT_EQ(1, count_matches("aa", "aaa"));
EXPECT_EQ(2, count_matches("aa", "aaaa"));
}
void test_sample_prints(const std::string &base_cmd) {
std::string cmd(base_cmd);
cmd += " num_samples=100 num_warmup=100";
std::string cmd_output = run_command(cmd).output;
// transformed data
EXPECT_EQ(1, count_matches("x=", cmd_output));
// transformed parameters
EXPECT_TRUE(count_matches("z=", cmd_output) >= 200);
// model
EXPECT_TRUE(count_matches("y=", cmd_output) >= 200);
// generated quantities [only on saved iterations, should be num samples]
EXPECT_TRUE(count_matches("w=", cmd_output) == 100);
}
void test_optimize_prints(const std::string &base_cmd) {
std::string cmd(base_cmd);
std::string cmd_output = run_command(cmd).output;
// transformed data
EXPECT_EQ(1, count_matches("x=", cmd_output));
// transformed parameters
EXPECT_TRUE(count_matches("z=", cmd_output) >= 1);
// model
EXPECT_TRUE(count_matches("y=", cmd_output) >= 1);
// generated quantities [only on saved iterations, should be num samples]
EXPECT_TRUE(count_matches("w=", cmd_output) == 1);
}
TEST(StanUiCommand, printReallyPrints) {
std::vector<std::string> path_vector;
path_vector.push_back("..");
path_vector.push_back("src");
path_vector.push_back("test");
path_vector.push_back("test-models");
path_vector.push_back("printer");
std::string path = "cd test ";
path += multiple_command_separator();
path += " ";
path += convert_model_path(path_vector);
// SAMPLING
// static HMC
// + adapt
test_sample_prints(
path
+ " sample algorithm=hmc engine=static metric=unit_e adapt engaged=0");
test_sample_prints(
path
+ " sample algorithm=hmc engine=static metric=diag_e adapt engaged=0");
test_sample_prints(
path
+ " sample algorithm=hmc engine=static metric=dense_e adapt engaged=0");
// - adapt
test_sample_prints(
path
+ " sample algorithm=hmc engine=static metric=unit_e adapt engaged=1");
test_sample_prints(
path
+ " sample algorithm=hmc engine=static metric=diag_e adapt engaged=1");
test_sample_prints(
path
+ " sample algorithm=hmc engine=static metric=dense_e adapt engaged=1");
// NUTS
// + adapt
test_sample_prints(
path + " sample algorithm=hmc engine=nuts metric=unit_e adapt engaged=0");
test_sample_prints(
path + " sample algorithm=hmc engine=nuts metric=diag_e adapt engaged=0");
test_sample_prints(
path
+ " sample algorithm=hmc engine=nuts metric=dense_e adapt engaged=0");
// - adapt
test_sample_prints(
path + " sample algorithm=hmc engine=nuts metric=unit_e adapt engaged=1");
test_sample_prints(
path + " sample algorithm=hmc engine=nuts metric=diag_e adapt engaged=1");
test_sample_prints(
path
+ " sample algorithm=hmc engine=nuts metric=dense_e adapt engaged=1");
// OPTIMIZATION
test_optimize_prints(path + " optimize algorithm=newton");
test_optimize_prints(path + " optimize algorithm=bfgs");
}
TEST(StanUiCommand, refresh_zero_ok) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("proper");
std::string command = convert_model_path(model_path) +
" sample num_samples=10 num_warmup=10 init=0 output "
"refresh=0 file=test/output.csv";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::OK), out.err_code);
EXPECT_EQ(0, count_matches("Iteration:", out.output));
}
TEST(StanUiCommand, refresh_nonzero_ok) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("proper");
std::string command = convert_model_path(model_path) +
" sample num_samples=10 num_warmup=10 init=0 output "
"refresh=1 file=test/output.csv";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::OK), out.err_code);
EXPECT_EQ(20, count_matches("Iteration:", out.output));
}
TEST(StanUiCommand, zero_init_value_fail) {
std::string expected_message = "Rejecting initial value";
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("value_fail");
std::string command = convert_model_path(model_path)
+ " sample init=0 output file=test/output.csv";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::NOT_OK), out.err_code);
EXPECT_TRUE(out.header.length() > 0U);
EXPECT_TRUE(out.body.length() > 0U);
EXPECT_EQ(1, count_matches(expected_message, out.body))
<< "Failed running: " << out.command;
}
TEST(StanUiCommand, zero_init_domain_fail) {
std::string expected_message = "Rejecting initial value";
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("domain_fail");
std::string command = convert_model_path(model_path)
+ " sample init=0 output file=test/output.csv";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::NOT_OK), out.err_code);
EXPECT_TRUE(out.header.length() > 0U);
EXPECT_TRUE(out.body.length() > 0U);
EXPECT_EQ(1, count_matches(expected_message, out.body))
<< "Failed running: " << out.command;
}
TEST(StanUiCommand, user_init_value_fail) {
std::string expected_message = "Rejecting initial value";
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("value_fail");
std::vector<std::string> init_path;
init_path.push_back("src");
init_path.push_back("test");
init_path.push_back("test-models");
init_path.push_back("value_fail.init.R");
std::string command = convert_model_path(model_path)
+ " sample init=" + convert_model_path(init_path)
+ " output file=test/output.csv";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::NOT_OK), out.err_code);
EXPECT_TRUE(out.header.length() > 0U);
EXPECT_TRUE(out.body.length() > 0U);
EXPECT_EQ(1, count_matches(expected_message, out.body))
<< "Failed running: " << out.command;
}
TEST(StanUiCommand, user_init_domain_fail) {
std::string expected_message = "Rejecting initial value";
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("domain_fail");
std::vector<std::string> init_path;
init_path.push_back("src");
init_path.push_back("test");
init_path.push_back("test-models");
init_path.push_back("domain_fail.init.R");
std::string command = convert_model_path(model_path)
+ " sample init=" + convert_model_path(init_path)
+ " output file=test/output.csv";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::NOT_OK), out.err_code);
EXPECT_TRUE(out.header.length() > 0U);
EXPECT_TRUE(out.body.length() > 0U);
EXPECT_EQ(1, count_matches(expected_message, out.body))
<< "Failed running: " << out.command;
}
TEST(StanUiCommand, CheckCommand_no_args) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("domain_fail"); // can use any model here
std::string command = convert_model_path(model_path);
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::NOT_OK), out.err_code);
}
TEST(StanUiCommand, CheckCommand_help) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("domain_fail"); // can use any model here
std::string command = convert_model_path(model_path) + " help";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::OK), out.err_code);
}
TEST(StanUiCommand, CheckCommand_unrecognized_argument) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("domain_fail"); // can use any model here
std::string command = convert_model_path(model_path) + " foo";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::NOT_OK), out.err_code);
}
TEST(StanUiCommand, timing_info) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("proper");
std::string command = convert_model_path(model_path) +
" sample num_samples=10 num_warmup=10 init=0 output "
"refresh=0 file=test/output.csv";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::OK), out.err_code);
std::fstream output_csv_stream("test/output.csv");
std::stringstream output_sstream;
output_sstream << output_csv_stream.rdbuf();
output_csv_stream.close();
std::string output = output_sstream.str();
EXPECT_EQ(1, count_matches("# Elapsed Time:", output));
EXPECT_EQ(1, count_matches(" seconds (Warm-up)", output));
EXPECT_EQ(1, count_matches(" seconds (Sampling)", output));
EXPECT_EQ(1, count_matches(" seconds (Total)", output));
}
TEST(StanUiCommand, run_info) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("proper");
std::string command = convert_model_path(model_path) +
" sample num_samples=10 num_warmup=10 init=0 output "
"refresh=0 file=test/output.csv";
run_command_output out = run_command(command);
EXPECT_EQ(int(cmdstan::return_codes::OK), out.err_code);
std::fstream output_csv_stream("test/output.csv");
std::stringstream output_sstream;
output_sstream << output_csv_stream.rdbuf();
output_csv_stream.close();
std::string output = output_sstream.str();
EXPECT_EQ(1, count_matches("# method = sample", output));
EXPECT_EQ(1, count_matches(" num_samples = 10", output));
EXPECT_EQ(1, count_matches(" num_warmup = 10", output));
EXPECT_EQ(1, count_matches(" init = 0", output));
}
TEST(StanUiCommand, random_seed_default) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("transformed_data_rng_test");
std::string command
= convert_model_path(model_path)
+ " sample num_samples=10 num_warmup=10 init=0 "
+ " data file=src/test/test-models/transformed_data_rng_test.init.R"
+ " output refresh=0 file=test/output.csv";
std::string cmd_output = run_command(command).output;
EXPECT_EQ(1, count_matches("y values:", cmd_output));
std::vector<std::string> lines = stan::io::split(cmd_output, "\n");
std::string random1;
for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end();
++it) {
if (stan::io::starts_with(*it, "y values:")) {
random1.assign(*it, 9, std::string::npos);
EXPECT_EQ(1, count_matches("[", random1));
EXPECT_EQ(1, count_matches("]", random1));
break;
}
}
cmd_output = run_command(command).output;
EXPECT_EQ(1, count_matches("y values:", cmd_output));
lines = stan::io::split(cmd_output, "\n");
std::string random2;
for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end();
++it) {
if (stan::io::starts_with(*it, "y values:")) {
random2.assign(*it, 9, std::string::npos);
EXPECT_EQ(1, count_matches("[", random2));
EXPECT_EQ(1, count_matches("]", random2));
break;
}
}
EXPECT_NE(random1, random2);
}
TEST(StanUiCommand, random_seed_specified_same) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("transformed_data_rng_test");
std::string command
= convert_model_path(model_path)
+ " sample num_samples=10 num_warmup=10 init=0 " + " random seed=12345 "
+ " data file=src/test/test-models/transformed_data_rng_test.init.R"
+ " output refresh=0 file=test/output.csv";
std::string cmd_output = run_command(command).output;
EXPECT_EQ(1, count_matches("y values:", cmd_output));
std::vector<std::string> lines = stan::io::split(cmd_output, "\n");
std::string random1;
for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end();
++it) {
if (stan::io::starts_with(*it, "y values:")) {
random1.assign(*it, 9, std::string::npos);
EXPECT_EQ(1, count_matches("[", random1));
EXPECT_EQ(1, count_matches("]", random1));
break;
}
}
cmd_output = run_command(command).output;
EXPECT_EQ(1, count_matches("y values:", cmd_output));
lines = stan::io::split(cmd_output, "\n");
std::string random2;
for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end();
++it) {
if (stan::io::starts_with(*it, "y values:")) {
random2.assign(*it, 9, std::string::npos);
EXPECT_EQ(1, count_matches("[", random2));
EXPECT_EQ(1, count_matches("]", random2));
break;
}
}
EXPECT_EQ(random1, random2);
}
TEST(StanUiCommand, random_seed_specified_different) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("transformed_data_rng_test");
std::string command
= convert_model_path(model_path)
+ " sample num_samples=10 num_warmup=10 init=0 " + " random seed=12345 "
+ " data file=src/test/test-models/transformed_data_rng_test.init.R"
+ " output refresh=0 file=test/output.csv";
std::string cmd_output = run_command(command).output;
EXPECT_EQ(1, count_matches("y values:", cmd_output));
std::vector<std::string> lines = stan::io::split(cmd_output, "\n");
std::string random1;
for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end();
++it) {
if (stan::io::starts_with(*it, "y values:")) {
random1.assign(*it, 9, std::string::npos);
EXPECT_EQ(1, count_matches("[", random1));
EXPECT_EQ(1, count_matches("]", random1));
break;
}
}
command = convert_model_path(model_path)
+ " sample num_samples=10 num_warmup=10 init=0 "
+ " random seed=45678 "
+ " data file=src/test/test-models/transformed_data_rng_test.init.R"
+ " output refresh=0 file=test/output.csv";
cmd_output = run_command(command).output;
EXPECT_EQ(1, count_matches("y values:", cmd_output));
lines = stan::io::split(cmd_output, "\n");
std::string random2;
for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end();
++it) {
if (stan::io::starts_with(*it, "y values:")) {
random2.assign(*it, 9, std::string::npos);
EXPECT_EQ(1, count_matches("[", random2));
EXPECT_EQ(1, count_matches("]", random2));
break;
}
}
EXPECT_NE(random1, random2);
}
TEST(StanUiCommand, random_seed_fail_1) {
std::string expected_message = "is not a valid value for \"seed\"";
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("transformed_data_rng_test");
std::string command
= convert_model_path(model_path)
+ " sample num_samples=10 num_warmup=10 init=0 " + " random seed=-2 "
+ " data file=src/test/test-models/transformed_data_rng_test.init.R"
+ " output refresh=0 file=test/output.csv";
std::string cmd_output = run_command(command).output;
run_command_output out = run_command(command);
EXPECT_EQ(1, count_matches(expected_message, out.body));
}
TEST(StanUiCommand, random_seed_fail_2) {
std::string expected_message = "is not a valid value for \"seed\"";
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("transformed_data_rng_test");
long long int max = std::numeric_limits<unsigned int>::max();
long long int maxplus = max + 100;
{
std::string command
= convert_model_path(model_path)
+ " sample num_samples=10 num_warmup=10 init=0 "
+ " random seed=" + std::to_string(max) + " "
+ " data file=src/test/test-models/transformed_data_rng_test.init.R"
+ " output refresh=0 file=test/output.csv";
std::string cmd_output = run_command(command).output;
run_command_output out = run_command(command);
EXPECT_EQ(0, count_matches(expected_message, out.body));
}
{
std::string command
= convert_model_path(model_path)
+ " sample num_samples=10 num_warmup=10 init=0 "
+ " random seed=" + std::to_string(maxplus) + " "
+ " data file=src/test/test-models/transformed_data_rng_test.init.R"
+ " output refresh=0 file=test/output.csv";
std::string cmd_output = run_command(command).output;
run_command_output out = run_command(command);
EXPECT_EQ(1, count_matches(expected_message, out.body));
}
}
TEST(StanUiCommand, json_input) {
std::vector<std::string> model_path;
model_path.push_back("src");
model_path.push_back("test");
model_path.push_back("test-models");
model_path.push_back("ndim_array");
std::string command = convert_model_path(model_path)
+ " sample algorithm=fixed_param"
+ " random seed=12345 "
+ " data file=src/test/test-models/ndim_array.data.json"
+ " output refresh=0 file=test/output.csv";
std::string cmd_output = run_command(command).output;
EXPECT_EQ(
1, count_matches("d1_1: [[0,1,2,3],[4,5,6,7],[8,9,10,11]]", cmd_output));
EXPECT_EQ(1,
count_matches("d1_2: [[12,13,14,15],[16,17,18,19],[20,21,22,23]]",
cmd_output));
}
//
struct dummy_stepsize_adaptation {
void set_mu(const double) {}
void set_delta(const double) {}
void set_gamma(const double) {}
void set_kappa(const double) {}
void set_t0(const double) {}
};
struct dummy_z {
Eigen::VectorXd q;
};
template <class ExceptionType>
struct sampler {
dummy_stepsize_adaptation _stepsize_adaptation;
dummy_z _z;
double get_nominal_stepsize() { return 0; }
dummy_stepsize_adaptation get_stepsize_adaptation() {
return _stepsize_adaptation;
}
void engage_adaptation() {}
dummy_z z() { return _z; }
void init_stepsize(stan::callbacks::writer &info_writer,
stan::callbacks::writer &error_writer) {
throw ExceptionType("throwing exception");
}
};