Skip to content

Commit

Permalink
Column major when building defms for data
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Nov 16, 2023
1 parent 04741d5 commit 86977d2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
27 changes: 20 additions & 7 deletions include/barry/models/defm/counters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,30 @@ inline void counter_transition(
{

auto indices = data.indices;

for (size_t i = 0u; i < (indices.size() - 1u); ++i)
auto sgn = data.logical;
int covaridx = indices[indices.size() - 1u];

// Notice that the indices vector contains:
// - 1st, the indices of the motif. That's why we set the lenght
// using -1.
// - the last is, the covariate index
for (size_t k = 0u; k < (indices.size() - 1u); ++k)
{
if (
std::floor(indices[i] / Array.nrow()) >=
static_cast<int>(Array.ncol())
)
if (indices[k] >= (Array.ncol()* Array.nrow()))
throw std::range_error("The motif includes entries out of range.");
}

// Counting
const auto & array = Array.get_data();
for (size_t k = 0u; k < (indices.size() - 1); ++k)
{
auto cellv = array[indices[k]];
if (sgn[k] && (cellv != 1))
return 0.0;
}

return 0.0;
// If nothing happens, then is one or the covaridx
return (covaridx < 1000) ? Array.D()(Array.nrow() - 1u, covaridx) : 1.0;

};

Expand Down
15 changes: 12 additions & 3 deletions include/barry/models/defm/defm-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ inline void DEFM::simulate(

// Setting the data
tmp_array.set_data(
new DEFMData(&tmp_array, X, (start_i + proc_n), X_ncol, ID_length),
new DEFMData(
&tmp_array, X, (start_i + proc_n), X_ncol, ID_length,
this->column_major
),
true // Delete the data
);

Expand Down Expand Up @@ -253,7 +256,10 @@ inline void DEFM::init()
// Creating the array for process n_proc and setting the data
DEFMArray array(M_order + 1u, Y_ncol);
array.set_data(
new DEFMData(&array, X, (start_i + n_proc), X_ncol, ID_length),
new DEFMData(
&array, X, (start_i + n_proc), X_ncol, ID_length,
this->column_major
),
true // Delete the data
);

Expand Down Expand Up @@ -377,7 +383,10 @@ inline std::vector< double > DEFM::logodds(
// Creating the array for process n_proc and setting the data
DEFMArray array(M_order + 1u, Y_ncol);
array.set_data(
new DEFMData(&array, X, (start_i + n_proc), X_ncol, ID_length),
new DEFMData(
&array, X, (start_i + n_proc), X_ncol, ID_length,
this->column_major
),
true // Delete the data
);

Expand Down
13 changes: 10 additions & 3 deletions include/barry/models/defm/defm-types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DEFMData {
size_t X_nrow; ///< Number of rows in the array of covariates.
std::vector< size_t > covar_sort; /// Value where the sorting of the covariates is stored.
std::vector< size_t > covar_used; /// Vector indicating which covariates are included in the model
bool column_major;

DEFMData() {};

Expand All @@ -38,9 +39,10 @@ class DEFMData {
const double * covariates_,
size_t obs_start_,
size_t X_ncol_,
size_t X_nrow_
size_t X_nrow_,
bool column_major_
) : array(array_), covariates(covariates_), obs_start(obs_start_),
X_ncol(X_ncol_), X_nrow(X_nrow_) {};
X_ncol(X_ncol_), X_nrow(X_nrow_), column_major(column_major_) {};

/**
* @brief Access to the row (i) colum (j) data
Expand Down Expand Up @@ -117,7 +119,12 @@ class DEFMRuleData {

inline double DEFMData::operator()(size_t i, size_t j) const
{
return *(covariates + (obs_start + j * X_nrow + i));

if (column_major)
return *(covariates + (obs_start + i + X_nrow * j));
else
return *(covariates + ((obs_start + i) * X_ncol + j));

}

inline size_t DEFMData::ncol() const {
Expand Down
2 changes: 1 addition & 1 deletion tests/15-defm-counts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ BARRY_TEST_CASE("DEFM counts work", "[DEFM counts]") {
};

DEFMArray A1(3, 3);
A1.set_data(new DEFMData(&A1, &covars[0u], 0, 2, 3), true);
A1.set_data(new DEFMData(&A1, &covars[0u], 0, 2, 3, true), true);
A1(0, 0) = 1;
A1(0, 1) = 1;
A1(1, 1) = 1;
Expand Down

0 comments on commit 86977d2

Please sign in to comment.