Skip to content

Commit

Permalink
fix type conversion in timeEffectMap
Browse files Browse the repository at this point in the history
  • Loading branch information
jianxiaoyang committed Nov 9, 2023
1 parent 18175ff commit a90dca2
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ Suggests:
microbenchmark,
cmprsk,
glmnet
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
21 changes: 16 additions & 5 deletions R/DataManagement.R
Original file line number Diff line number Diff line change
Expand Up @@ -835,18 +835,29 @@ loadNewSqlCyclopsDataTimeEffectsDF <- function(object,
#' @keywords internal
loadNewSqlCyclopsDataTimeInteraction <- function(object,
covariateId,
timeEffectMap) {
timeEffectCovId,
timeEffectTimeId) {

if (!isInitialized(object)) stop("Object is no longer or improperly initialized.")

if (!all(timeEffectMap$covariateId %in% covariateId)) stop("Invalid covariateId for time effects.")
if (!all(timeEffectCovId %in% covariateId)) stop("Invalid covariateId for time effects.")

timeEffectMap$timeEffectId <- timeEffectMap$timeEffectId - 1 # Cpp start from 0
timeEffectTimeId <- timeEffectTimeId - 1 # Cpp start from 0

index <- .loadCyclopsDataTimeInteraction(object, timeEffectMap)
if (!bit64::is.integer64(timeEffectCovId)) {
timeEffectCovId <- bit64::as.integer64(timeEffectCovId)
}

if (!bit64::is.integer64(timeEffectTimeId)) {
timeEffectTimeId <- bit64::as.integer64(timeEffectTimeId)
}

index <- .loadCyclopsDataTimeInteraction(object,
timeEffectCovId,
timeEffectTimeId)

if (!is.null(object$coefficientNames)) {
timeEffectName <- paste0("x", timeEffectMap$covariateId, ":time", timeEffectMap$timeEffectId + 1)
timeEffectName <- paste0("x", timeEffectCovId, ":time", timeEffectTimeId + 1)
object$coefficientNames <- append(object$coefficientNames, timeEffectName)
}
}
Expand Down
6 changes: 4 additions & 2 deletions R/NewDataConversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ convertToCyclopsData.data.frame <- function(outcomes,
if (!all(timeEffectMap$timeEffectId %in% 1:ncol(timeEffects))) stop("Invalid timeEffectId for interaction terms.")
loadNewSqlCyclopsDataTimeInteraction(object = dataPtr,
covariateId = covariates$covariateId,
timeEffectMap = timeEffectMap)
timeEffectCovId = timeEffectMap$covariateId,
timeEffectTimeId = timeEffectMap$timeEffectId)
}
}

Expand Down Expand Up @@ -392,7 +393,8 @@ convertToCyclopsData.tbl_dbi <- function(outcomes,
if (!all(timeEffectMap$timeEffectId %in% 1:ncol(timeEffects))) stop("Invalid timeEffectId for interaction terms.")
loadNewSqlCyclopsDataTimeInteraction(object = dataPtr,
covariateId = covariates$covariateId,
timeEffectMap = timeEffectMap)
timeEffectCovId = timeEffectMap$covariateId,
timeEffectTimeId = timeEffectMap$timeEffectId)
}
}

Expand Down
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ getTimeVector <- function(object) {
.Call(`_Cyclops_cyclopsLoadDataTimeEffectsDF`, x, timeEffects)
}

.loadCyclopsDataTimeInteraction <- function(x, timeEffectMap) {
.Call(`_Cyclops_cyclopsLoadDataTimeInteraction`, x, timeEffectMap)
.loadCyclopsDataTimeInteraction <- function(x, covariateId, timeEffectId) {
.Call(`_Cyclops_cyclopsLoadDataTimeInteraction`, x, covariateId, timeEffectId)
}

.appendSqlCyclopsData <- function(x, oStratumId, oRowId, oY, oTime, cRowId, cCovariateId, cCovariateValue) {
Expand Down
6 changes: 3 additions & 3 deletions man/convertToCyclopsData.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,14 +709,15 @@ BEGIN_RCPP
END_RCPP
}
// cyclopsLoadDataTimeInteraction
int cyclopsLoadDataTimeInteraction(Environment x, DataFrame timeEffectMap);
RcppExport SEXP _Cyclops_cyclopsLoadDataTimeInteraction(SEXP xSEXP, SEXP timeEffectMapSEXP) {
int cyclopsLoadDataTimeInteraction(Environment x, const std::vector<double>& covariateId, const std::vector<double>& timeEffectId);
RcppExport SEXP _Cyclops_cyclopsLoadDataTimeInteraction(SEXP xSEXP, SEXP covariateIdSEXP, SEXP timeEffectIdSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Environment >::type x(xSEXP);
Rcpp::traits::input_parameter< DataFrame >::type timeEffectMap(timeEffectMapSEXP);
rcpp_result_gen = Rcpp::wrap(cyclopsLoadDataTimeInteraction(x, timeEffectMap));
Rcpp::traits::input_parameter< const std::vector<double>& >::type covariateId(covariateIdSEXP);
Rcpp::traits::input_parameter< const std::vector<double>& >::type timeEffectId(timeEffectIdSEXP);
rcpp_result_gen = Rcpp::wrap(cyclopsLoadDataTimeInteraction(x, covariateId, timeEffectId));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -840,7 +841,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_Cyclops_cyclopsLoadDataMultipleX", (DL_FUNC) &_Cyclops_cyclopsLoadDataMultipleX, 8},
{"_Cyclops_cyclopsLoadDataX", (DL_FUNC) &_Cyclops_cyclopsLoadDataX, 7},
{"_Cyclops_cyclopsLoadDataTimeEffectsDF", (DL_FUNC) &_Cyclops_cyclopsLoadDataTimeEffectsDF, 2},
{"_Cyclops_cyclopsLoadDataTimeInteraction", (DL_FUNC) &_Cyclops_cyclopsLoadDataTimeInteraction, 2},
{"_Cyclops_cyclopsLoadDataTimeInteraction", (DL_FUNC) &_Cyclops_cyclopsLoadDataTimeInteraction, 3},
{"_Cyclops_cyclopsAppendSqlData", (DL_FUNC) &_Cyclops_cyclopsAppendSqlData, 8},
{"_Cyclops_cyclopsGetInterceptLabel", (DL_FUNC) &_Cyclops_cyclopsGetInterceptLabel, 1},
{"_Cyclops_cyclopsReadFileData", (DL_FUNC) &_Cyclops_cyclopsReadFileData, 2},
Expand Down
13 changes: 4 additions & 9 deletions src/RcppModelData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,19 +640,14 @@ int cyclopsLoadDataTimeEffectsDF(Environment x,

// [[Rcpp::export(".loadCyclopsDataTimeInteraction")]]
int cyclopsLoadDataTimeInteraction(Environment x,
DataFrame timeEffectMap) {
const std::vector<double>& covariateId,
const std::vector<double>& timeEffectId) {

using namespace bsccs;
XPtr<AbstractModelData> data = parseEnvironmentForPtr(x);

std::vector<std::pair<int, int>> tm;
NumericVector cId = timeEffectMap["covariateId"];
NumericVector tId = timeEffectMap["timeEffectId"];
for (int i = 0; i < timeEffectMap.nrows(); i++) {
tm.push_back(std::make_pair(cId[i], tId[i]));
}

return data->loadTimeInteraction(tm);
return data->loadTimeInteraction(reinterpret_cast<const std::vector<int64_t>&>(covariateId),
reinterpret_cast<const std::vector<int64_t>&>(timeEffectId));
}

// NOTE: IdType does not get exported into RcppExports, so hard-coded here
Expand Down
14 changes: 8 additions & 6 deletions src/cyclops/ModelData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,21 +399,23 @@ int ModelData<RealType>::loadTimeEffectsDF(

template <typename RealType>
int ModelData<RealType>::loadTimeInteraction(
std::vector<std::pair<int, int>>& timeEffectMap) {
const std::vector<int64_t>& oCovariateId,
const std::vector<int64_t>& oTimeEffectId) {

int numOfCov = getNumberOfColumns();
int totalNumOfCov = numOfCov;

// time effect with baseline covariates
for (auto t : timeEffectMap) {
for (size_t i = 0; i < oCovariateId.size(); i++) {
// auto sharedPtrToColumnsVector = make_shared<IntVector>(column.getColumnsVector());
X.push_back(X.getColumn(t.first).getColumnsVectorPtr(),
X.getColumn(t.first).getDataVectorPtr(),
X.getFormatType(t.first));
int covId = getColumnIndexByName(oCovariateId[i]);
X.push_back(X.getColumn(covId).getColumnsVectorPtr(),
X.getColumn(covId).getDataVectorPtr(),
X.getFormatType(covId));
X.getColumn(totalNumOfCov++).convertColumnToDense(getNumberOfRows());
int index = getNumberOfColumns() - 1;
X.getColumn(index).add_label(index);
mapTimeEffects.addTimeEffectColumn(t.second);
mapTimeEffects.addTimeEffectColumn(oTimeEffectId[i]);
}
mapTimeEffects.setNumInteraction(totalNumOfCov);

Expand Down
6 changes: 4 additions & 2 deletions src/cyclops/ModelData.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ class AbstractModelData {
) = 0;

virtual int loadTimeInteraction(
std::vector<std::pair<int, int>>& timeEffectMap
const std::vector<int64_t>& covariateId,
const std::vector<int64_t>& timeEffectId
) = 0;

virtual size_t append(
Expand Down Expand Up @@ -396,7 +397,8 @@ class ModelData : public AbstractModelData {
);

int loadTimeInteraction(
std::vector<std::pair<int, int>>& timeEffectMap
const std::vector<int64_t>& covariateId,
const std::vector<int64_t>& timeEffectId
);

const int* getPidVector() const;
Expand Down

0 comments on commit a90dca2

Please sign in to comment.