5#ifndef STOCHTREE_DATA_H_
6#define STOCHTREE_DATA_H_
9#include <stochtree/io.h>
10#include <stochtree/log.h>
11#include <stochtree/meta.h>
35 std::vector<int>& column_indices, Eigen::MatrixXd& data,
36 data_size_t num_rows) {
37 std::vector<std::pair<int, double>> oneline_features;
38 auto& ref_text_data = *text_data;
41 for (data_size_t i = 0; i < num_rows; ++i) {
43 oneline_features.clear();
44 parser->ParseOneLine(ref_text_data[i].c_str(), &oneline_features);
47 ref_text_data[i].clear();
50 int feature_counter = 0;
51 for (
auto& inner_data : oneline_features) {
52 int feature_idx = inner_data.first;
53 column_matched = (std::find(column_indices.begin(), column_indices.end(), feature_idx) != column_indices.end());
55 data(i, feature_counter) = inner_data.second;
76 int column_index, Eigen::VectorXd& data, data_size_t num_rows) {
77 std::vector<std::pair<int, double>> oneline_features;
78 auto& ref_text_data = *text_data;
80 for (data_size_t i = 0; i < num_rows; ++i) {
82 oneline_features.clear();
83 parser->ParseOneLine(ref_text_data[i].c_str(), &oneline_features);
86 ref_text_data[i].clear();
89 for (
auto& inner_data : oneline_features) {
90 int feature_idx = inner_data.first;
91 if (column_index == feature_idx) {
92 data(i) = inner_data.second;
100static inline std::vector<std::string> LoadTextDataToMemory(
const char* filename,
int* num_global_data,
bool header) {
101 size_t file_load_progress_interval_bytes = size_t(10) * 1024 * 1024 * 1024;
102 TextReader<data_size_t> text_reader(filename, header, file_load_progress_interval_bytes);
104 *num_global_data = text_reader.ReadAllLines();
105 return std::move(text_reader.Lines());
108static inline void FeatureUnpack(std::vector<int>& categorical_variables,
const char* var_id) {
109 std::string var_clean = Common::RemoveQuotationSymbol(Common::Trim(var_id));
111 bool success = Common::AtoiAndCheck(var_clean.c_str(), &out);
113 categorical_variables.push_back(out);
115 Log::Warning(
"Parsed variable index %s cannot be cast to an integer", var_clean.c_str());
119static inline std::vector<int> Str2FeatureVec(
const char* parameters) {
120 std::vector<int> feature_vec;
121 auto args = Common::Split(parameters,
",");
122 for (
const auto& arg : args) {
123 FeatureUnpack(feature_vec, Common::Trim(arg).c_str());
142 ColumnMatrix(
double* data_ptr, data_size_t num_row,
int num_col,
bool is_row_major);
151 ColumnMatrix(std::string filename, std::string column_index_string,
bool header =
true,
bool precise_float_parser =
false);
159 double GetElement(data_size_t row_num,
int col_num) {
return data_(row_num, col_num); }
167 void SetElement(data_size_t row_num,
int col_num,
double value) { data_(row_num, col_num) = value; }
176 void LoadData(
double* data_ptr, data_size_t num_row,
int num_col,
bool is_row_major);
178 inline data_size_t
NumRows() {
return data_.rows(); }
182 inline Eigen::MatrixXd&
GetData() {
return data_; }
185 Eigen::MatrixXd data_;
211 ColumnVector(std::string filename,
int column_index,
bool header =
true,
bool precise_float_parser =
false);
225 void SetElement(data_size_t row,
double value) { data_(row) = value; }
232 void LoadData(
double* data_ptr, data_size_t num_row);
258 inline data_size_t
NumRows() {
return data_.size(); }
260 inline Eigen::VectorXd&
GetData() {
return data_; }
263 Eigen::VectorXd data_;
264 void UpdateData(
double* data_ptr, data_size_t num_row, std::function<
double(
double,
double)> op);
285 void AddCovariates(
double* data_ptr, data_size_t num_row,
int num_col,
bool is_row_major) {
286 covariates_ =
ColumnMatrix(data_ptr, num_row, num_col, is_row_major);
287 num_observations_ = num_row;
288 num_covariates_ = num_col;
289 has_covariates_ =
true;
299 void AddBasis(
double* data_ptr, data_size_t num_row,
int num_col,
bool is_row_major) {
300 basis_ =
ColumnMatrix(data_ptr, num_row, num_col, is_row_major);
301 num_basis_ = num_col;
312 has_var_weights_ =
true;
320 void AddCovariatesFromCSV(std::string filename, std::string column_index_string,
bool header =
true,
bool precise_float_parser =
false) {
321 covariates_ =
ColumnMatrix(filename, column_index_string, header, precise_float_parser);
322 num_observations_ = covariates_.NumRows();
323 num_covariates_ = covariates_.NumCols();
324 has_covariates_ =
true;
332 void AddBasisFromCSV(std::string filename, std::string column_index_string,
bool header =
true,
bool precise_float_parser =
false) {
333 basis_ =
ColumnMatrix(filename, column_index_string, header, precise_float_parser);
334 num_basis_ = basis_.NumCols();
344 var_weights_ =
ColumnVector(filename, column_index, header, precise_float_parser);
345 has_var_weights_ =
true;
365 inline double CovariateValue(data_size_t row,
int col) {
return covariates_.GetElement(row, col); }
372 inline double BasisValue(data_size_t row,
int col) {
return basis_.GetElement(row, col); }
378 inline double VarWeightValue(data_size_t row) {
return var_weights_.GetElement(row); }
390 inline Eigen::MatrixXd&
GetBasis() {
return basis_.GetData(); }
405 void UpdateBasis(
double* data_ptr, data_size_t num_row,
int num_col,
bool is_row_major) {
407 CHECK_EQ(num_col, num_basis_);
410 for (data_size_t i = 0; i < num_row; ++i) {
411 for (
int j = 0; j < num_col; ++j) {
414 temp_value =
static_cast<double>(*(data_ptr +
static_cast<data_size_t
>(num_col) * i + j));
417 temp_value =
static_cast<double>(*(data_ptr +
static_cast<data_size_t
>(num_row) * j + i));
419 basis_.SetElement(i, j, temp_value);
431 CHECK(has_var_weights_);
434 for (data_size_t i = 0; i < num_row; ++i) {
436 temp_value = std::exp(
static_cast<double>(*(data_ptr + i)));
438 temp_value =
static_cast<double>(*(data_ptr + i));
439 var_weights_.SetElement(i, temp_value);
450 covariates_.SetElement(row_id, col, new_value);
461 basis_.SetElement(row_id, col, new_value);
471 CHECK(has_var_weights_);
473 var_weights_.SetElement(row_id, std::exp(new_value));
475 var_weights_.SetElement(row_id, new_value);
482 if (!has_auxiliary_data_) has_auxiliary_data_ =
true;
483 auxiliary_data_.resize(num_auxiliary_dims_ + 1);
484 auxiliary_data_[num_auxiliary_dims_].resize(dim_size);
485 num_auxiliary_dims_++;
487 double GetAuxiliaryDataValue(
int dim_idx, data_size_t element_idx) {
488 return auxiliary_data_[dim_idx][element_idx];
490 void SetAuxiliaryDataValue(
int dim_idx, data_size_t element_idx,
double value) {
491 auxiliary_data_[dim_idx][element_idx] = value;
493 std::vector<double>& GetAuxiliaryDataVector(
int dim_idx) {
494 return auxiliary_data_[dim_idx];
496 const std::vector<double>& GetAuxiliaryDataVectorConst(
int dim_idx) {
497 return auxiliary_data_[dim_idx];
499 bool HasAuxiliaryDimension(
int dim_idx) {
500 return (num_auxiliary_dims_ > dim_idx) & (dim_idx >= 0);
504 ColumnMatrix covariates_;
506 ColumnVector var_weights_;
507 data_size_t num_observations_{0};
508 int num_covariates_{0};
510 bool has_covariates_{
false};
511 bool has_basis_{
false};
512 bool has_var_weights_{
false};
517 std::vector<std::vector<double>> auxiliary_data_;
518 int num_auxiliary_dims_{0};
519 bool has_auxiliary_data_{
false};
536 void AddBasis(
double* data_ptr, data_size_t num_row,
int num_col,
bool is_row_major) {
537 basis_ =
ColumnMatrix(data_ptr, num_row, num_col, is_row_major);
538 num_basis_ = num_col;
549 has_var_weights_ =
true;
559 void UpdateBasis(
double* data_ptr, data_size_t num_row,
int num_col,
bool is_row_major) {
561 CHECK_EQ(num_col, num_basis_);
564 for (data_size_t i = 0; i < num_row; ++i) {
565 for (
int j = 0; j < num_col; ++j) {
568 temp_value =
static_cast<double>(*(data_ptr +
static_cast<data_size_t
>(num_col) * i + j));
571 temp_value =
static_cast<double>(*(data_ptr +
static_cast<data_size_t
>(num_row) * j + i));
573 basis_.SetElement(i, j, temp_value);
585 CHECK(has_var_weights_);
588 for (data_size_t i = 0; i < num_row; ++i) {
590 temp_value = std::exp(
static_cast<double>(*(data_ptr + i)));
592 temp_value =
static_cast<double>(*(data_ptr + i));
593 var_weights_.SetElement(i, temp_value);
604 CHECK(has_group_labels_);
607 for (data_size_t i = 0; i < num_row; ++i) {
608 group_labels_[i] = group_labels[i];
618 group_labels_ = group_labels;
619 has_group_labels_ =
true;
628 group_labels_.resize(num_rows);
629 for (
int i = 0; i < num_rows; ++i) {
630 group_labels_[i] = group_labels[i];
632 has_group_labels_ =
true;
650 inline double BasisValue(data_size_t row,
int col) {
return basis_.GetElement(row, col); }
656 inline double VarWeightValue(data_size_t row) {
return var_weights_.GetElement(row); }
662 inline int GroupId(data_size_t row) {
return group_labels_[row]; }
668 inline Eigen::MatrixXd&
GetBasis() {
return basis_.GetData(); }
685 std::vector<int> group_labels_;
687 bool has_basis_{
false};
688 bool has_var_weights_{
false};
689 bool has_group_labels_{
false};
Internal wrapper around Eigen::MatrixXd interface for multidimensional floating point data.
Definition data.h:131
data_size_t NumRows()
Number of rows in the object's internal Eigen::MatrixXd.
Definition data.h:178
void SetElement(data_size_t row_num, int col_num, double value)
Update an observation in the object's internal Eigen::MatrixXd to a new value.
Definition data.h:167
int NumCols()
Number of columns in the object's internal Eigen::MatrixXd.
Definition data.h:180
ColumnMatrix(std::string filename, std::string column_index_string, bool header=true, bool precise_float_parser=false)
Construct a new ColumnMatrix object from CSV file.
Eigen::MatrixXd & GetData()
Return a reference to the object's internal Eigen::MatrixXd, for interfaces that require a raw matrix...
Definition data.h:182
void LoadData(double *data_ptr, data_size_t num_row, int num_col, bool is_row_major)
Update the data in a ColumnMatrix object from an in-memory data buffer. This will erase the existing ...
double GetElement(data_size_t row_num, int col_num)
Returns the value stored at (row, col) in the object's internal Eigen::MatrixXd.
Definition data.h:159
ColumnMatrix(double *data_ptr, data_size_t num_row, int num_col, bool is_row_major)
Construct a new ColumnMatrix object from in-memory data buffer.
Internal wrapper around Eigen::VectorXd interface for univariate floating point data....
Definition data.h:193
void SetElement(data_size_t row, double value)
Returns the value stored at position row in the object's internal Eigen::VectorXd.
Definition data.h:225
void LoadData(double *data_ptr, data_size_t num_row)
Update the data in a ColumnVector object from an in-memory data buffer. This will erase the existing ...
void OverwriteData(double *data_ptr, data_size_t num_row)
Update the data in a ColumnVector object from an in-memory data buffer, by substituting each value ob...
void SubtractFromData(double *data_ptr, data_size_t num_row)
Update the data in a ColumnVector object from an in-memory data buffer, by subtracting each value obt...
ColumnVector(double *data_ptr, data_size_t num_row)
Construct a new ColumnVector object from in-memory data buffer.
Eigen::VectorXd & GetData()
Return a reference to the object's internal Eigen::VectorXd, for interfaces that require a raw vector...
Definition data.h:260
data_size_t NumRows()
Number of rows in the object's internal Eigen::VectorXd.
Definition data.h:258
void AddToData(double *data_ptr, data_size_t num_row)
Update the data in a ColumnVector object from an in-memory data buffer, by adding each value obtained...
double GetElement(data_size_t row)
Returns the value stored at position row in the object's internal Eigen::VectorXd.
Definition data.h:218
ColumnVector(std::string filename, int column_index, bool header=true, bool precise_float_parser=false)
Construct a new ColumnMatrix object from CSV file.
API for loading and accessing data used to sample tree ensembles The covariates / bases / weights use...
Definition data.h:272
void AddAuxiliaryDimension(int dim_size)
Auxiliary data management methods Methods to initialize, get, and set auxiliary data for BART models ...
Definition data.h:481
double BasisValue(data_size_t row, int col)
Returns a dataset's basis value stored at (row, col).
Definition data.h:372
void AddVarianceWeights(double *data_ptr, data_size_t num_row)
Copy / load variance weights from raw memory buffer (often pointer to data in a R vector or numpy arr...
Definition data.h:310
data_size_t NumObservations()
Number of observations (rows) in the dataset.
Definition data.h:354
void SetCovariateValue(data_size_t row_id, int col, double new_value)
Update an observation in the internal covariate matrix to a new value.
Definition data.h:449
bool HasCovariates()
Whether or not a ForestDataset has (yet) loaded covariate data.
Definition data.h:348
void SetVarWeightValue(data_size_t row_id, double new_value, bool exponentiate=true)
Update an observation in the internal variance weight vector to a new value.
Definition data.h:470
void AddCovariates(double *data_ptr, data_size_t num_row, int num_col, bool is_row_major)
Copy / load covariates from raw memory buffer (often pointer to data in a R matrix or numpy array).
Definition data.h:285
Eigen::MatrixXd & GetCovariates()
Return a reference to the raw Eigen::MatrixXd storing the covariate data.
Definition data.h:384
Eigen::MatrixXd & GetBasis()
Return a reference to the raw Eigen::MatrixXd storing the basis data.
Definition data.h:390
int NumCovariates()
Number of covariate columns in the dataset.
Definition data.h:356
Eigen::VectorXd & GetVarWeights()
Return a reference to the raw Eigen::VectorXd storing the variance weights.
Definition data.h:396
ForestDataset()
Default constructor. No data is loaded at construction time.
Definition data.h:275
void AddBasisFromCSV(std::string filename, std::string column_index_string, bool header=true, bool precise_float_parser=false)
Copy / load basis matrix from CSV file.
Definition data.h:332
void UpdateBasis(double *data_ptr, data_size_t num_row, int num_col, bool is_row_major)
Update the data in the internal basis matrix to new values stored in a raw double array.
Definition data.h:405
bool HasBasis()
Whether or not a ForestDataset has (yet) loaded basis data.
Definition data.h:350
void AddVarianceWeightsFromCSV(std::string filename, int column_index, bool header=true, bool precise_float_parser=false)
Copy / load variance / case weights from CSV file.
Definition data.h:343
double VarWeightValue(data_size_t row)
Returns a dataset's variance weight stored at element row.
Definition data.h:378
void AddCovariatesFromCSV(std::string filename, std::string column_index_string, bool header=true, bool precise_float_parser=false)
Copy / load covariates from CSV file.
Definition data.h:320
bool HasVarWeights()
Whether or not a ForestDataset has (yet) loaded variance weights.
Definition data.h:352
int NumBasis()
Number of bases in the dataset. This is 0 if the dataset has not been provided a basis matrix.
Definition data.h:358
void AddBasis(double *data_ptr, data_size_t num_row, int num_col, bool is_row_major)
Copy / load basis matrix from raw memory buffer (often pointer to data in a R matrix or numpy array).
Definition data.h:299
double CovariateValue(data_size_t row, int col)
Returns a dataset's covariate value stored at (row, col).
Definition data.h:365
void UpdateVarWeights(double *data_ptr, data_size_t num_row, bool exponentiate=true)
Update the data in the internal variance weight vector to new values stored in a raw double array.
Definition data.h:430
void SetBasisValue(data_size_t row_id, int col, double new_value)
Update an observation in the internal basis matrix to a new value.
Definition data.h:459
API for loading and accessing data used to sample (additive) random effects.
Definition data.h:523
bool HasGroupLabels()
Whether or not a RandomEffectsDataset has (yet) loaded group labels.
Definition data.h:643
void UpdateGroupLabels(std::vector< int > &group_labels, data_size_t num_row)
Update a RandomEffectsDataset's group indices.
Definition data.h:603
std::vector< int > & GetGroupLabels()
Return a reference to the raw std::vector storing the group labels.
Definition data.h:680
RandomEffectsDataset()
Default constructor. No data is loaded at construction time.
Definition data.h:526
void UpdateVarWeights(double *data_ptr, data_size_t num_row, bool exponentiate=true)
Update the data in the internal variance weight vector to new values stored in a raw double array.
Definition data.h:584
void AddVarianceWeights(double *data_ptr, data_size_t num_row)
Copy / load variance weights from raw memory buffer (often pointer to data in a R vector or numpy arr...
Definition data.h:547
bool HasVarWeights()
Whether or not a RandomEffectsDataset has (yet) loaded variance weights.
Definition data.h:641
void AddBasis(double *data_ptr, data_size_t num_row, int num_col, bool is_row_major)
Copy / load basis matrix from raw memory buffer (often pointer to data in a R matrix or numpy array).
Definition data.h:536
data_size_t NumObservations()
Number of observations (rows) in the dataset.
Definition data.h:635
int GroupId(data_size_t row)
Returns a dataset's group label stored at element row.
Definition data.h:662
void UpdateBasis(double *data_ptr, data_size_t num_row, int num_col, bool is_row_major)
Update the data in the internal basis matrix to new values stored in a raw double array.
Definition data.h:559
double BasisValue(data_size_t row, int col)
Returns a dataset's basis value stored at (row, col).
Definition data.h:650
double VarWeightValue(data_size_t row)
Returns a dataset's variance weight stored at element row.
Definition data.h:656
void AddGroupLabels(std::vector< int > &group_labels)
Copy / load group indices for random effects.
Definition data.h:617
int NumBases()
Number of columns of the basis vector in the dataset.
Definition data.h:637
Eigen::MatrixXd & GetBasis()
Return a reference to the raw Eigen::MatrixXd storing the basis data.
Definition data.h:668
Eigen::VectorXd & GetVarWeights()
Return a reference to the raw Eigen::VectorXd storing the variance weights.
Definition data.h:674
void AddGroupLabels(int *group_labels, int num_rows)
Copy / load group indices for random effects.
Definition data.h:627
bool HasBasis()
Whether or not a RandomEffectsDataset has (yet) loaded basis data.
Definition data.h:639
static void ExtractSingleFeatureFromMemory(std::vector< std::string > *text_data, const Parser *parser, int column_index, Eigen::VectorXd &data, data_size_t num_rows)
Extract a single feature from the raw data loaded from a file into an Eigen::VectorXd....
Definition data.h:75
static void ExtractMultipleFeaturesFromMemory(std::vector< std::string > *text_data, const Parser *parser, std::vector< int > &column_indices, Eigen::MatrixXd &data, data_size_t num_rows)
Extract multiple features from the raw data loaded from a file into an Eigen::MatrixXd....
Definition data.h:34
A collection of random number generation utilities.
Definition bart.h:15