Skip to content

Commit

Permalink
more cleanup, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mitzimorris committed Oct 3, 2024
1 parent 7704ba4 commit 7f42042
Show file tree
Hide file tree
Showing 4 changed files with 1,121 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/stan/io/stan_csv_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,15 @@ class stan_csv_reader {
}

static bool read_samples(std::istream& in, Eigen::MatrixXd& samples,
stan_csv_timing& timing, std::ostream* out) {
stan_csv_timing& timing) {
std::stringstream ss;
std::string line;

int rows = 0;
int cols = -1;

if (in.peek() == '#' || in.good() == false)
return false;
return false; // need at least one data row

while (in.good()) {
bool comment_line = (in.peek() == '#');
Expand Down Expand Up @@ -375,9 +375,9 @@ class stan_csv_reader {
std::getline(in, line); // discard variational estimate
}

if (!read_samples(in, data.samples, data.timing, out)) {
if (!read_samples(in, data.samples, data.timing)) {
if (out)
*out << "Warning: non-fatal error reading samples" << std::endl;
*out << "Unable to parse sample" << std::endl;
}
return data;
}
Expand Down
22 changes: 21 additions & 1 deletion src/test/unit/io/stan_csv_reader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ TEST_F(StanIoStanCsvReader, read_samples1) {
stan::io::stan_csv_timing timing;

EXPECT_TRUE(stan::io::stan_csv_reader::read_samples(samples1_stream, samples,
timing, 0));
timing));

ASSERT_EQ(5, samples.rows());
ASSERT_EQ(55, samples.cols());
Expand Down Expand Up @@ -569,3 +569,23 @@ TEST_F(StanIoStanCsvReader, fixed_param) {
fixed_param = stan::io::stan_csv_reader::parse(fixed_param_stream, &out);
ASSERT_EQ(10, fixed_param.samples.rows());
}


TEST_F(StanIoStanCsvReader, no_samples) {
std::ifstream no_samples_stream;
no_samples_stream.open("src/test/unit/io/test_csv_files/bernoulli_no_samples.csv");
std::stringstream out;
stan::io::stan_csv no_samples = stan::io::stan_csv_reader::parse(no_samples_stream, &out);
no_samples_stream.close();
ASSERT_EQ(out.str(), "Unable to parse sample\n");
}


TEST_F(StanIoStanCsvReader, variational) {
std::ifstream variational_stream;
variational_stream.open("src/test/unit/io/test_csv_files/bernoulli_variational.csv");
std::stringstream out;
stan::io::stan_csv variational = stan::io::stan_csv_reader::parse(variational_stream, &out);
variational_stream.close();
ASSERT_EQ(1000, variational.metadata.num_samples);
}
57 changes: 57 additions & 0 deletions src/test/unit/io/test_csv_files/bernoulli_no_samples.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# stan_version_major = 2
# stan_version_minor = 35
# stan_version_patch = 0
# model = bernoulli_model
# start_datetime = 2024-10-03 16:54:16 UTC
# method = sample (Default)
# sample
# num_samples = 0
# num_warmup = 1000 (Default)
# save_warmup = false (Default)
# thin = 1 (Default)
# adapt
# engaged = true (Default)
# gamma = 0.05 (Default)
# delta = 0.8 (Default)
# kappa = 0.75 (Default)
# t0 = 10 (Default)
# init_buffer = 75 (Default)
# term_buffer = 50 (Default)
# window = 25 (Default)
# save_metric = false (Default)
# algorithm = hmc (Default)
# hmc
# engine = nuts (Default)
# nuts
# max_depth = 10 (Default)
# metric = diag_e (Default)
# metric_file = (Default)
# stepsize = 1 (Default)
# stepsize_jitter = 0 (Default)
# num_chains = 1 (Default)
# id = 1 (Default)
# data
# file = examples/bernoulli/bernoulli.data.json
# init = 2 (Default)
# random
# seed = 1517767693 (Default)
# output
# file = output.csv (Default)
# diagnostic_file = (Default)
# refresh = 100 (Default)
# sig_figs = -1 (Default)
# profile_file = profile.csv (Default)
# save_cmdstan_config = false (Default)
# num_threads = 1 (Default)
# stanc_version = stanc3 v2.35.0
# stancflags =
lp__,accept_stat__,stepsize__,treedepth__,n_leapfrog__,divergent__,energy__,theta
# Adaptation terminated
# Step size = 1.22726
# Diagonal elements of inverse mass matrix:
# 0.489285
#
# Elapsed Time: 0.005 seconds (Warm-up)
# 0 seconds (Sampling)
# 0.005 seconds (Total)
#
Loading

0 comments on commit 7f42042

Please sign in to comment.