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>
109 std::string
var_clean = Common::RemoveQuotationSymbol(Common::Trim(
var_id));
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) {
182 inline Eigen::MatrixXd&
GetData() {
return data_; }
185 Eigen::MatrixXd data_;
260 inline Eigen::VectorXd&
GetData() {
return data_; }
263 Eigen::VectorXd data_;
289 has_covariates_ =
true;
312 has_var_weights_ =
true;
322 num_observations_ = covariates_.
NumRows();
323 num_covariates_ = covariates_.
NumCols();
324 has_covariates_ =
true;
345 has_var_weights_ =
true;
431 CHECK(has_var_weights_);
471 CHECK(has_var_weights_);
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_++;
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) {
504 ColumnMatrix covariates_;
506 ColumnVector var_weights_;
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};
549 has_var_weights_ =
true;
585 CHECK(has_var_weights_);
604 CHECK(has_group_labels_);
619 has_group_labels_ =
true;
632 has_group_labels_ =
true;
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
static void GFRSampleOneIter(TreeEnsemble &active_forest, ForestTracker &tracker, ForestContainer &forests, LeafModel &leaf_model, ForestDataset &dataset, ColumnVector &residual, TreePrior &tree_prior, std::mt19937 &gen, std::vector< double > &variable_weights, std::vector< int > &sweep_update_indices, double global_variance, std::vector< FeatureType > &feature_types, int cutpoint_grid_size, bool keep_forest, bool pre_initialized, bool backfitting, int num_features_subsample, int num_threads, LeafSuffStatConstructorArgs &... leaf_suff_stat_args)
Definition tree_sampler.h:816
A collection of random number generation utilities.
Definition bart.h:15