Skip to content

Commit

Permalink
S3 plot dispatch for ModvegeSite, removed redundant "store_results" o…
Browse files Browse the repository at this point in the history
…ption, bugfixes
  • Loading branch information
kuadrat committed Nov 9, 2023
1 parent e2d84bc commit 8011764
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 49 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: growR
Type: Package
Version: 1.0.0.9008
Version: 1.0.9.9000
Date: 2023-09-27
Authors@R: person(
given = "Kevin",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method("*",FunctionalGroup)
S3method("+",FunctionalGroup)
S3method(plot,ModvegeSite)
export(FG_A)
export(FG_B)
export(FG_C)
Expand Down
11 changes: 10 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
concerns `ModvegeSite$plot()` and `analyze_parameter_scan()` as well as the
`compare.R` script.

* S3 dispatch for plot method of `ModvegeSite` objects -> `plot(mvs)` is now
possible if `mvs` is a `ModvegeSite` instance.

## Changed

* Input data CSV files are now actual CSV files, instead of
Expand All @@ -22,17 +25,23 @@
`check_parameters` now only throws an error if any of the really
*required* parameters are missing.

## Fixed

* autocut: `get_annual_gross_yield` was incorrectly hardcoded to return 1.

## Removed

* Removed superfluous weather inputs.

* Removed automatic temperature *correction*.

* Redundant argument *store_results* in `growR_run_loop`.

# growR 1.0.0

* Initial CRAN submission.

# grower 0.0.1
# growR 0.0.1

* Renaming from `rmodvege` to `growR`.

Expand Down
2 changes: 1 addition & 1 deletion R/management.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ManagementData = R6Class(
if (intensity %in% choices) {
self$intensity = intensity
logger(sprintf("[autocut]Setting management intensity to `%s`.",
intensity))
intensity), level = INFO)
} else {
self$intensity = "high"
}
Expand Down
29 changes: 26 additions & 3 deletions R/modvegesite.R
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,19 @@ ModvegeSite = R6Class(
#'
#' Creates a simple base R plot showing the BM with cutting events and,
#' if applicable, target biomass, dBM, cBM and hvBM.
#' Can only be sensibly run *after* a simulation has been carried out,
#' i.e. after this instance's `run()` method has been called.
#'
#' @param smooth_interval Int. Number of days over which the variable
#' `dBM` is smoothened.
#' @return None Creates a plot of the result.
#' @param ... Further arguments are discarded.
#' @return NULL Creates a plot of the result in the active device.
#'
plot = function(smooth_interval = 28) {
plot = function(smooth_interval = 28, ...) {
if (private$current_DOY == 1) {
warning("Cannot plot results because simulation has not yet been run.")
return()
}
oldpar = par(no.readonly = TRUE)
on.exit(par(oldpar))
par(mfrow = c(2, 2))
Expand All @@ -490,7 +497,6 @@ ModvegeSite = R6Class(
plot(self$cBM, type = "l", xlab = xlab, ylab = "cBM (kg / ha)")
plot(self$hvBM, type = "l", xlab = xlab, ylab = "hvBM (kg / ha)")
}

)
), # End of public attributes

Expand Down Expand Up @@ -965,3 +971,20 @@ ModvegeSite = R6Class(

) # End of private attributes
)

## S3 dispatch methods

#' Plot ModVege simulation result overview
#'
#' This wraps the `ModvegeSite` instance's `plot()` method.
#'
#' @param x A [ModvegeSite] instance.
#' @param ... Arguments are passed on to [ModvegeSite]`$plot()`.
#' @return NULL, but plots to the active device.
#'
#' @md
#' @export
plot.ModvegeSite = function(x, ...) {
x$plot(...)
}

5 changes: 1 addition & 4 deletions R/parameter_scan.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ run_parameter_scan = function(environment, param_values, force = FALSE,
level = INFO
)
environment$parameters$set_parameters(parameter_sets[[i]])
mvs = growR_run_loop(c(environment),
output_dir = "",
store_results = TRUE
)
mvs = growR_run_loop(c(environment), output_dir = "")
results[[i]] = list(params = parameter_sets[[i]],
data = mvs[[1]])
}
Expand Down
26 changes: 9 additions & 17 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,21 @@
#' @param modvege_environments A list of [ModvegeEnvironment] instances.
#' @param output_dir string; name of directory to which output files are to
#' be written. If `output_dir == ""` (default), no files are written.
#' @param store_results boolean; If TRUE, return a list of the [ModvegeSite]
#' objects which were run.
#'
#' @return If `store_results == TRUE`, a list of the format
#' `[[run]][[year]]` containing clones of the [ModvegeSite] instances that
#' were run. Otherwise an empty list. Defaults to
#' getOption("growR.output_dir").
#' @return A list of the format `[[run]][[year]]` containing clones of
#' the [ModvegeSite] instances that were run. Also write to files, if
#' *output_dir* is nonempty.
#'
#' @examples
#' env1 = create_example_environment(site = "posieux")
#' env2 = create_example_environment(site = "sorens")
#'
#' growR_run_loop(c(env1, env2), output_dir = "",
#' store_results = TRUE)
#' growR_run_loop(c(env1, env2), output_dir = "")
#'
#' @md
#' @export
#'
growR_run_loop = function(modvege_environments,
output_dir = "",
store_results = FALSE) {
growR_run_loop = function(modvege_environments, output_dir = "") {
# Parse output dir
if (output_dir == "") {
write_files = FALSE
Expand Down Expand Up @@ -62,19 +56,17 @@ growR_run_loop = function(modvege_environments,
#-Write-output------------------------------------------------------------

if (write_files) {
out_file = sprintf("%s%s%s_%s.dat",
output_dir,
out_file = sprintf("%s%s_%s.dat",
run_environment$site_name,
run_environment$run_name_in_filename,
this_year)
out_path = file.path(output_dir, out_file)
logger("Entering `ModvegeSite$write_output`", level = TRACE)
modvege$write_output(out_file, force = TRUE)
modvege$write_output(out_path, force = TRUE)
}

#-Store-output------------------------------------------------------------
if (store_results) {
results[[run]][[i_year]] = modvege$clone(deep = TRUE)
}
results[[run]][[i_year]] = modvege$clone(deep = TRUE)
} # End of loop over simulation years
} # End of loop over runs
logger("All runs completed.", level = INFO)
Expand Down
10 changes: 5 additions & 5 deletions R/support_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ fC.ST = function(x){1} # function(x){1 + .1*(x - 360)/(720 - 360)}
#' @md
#' @export
get_annual_gross_yield = function(elevation, intensity = "high") {
#mask = yield_parameters$intensity == intensity
#a = yield_parameters[mask, ]$a
#b = yield_parameters[mask, ]$b
#return(a + b * max(elevation, 500))
return(1)
mask = yield_parameters$intensity == intensity
a = yield_parameters[mask, ]$a
b = yield_parameters[mask, ]$b
return(a + b * max(elevation, 500))
# return(1)
}

#' Get number of expected cuts
Expand Down
8 changes: 6 additions & 2 deletions man/ModvegeSite.Rd

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

15 changes: 5 additions & 10 deletions man/growR_run_loop.Rd

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

19 changes: 19 additions & 0 deletions man/plot.ModvegeSite.Rd

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

3 changes: 1 addition & 2 deletions tests/testthat/test-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ test_that("growR_run_loop with example config", {
for (env in envs) {
env$years = env$years[[1]]
}
expect_no_error(growR_run_loop(envs,
store_results = FALSE))
expect_no_error(growR_run_loop(envs))
unlink(.tmpdir(), recursive = TRUE)
})
5 changes: 2 additions & 3 deletions vignettes/growR.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ information needed in order to run a ModVege simulation.
We are now ready to do so:
```{r run_simulation}
results = growR_run_loop(environments,
output_dir = file.path(working_dir, "output"),
store_results = TRUE)
output_dir = file.path(working_dir, "output"))
```
This will simulate grass growth for every year in every environment present
in `environments`. After some console output, we now have the results of
Expand Down Expand Up @@ -230,7 +229,7 @@ new_envs = c(env0, env1, env2)

And we're ready to run and inspect our next run of simulations:
```{r ni_screening}
new_results = growR_run_loop(new_envs, store_results = TRUE)
new_results = growR_run_loop(new_envs)
# Plot all results
for (run in new_results) {
print(run[[1]]$parameters$NI)
Expand Down

0 comments on commit 8011764

Please sign in to comment.