Skip to content

Commit c2bc877

Browse files
committed
use csv reader for roach_data
1 parent 10d5012 commit c2bc877

12 files changed

Lines changed: 483 additions & 1048072 deletions

File tree

test/unit/math/laplace/aki_ex_test.cpp

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
#include <stan/math/mix.hpp>
44
#include <test/unit/math/rev/fun/util.hpp>
55
#include <test/unit/math/laplace/laplace_utility.hpp>
6-
#include <test/unit/math/laplace/roach_data/mu_bad.hpp>
7-
#include <test/unit/math/laplace/roach_data/sigma_bad.hpp>
8-
#include <test/unit/math/laplace/roach_data/y_bad.hpp>
9-
#include <test/unit/math/laplace/roach_data/mu.hpp>
106
#include <test/unit/math/laplace/roach_data/sigmaz.hpp>
117
#include <test/unit/math/laplace/roach_data/y.hpp>
8+
#include <test/unit/math/laplace/csv_reader.hpp>
129

1310
#include <gtest/gtest.h>
1411
#include <iostream>
@@ -79,22 +76,20 @@ struct cov_fun_functor {
7976
TEST(WriteArrayBodySimple, ExceededIteration) {
8077
stan::test::relative_tolerance rel_tol(5e-2);
8178
const double integrate_1d_reltol = 1e-8;
82-
auto&& mu_bad_raw = stan::test::laplace::roach::mu_bad_raw;
83-
Eigen::Map<const Eigen::VectorXd> mu_bad(mu_bad_raw.data(),
84-
mu_bad_raw.size());
85-
auto&& sigma_bad_raw = stan::test::laplace::roach::sigma_bad_raw;
86-
Eigen::Map<const Eigen::VectorXd> sigma_bad(sigma_bad_raw.data(),
87-
sigma_bad_raw.size());
88-
auto&& y_bad_raw = stan::test::laplace::roach::y_bad_raw;
89-
Eigen::Map<const Eigen::Matrix<int, -1, 1>> y_bad(y_bad_raw.data(),
90-
y_bad_raw.size());
91-
const int num_samples = mu_bad.cols();
92-
const int N = mu_bad.rows();
79+
auto mu_samples = stan::math::test::laplace::read_matrix_csv(
80+
"./test/unit/math/laplace/roach_data/mu_bad.csv");
81+
auto sigmaz_samples = stan::math::test::laplace::read_matrix_csv(
82+
"./test/unit/math/laplace/roach_data/sigma_bad.csv");
83+
auto y_samples_dbl = stan::math::test::laplace::read_matrix_csv(
84+
"./test/unit/math/laplace/roach_data/y_bad.csv");
85+
auto y_samples = y_samples_dbl.cast<int>();
86+
const int num_samples = mu_samples.cols();
87+
const int N = mu_samples.rows();
9388
std::ostream* pstream = nullptr;
9489
for (int i = 1; i <= N; ++i) {
95-
auto y = y_bad(i - 1);
96-
auto mu = mu_bad(i - 1);
97-
auto sigmaz = sigma_bad(i - 1);
90+
auto y = y_samples(i - 1, 0);
91+
auto mu = mu_samples(i - 1, 0);
92+
auto sigmaz = sigmaz_samples(i - 1, 0);
9893
double ll_laplace_val{0};
9994
try {
10095
ll_laplace_val = stan::math::laplace_marginal(
@@ -129,15 +124,10 @@ TEST(WriteArrayBodySimple, ExceededIteration) {
129124
TEST(WriteArrayBodySimple, ExecutesBodyWithHardcodedData) {
130125
stan::test::relative_tolerance rel_tol(5e-1);
131126
const double integrate_1d_reltol = 1e-8;
132-
auto&& y = stan::test::laplace::roach::y;
133-
auto&& sigmaz_samples = stan::test::laplace::roach::sigmaz;
134-
auto&& mu_raw = stan::test::laplace::roach::mu_raw;
135-
std::size_t mu_cols = mu_raw.size() / y.size();
136-
Eigen::MatrixXd mu_samples(y.size(), mu_cols);
137-
for (std::size_t i = 0; i < y.size(); ++i) {
138-
mu_samples.row(i) = Eigen::Map<const Eigen::RowVectorXd>(
139-
mu_raw.data() + i * mu_cols, mu_cols);
140-
}
127+
auto&& y = stan::math::test::roaches::y;
128+
auto&& sigmaz_samples = stan::math::test::roaches::sigmaz;
129+
auto mu_samples = stan::math::test::laplace::read_matrix_csv(
130+
"./test/unit/math/laplace/roach_data/mu.csv");
141131
const int num_samples = mu_samples.cols();
142132
const int N = mu_samples.rows();
143133
std::ostream* pstream = nullptr;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#pragma once
2+
#include <stan/math/prim/fun/Eigen.hpp> // or <Eigen/Dense>
3+
#include <fstream>
4+
#include <sstream>
5+
#include <stdexcept>
6+
#include <string>
7+
#include <vector>
8+
9+
namespace stan::math::test::laplace {
10+
11+
/**
12+
* Reads a CSV of doubles into a dynamically sized Eigen::Matrix.
13+
*
14+
* @param file_path Path to a CSV file with R-style numeric matrix
15+
* (no header, no row names, comma-separated).
16+
* @return Matrix(rows, cols) filled from the file.
17+
* @throws std::runtime_error on I/O error or inconsistent column counts.
18+
*/
19+
inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> read_matrix_csv(
20+
const std::string& file_path) {
21+
std::ifstream in(file_path);
22+
if (!in.is_open()) {
23+
throw std::runtime_error("Could not open CSV file: " + file_path);
24+
}
25+
26+
std::vector<std::vector<double>> buffer;
27+
std::string line;
28+
while (std::getline(in, line)) {
29+
if (line.empty()) {
30+
continue; // skip blank lines
31+
}
32+
std::vector<double> row;
33+
std::stringstream ss(line);
34+
std::string cell;
35+
while (std::getline(ss, cell, ',')) {
36+
try {
37+
row.push_back(std::stod(cell));
38+
} catch (const std::invalid_argument&) {
39+
throw std::runtime_error("Non-numeric value in CSV at line: " + line);
40+
}
41+
}
42+
if (!buffer.empty() && row.size() != buffer[0].size()) {
43+
throw std::runtime_error("Inconsistent column count in CSV: expected "
44+
+ std::to_string(buffer[0].size()) + " but got "
45+
+ std::to_string(row.size()));
46+
}
47+
buffer.push_back(std::move(row));
48+
}
49+
50+
// If empty file, return a 0×0 matrix
51+
if (buffer.empty()) {
52+
return Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(0, 0);
53+
}
54+
55+
const int rows = static_cast<int>(buffer.size());
56+
const int cols = static_cast<int>(buffer[0].size());
57+
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> mat(rows, cols);
58+
for (int i = 0; i < rows; ++i) {
59+
for (int j = 0; j < cols; ++j) {
60+
mat(i, j) = buffer[i][j];
61+
}
62+
}
63+
return mat;
64+
}
65+
66+
} // namespace stan::math::test::laplace

test/unit/math/laplace/roach_data/mu.csv

Lines changed: 262 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)