Skip to content

Commit

Permalink
Merge pull request #12 from UofUEpiBio/rename-actions-to-events
Browse files Browse the repository at this point in the history
Rename actions to events
  • Loading branch information
gvegayon authored Feb 3, 2024
2 parents a82f994 + 037708a commit 5fd7ad1
Show file tree
Hide file tree
Showing 35 changed files with 953 additions and 279 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local-update:
rsync -avz ../epiworld/include/epiworld inst/include/.

check: build
R CMD check epiworldR_*.tar.gz
cd .. && R CMD check epiworldR_*.tar.gz

clean:
Rscript --vanilla -e 'devtools::clean_dll()'
Expand Down
8 changes: 6 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ S3method(plot,epiworld_surv)
S3method(print,epiworld_agent)
S3method(print,epiworld_agents)
S3method(print,epiworld_agents_tools)
S3method(print,epiworld_globalaction)
S3method(print,epiworld_globalevent)
S3method(print,epiworld_model)
S3method(print,epiworld_saver)
S3method(print,epiworld_tool)
Expand Down Expand Up @@ -90,7 +90,7 @@ export(ModelSIRLogit)
export(ModelSIS)
export(ModelSISD)
export(ModelSURV)
export(add_global_action)
export(add_globalevent)
export(add_tool)
export(add_tool_agent)
export(add_tool_n)
Expand Down Expand Up @@ -131,6 +131,10 @@ export(globalaction_fun)
export(globalaction_set_params)
export(globalaction_tool)
export(globalaction_tool_logit)
export(globalevent_fun)
export(globalevent_set_params)
export(globalevent_tool)
export(globalevent_tool_logit)
export(has_tool)
export(has_virus)
export(initial_states)
Expand Down
24 changes: 12 additions & 12 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# Generated by cpp11: do not edit by hand

globalaction_tool_logit_cpp <- function(tool, vars, coefs, name, day) {
.Call(`_epiworldR_globalaction_tool_logit_cpp`, tool, vars, coefs, name, day)
globalevent_tool_logit_cpp <- function(tool, vars, coefs, name, day) {
.Call(`_epiworldR_globalevent_tool_logit_cpp`, tool, vars, coefs, name, day)
}

globalaction_tool_cpp <- function(tool, prob, name, day) {
.Call(`_epiworldR_globalaction_tool_cpp`, tool, prob, name, day)
globalevent_tool_cpp <- function(tool, prob, name, day) {
.Call(`_epiworldR_globalevent_tool_cpp`, tool, prob, name, day)
}

globalaction_set_param_cpp <- function(param, value, name, day) {
.Call(`_epiworldR_globalaction_set_param_cpp`, param, value, name, day)
globalevent_set_param_cpp <- function(param, value, name, day) {
.Call(`_epiworldR_globalevent_set_param_cpp`, param, value, name, day)
}

print_global_action_cpp <- function(action) {
.Call(`_epiworldR_print_global_action_cpp`, action)
}

add_global_action_cpp <- function(model, action) {
.Call(`_epiworldR_add_global_action_cpp`, model, action)
add_globalevent_cpp <- function(model, action) {
.Call(`_epiworldR_add_globalevent_cpp`, model, action)
}

rm_global_action_cpp <- function(model, name) {
.Call(`_epiworldR_rm_global_action_cpp`, model, name)
rm_globalevent_cpp <- function(model, name) {
.Call(`_epiworldR_rm_globalevent_cpp`, model, name)
}

globalaction_fun_cpp <- function(fun, name, day) {
.Call(`_epiworldR_globalaction_fun_cpp`, fun, name, day)
globalevent_fun_cpp <- function(fun, name, day) {
.Call(`_epiworldR_globalevent_fun_cpp`, fun, name, day)
}

get_agents_cpp <- function(model) {
Expand Down
7 changes: 7 additions & 0 deletions R/functions-renamed.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' Deprecated functions in epiworldR
#' @description
#' Starting version 0.0-4, epiworld changed how it refered to "actions."
#' Following more traditional ABMs, actions are now called "events."
#' @param ... Arguments to be passed to the new function.
#' @name epiworldR-deprecated
NULL
120 changes: 84 additions & 36 deletions R/global-actions.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#'
#'
#' # Adding a global action
#' vaccine_day_20 <- globalaction_tool(epitool, .2, day = 20)
#' add_global_action(model_sirconn, vaccine_day_20)
#' vaccine_day_20 <- globalevent_tool(epitool, .2, day = 20)
#' add_globalevent(model_sirconn, vaccine_day_20)
#'
#' # Running and printing
#' run(model_sirconn, ndays = 40, seed = 1912)
Expand All @@ -49,107 +49,143 @@
#' recovery_rate = 0.95
#' )
#'
#' closure_day_10 <- globalaction_set_params("Contact rate", 0, day = 10)
#' add_global_action(model_sirconn2, closure_day_10)
#' closure_day_10 <- globalevent_set_params("Contact rate", 0, day = 10)
#' add_globalevent(model_sirconn2, closure_day_10)
#'
#' # Running and printing
#' run(model_sirconn2, ndays = 40, seed = 1912)
#' model_sirconn2
#' plot_incidence(model_sirconn2)
#' @returns
#' - The `globalaction_set_params` function returns an object of class
#' [epiworld_globalaction_set_param] and [epiworld_globalaction].
#' - The `globalevent_set_params` function returns an object of class
#' [epiworld_globalevent_set_param] and [epiworld_globalevent].
#'
#' - `globalaction_tool` returns an object of class
#' [epiworld_globalaction_tool] and [epiworld_globalaction].
#' - `globalevent_tool` returns an object of class
#' [epiworld_globalevent_tool] and [epiworld_globalevent].
#'
#' - `globalaction_tool_logit` returns an object of class
#' [epiworld_globalaction_tool_logit] and [epiworld_globalaction].
#' - `globalevent_tool_logit` returns an object of class
#' [epiworld_globalevent_tool_logit] and [epiworld_globalevent].
#' @aliases
#' epiworld_globalaction_set_param
#' epiworld_globalaction_tool
#' epiworld_globalaction_tool_logit
#' epiworld_globalaction
#' epiworld_globalevent_set_param
#' epiworld_globalevent_tool
#' epiworld_globalevent_tool_logit
#' epiworld_globalevent
#' actions
#'
globalaction_tool <- function(
globalevent_tool <- function(
tool, prob,
name = get_name_tool(tool), day = -99
) {

structure(
globalaction_tool_cpp(tool, prob, name, day),
class = c("epiworld_globalaction_tool", "epiworld_globalaction"),
globalevent_tool_cpp(tool, prob, name, day),
class = c("epiworld_globalevent_tool", "epiworld_globalevent"),
tool = tool,
call = match.call()
)
}

#' @export
#' @rdname epiworldR-deprecated
globalaction_tool <- function(...) {

.Deprecated(
new = "globalevent_tool"
)

globalevent_tool(...)

}

#' @export
#' @rdname global-actions
#' @param vars Integer vector. The position of the variables in the model.
#' @param coefs Numeric vector. The coefficients of the logistic regression.
#' @details The function `globalaction_tool_logit` allows to specify a logistic
#' @details The function `globalevent_tool_logit` allows to specify a logistic
#' regression model for the probability of using a tool. The model is specified
#' by the vector of coefficients `coefs` and the vector of variables `vars`.
#' `vars` is an integer vector indicating the position of the variables in the
#' model.
globalaction_tool_logit <- function(
globalevent_tool_logit <- function(
tool, vars, coefs,
name = get_name_tool(tool), day = -99
) {

stopifnot_tool(tool)

structure(
globalaction_tool_logit_cpp(
globalevent_tool_logit_cpp(
tool,
as.integer(vars),
as.double(coefs),
name,
as.integer(day)
),
class = c("epiworld_globalaction_tool_logit", "epiworld_globalaction"),
class = c("epiworld_globalevent_tool_logit", "epiworld_globalevent"),
tool = tool,
call = match.call()
)

}

#' @export
#' @rdname epiworldR-deprecated
globalaction_tool_logit <- function(...) {

.Deprecated(
new = "globalevent_tool_logit"
)

globalevent_tool_logit(...)

}

#' @export
#' @param param Character scalar. The name of the parameter to be set.
#' @param value Numeric scalar. The value of the parameter.
#' @rdname global-actions
#' @details The function `globalaction_set_param` allows to set a parameter of
#' @details The function `globalevent_set_param` allows to set a parameter of
#' the model. The parameter is specified by its name `param` and the value by
#' `value`.
globalaction_set_params <- function(
globalevent_set_params <- function(
param, value,
name = paste0("Set ", param, " to ", value), day = -99
) {

structure(
globalaction_set_param_cpp(
globalevent_set_param_cpp(
param,
as.double(value),
name,
as.integer(day)
),
class = c("epiworld_globalaction_set_param", "epiworld_globalaction"),
class = c("epiworld_globalevent_set_param", "epiworld_globalevent"),
param = param,
value = as.double(value),
call = match.call()
)
}

#' @export
#' @rdname epiworldR-deprecated
globalaction_set_params <- function(...) {

.Deprecated(
new = "globalevent_set_params"
)

globalevent_set_params(...)

}

#' @export
#' @rdname global-actions
#' @param fun Function. The function to be executed.
#' @details The function `globalaction_fun` allows to specify a function to be
#' @details The function `globalevent_fun` allows to specify a function to be
#' executed at a given day. The function object must receive an object of class
#' [epiworld_model] as only argument.
#' @examples
#' # Example using `globalaction_fun` to record the state of the
#' # Example using `globalevent_fun` to record the state of the
#' # agents at each time step.
#'
#' # We start by creating an SIR connected model
Expand Down Expand Up @@ -179,27 +215,39 @@ globalaction_set_params <- function(
#' )
#'
#' }
globalaction_fun <- function(
globalevent_fun <- function(
fun, name = deparse(substitute(fun)), day = -99
) {

structure(
globalaction_fun_cpp(fun, name, as.integer(day)),
class = c("epiworld_globalaction_fun", "epiworld_globalaction"),
globalevent_fun_cpp(fun, name, as.integer(day)),
class = c("epiworld_globalevent_fun", "epiworld_globalevent"),
fun = fun,
call = match.call()
)

}

#' @export
print.epiworld_globalaction <- function(x, ...) {
#' @rdname epiworldR-deprecated
globalaction_fun <- function(...) {

.Deprecated(
new = "globalevent_fun"
)

globalevent_fun(...)

}

#' @export
print.epiworld_globalevent <- function(x, ...) {

print_global_action_cpp(x)
cat("Call: ", deparse(attr(x, "call")), "\n")
if (length(attr(x, "tool"))) {
cat("Tool: ", get_name_tool(attr(x, "tool")), "\n")
} else if (inherits(x, "epiworld_globalaction_set_param")) {
} else if (inherits(x, "epiworld_globalevent_set_param")) {
cat("Parameter: ", attr(x, "param"), "\n")
cat("Value: ", attr(x, "value"), "\n")
}
Expand All @@ -215,20 +263,20 @@ print.epiworld_globalaction <- function(x, ...) {
#' @param name Character scalar. The name of the action.
#' @rdname global-actions
#' @seealso epiworld-model
#' @details The function `add_global_action` adds a global action to a model.
#' @details The function `add_globalevent` adds a global action to a model.
#' The model checks for actions to be executed at each time step. If the added
#' action matches the current time step, the action is executed. When `day` is
#' negative, the action is executed at each time step. When `day` is positive,
#' the action is executed at the specified time step.
#' @returns
#' - The function `add_global_action` returns the model with the added
#' - The function `add_globalevent` returns the model with the added
#' action.
add_global_action <- function(model, action) {
add_globalevent <- function(model, action) {

if (length(attr(action, "tool")))
add_tool_n(model, attr(action, "tool"), 0)

invisible(add_global_action_cpp(model, action))
invisible(add_globalevent_cpp(model, action))

}

2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ net <- get_transmissions(sir)
# Plotting
library(epiworldR)
library(netplot)
library(epiworldR)
x <- igraph::graph_from_edgelist(
as.matrix(net[,2:3]) + 1
)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ net <- get_transmissions(sir)

# Plotting
library(epiworldR)
library(netplot)
library(epiworldR)
#> Loading required package: grid
x <- igraph::graph_from_edgelist(
as.matrix(net[,2:3]) + 1
Expand Down
Loading

0 comments on commit 5fd7ad1

Please sign in to comment.