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

Issue/3200 pathfinder diagnostic #3202

Merged
merged 52 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b433885
Merge branch 'develop' of https://github.com/stan-dev/stan into develop
mitzimorris Jun 30, 2023
782321d
replace boost::lexical_cast
mitzimorris Jun 30, 2023
bc7db02
get rid of boost::lexical_cast, add optional newline to json_writer e…
mitzimorris Jun 30, 2023
cf4d0f2
add optional newline to json_writer begin_record
mitzimorris Jun 30, 2023
dc3e4e7
checkpointing, passes unit tests
mitzimorris Jun 30, 2023
0c50ee1
checkpointing, passes unit tests
mitzimorris Jun 30, 2023
326bc78
newlines tweaks
mitzimorris Jun 30, 2023
791933f
lint fix
mitzimorris Jun 30, 2023
e6cc049
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jun 30, 2023
697cdd3
redo callbacks
mitzimorris Jul 2, 2023
38ca1a4
redo callbacks
mitzimorris Jul 2, 2023
bde8df9
merge fix
mitzimorris Jul 2, 2023
fd1451d
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 2, 2023
4455ef1
fix
mitzimorris Jul 2, 2023
863a03a
merge fix
mitzimorris Jul 2, 2023
7a976a5
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 2, 2023
b834930
Merge branch 'issue/3200-pathfinder-diagnostic' of https://github.com…
mitzimorris Jul 2, 2023
9cf71ee
fix typo
mitzimorris Jul 2, 2023
1c26789
use json_writer in pathfinder unit tests
mitzimorris Jul 2, 2023
e1c4ac4
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 2, 2023
00b4d07
output tweak
mitzimorris Jul 2, 2023
4644f71
Merge branch 'issue/3200-pathfinder-diagnostic' of https://github.com…
mitzimorris Jul 2, 2023
9616add
newline tweaks
mitzimorris Jul 2, 2023
f0858fc
newline tweaks
mitzimorris Jul 2, 2023
ca9bfba
newline tweaks
mitzimorris Jul 2, 2023
2934566
newline tweaks
mitzimorris Jul 2, 2023
2c57777
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 2, 2023
ab0b457
newline tweaks
mitzimorris Jul 2, 2023
7a4e434
newline tweaks
mitzimorris Jul 3, 2023
4ffec26
merge fix
mitzimorris Jul 3, 2023
19e6196
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 3, 2023
a9bb20c
fix doc comments for fn newline
mitzimorris Jul 3, 2023
9f4d32b
Merge branch 'issue/3200-pathfinder-diagnostic' of https://github.com…
mitzimorris Jul 3, 2023
0342612
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 3, 2023
283cb9c
simplify
mitzimorris Jul 4, 2023
59885de
merge fix
mitzimorris Jul 4, 2023
e8b757c
add newline between records
mitzimorris Jul 6, 2023
0897ac4
Merge commit '33642c2299b68ea7ce56b728ccf2e168d949de65' into HEAD
yashikno Jul 6, 2023
a635d11
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 6, 2023
3f1cdb3
more unit tests, more fixes
mitzimorris Jul 6, 2023
db790b7
merge fix
mitzimorris Jul 6, 2023
1d734ab
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 6, 2023
fc36322
validate json diagnostics
mitzimorris Jul 6, 2023
3312931
Merge branch 'issue/3200-pathfinder-diagnostic' of https://github.com…
mitzimorris Jul 6, 2023
957770a
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 6, 2023
0275977
backout lexical_cast changes
mitzimorris Jul 8, 2023
e4a1d48
Merge branch 'issue/3200-pathfinder-diagnostic' of https://github.com…
mitzimorris Jul 8, 2023
419ac45
changes per code review, more unit tests
mitzimorris Jul 8, 2023
dd620c6
Merge commit 'f94e9bbd67132857afcff1dadeb6ee1cb19bcbb8' into HEAD
yashikno Jul 8, 2023
c5bc3eb
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 8, 2023
a97891e
Merge branch 'issue/3200-pathfinder-diagnostic' of https://github.com…
mitzimorris Jul 8, 2023
11b5a77
more tests
mitzimorris Jul 8, 2023
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
16 changes: 10 additions & 6 deletions src/stan/callbacks/json_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ class json_writer {
// separator)
int record_depth_ = 0;
// Whether or not the record's parent object needs a comma separator
bool record_needs_comma_;
bool record_needs_comma_ = false;

/**
* Writes a comma separator for the record's parent object if needed.
*/
void write_record_comma_if_needed() {
if (record_depth_ > 0 && record_needs_comma_) {
*output_ << ",";
*output_ << ",\n";
record_needs_comma_ = false;
mitzimorris marked this conversation as resolved.
Show resolved Hide resolved
} else {
write_sep();
}
}

Expand Down Expand Up @@ -265,8 +268,8 @@ class json_writer {
return;
write_record_comma_if_needed();
*output_ << "{";
record_needs_comma_ = false;
record_depth_++;
record_element_needs_comma_ = false;
mitzimorris marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -277,9 +280,9 @@ class json_writer {
if (output_ == nullptr)
return;
write_record_comma_if_needed();
*output_ << "\"" << key << "\": {";
record_needs_comma_ = false;
*output_ << "\"" << key << "\" : {";
record_depth_++;
record_element_needs_comma_ = false;
}
/**
* Writes "}", final token of a JSON record.
Expand All @@ -291,8 +294,9 @@ class json_writer {
record_depth_--;
if (record_depth_ > 0) {
mitzimorris marked this conversation as resolved.
Show resolved Hide resolved
record_needs_comma_ = true;
} else {
*output_ << "\n";
}
record_element_needs_comma_ = false;
}

/**
Expand Down
27 changes: 21 additions & 6 deletions src/stan/callbacks/structured_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@ class structured_writer {
* Virtual destructor.
*/
virtual ~structured_writer() {}

/**
* Writes "{", initial token of a JSON record.
* Writes start token of a structured record.
*/
virtual void begin_record() {}

/**
* Writes "\"key\" : {", initial token of a named JSON record.
* Writes key followed by start token of a structured record.
* @param[in] key The name of the record.
*/
virtual void begin_record(const std::string&) {}
virtual void begin_record(const std::string&, bool newline = false) {}

/**
* Writes "}", final token of a JSON record.
* Writes end token of a structured record.
*/
virtual void end_record() {}

/**
* Writes "[", initial token of a JSON list.
* Writes start token of a list.
*/
virtual void begin_list() {}

/**
* Writes "]", final token of a JSON list.
* Writes end token of a list.
*/
virtual void end_list() {}

Expand All @@ -47,16 +52,19 @@ class structured_writer {
* @param key Name of the value pair
*/
virtual void write(const std::string& key) {}

/**
* Write a key-value pair where the value is a string.
* @param key Name of the value pair
* @param value string to write.
*/
virtual void write(const std::string& key, const std::string& value) {}

/**
* No-op
*/
virtual void write() {}

/**
* Write a key-value pair where the value is a bool.
* @param key Name of the value pair
Expand All @@ -70,18 +78,21 @@ class structured_writer {
* @param value int to write.
*/
virtual void write(const std::string& key, int value) {}

/**
* Write a key-value pair where the value is an `std::size_t`.
* @param key Name of the value pair
* @param value `std::size_t` to write.
*/
virtual void write(const std::string& key, std::size_t value) {}

/**
* Write a key-value pair where the value is a double.
* @param key Name of the value pair
* @param value double to write.
*/
virtual void write(const std::string& key, double value) {}

/**
* Write a key-value pair where the value is a complex value.
* @param key Name of the value pair
Expand All @@ -96,25 +107,29 @@ class structured_writer {
*/
virtual void write(const std::string& key, const std::vector<double> values) {
}

/**
* Write a key-value pair where the value is a vector of strings to be made a
* list.
* @param key Name of the value pair
* @param values vector of strings to write.
*/
void write(const std::string& key, const std::vector<std::string>& values) {}

/**
* Write a key-value pair where the value is an Eigen Matrix.
* @param key Name of the value pair
* @param mat Eigen Matrix to write.
*/
void write(const std::string& key, const Eigen::MatrixXd& mat) {}

/**
* Write a key-value pair where the value is an Eigen Vector.
* @param key Name of the value pair
* @param vec Eigen Vector to write.
*/
void write(const std::string& key, const Eigen::VectorXd& vec) {}

/**
* Write a key-value pair where the value is a Eigen RowVector.
* @param key Name of the value pair
Expand Down
1 change: 0 additions & 1 deletion src/stan/callbacks/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define STAN_CALLBACKS_WRITER_HPP

#include <stan/math/prim/fun/Eigen.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include <vector>

Expand Down
15 changes: 7 additions & 8 deletions src/stan/io/dump.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <stan/io/validate_dims.hpp>
#include <stan/io/var_context.hpp>
#include <stan/math/prim.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <limits>
#include <map>
Expand Down Expand Up @@ -242,8 +241,8 @@ class dump_reader {
scan_optional_long();
size_t d = 0;
try {
d = boost::lexical_cast<size_t>(buf_);
} catch (const boost::bad_lexical_cast& exc) {
d = std::stoull(buf_);
} catch (const std::exception& exc) {
mitzimorris marked this conversation as resolved.
Show resolved Hide resolved
std::string msg = "value " + buf_ + " beyond array dimension range";
throw std::invalid_argument(msg);
}
Expand All @@ -269,8 +268,8 @@ class dump_reader {
int get_int() {
int n = 0;
try {
n = boost::lexical_cast<int>(buf_);
} catch (const boost::bad_lexical_cast& exc) {
n = std::stol(buf_);
mitzimorris marked this conversation as resolved.
Show resolved Hide resolved
} catch (const std::exception& exc) {
std::string msg = "value " + buf_ + " beyond int range";
throw std::invalid_argument(msg);
}
Expand All @@ -280,17 +279,17 @@ class dump_reader {
double scan_double() {
double x = 0;
try {
x = boost::lexical_cast<double>(buf_);
x = std::stod(buf_);
if (x == 0)
validate_zero_buf(buf_);
} catch (const boost::bad_lexical_cast& exc) {
} catch (const std::exception& exc) {
std::string msg = "value " + buf_ + " beyond numeric range";
throw std::invalid_argument(msg);
}
return x;
}

// scan number stores number or throws bad lexical cast exception
// scan number stores number or throws exception
void scan_number(bool negate_val) {
// must take longest first!
if (scan_chars("Inf")) {
Expand Down
4 changes: 2 additions & 2 deletions src/stan/io/validate_zero_buf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace io {
* operator[](size_t)</code>.
*
* @tparam B Character buffer type
* @throw <code>boost::bad_lexical_cast</code> if the buffer
* @throw <code>std::bad_cast</code> if the buffer
* contains non-zero characters before an exponentiation symbol.
*/
template <typename B>
Expand All @@ -23,7 +23,7 @@ void validate_zero_buf(const B& buf) {
if (buf[i] == 'e' || buf[i] == 'E')
return;
if (buf[i] >= '1' && buf[i] <= '9')
boost::conversion::detail::throw_bad_cast<std::string, double>();
throw std::bad_cast();
}
}

Expand Down
1 change: 0 additions & 1 deletion src/stan/mcmc/hmc/hamiltonians/ps_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <stan/callbacks/writer.hpp>
#include <stan/math/prim/fun/Eigen.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include <vector>

Expand Down
1 change: 0 additions & 1 deletion src/stan/variational/advi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <stan/variational/families/normal_fullrank.hpp>
#include <stan/variational/families/normal_meanfield.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/lexical_cast.hpp>
#include <algorithm>
#include <chrono>
#include <limits>
Expand Down
38 changes: 26 additions & 12 deletions src/test/unit/callbacks/json_writer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,30 @@ class StanInterfaceCallbacksJsonWriter : public ::testing::Test {
stan::callbacks::json_writer<std::stringstream, deleter_noop> writer;
};

TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end) {
TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record) {
writer.begin_record();
writer.end_record();
EXPECT_EQ("{}", ss.str());
EXPECT_EQ("{}\n", ss.str());
}

TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_nested) {
std::string key("key");
std::string value("value");
writer.begin_record();
writer.begin_record("1");
writer.write(key, value);
writer.end_record();
writer.begin_record("2");
writer.write(key, value);
writer.begin_record("2.1");
writer.write(key, value);
writer.end_record();
writer.end_record();
writer.end_record();
EXPECT_EQ(
"{\"1\" : {\"key\" : \"value\"},\n\"2\" : {\"key\" : \"value\","
" \"2.1\" : {\"key\" : \"value\"}}}\n",
ss.str());
}

TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) {
Expand Down Expand Up @@ -58,7 +78,7 @@ TEST_F(StanInterfaceCallbacksJsonWriter, single_member) {
writer.begin_record();
writer.write(key, value);
writer.end_record();
EXPECT_EQ("{\"key\" : \"value\"}", ss.str());
EXPECT_EQ("{\"key\" : \"value\"}\n", ss.str());
}

TEST_F(StanInterfaceCallbacksJsonWriter, more_members) {
Expand All @@ -68,18 +88,12 @@ TEST_F(StanInterfaceCallbacksJsonWriter, more_members) {
writer.write(key, value);
writer.write(key, value);

EXPECT_EQ(
"{\"key\" : \"value\""
", \"key\" : \"value\"",
ss.str());
EXPECT_EQ("{\"key\" : \"value\", \"key\" : \"value\"", ss.str());

writer.write(key, value);
writer.end_record();
EXPECT_EQ(
"{\"key\" : \"value\""
", \"key\" : \"value\""
", \"key\" : \"value\"}",
ss.str());
EXPECT_EQ("{\"key\" : \"value\", \"key\" : \"value\", \"key\" : \"value\"}\n",
ss.str());
}

TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector_precision2) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/unit/io/validate_zero_buf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ TEST(ioValidateZeroBuf, tester) {
EXPECT_NO_THROW(validate_zero_buf(s));

s = "1.0";
EXPECT_THROW(validate_zero_buf(s), boost::bad_lexical_cast);
EXPECT_THROW(validate_zero_buf(s), std::bad_cast);

s = "1e1";
EXPECT_THROW(validate_zero_buf(s), boost::bad_lexical_cast);
EXPECT_THROW(validate_zero_buf(s), std::bad_cast);
}
6 changes: 3 additions & 3 deletions src/test/unit/services/pathfinder/eight_schools_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stan/callbacks/json_writer.hpp>
#include <stan/callbacks/stream_writer.hpp>
#include <stan/callbacks/structured_writer.hpp>
#include <stan/callbacks/json_writer.hpp>
#include <stan/math.hpp>
#include <stan/io/array_var_context.hpp>
#include <stan/io/empty_var_context.hpp>
Expand Down Expand Up @@ -82,8 +82,8 @@ TEST_F(ServicesPathfinderEightSchools, multi) {
std::unique_ptr<std::ostream> empty_ostream(nullptr);
stan::test::test_logger logger(std::move(empty_ostream));
std::vector<stan::callbacks::writer> single_path_parameter_writer(num_paths);
std::vector<stan::callbacks::structured_writer> single_path_diagnostic_writer(
num_paths);
std::vector<stan::callbacks::json_writer<std::stringstream>>
single_path_diagnostic_writer(num_paths);
std::vector<std::unique_ptr<decltype(init_init_context())>> single_path_inits;
for (int i = 0; i < num_paths; ++i) {
single_path_inits.emplace_back(
Expand Down
3 changes: 3 additions & 0 deletions src/test/unit/services/pathfinder/normal_glm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <test/unit/services/instrumented_callbacks.hpp>
#include <test/unit/services/pathfinder/util.hpp>
#include <test/unit/services/util.hpp>
#include <rapidjson/document.h>
#include <gtest/gtest.h>

// Locally tests can use threads but for jenkins we should just use 1 thread
Expand Down Expand Up @@ -131,6 +132,8 @@ TEST_F(ServicesPathfinderGLM, single) {
for (int i = 2; i < all_mean_vals.cols(); ++i) {
EXPECT_NEAR(0, all_sd_vals(2, i), .1);
}
rapidjson::Document document;
ASSERT_FALSE(document.Parse<0>(diagnostic_ss.str().c_str()).HasParseError());
}

TEST_F(ServicesPathfinderGLM, multi) {
Expand Down