Skip to content
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
1 change: 1 addition & 0 deletions make/tests
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ test/interface/datetime_test$(EXE): $(call depends_on_test_models,
test/interface/elapsed_time_test$(EXE): $(call depends_on_test_models, test_model)
test/interface/fixed_param_sampler_test$(EXE): $(call depends_on_test_models, empty proper)
test/interface/generated_quantities_test$(EXE): $(call depends_on_test_models, bern_gq_model bern_extra_model gq_non_scalar test_model)
test/interface/save_trans_data_json_test$(EXE): $(call depends_on_test_models, gq_non_scalar)
test/interface/laplace_test$(EXE): $(call depends_on_test_models, multi_normal_model simple_jacobian_model simplex_model)
test/interface/log_prob_test$(EXE): $(call depends_on_test_models, bern_log_prob_model bern_gq_model simplex_model)
test/interface/metric_test$(EXE): $(call depends_on_test_models, test_model proper)
Expand Down
2 changes: 2 additions & 0 deletions src/cmdstan/arguments/arg_output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CMDSTAN_ARGUMENTS_ARG_OUTPUT_HPP

#include <cmdstan/arguments/arg_diagnostic_file.hpp>
#include <cmdstan/arguments/arg_transformed_data_file.hpp>
#include <cmdstan/arguments/arg_output_file.hpp>
#include <cmdstan/arguments/arg_output_sig_figs.hpp>
#include <cmdstan/arguments/arg_profile_file.hpp>
Expand All @@ -27,6 +28,7 @@ class arg_output : public categorical_argument {
"Save the CmdStan configuration (parsed arguments + default values) as "
"JSON alongside the output files",
false));
_subarguments.push_back(new arg_transformed_data_file());
}
};

Expand Down
23 changes: 23 additions & 0 deletions src/cmdstan/arguments/arg_transformed_data_file.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef CMDSTAN_ARGUMENTS_ARG_TRANSFORMED_DATA_FILE_HPP
#define CMDSTAN_ARGUMENTS_ARG_TRANSFORMED_DATA_FILE_HPP

#include <cmdstan/arguments/singleton_argument.hpp>

namespace cmdstan {

class arg_transformed_data_file : public string_argument {
public:
arg_transformed_data_file() : string_argument() {
_name = "transformed_data";
_description
= "Auxiliary output file for the contents of the transformed data "
"block, written as JSON.";
_validity = "File should not already exist";
_default = "\"\"";
_default_value = "";
_value = _default_value;
}
};

} // namespace cmdstan
#endif
10 changes: 10 additions & 0 deletions src/cmdstan/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ int command(int argc, const char *argv[]) {
init_null_writers(metric_json_writers, num_chains);
}

// write transformed data file if requested
{
std::string transformed_data_file
= get_arg_val<string_argument>(parser, "output", "transformed_data");
if (!transformed_data_file.empty()) {
auto td_file = file::safe_create(transformed_data_file, sig_figs);
model.write_transformed_data_json(td_file.get());
}
}

// Setup initial parameter values - arg "init"
// arg is either filename or init radius value
std::string init = get_arg_val<string_argument>(parser, "init");
Expand Down
2 changes: 1 addition & 1 deletion src/cmdstan/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ std::ifstream safe_open(const std::string &fname) {
}

/**
* Opens input stream for file and sets exception flags.
* Opens output stream for file and sets exception flags.
*
* @param fname name of file which exists and has read perms.
* @param sig_figs number of digits to print
Expand Down
46 changes: 46 additions & 0 deletions src/test/interface/save_trans_data_json_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <test/utility.hpp>
#include <fstream>
#include <gtest/gtest.h>

using cmdstan::test::convert_model_path;
using cmdstan::test::count_matches;
using cmdstan::test::file_exists;
using cmdstan::test::is_valid_JSON;
using cmdstan::test::run_command;
using cmdstan::test::run_command_output;

class CmdStanTDJson : public testing::Test {
public:
void SetUp() {
gq_non_scalar_model = {"src", "test", "test-models", "gq_non_scalar"};
output_json = {"test", "transformed_data.json"};
}

void TearDown() { std::remove(convert_model_path(output_json).c_str()); }

std::vector<std::string> gq_non_scalar_model;
std::vector<std::string> output_json;
};

TEST_F(CmdStanTDJson, save) {
std::stringstream ss;
ss << convert_model_path(gq_non_scalar_model) << " random seed=1234"
<< " method=sample output transformed_data="
<< convert_model_path(output_json) << " 2>&1";
std::string cmd = ss.str();
run_command_output out = run_command(cmd);
ASSERT_FALSE(out.hasError);
ASSERT_TRUE(file_exists(convert_model_path(output_json)));

std::fstream result_stream(convert_model_path(output_json));
std::stringstream result_sstream;
result_sstream << result_stream.rdbuf();
result_stream.close();
std::string transformed_data = result_sstream.str();
ASSERT_TRUE(is_valid_JSON(transformed_data));

EXPECT_EQ(count_matches("X", transformed_data), 1);
EXPECT_EQ(count_matches("y", transformed_data), 1);
EXPECT_EQ(count_matches("N", transformed_data), 1);
EXPECT_EQ(count_matches("K", transformed_data), 1);
}