Skip to content

Commit

Permalink
get log-likelihood gradient R wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
msuchard committed Jul 7, 2024
1 parent c464e2d commit 2cb36c3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ jni
Changes:

1. add JNI interface for manipulating Cyclops objects in an across-language persistent cache
2. add `cyclopsGetLogLikelihoodGradient()`

Cyclops v3.4.1
==============
Expand Down
12 changes: 12 additions & 0 deletions R/ModelFit.R
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,18 @@ fitCyclopsModel <- function(cyclopsData,
return(fit)
}

#' @title Get full path to installed Cyclops library
#'
#' @description
#' \code{cyclopsLibraryFileName} returns the full path to the installed Cyclops .so/.dll file
#'
#' @return String
cyclopsLibraryFileName <- function() {
normalizePath(system.file("libs", .Platform$r_arch,
paste0("Cyclops", .Platform$dynlib.ext),
package = "Cyclops"))
}

#' @title Place Cyclops model fit object in a persistent cache
#'
#' @description
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
.Call(`_Cyclops_cyclopsRunBootstrap`, inRcppCcdInterface, outFileName, treatmentId, replicates)
}

.cyclopsGetLogLikelihoodGradient <- function(inRcppCcdInterface) {
.Call(`_Cyclops_cyclopsGetLogLikelihoodGradient`, inRcppCcdInterface)
}

.cyclopsLogModel <- function(inRcppCcdInterface) {
.Call(`_Cyclops_cyclopsLogModel`, inRcppCcdInterface)
}
Expand Down
22 changes: 22 additions & 0 deletions src/RcppCyclopsInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,28 @@ List cyclopsRunBootstrap(SEXP inRcppCcdInterface, const std::string& outFileName
return list;
}

// [[Rcpp::export(".cyclopsGetLogLikelihoodGradient")]]
NumericVector cyclopsGetLogLikelihoodGradient(SEXP inRcppCcdInterface) {
using namespace bsccs;

XPtr<RcppCcdInterface> interface(inRcppCcdInterface);

auto& ccd = interface->getCcd();
auto& data = interface->getModelData();


const auto offset = data.getHasOffsetCovariate();
const auto length = ccd.getBetaSize() - offset;

NumericVector gradient(length);

for (int i = 0; i < length; ++i) {
gradient[i] = ccd.getLogLikelihoodGradient(i + offset);
}

return gradient;
}

// [[Rcpp::export(".cyclopsLogModel")]]
List cyclopsLogModel(SEXP inRcppCcdInterface) {
using namespace bsccs;
Expand Down
12 changes: 12 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// cyclopsGetLogLikelihoodGradient
NumericVector cyclopsGetLogLikelihoodGradient(SEXP inRcppCcdInterface);
RcppExport SEXP _Cyclops_cyclopsGetLogLikelihoodGradient(SEXP inRcppCcdInterfaceSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type inRcppCcdInterface(inRcppCcdInterfaceSEXP);
rcpp_result_gen = Rcpp::wrap(cyclopsGetLogLikelihoodGradient(inRcppCcdInterface));
return rcpp_result_gen;
END_RCPP
}
// cyclopsLogModel
List cyclopsLogModel(SEXP inRcppCcdInterface);
RcppExport SEXP _Cyclops_cyclopsLogModel(SEXP inRcppCcdInterfaceSEXP) {
Expand Down Expand Up @@ -895,6 +906,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_Cyclops_cyclopsClearCacheForJava", (DL_FUNC) &_Cyclops_cyclopsClearCacheForJava, 0},
{"_Cyclops_cyclopsFitModel", (DL_FUNC) &_Cyclops_cyclopsFitModel, 1},
{"_Cyclops_cyclopsRunBootstrap", (DL_FUNC) &_Cyclops_cyclopsRunBootstrap, 4},
{"_Cyclops_cyclopsGetLogLikelihoodGradient", (DL_FUNC) &_Cyclops_cyclopsGetLogLikelihoodGradient, 1},
{"_Cyclops_cyclopsLogModel", (DL_FUNC) &_Cyclops_cyclopsLogModel, 1},
{"_Cyclops_cyclopsInitializeModel", (DL_FUNC) &_Cyclops_cyclopsInitializeModel, 4},
{"_Cyclops_listGPUDevices", (DL_FUNC) &_Cyclops_listGPUDevices, 0},
Expand Down

0 comments on commit 2cb36c3

Please sign in to comment.