Skip to content

Commit

Permalink
Merge pull request #43 from USCbiostats/gw-eigen-fix
Browse files Browse the repository at this point in the history
fix handling empty external matrix
  • Loading branch information
gmweaver authored Jun 26, 2024
2 parents 783093b + 248633f commit 14b6395
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/XrnetUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ Eigen::MatrixXd computeResponseRcpp(SEXP X,
double logit_inv(double x) {
return 1 / (1 + std::exp(-x));
}

Eigen::Map<const Eigen::MatrixXd> createEigenMapFromRcppNumericMatrix(const Rcpp::NumericMatrix& mat) {
if (mat.rows() == 0 || mat.cols() == 0) {
Eigen::MatrixXd emptyMat;
return Eigen::Map<const Eigen::MatrixXd>(emptyMat.data(), emptyMat.rows(), emptyMat.cols());
}
return Eigen::Map<const Eigen::MatrixXd>(mat.begin(), mat.rows(), mat.cols());
}
2 changes: 2 additions & 0 deletions src/XrnetUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ void compute_penalty(Eigen::Ref<Eigen::VectorXd> path,

double logit_inv(double x);

Eigen::Map<const Eigen::MatrixXd> createEigenMapFromRcppNumericMatrix(const Rcpp::NumericMatrix& mat);

template <typename TX>
Eigen::MatrixXd computeResponse(const TX & X,
const Eigen::Ref<const Eigen::MatrixXd> & Fixed,
Expand Down
7 changes: 4 additions & 3 deletions src/rcpp_wrappers_fit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "GaussianSolver.h"
#include "BinomialSolver.h"


template <typename TX, typename TZ>
Rcpp::List fitModel(const TX & x,
const bool & is_sparse_x,
Expand Down Expand Up @@ -198,7 +199,7 @@ Rcpp::List fitModelRcpp(SEXP x,
);
else {
Rcpp::NumericMatrix ext_mat(ext);
MapMat extmap((const double *) &ext_mat[0], ext_mat.rows(), ext_mat.cols());
MapMat extmap = createEigenMapFromRcppNumericMatrix(ext);
return fitModel<MapMat, MapMat>(
xmap, is_sparse_x, y, extmap, fixed, weights_user,
intr, stnd, penalty_type, cmult, quantiles, num_penalty,
Expand All @@ -221,7 +222,7 @@ Rcpp::List fitModelRcpp(SEXP x,
}
else {
Rcpp::NumericMatrix ext_mat(ext);
MapMat extmap((const double *) &ext_mat[0], ext_mat.rows(), ext_mat.cols());
MapMat extmap = createEigenMapFromRcppNumericMatrix(ext);
return fitModel<MapMat, MapMat>(
xmap, is_sparse_x, y, extmap, fixed, weights_user, intr, stnd,
penalty_type, cmult, quantiles, num_penalty,
Expand All @@ -241,7 +242,7 @@ Rcpp::List fitModelRcpp(SEXP x,
);
else {
Rcpp::NumericMatrix ext_mat(ext);
MapMat extmap((const double *) &ext_mat[0], ext_mat.rows(), ext_mat.cols());
MapMat extmap = createEigenMapFromRcppNumericMatrix(ext);
return fitModel<MapSpMat, MapMat>(
Rcpp::as<MapSpMat>(x), is_sparse_x, y, extmap, fixed, weights_user,
intr, stnd, penalty_type, cmult, quantiles, num_penalty,
Expand Down

0 comments on commit 14b6395

Please sign in to comment.