Skip to content

Commit 064adc4

Browse files
committed
Copy new tests from #1290
Also fix CSV precision bug in matrix outputs
1 parent 4cd3325 commit 064adc4

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

src/cmdstan/stansummary_helper.hpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,7 @@ void write_all_model_params(const stan::mcmc::chainset &chains,
557557
if (as_csv) {
558558
*out << "\"" << chains.param_name(row_maj_index_chains) << "\"";
559559
for (int j = 0; j < params.cols(); j++) {
560-
*out << "," << std::fixed
561-
<< std::setprecision(compute_precision(
562-
params(row_maj_index, j), sig_figs, false))
563-
<< params(row_maj_index, j);
560+
*out << "," << params(row_maj_index, j);
564561
}
565562
} else {
566563
*out << std::setw(max_name_length + 1) << std::left
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name,Mean,MCSE,StdDev,MAD,5%,50%,95%,ESS_bulk,ESS_tail,R_hat
2+
"lp__",-15.3787,1.17576,3.71807,0.779848,-20.9245,-14.8305,-12.6946,5,5,1.31109
3+
"accept_stat__",0.68789,0.0961912,0.304183,0.252626,0.208269,0.779947,0.989055,5,5,1.05408
4+
"stepsize__",0.894277,nan,1.17028e-16,0,0.894277,0.894277,0.894277,nan,nan,nan
5+
"treedepth__",0.9,0.1,0.316228,0,0.45,1,1,nan,nan,nan
6+
"x[1]",0.608916,0.0690549,0.218371,0.108465,0.284524,0.666485,0.866975,5,5,0.905216
7+
"x[2]",0.479369,0.051013,0.161317,0.164219,0.263389,0.451793,0.686842,5,5,1.00156
8+
"y[1,1]",0.425556,0.124575,0.393941,0.150819,0.0588995,0.164637,0.926039,5,5,1.431
9+
"y[1,2]",0.595682,0.0758338,0.239808,0.178212,0.249647,0.663856,0.850511,5,5,1.11989
10+
"y[1,3]",0.473713,0.104663,0.330973,0.39317,0.0677836,0.483405,0.863312,5,5,0.986085
11+
"y[2,1]",0.511464,0.0868387,0.274608,0.383569,0.167417,0.487338,0.895304,5,5,1.43105
12+
"y[2,2]",0.559192,0.0872593,0.275938,0.307409,0.230475,0.525767,0.932888,5,5,0.915761
13+
"y[2,3]",0.51956,0.0997935,0.315575,0.212694,0.0361943,0.536618,0.930452,5,5,1.32719
14+
# Inference for Stan model: issue_342_model
15+
# 1 chains: each with iter=10; warmup=1000; thin=1; 10 iterations saved.
16+
#
17+
# Warmup took 0.23 seconds
18+
# Sampling took 0.0018 seconds
19+
# Samples were drawn using hmc with nuts.
20+
# For each parameter, ESS_bulk and ESS_tail measure the effective sample size
21+
for the entire sample (bulk) and for the the .05 and .95 tails (tail),
22+
# and R_hat measures the potential scale reduction on split chains.
23+
At convergence R_hat will be very close to 1.00.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Inference for Stan model: issue_342_model
2+
1 chains: each with iter=10; warmup=1000; thin=1; 10 iterations saved.
3+
4+
Warmup took 0.23 seconds
5+
Sampling took 0.0018 seconds
6+
7+
Mean MCSE StdDev MAD 5% 50% 95% ESS_bulk ESS_tail R_hat
8+
9+
lp__ -15 1.2 3.7 0.78 -21 -15 -13 5.0 5.0 1.3
10+
accept_stat__ 0.69 0.096 3.0e-01 0.25 0.21 0.78 0.99 5.0 5.0 1.1
11+
stepsize__ 0.89 nan 1.2e-16 0.00 0.89 0.89 0.89 nan nan nan
12+
treedepth__ 0.90 0.10 3.2e-01 0.00 0.45 1.0 1.0 nan nan nan
13+
14+
x[1] 0.61 0.069 0.22 0.11 0.28 0.67 0.87 5.0 5.0 0.91
15+
x[2] 0.48 0.051 0.16 0.16 0.26 0.45 0.69 5.0 5.0 1.0
16+
y[1,1] 0.43 0.12 0.39 0.15 0.059 0.16 0.93 5.0 5.0 1.4
17+
y[1,2] 0.60 0.076 0.24 0.18 0.25 0.66 0.85 5.0 5.0 1.1
18+
y[1,3] 0.47 0.10 0.33 0.39 0.068 0.48 0.86 5.0 5.0 0.99
19+
y[2,1] 0.51 0.087 0.27 0.38 0.17 0.49 0.90 5.0 5.0 1.4
20+
y[2,2] 0.56 0.087 0.28 0.31 0.23 0.53 0.93 5.0 5.0 0.92
21+
y[2,3] 0.52 0.100 0.32 0.21 0.036 0.54 0.93 5.0 5.0 1.3
22+
23+
Samples were drawn using hmc with nuts.
24+
For each parameter, ESS_bulk and ESS_tail measure the effective sample size
25+
for the entire sample (bulk) and for the the .05 and .95 tails (tail),
26+
and R_hat measures the potential scale reduction on split chains.
27+
At convergence R_hat will be very close to 1.00.

src/test/interface/stansummary_test.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,65 @@ TEST(CommandStansummary, check_csv_output_include_param) {
685685
if (return_code != 0)
686686
FAIL();
687687
}
688+
689+
TEST(CommandStansummary, check_reorder_stats) {
690+
std::string path_separator;
691+
path_separator.push_back(get_path_separator());
692+
std::string csv_file = "src" + path_separator + "test" + path_separator
693+
+ "interface" + path_separator + "matrix_output.csv";
694+
std::stringstream ss_command;
695+
ss_command << "bin" << path_separator << "stansummary " << csv_file;
696+
run_command_output out = run_command(ss_command.str());
697+
ASSERT_FALSE(out.hasError);
698+
699+
std::string expected_file = "src" + path_separator + "test" + path_separator
700+
+ "interface" + path_separator + "example_output"
701+
+ path_separator + "matrix_summary.nom";
702+
std::ifstream expected_output(expected_file.c_str());
703+
EXPECT_FALSE(expected_output.bad());
704+
std::stringstream ss;
705+
ss << expected_output.rdbuf();
706+
EXPECT_EQ(ss.str(), out.output);
707+
}
708+
709+
TEST(CommandStansummary, check_reorder_stats_csv) {
710+
std::string path_separator;
711+
path_separator.push_back(get_path_separator());
712+
std::string target_csv_file = "test" + path_separator + "interface"
713+
+ path_separator
714+
+ "tmp_test_target_csv_file_e.csv";
715+
std::string csv_file = "src" + path_separator + "test" + path_separator
716+
+ "interface" + path_separator + "matrix_output.csv";
717+
std::stringstream ss_command;
718+
ss_command << "bin" << path_separator << "stansummary "
719+
<< "-c " << target_csv_file << " " << csv_file;
720+
run_command_output out = run_command(ss_command.str());
721+
ASSERT_FALSE(out.hasError);
722+
723+
std::string expected_file = "src" + path_separator + "test" + path_separator
724+
+ "interface" + path_separator + "example_output"
725+
+ path_separator + "matrix_summary.csv";
726+
std::ifstream expected_output(expected_file.c_str());
727+
EXPECT_FALSE(expected_output.bad());
728+
std::stringstream ss_expected;
729+
ss_expected << expected_output.rdbuf();
730+
731+
std::ifstream target_stream(target_csv_file.c_str());
732+
if (!target_stream.is_open()) {
733+
std::cerr << "Failed to open file: " << target_csv_file << "\n";
734+
std::cerr << "Error: " << std::strerror(errno) << std::endl;
735+
FAIL();
736+
}
737+
std::stringstream ss_actual;
738+
ss_actual << target_stream.rdbuf();
739+
target_stream.close();
740+
741+
EXPECT_EQ(ss_expected.str(), ss_actual.str());
742+
743+
int return_code = std::remove(target_csv_file.c_str());
744+
if (return_code != 0) {
745+
std::cerr << "Failed to remove file: " << target_csv_file << "\n";
746+
std::cerr << "Error: " << std::strerror(errno) << std::endl;
747+
FAIL();
748+
}
749+
}

0 commit comments

Comments
 (0)