Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow standalone generate_quantities using non-HMC fit #1106

Merged
merged 4 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/cmdstan/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,6 @@ context_vector get_vec_var_context(const std::string &file, size_t num_chains) {
return context_vector(num_chains, std::make_shared<dump>(dump(stream)));
}

static constexpr int hmc_fixed_cols
= 7; // hmc sampler outputs columns __lp + 6

namespace internal {

/**
Expand Down Expand Up @@ -571,25 +568,31 @@ int command(int argc, const char *argv[]) {
stream.close();
std::vector<std::string> param_names;
model.constrained_param_names(param_names, false, false);
size_t meta_cols = 0;
for (auto col_name : fitted_params.header) {
if (boost::algorithm::ends_with(col_name, "__")) {
meta_cols++;
} else {
break;
}
}
size_t num_cols = param_names.size();
size_t num_rows = fitted_params.samples.rows();
// check that all parameter names are in sample, in order
if (num_cols + hmc_fixed_cols > fitted_params.header.size()) {
if (num_cols + meta_cols > fitted_params.header.size()) {
msg << "Mismatch between model and fitted_parameters csv file \"" << fname
<< "\"" << std::endl;
throw std::invalid_argument(msg.str());
}
for (size_t i = 0; i < num_cols; ++i) {
if (param_names[i].compare(fitted_params.header[i + hmc_fixed_cols])
!= 0) {
if (param_names[i].compare(fitted_params.header[i + meta_cols]) != 0) {
msg << "Mismatch between model and fitted_parameters csv file \""
<< fname << "\"" << std::endl;
throw std::invalid_argument(msg.str());
}
}
return_code = stan::services::standalone_generate(
model,
fitted_params.samples.block(0, hmc_fixed_cols, num_rows, num_cols),
model, fitted_params.samples.block(0, meta_cols, num_rows, num_cols),
random_seed, interrupt, logger, sample_writers[0]);
} else if (user_method->arg("diagnose")) {
list_argument *test = dynamic_cast<list_argument *>(
Expand Down
84 changes: 84 additions & 0 deletions src/test/interface/generated_quantities_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class CmdStan : public testing::Test {
= {"src", "test", "test-models", "bern_fitted_params.csv"};
bern_fitted_params_warmup
= {"src", "test", "test-models", "bern_fitted_params_warmup.csv"};
bern_optimized_params
= {"src", "test", "test-models", "bern_optimized_params.csv"};
bern_variational_params
= {"src", "test", "test-models", "bern_optimized_params.csv"};
bern_fitted_params_thin
= {"src", "test", "test-models", "bern_fitted_params_thin.csv"};
default_file_path = {"src", "test", "test-models", "output.csv"};
Expand All @@ -34,6 +38,8 @@ class CmdStan : public testing::Test {
std::vector<std::string> bern_data;
std::vector<std::string> bern_fitted_params;
std::vector<std::string> bern_fitted_params_warmup;
std::vector<std::string> bern_optimized_params;
std::vector<std::string> bern_variational_params;
std::vector<std::string> bern_fitted_params_thin;
std::vector<std::string> default_file_path;
std::vector<std::string> dev_null_path;
Expand Down Expand Up @@ -160,6 +166,84 @@ TEST_F(CmdStan, generate_quantities_warmup) {
ASSERT_EQ(fitted_params.samples.rows(), gq_output.samples.rows());
}

TEST_F(CmdStan, generate_quantities_after_optimization) {
std::stringstream ss;
ss << convert_model_path(bern_gq_model)
<< " data file=" << convert_model_path(bern_data)
<< " output file=" << convert_model_path(default_file_path)
<< " method=generate_quantities fitted_params="
<< convert_model_path(bern_optimized_params);
std::string cmd = ss.str();
run_command_output out = run_command(cmd);
ASSERT_FALSE(out.hasError);

std::stringstream msg;
std::string fp_path = convert_model_path(bern_optimized_params);
std::string gq_output_path = convert_model_path(default_file_path);

std::ifstream fp_stream(fp_path.c_str());
stan::io::stan_csv fitted_params;
stan::io::stan_csv_reader::read_metadata(fp_stream, fitted_params.metadata,
&msg);
stan::io::stan_csv_reader::read_header(fp_stream, fitted_params.header, &msg,
false);
stan::io::stan_csv_reader::read_samples(fp_stream, fitted_params.samples,
fitted_params.timing, &msg);
fp_stream.close();

std::ifstream gq_stream(gq_output_path.c_str());
stan::io::stan_csv gq_output;
stan::io::stan_csv_reader::read_samples(gq_stream, gq_output.samples,
gq_output.timing, &msg);
stan::io::stan_csv_reader::read_metadata(gq_stream, gq_output.metadata, &msg);
stan::io::stan_csv_reader::read_header(gq_stream, gq_output.header, &msg,
false);
stan::io::stan_csv_reader::read_samples(gq_stream, gq_output.samples,
gq_output.timing, &msg);
gq_stream.close();

ASSERT_EQ(fitted_params.samples.rows(), gq_output.samples.rows());
}

TEST_F(CmdStan, generate_quantities_after_vb) {
std::stringstream ss;
ss << convert_model_path(bern_gq_model)
<< " data file=" << convert_model_path(bern_data)
<< " output file=" << convert_model_path(default_file_path)
<< " method=generate_quantities fitted_params="
<< convert_model_path(bern_variational_params);
std::string cmd = ss.str();
run_command_output out = run_command(cmd);
ASSERT_FALSE(out.hasError);

std::stringstream msg;
std::string fp_path = convert_model_path(bern_variational_params);
std::string gq_output_path = convert_model_path(default_file_path);

std::ifstream fp_stream(fp_path.c_str());
stan::io::stan_csv fitted_params;
stan::io::stan_csv_reader::read_metadata(fp_stream, fitted_params.metadata,
&msg);
stan::io::stan_csv_reader::read_header(fp_stream, fitted_params.header, &msg,
false);
stan::io::stan_csv_reader::read_samples(fp_stream, fitted_params.samples,
fitted_params.timing, &msg);
fp_stream.close();

std::ifstream gq_stream(gq_output_path.c_str());
stan::io::stan_csv gq_output;
stan::io::stan_csv_reader::read_samples(gq_stream, gq_output.samples,
gq_output.timing, &msg);
stan::io::stan_csv_reader::read_metadata(gq_stream, gq_output.metadata, &msg);
stan::io::stan_csv_reader::read_header(gq_stream, gq_output.header, &msg,
false);
stan::io::stan_csv_reader::read_samples(gq_stream, gq_output.samples,
gq_output.timing, &msg);
gq_stream.close();

ASSERT_EQ(fitted_params.samples.rows(), gq_output.samples.rows());
}

TEST_F(CmdStan, generate_quantities_thin) {
std::stringstream ss;
ss << convert_model_path(bern_gq_model)
Expand Down
35 changes: 35 additions & 0 deletions src/test/test-models/bern_optimized_params.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# stan_version_major = 2
# stan_version_minor = 30
# stan_version_patch = 0
# model = bernoulli_model
# start_datetime = 2022-07-06 18:18:41 UTC
# method = optimize
# optimize
# algorithm = lbfgs (Default)
# lbfgs
# init_alpha = 0.001 (Default)
# tol_obj = 9.9999999999999998e-13 (Default)
# tol_rel_obj = 10000 (Default)
# tol_grad = 1e-08 (Default)
# tol_rel_grad = 10000000 (Default)
# tol_param = 1e-08 (Default)
# history_size = 5 (Default)
# iter = 2000 (Default)
# save_iterations = 0 (Default)
# id = 1 (Default)
# data
# file = bernoulli.data.json
# init = 2 (Default)
# random
# seed = 3689276984 (Default)
# output
# file = output.csv (Default)
# diagnostic_file = (Default)
# refresh = 100 (Default)
# sig_figs = -1 (Default)
# profile_file = profile.csv (Default)
# num_threads = 1 (Default)
# stanc_version = %%NAME%%3 %%VERSION%%
# stancflags =
lp__,theta
-5.00402,0.2
Loading