-
-
Notifications
You must be signed in to change notification settings - Fork 24
Allow deprecated syntax for specified versions of specified packages #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,16 +39,27 @@ | |
| #' | ||
| rstan_config <- function(pkgdir = ".") { | ||
| pkgdir <- .check_pkgdir(pkgdir) # check if package root directory | ||
| pkg_dcf <- read.dcf(file.path(pkgdir, "DESCRIPTION")) | ||
| pkg_name <- pkg_dcf[1, "Package"] | ||
| pkg_ver <- pkg_dcf[1, "Version"] | ||
|
|
||
| # get stan model files | ||
| stan_files <- list.files(file.path(pkgdir, "inst", "stan"), | ||
| full.names = TRUE, | ||
| pattern = "(\\.stan$)|(\\.stanfunctions$)") | ||
| if (length(stan_files) != 0) { | ||
| is_excepted <- isTRUE(stanc_exceptions[[pkg_name]] == pkg_ver) && (utils::packageVersion("StanHeaders") >= "2.36") | ||
|
|
||
| if (is_excepted) { | ||
| .update_deprecations(pkg_name, stan_files) | ||
| } | ||
|
|
||
| # add R & src folders in case run from configure[.win] script | ||
| .add_standir(pkgdir, "R", msg = FALSE, warn = FALSE) | ||
| .add_standir(pkgdir, "src", msg = FALSE, warn = FALSE) | ||
| # convert all .stan files to .cc/.hpp pairs | ||
| sapply(stan_files, .make_cc, pkgdir = pkgdir) | ||
| sapply(stan_files, .make_cc, pkgdir = pkgdir, pkg_name = pkg_name, | ||
| is_excepted = is_excepted) | ||
| # update package Makevars | ||
| acc <- .setup_Makevars(pkgdir, add = TRUE) | ||
| ## .add_Makevars(pkgdir) | ||
|
|
@@ -65,7 +76,7 @@ rstan_config <- function(pkgdir = ".") { | |
| # register exported modules as native routines | ||
| Rcpp::compileAttributes(pkgdir) | ||
| # update R/stanmodels.R with current set of models | ||
| stanmodels <- .update_stanmodels(pkgdir) | ||
| stanmodels <- .update_stanmodels(pkgdir, pkg_name, is_excepted) | ||
| acc <- acc | .add_stanfile(stanmodels, pkgdir, "R", "stanmodels.R") | ||
| invisible(acc) | ||
| } | ||
|
|
@@ -167,7 +178,7 @@ rstan_config <- function(pkgdir = ".") { | |
| # If the .stan file has a functions block but no parameters block, then there | ||
| # is no module definition but the functions are compiled and exported to the | ||
| # package's namespace. | ||
| .make_cc <- function(file_name, pkgdir) { | ||
| .make_cc <- function(file_name, pkgdir, pkg_name, is_excepted) { | ||
| model_name <- sub("[.]stan$", "", basename(file_name)) # model name | ||
| ## path to src/stan_files | ||
| ## stan_path <- file.path(pkgdir, "src", "stan_files") | ||
|
|
@@ -221,6 +232,7 @@ rstan_config <- function(pkgdir = ".") { | |
| cat("#include <exporter.h>", | ||
| eigen_incl, | ||
| "#include <stan/math/prim/meta.hpp>", | ||
| "#include <stan/services/util/create_rng.hpp>", | ||
| file = file.path(pkgdir, "src", | ||
| paste(pkgname, "types.h", sep = "_")), | ||
| sep = "\n") | ||
|
|
@@ -245,6 +257,9 @@ rstan_config <- function(pkgdir = ".") { | |
| if (utils::packageVersion('StanHeaders') >= "2.34") { | ||
| cppcode <- gsub("boost::ecuyer1988", "stan::rng_t", cppcode, fixed = TRUE) | ||
| } | ||
| if (is_excepted && !is.null(cpp_pre_process[[pkg_name]])) { | ||
| cppcode <- cpp_pre_process[[pkg_name]](cppcode) | ||
| } | ||
| # Stan header file | ||
| hdr_name <- .stan_prefix(model_name, ".h") | ||
| # get license file (if any) | ||
|
|
@@ -316,7 +331,7 @@ rstan_config <- function(pkgdir = ".") { | |
| } | ||
|
|
||
| # rewrites stanmodels.R reflecting current list of stan files | ||
| .update_stanmodels <- function(pkgdir) { | ||
| .update_stanmodels <- function(pkgdir, pkg_name, is_excepted) { | ||
| model_names <- list.files(file.path(pkgdir, "inst", "stan"), | ||
| pattern = "*.stan$") | ||
| only_functions <- sapply(model_names, FUN = function(nm) { | ||
|
|
@@ -354,6 +369,23 @@ rstan_config <- function(pkgdir = ".") { | |
| stanmodels[(model_line+2):load_line], | ||
| load_module, | ||
| stanmodels[(load_line+2):nlines]) | ||
| if (!is.null(cpp_pre_process[[pkg_name]])) { | ||
| process_fun <- c("process_fun <- ", deparse(cpp_pre_process[[pkg_name]])) | ||
|
|
||
| process_text <- c( | ||
| "process_fun <- ", deparse(cpp_pre_process[[pkg_name]]), | ||
| "stanfit$model_code <- process_fun(stanfit$model_code)", | ||
| "stanfit$model_cpp$model_cppcode <- process_fun(stanfit$model_cpp$model_cppcode)" | ||
| ) | ||
|
|
||
| insert_loc <- grep("# create stanmodel object$", stanmodels) | ||
| nlines <- length(stanmodels) | ||
| stanmodels <- c( | ||
| stanmodels[1:(insert_loc-1)], | ||
| process_text, | ||
| stanmodels[insert_loc:nlines] | ||
| ) | ||
| } | ||
| } | ||
| stanmodels | ||
| } | ||
|
|
@@ -402,3 +434,21 @@ rstan_config <- function(pkgdir = ".") { | |
| # Update model code with type declarations | ||
| gsub("auto", rtn_type, cpp_lines[decl_line], fixed = TRUE) | ||
| } | ||
|
|
||
| .update_deprecations <- function(pkg_name, stan_files) { | ||
| post_process <- stan_post_process[[pkg_name]] | ||
| if (is.null(post_process)) { | ||
| post_process <- function(x) x | ||
| } | ||
| ctx <- QuickJSR::JSContext$new(stack_size = 4 * 1024 * 1024) | ||
| ctx$source(system.file("stanc.2.32.js", package = "rstantools", mustWork = TRUE)) | ||
| stanc_process <- utils::getFromNamespace("stanc_process", "rstan") | ||
| sapply(stan_files, function(stanfile) { | ||
| model_code <- stanc_process(stanfile) | ||
| model_code <- ctx$call("stanc", "dummy", model_code, as.array("print-canonical"))$result | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check |
||
| model_code <- post_process(model_code) | ||
| writeLines(model_code, con = stanfile) | ||
| invisible(NULL) | ||
| }) | ||
| invisible(NULL) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||||||||||||||||||||||
| stanc_exceptions <- list( | ||||||||||||||||||||||||||||||
| AovBay = "0.1.0", | ||||||||||||||||||||||||||||||
| baggr = "0.8.2", | ||||||||||||||||||||||||||||||
| BayesPET = "0.1.0", | ||||||||||||||||||||||||||||||
| bbmix = "1.0.0", | ||||||||||||||||||||||||||||||
| bclogit = "1.1", | ||||||||||||||||||||||||||||||
| BINtools = "0.2.0", | ||||||||||||||||||||||||||||||
| bmgarch = "2.1.0", | ||||||||||||||||||||||||||||||
| bmggum = "0.1.0", | ||||||||||||||||||||||||||||||
| BoundIRT = "0.5.0", | ||||||||||||||||||||||||||||||
| BRRAT = "0.0.2", | ||||||||||||||||||||||||||||||
| bsynth = "1.0", | ||||||||||||||||||||||||||||||
| bws = "0.1.0", | ||||||||||||||||||||||||||||||
| CARME = "0.1.1", | ||||||||||||||||||||||||||||||
| cbq = "0.2.0.4", | ||||||||||||||||||||||||||||||
| cloneRate = "0.2.3", | ||||||||||||||||||||||||||||||
| CNVRG = "1.0.0", | ||||||||||||||||||||||||||||||
| DCPO = "0.5.3", | ||||||||||||||||||||||||||||||
| densEstBayes = "1.0-2.2", | ||||||||||||||||||||||||||||||
| EcoEnsemble = "1.2.0", | ||||||||||||||||||||||||||||||
| EpiPvr = "0.0.1", | ||||||||||||||||||||||||||||||
| fcirt = "0.2.1", | ||||||||||||||||||||||||||||||
| FlexReg = "1.4.2", | ||||||||||||||||||||||||||||||
| glmmPen = "1.5.4.8", | ||||||||||||||||||||||||||||||
| HHBayes = "0.1.1", | ||||||||||||||||||||||||||||||
| hmde = "1.4.0", | ||||||||||||||||||||||||||||||
| imt = "1.0.0", | ||||||||||||||||||||||||||||||
| imuGAP = "0.1.0", | ||||||||||||||||||||||||||||||
| MetaStan = "1.0.0", | ||||||||||||||||||||||||||||||
| morseTKTD = "0.1.3", | ||||||||||||||||||||||||||||||
| multipleDL = "1.0.0", | ||||||||||||||||||||||||||||||
| networkscaleup = "0.2-2", | ||||||||||||||||||||||||||||||
| phacking = "0.2.1", | ||||||||||||||||||||||||||||||
| postlink = "0.1.0", | ||||||||||||||||||||||||||||||
| psBayesborrow = "1.1.0", | ||||||||||||||||||||||||||||||
| publipha = "0.1.2", | ||||||||||||||||||||||||||||||
| SLGP = "1.0.2", | ||||||||||||||||||||||||||||||
| survstan = "0.0.7.1", | ||||||||||||||||||||||||||||||
| trialr = "0.1.6", | ||||||||||||||||||||||||||||||
| truncnormbayes = "0.0.3", | ||||||||||||||||||||||||||||||
| WhiteLabRt = "1.0.1", | ||||||||||||||||||||||||||||||
| YPPE = "1.0.1", | ||||||||||||||||||||||||||||||
| zoid = "1.3.1" | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Additional deprecations not covered by 2.32 stanc3 canonicalise | ||||||||||||||||||||||||||||||
| stan_post_process <- list( | ||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are going to run every time rstan_config() runs, right? For example, for survstan: first run: offset -> offset_par
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the CRAN revdep workaround this probably isn't an issue, but I think this would be a problem when any package developer locally runs We actually have a test for this, but it doesn't catch this because it's using a fake RStanTest package, not one of the packages in rstantools/tests/testthat/test-rstan_config.R Lines 83 to 96 in e03b02a
|
||||||||||||||||||||||||||||||
| publipha = function(model_code) { | ||||||||||||||||||||||||||||||
| model_code <- gsub("real (lower|upper)", "real \\1_par", model_code) | ||||||||||||||||||||||||||||||
| model_code <- gsub("normal_(cdf|lccdf|lcdf)\\((-)?(upper|lower)(,| \\|)", "normal_\\1(\\2\\3_par |", model_code) | ||||||||||||||||||||||||||||||
| model_code | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| survstan = function(model_code) { | ||||||||||||||||||||||||||||||
| model_code <- gsub("offset", "offset_par", model_code, fixed = TRUE) | ||||||||||||||||||||||||||||||
| model_code | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| cbq = function(model_code) { | ||||||||||||||||||||||||||||||
| model_code <- gsub("offset", "offset_par", model_code, fixed = TRUE) | ||||||||||||||||||||||||||||||
| model_code | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| cpp_pre_process <- list( | ||||||||||||||||||||||||||||||
| survstan = function(cpp_code) { | ||||||||||||||||||||||||||||||
| cpp_code <- gsub("offset_par", "offset", cpp_code, fixed = TRUE) | ||||||||||||||||||||||||||||||
| cpp_code | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| cbq = function(cpp_code) { | ||||||||||||||||||||||||||||||
| cpp_code <- gsub("offset_par", "offset", cpp_code, fixed = TRUE) | ||||||||||||||||||||||||||||||
| cpp_code | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| # rstan 2.32 does not account for new stanc mangling | ||||||||||||||||||||||||||||||
| disbayes = function(cpp_code) { | ||||||||||||||||||||||||||||||
| cpp_code <- gsub("_stan_mips", "mips", cpp_code, fixed = TRUE) | ||||||||||||||||||||||||||||||
| cpp_code | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
is_exceptedis actually ignored, so e.g. every future survstan will get the offset_par -> offset change instead of forcing them to change in the future, which I think was the original intentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your previous commit that removed using
is_exceptedbecause of disbayes mangling, which makes sense, but I think we need to keep it for the other packages.