Skip to content

Commit a721db4

Browse files
committed
Added resolve argument to steadyStates
long expressions from steady states get simplified and resolved if TRUE
1 parent fea93d9 commit a721db4

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ Suggests:
3737
pander,
3838
knitr,
3939
rmarkdown,
40-
RoxygenNote: 7.3.2
40+
RoxygenNote: 7.3.3
4141
VignetteBuilder: knitr
4242
Encoding: UTF-8

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ importFrom(parallel,mclapply)
325325
importFrom(plyr,ddply)
326326
importFrom(plyr,summarise)
327327
importFrom(reticulate,import)
328+
importFrom(reticulate,py_require)
328329
importFrom(reticulate,py_run_string)
330+
importFrom(reticulate,source_python)
329331
importFrom(rjson,fromJSON)
330332
importFrom(rlang,UQS)
331333
importFrom(rlang,enquo)

R/steadyStates.R

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#' compatible output. To obtain an output appropriate for d2d [2] "M" must be
1616
#' selected.
1717
#' @param testSteady Boolean, if "T" the correctness of the obtained steady states is numerically checked (this can be very time intensive). If "F" this is skipped.
18-
#'
18+
#' @param resolve Boolean. If TRUE, recursive dependencies are resolved, meaning that independent equations are substituted into each expression and then simplified.
19+
#'
1920
#' @return Character vector of steady-state equations.
2021
#'
2122
#' @references [1]
@@ -29,36 +30,43 @@
2930
#'
3031
#' @export
3132
#' @importFrom utils write.table
33+
#' @importFrom reticulate py_require source_python
3234
#' @example inst/examples/steadystates.R
33-
steadyStates <- function(model, file=NULL, rates = NULL, forcings = NULL, givenCQs = NULL, neglect=NULL, sparsifyLevel = 2, outputFormat = "R", testSteady = "T") {
34-
35-
require(reticulate)
35+
steadyStates <- function(model, file=NULL, rates = NULL, forcings = NULL, givenCQs = NULL, neglect=NULL, sparsifyLevel = 2, outputFormat = "R", testSteady = "T", resolve = TRUE) {
3636

3737
# Check if model is an equation list
3838
if (inherits(model, "eqnlist")) {
3939
if(is.null(file)) file <- "reactions_for_Alyssa"
4040
write.eqnlist(model, file = paste0(file, "_model.csv"))
4141
model <- paste0(file, "_model.csv")
4242
}
43-
43+
4444
if (!is.null(givenCQs) && length(names(givenCQs)) > 0)
4545
stop("givenCQs must not have names. Please unname() them.")
4646

4747

4848
# Calculate steady states.
49-
source_python(system.file("code/AlyssaPetit_ver1.1.py", package = "dMod"))
49+
reticulate::py_require("sympy")
50+
reticulate::source_python(system.file("code/AlyssaPetit_ver1.1.py", package = "dMod"))
5051
m_ss <- Alyssa(model, as.list(forcings), as.list(givenCQs), as.list(neglect), sparsifyLevel, outputFormat, testSteady)
5152

5253
# Write steady states to disk.
53-
if(length(m_ss)>1){
54+
if(length(m_ss)>0){
5455
m_ssChar <- do.call(c, lapply(strsplit(m_ss, "="), function(eq) {
5556
out <- eq[2]
5657
names(out) <- eq[1]
58+
5759
return(out)
5860
}))
5961
if(!is.null(file) & is.character(file))
6062
saveRDS(object = m_ssChar, file = file)
6163

64+
if (resolve) {
65+
simplify <- import("sympy")$simplify
66+
m_ssChar <- lapply(resolveRecurrence(m_ssChar), function(expr) {
67+
simplify(expr) %>% as.character()
68+
}) %>% unlist(use.names = TRUE)
69+
}
6270
return(m_ssChar)
6371
} else return(0)
6472
}

man/steadyStates.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)