Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,23 @@ name: R-CMD-check
permissions: read-all

jobs:
# a tree that already passed this workflow, e.g. a pull request merged by fast-forward, is not
# checked again; the skipped jobs count as passed
pre:
runs-on: ubuntu-latest
outputs:
should_skip: ${{ steps.skip.outputs.should_skip }}
steps:
- id: skip
uses: fkirc/skip-duplicate-actions@v5
with:
skip_after_successful_duplicate: 'true'
concurrent_skipping: 'never'
do_not_skip: '["workflow_dispatch", "schedule"]'

R-CMD-check:
needs: pre
if: needs.pre.outputs.should_skip != 'true'
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})
Expand Down Expand Up @@ -106,6 +122,8 @@ jobs:
# gcc, FlexiBLAS and the hardening flags differ from Ubuntu's. No RSPM binaries
# here, so dependencies build from source.
fedora:
needs: pre
if: needs.pre.outputs.should_skip != 'true'
runs-on: ubuntu-latest
container: fedora:latest
name: fedora (R release, gcc/FlexiBLAS)
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: dMod2
Type: Package
Title: Parameter Estimation in Dynamical Models
Version: 0.16.0
Version: 0.16.1
Date: 2026-11-16
Authors@R: c(
person("Simon", "Beyer", email = "simon.beyer@fdm.uni-freiburg.de",
Expand Down
16 changes: 16 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# dMod2 0.16.1

* `derivMode` alone selects the derivatives a constructor builds: `odemodel()`, `Y()`, `P()`,
`Pexpl()`, `Pimpl()` and `importPEtab()` take one or more of `"forward"` (default),
`"reverse"`, `"forward-forward"` and `"forward-reverse"`, or `"none"`. The build arguments
`deriv` and `deriv2` are removed: `deriv2 = TRUE` is `derivMode = "forward-forward"`,
`deriv = FALSE` is `derivMode = "none"`. Passing `deriv` or `deriv2` is an error.
* A second-order direction, `"forward-forward"` or `"forward-reverse"`, builds `"forward"` as
well. `"none"` combines with no other direction. `odemodel(derivMode = "reverse")` builds no
forward sensitivities. `Pimpl()` obtains the reverse directions from the forward ones.
* `deriv`, `deriv2` and `sweep` of a call keep their meaning. A call that asks for a derivative
the function object was built without is an error that names the missing `derivMode`
direction; `Y()`, `Pexpl()` and `Pimpl()` built without first derivatives no longer return
values alone on `deriv = TRUE`.
* The `odemodel` object holds the built directions in `attr(, "derivMode")`.

# dMod2 0.16.0

* The plotting theme and color helpers are named after the package: `theme_dMod2()`,
Expand Down
4 changes: 2 additions & 2 deletions R/EM.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@
#' default `TRUE`.}
#' \item{`derivMode`}{how FOCEI gets the second derivatives of the volume
#' term: `"forward-reverse"` (default), sweeps for models built with
#' `derivMode = c("forward", "reverse", "forward-reverse")`, or
#' `derivMode = c("reverse", "forward-reverse")`, or
#' `"forward-forward"`, the second-order sensitivities of models built
#' with `deriv2 = TRUE`. Without the mode asked for the fit takes the
#' with `derivMode = "forward-forward"`. Without the mode asked for the fit takes the
#' other, with a message.}
#' \item{`boundary`}{the largest rise of -2 log L at which a variance is
#' set to 0, default 0.1, `FALSE` for none. With one grouping and several
Expand Down
27 changes: 14 additions & 13 deletions R/PEtabInterface.R
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,8 @@ readPEtabTables <- function(yamlPath) {
# sub-conditions with this preequilibration condition.
eq_of[sub] <- peq
if (is.null(eq_model[[peq]]) && length(peq_reactions$states))
eq_model[[peq]] <- Pimpl(peq_reactions, condition = NULL, deriv2 = stability,
eq_model[[peq]] <- Pimpl(peq_reactions, condition = NULL,
derivMode = if (stability) "forward-forward" else "forward",
compile = FALSE, outdir = outdir,
modelname = paste(modelname,
sanitizeConditions(peq),
Expand Down Expand Up @@ -1545,7 +1546,7 @@ readPEtabTables <- function(yamlPath) {
events = NULL,
options = NULL, optionsSens = NULL,
sparse = NULL,
deriv = TRUE, derivMode = "forward",
derivMode = "forward",
outdir = .dmodOutdir()) {
# No species means no dynamics: Xt() supplies the time axis and the
# observables are evaluated from parameters alone.
Expand All @@ -1557,7 +1558,7 @@ readPEtabTables <- function(yamlPath) {
# which would apply the initial values there instead.
args <- list(reactions, modelname = modelname, backend = backend,
events = events, compile = FALSE, includeTimeZero = FALSE,
deriv = deriv, derivMode = derivMode, outdir = outdir)
derivMode = derivMode, outdir = outdir)
if (!is.null(sparse) && backend != "deSolve") args$sparse <- sparse
m <- do.call(odemodel, args)
opts <- list(m)
Expand Down Expand Up @@ -1648,7 +1649,7 @@ readPEtabTables <- function(yamlPath) {
switches = NULL,
options = NULL, optionsSens = NULL,
sparse = NULL,
deriv = TRUE, derivMode = "forward", cores = 1L,
derivMode = "forward", cores = 1L,
outdir = .dmodOutdir(), stability = TRUE) {

# `importSbml` renames ids that R cannot parse or C++ reserves; the PEtab
Expand Down Expand Up @@ -1791,7 +1792,7 @@ readPEtabTables <- function(yamlPath) {
options = options,
optionsSens = optionsSens,
sparse = sparse,
deriv = deriv, derivMode = derivMode,
derivMode = derivMode,
outdir = outdir)
g <- .petab_build_observation_fn(obs_meta$obs, obs_meta$obs_trafo,
sbml$reactions,
Expand Down Expand Up @@ -1958,12 +1959,11 @@ readPEtabTables <- function(yamlPath) {
#' Default `1`.
#' @param modelname Base name of the generated files. Default `NULL`, the base
#' name of the YAML file.
#' @param deriv Logical. If `FALSE`, the model is built without sensitivities,
#' which needs `backend = "cppDE"` or `"Sundials"`; the objective then has to
#' be called with `deriv = FALSE`. Default `TRUE`.
#' @param derivMode Derivative directions to build, passed to [odemodel()]:
#' `"forward"` (default), `"reverse"` or `c("forward", "reverse")`.
#' `"reverse"` needs `backend = "cppDE"` or `"Sundials"`. First order only.
#' @param derivMode Derivative directions to build, passed to [odemodel()],
#' [Y()] and [P()]: `"forward"` (default), `"reverse"`,
#' `c("forward", "reverse")` or `"none"`. `"reverse"` and `"none"` need
#' `backend = "cppDE"` or `"Sundials"`; with `"none"` the objective has to be
#' called with `deriv = FALSE`. First order only.
#' @param options,optionsSens Lists passed to [Xs()]: the solver options of
#' every solve and their overrides for the solves with sensitivities.
#' Default `NULL`, the backend's defaults.
Expand Down Expand Up @@ -2011,7 +2011,7 @@ readPEtabTables <- function(yamlPath) {
#' @export
importPEtab <- function(yamlPath, backend,
compile = TRUE, cores = 1L, modelname = NULL,
deriv = TRUE, derivMode = "forward",
derivMode = "forward",
options = NULL, optionsSens = NULL,
sparse = NULL, stability = TRUE, outdir = .dmodOutdir()) {

Expand All @@ -2025,6 +2025,8 @@ importPEtab <- function(yamlPath, backend,
"\"Sundials\").")
backend <- match.arg(backend, c("deSolve", "cppDE", "Sundials"))
derivMode <- .matchDerivMode(derivMode, c("forward", "reverse"))
if (identical(derivMode, "none") && backend == "deSolve")
stop("derivMode = \"none\" needs backend = 'cppDE' or 'Sundials'.", call. = FALSE)
if ("reverse" %in% derivMode && backend == "deSolve")
stop("derivMode = \"reverse\" needs backend = 'cppDE' or 'Sundials'; ",
"the deSolve backend goes forward only.", call. = FALSE)
Expand Down Expand Up @@ -2120,7 +2122,6 @@ importPEtab <- function(yamlPath, backend,
options = options,
optionsSens = optionsSens,
sparse = sparse,
deriv = deriv,
derivMode = derivMode,
cores = cores,
outdir = outdir,
Expand Down
2 changes: 1 addition & 1 deletion R/constraintStability.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#' with other terms keeps the value only, so the attribute is read from a call
#' of the term itself.
#'
#' @param pSS A parfn of [Pimpl()], built with `deriv2 = TRUE`.
#' @param pSS A parfn of [Pimpl()], built with `derivMode = "forward-forward"`.
#' @param p The parfn whose output `pSS` reads, or `NULL` where `pSS` reads the
#' parameters of the objective.
#' @param weight \eqn{w}, the value of the term where the real part of an
Expand Down
2 changes: 1 addition & 1 deletion R/emResults.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ print.summary.em <- function(x, digits = 4, ...) {
if (identical(x$seMethod, "none")) cat("No standard errors; confint(method = \"profile\") gives intervals\n")
if (isTRUE(x$partialCurvature))
cat("Standard errors without the curvature of the prediction in the random effects within the volume term",
"(forward over reverse; deriv2 = TRUE gives the full one)\n")
"(forward over reverse; derivMode = \"forward-forward\" gives the full one)\n")
if (!is.null(x$omega)) {
cat("\nRandom effects:\n")
om <- x$omega
Expand Down
10 changes: 5 additions & 5 deletions R/nlmeFocei.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
.volumeFrames <- function(design, cn, pred, fixed, deriv2 = TRUE) {
if (deriv2 && (is.null(attr(pred, "deriv")) || is.null(attr(pred, "deriv2"))))
stop("em: the prediction of condition ", cn, " has no second derivatives; build the ",
"model with deriv2 = TRUE.", call. = FALSE)
"model with derivMode = \"forward-forward\".", call. = FALSE)
if (is.null(design$errfn)) return(list(pred = pred, err = NULL))
inner <- getParameters(pred)
fixedInner <- as.parvec(inner[attr(inner, "fixed")], deriv = FALSE, deriv2 = FALSE)
Expand All @@ -300,7 +300,7 @@
for (cn in conds)
if (deriv2 && (is.null(attr(preds[[cn]], "deriv")) || is.null(attr(preds[[cn]], "deriv2"))))
stop("em: the prediction of condition ", cn, " has no second derivatives; build the ",
"model with deriv2 = TRUE.", call. = FALSE)
"model with derivMode = \"forward-forward\".", call. = FALSE)
node <- if (!is.null(design$errfn)) .fnNode(design$errfn)
if (is.null(node))
return(setNames(lapply(conds, function(cn) .volumeFrames(design, cn, preds[[cn]], fixed, deriv2)), conds))
Expand Down Expand Up @@ -361,7 +361,7 @@
if (is.null(e1)) return(rows)
if (is.null(e2) && any(eta %in% dimnames(e1)[[3]]))
stop("FOCEI with a random effect in the error model needs the second derivatives of the error model: ",
"build the error model with deriv2 = TRUE.", call. = FALSE)
"build the error model with derivMode = \"forward-forward\".", call. = FALSE)
# rows whose observable has no error-model derivative keep zero derivatives
ie <- te + (match(d$name, dimnames(e1)[[2]]) - 1L) * nrow(err)
none <- is.na(ie) | !model
Expand Down Expand Up @@ -759,11 +759,11 @@
.derivModeMessage(if (!is.null(control$derivMode))
paste0("FOCEI: the chain has no ", want, " second derivatives; it takes ", other, ".")
else paste0("em: second derivatives forward over forward; models built with derivMode = ",
"c(\"forward\", \"reverse\", \"forward-reverse\") give them forward over reverse, which is faster."))
"c(\"reverse\", \"forward-reverse\") give them forward over reverse, which is faster."))
return(other)
}
stop("FOCEI needs second derivatives of the prediction and the error model: build the models ",
"with derivMode = c(\"forward\", \"reverse\", \"forward-reverse\") or with deriv2 = TRUE, ",
"with derivMode = c(\"reverse\", \"forward-reverse\") or \"forward-forward\", ",
"or set control = list(secondOrderCorrection = FALSE), which biases the estimates.", call. = FALSE)
}

Expand Down
51 changes: 19 additions & 32 deletions R/odeClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ print.odemodel <- function(x, ...) {
if (!is.null(extended)) {
cat(" Sens1: ", as.character(extended), "\n", sep = "")
} else {
cat(" Sens1: not compiled (deriv = FALSE)\n", sep = "")
cat(" Sens1: not built (derivMode without \"forward\")\n", sep = "")
}
if (isCppDE) {
if (!is.null(extended2)) {
cat(" Sens2: ", as.character(extended2), "\n", sep = "")
} else {
cat(" Sens2: not compiled (deriv2 = FALSE)\n", sep = "")
cat(" Sens2: not built (derivMode without \"forward-forward\")\n", sep = "")
}
if (!is.null(reversed))
cat(" Adjoint: ", as.character(reversed), "\n", sep = "")
Expand Down Expand Up @@ -68,23 +68,20 @@ print.odemodel <- function(x, ...) {
#' @param f Named character vector, [eqnvec] or [eqnlist] with the right-hand
#' sides; the names are the states. The functions and operators an equation
#' may use are listed in [cppDE::expressions].
#' @param deriv logical, build first-order sensitivities. Default `TRUE`.
#' `FALSE` builds the states alone, for [Xf()], and `derivMode` has no
#' effect.
#' @param deriv2 logical, build second-order sensitivities. Default `FALSE`.
#' `TRUE` is the same as adding `"forward-forward"` to `derivMode`, needs
#' `backend = "cppDE"` and implies `deriv = TRUE`.
#' @param derivMode character, the derivative directions to build, one or
#' more of:
#' * `"forward"` (default): sensitivity equations. `deriv = TRUE` builds them
#' also when `derivMode` leaves out `"forward"`.
#' * `"forward"` (default): first-order sensitivities.
#' * `"reverse"`: adjoint gradient, needed for `obj(..., sweep = "reverse")`.
#' Not available on `backend = "deSolve"`.
#' * `"forward-forward"`: second-order sensitivities.
#' * `"forward-forward"`: second-order sensitivities, needed for
#' `deriv2 = TRUE` in a call. `backend = "cppDE"` only.
#' * `"forward-reverse"`: adjoint Hessian, needed for
#' `obj(..., sweep = "reverse", deriv2 = TRUE)`. `backend = "cppDE"` only.
#' * `"none"`: the states alone, for [Xf()].
#'
#' Each direction is compiled separately.
#' Each direction is compiled separately. The two second-order directions
#' build `"forward"` as well. A call that asks for a derivative not built is
#' an error.
#' @param forcings character, the names of the forcings in `f`. Default
#' `NULL`. Their data are given to [Xs()] or [Xf()].
#' @param events an [eventlist], or a `data.frame` accepted by
Expand Down Expand Up @@ -134,40 +131,29 @@ print.odemodel <- function(x, ...) {
#'
#' @example inst/examples/odemodel.R
#' @export
odemodel <- function(f, deriv = TRUE, deriv2 = FALSE, derivMode = "forward",
odemodel <- function(f, derivMode = "forward",
forcings=NULL, events = NULL,
fixed = NULL, modelname = "odemodel", backend = c("cppDE", "Sundials", "deSolve"),
verbose = FALSE, outdir = .dmodOutdir(), ...) {

f <- as.eqnvec(f)
backend <- match.arg(backend)
derivMode <- .matchDerivMode(derivMode, c("forward", "reverse",
"forward-forward", "forward-reverse"))
# without derivatives there is no direction to build
if (!deriv && !deriv2) derivMode <- "forward"
dots <- list(...)
.rejectBuildDeriv("odemodel", dots)
derivMode <- .matchDerivMode(derivMode)
deriv <- "forward" %in% derivMode
deriv2 <- "forward-forward" %in% derivMode
reverse <- "reverse" %in% derivMode
reverse2 <- "forward-reverse" %in% derivMode
# The two spellings of forward over forward name the same build product.
if ("forward-forward" %in% derivMode) deriv2 <- TRUE
if (reverse && backend == "deSolve")
stop("derivMode = \"reverse\" needs backend = 'cppDE' or 'Sundials'.",
call. = FALSE)
if (reverse2 && backend != "cppDE")
stop("derivMode = \"forward-reverse\" needs backend = 'cppDE'.",
call. = FALSE)

if (deriv2 && !deriv) {
warning("`deriv2 = TRUE` implies `deriv = TRUE`. Setting deriv = TRUE.",
call. = FALSE)
deriv <- TRUE
}

dots <- list(...)

if (deriv2 && backend == "deSolve")
stop("Second-order sensitivities require backend = 'cppDE'.")
if (deriv2 && backend == "Sundials")
stop("Second-order sensitivities are not available with CVODE; use backend = 'cppDE'.")
if (deriv2 && backend != "cppDE")
stop("derivMode = \"forward-forward\" needs backend = 'cppDE'.",
call. = FALSE)

# cOde::funC has no outdir and always writes to the working directory, so a
# non-default outdir would be silently ignored under the deSolve backend.
Expand Down Expand Up @@ -334,5 +320,6 @@ odemodel <- function(f, deriv = TRUE, deriv2 = FALSE, derivMode = "forward",
attr(out, "compileInfo") <- .collectCompileInfo(out$func, out$extended,
out$extended2, out$reversed,
out$reversed2)
attr(out, "derivMode") <- derivMode
return(out)
}
Loading
Loading