Skip to content
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

Simplifies code and add initial_state #10

Merged
merged 7 commits into from
Dec 3, 2023
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
20 changes: 17 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ docker-debug:
EPI_CONFIG="-DEPI_DEBUG -Wall -pedantic -g" R CMD INSTALL \
--no-docs --build .

install: build
which R
cd ../ && R CMD INSTALL epiworldR_*.tar.gz
install-dev: clean
sed -i -E 's/@useDynLib\s+[a-zA-Z]+/@useDynLib epiworldRdev/g' R/epiworldR-package.R
sed -i -E 's/useDynLib\(+[a-zA-Z]+/useDynLib(epiworldRdev/g' NAMESPACE
sed -i -E 's/^Package:.+/Package: epiworldRdev/g' DESCRIPTION
sed -i -E 's/^library\([a-zA-Z]+\)/library(epiworldRdev)/g' README.*
Rscript --vanilla -e 'roxygen2::roxygenize()'
EPI_DEV=yes R CMD INSTALL .& $(MAKE) clean

install:
$(MAKE) clean
R CMD INSTALL .


README.md: README.Rmd
Rscript --vanilla -e 'rmarkdown::render("README.Rmd")'
Expand All @@ -29,6 +38,11 @@ check: build

clean:
Rscript --vanilla -e 'devtools::clean_dll()'
sed -i -E 's/@useDynLib\s+[a-zA-Z]+/@useDynLib epiworldR/g' R/epiworldR-package.R
sed -i -E 's/useDynLib\(+[a-zA-Z]+/useDynLib(epiworldR/g' NAMESPACE
sed -i -E 's/^Package:.+/Package: epiworldR/g' DESCRIPTION
# sed -i -E 's/^\\(name|alias|title)\{[a-zA-Z]+/\\\1{epiworldR/g' man/epiworldR-package.Rd
sed -i -E 's/^library\([a-zA-Z]+\)/library(epiworldR)/g' README.*

docs:
Rscript --vanilla -e 'devtools::document()'
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export(get_agents)
export(get_agents_data_ncols)
export(get_agents_states)
export(get_agents_tools)
export(get_agents_viruses)
export(get_generation_time)
export(get_hist_tool)
export(get_hist_total)
Expand Down Expand Up @@ -135,6 +134,7 @@ export(globalaction_tool)
export(globalaction_tool_logit)
export(has_tool)
export(has_virus)
export(initial_states)
export(make_saver)
export(plot_generation_time)
export(plot_incidence)
Expand Down
5 changes: 5 additions & 0 deletions R/ModelSEIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#' @export
#' @family Models
#' @aliases epiworld_seir
#' @details
#' The [initial_states] function allows the user to set the initial state of the
#' model. The user must provide a vector of proportions indicating the following
#' values: (1) Proportion of non-infected agents who are removed, and (2)
#' Proportion of exposed agents to be set as infected.
#' @returns
#' - The `ModelSEIR`function returns a model of class [epiworld_model].
#' @examples
Expand Down
6 changes: 6 additions & 0 deletions R/ModelSEIRD.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#' @param x Object of class SEIRD.
#' @param ... Currently ignore.
#' @export
#' @details
#' The [initial_states] function allows the user to set the initial state of the
#' model. The user must provide a vector of proportions indicating the following
#' values: (1) Proportion of exposed agents who are infected, (2)
#' proportion of non-infected agents already removed, and (3) proportion of
#' non-ifected agents already deceased.
#' @family Models
#' @aliases epiworld_seird
#' @returns
Expand Down
6 changes: 6 additions & 0 deletions R/ModelSEIRDCONN.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#' @param ... Currently ignore.
#' @param n Number of individuals in the population.
#' @export
#' @details
#' The [initial_states] function allows the user to set the initial state of the
#' model. The user must provide a vector of proportions indicating the following
#' values: (1) Proportion of exposed agents who are infected, (2)
#' proportion of non-infected agents already removed, and (3) proportion of
#' non-ifected agents already deceased.
#' @family Models
#' @aliases epiworld_seirdconn
#' @returns
Expand Down
4 changes: 4 additions & 0 deletions R/ModelSIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#' @param ... Additional arguments passed to [graphics::plot].
#' @export
#' @family Models
#' @details
#' The [initial_states] function allows the user to set the initial state of the
#' model. In particular, the user can specify how many of the non-infected
#' agents have been removed at the beginning of the simulation.
#' @aliases epiworld_sir
#' @returns
#' - The `ModelSIR` function returns a model of class [epiworld_model].
Expand Down
4 changes: 4 additions & 0 deletions R/ModelSIRCONN.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#' @param n Number of individuals in the population.
#' @export
#' @family Models
#' @details
#' The [initial_states] function allows the user to set the initial state of the
#' model. In particular, the user can specify how many of the non-infected
#' agents have been removed at the beginning of the simulation.
#' @returns
#' - The `ModelSIRCONN`function returns a model of class [epiworld_model].
#' @aliases epiworld_sirconn
Expand Down
5 changes: 5 additions & 0 deletions R/ModelSIRD.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#' @param x Object of class SIR.
#' @param ... Additional arguments passed to [graphics::plot].
#' @export
#' @details
#' The [initial_states] function allows the user to set the initial state of the
#' model. The user must provide a vector of proportions indicating the following
#' values: (1) proportion of non-infected agents already removed, and (2) proportion of
#' non-ifected agents already deceased.
#' @family Models
#' @aliases epiworld_sird
#' @returns
Expand Down
5 changes: 5 additions & 0 deletions R/ModelSIRDCONN.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#' @param ... Currently ignore.
#' @param n Number of individuals in the population.
#' @export
#' @details
#' The [initial_states] function allows the user to set the initial state of the
#' model. The user must provide a vector of proportions indicating the following
#' values: (1) proportion of non-infected agents already removed, and (2) proportion of
#' non-ifected agents already deceased.
#' @family Models
#' @returns
#' - The `ModelSIRDCONN`function returns a model of class [epiworld_model].
Expand Down
12 changes: 4 additions & 8 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ get_network_cpp <- function(model) {
.Call(`_epiworldR_get_network_cpp`, model)
}

initial_states_cpp <- function(model, proportions) {
.Call(`_epiworldR_initial_states_cpp`, model, proportions)
}

tool_cpp <- function(name, susceptibility_reduction, transmission_reduction, recovery_enhancer, death_reduction) {
.Call(`_epiworldR_tool_cpp`, name, susceptibility_reduction, transmission_reduction, recovery_enhancer, death_reduction)
}
Expand Down Expand Up @@ -427,11 +431,3 @@ get_name_virus_cpp <- function(virus) {
set_name_virus_cpp <- function(virus, name) {
.Call(`_epiworldR_set_name_virus_cpp`, virus, name)
}

get_agents_viruses_cpp <- function(model) {
.Call(`_epiworldR_get_agents_viruses_cpp`, model)
}

print_agent_viruses_cpp <- function(viruses) {
.Call(`_epiworldR_print_agent_viruses_cpp`, viruses)
}
5 changes: 5 additions & 0 deletions R/epiworldR-package.R.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#' @EPIWORLD_NAME@
#' @useDynLib @EPIWORLD_NAME@, .registration = TRUE
#' @importFrom graphics boxplot plot
"_PACKAGE"

14 changes: 14 additions & 0 deletions R/model-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,17 @@ get_tool <- function(model, tool_pos) {
class = "epiworld_tool"
)
}

#' @export
#' @param proportions Numeric vector. Proportions in which agents will be
#' distributed (see details).
#' @return
#' - `inital_states` returns the model with an updated initial state.
#' @rdname epiworld-methods
initial_states <- function(model, proportions) {

stopifnot_model(model)
invisible(initial_states_cpp(model, proportions))

}

19 changes: 0 additions & 19 deletions R/virus.R
Original file line number Diff line number Diff line change
Expand Up @@ -536,25 +536,6 @@ set_incubation_fun <- function(virus, model, vfun) {

}

#' @export
#' @rdname agents_smallworld
#' @returns
#' - `get_agents_viruses` returns a list of class `epiworld_agents_viruses`
#' with `epiworld_viruses` (list of lists).
get_agents_viruses <- function(model) {

stopifnot_model(model)

res <- lapply(
get_agents_viruses_cpp(model),
`class<-`,
"epiworld_viruses"
)

structure(res, class = c("epiworld_agents_viruses", class(res)))

}

#' @export
#' @rdname virus
#' @param max_print Numeric scalar. Maximum number of viruses to print.
Expand Down
12 changes: 7 additions & 5 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ library(epiworldR)

# Creating a SIR model
sir <- ModelSIR(
name = "COVID-19",
prevalence = .01,
name = "COVID-19",
prevalence = .01,
transmission_rate = .7,
recovery = .3
recovery = .3
) |>
# Adding a Small world population
agents_smallworld(n = 100000, k = 10, d = FALSE, p = .01) |>
Expand Down Expand Up @@ -204,9 +204,11 @@ sir <- ModelSIR(
net <- get_transmissions(sir)

# Plotting
library(epiworldR)
library(netplot)
library(igraph)
x <- graph_from_edgelist(as.matrix(net[,2:3]) + 1)
x <- igraph::graph_from_edgelist(
as.matrix(net[,2:3]) + 1
)

nplot(x, edge.curvature = 0, edge.color = "gray", skip.vertex=TRUE)
```
Expand Down
53 changes: 22 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ library(epiworldR)

# Creating a SIR model
sir <- ModelSIR(
name = "COVID-19",
prevalence = .01,
name = "COVID-19",
prevalence = .01,
transmission_rate = .7,
recovery = .3
recovery = .3
) |>
# Adding a Small world population
agents_smallworld(n = 100000, k = 10, d = FALSE, p = .01) |>
Expand Down Expand Up @@ -127,8 +127,8 @@ summary(sir)
#> Number of entities : 0
#> Days (duration) : 50 (of 50)
#> Number of viruses : 1
#> Last run elapsed t : 196.00ms
#> Last run speed : 25.43 million agents x day / second
#> Last run elapsed t : 166.00ms
#> Last run speed : 30.01 million agents x day / second
#> Rewiring : off
#>
#> Global actions:
Expand Down Expand Up @@ -303,16 +303,16 @@ rn <- get_reproductive_number(model_logit)
# Looking into the agents
get_agents(model_logit)
#> Agents from the model "Susceptible-Infected-Removed (SIR) (logit)":
#> Agent: 0, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 1, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 2, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 3, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 4, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 5, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 6, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 7, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 8, state: Susceptible (0), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 9, state: Recovered (2), Nvirus: 0, NTools: 0, NNeigh: 8
#> Agent: 0, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 1, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 2, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 3, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 4, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 5, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 6, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 7, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 8, state: Susceptible (0), Has virus: no, NTools: 0, NNeigh: 8
#> Agent: 9, state: Recovered (2), Has virus: no, NTools: 0, NNeigh: 8
#> ... 99990 more agents ...
```

Expand Down Expand Up @@ -345,21 +345,12 @@ sir <- ModelSIR(
net <- get_transmissions(sir)

# Plotting
library(epiworldR)
library(netplot)
#> Loading required package: grid
library(igraph)
#>
#> Attaching package: 'igraph'
#> The following object is masked from 'package:netplot':
#>
#> ego
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
x <- graph_from_edgelist(as.matrix(net[,2:3]) + 1)
x <- igraph::graph_from_edgelist(
as.matrix(net[,2:3]) + 1
)

nplot(x, edge.curvature = 0, edge.color = "gray", skip.vertex=TRUE)
```
Expand Down Expand Up @@ -411,10 +402,10 @@ head(ans$reproductive)
#> sim_num virus_id virus source source_exposure_date rt
#> 1 1 0 COVID-19 767 11 0
#> 2 1 0 COVID-19 835 10 0
#> 3 1 0 COVID-19 466 9 0
#> 3 1 0 COVID-19 793 9 0
#> 4 1 0 COVID-19 612 9 0
#> 5 1 0 COVID-19 793 9 0
#> 6 1 0 COVID-19 20 8 0
#> 5 1 0 COVID-19 466 9 0
#> 6 1 0 COVID-19 920 8 0

plot(ans$reproductive)
```
Expand Down
17 changes: 16 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ PACKAGE_URL=''

ac_subst_vars='LTLIBOBJS
LIBOBJS
EPIWORLD_NAME
OPENMP_FLAG
CXXCPP
OBJEXT
Expand Down Expand Up @@ -3365,11 +3366,24 @@ printf "%s\n" "found and suitable" >&6; }
fi
fi

# Checking if the development version is being built
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether to build the dev version" >&5
printf %s "checking whether to build the dev version... " >&6; }
epiworldname="epiworldR"
if test x"${EPI_DEV}" != x"" ; then
epiworldname="epiworldRdev"
fi

## now use all these
OPENMP_FLAG="${openmp_flag}"

ac_config_files="$ac_config_files src/Makevars"
EPIWORLD_NAME="${epiworldname}"

ac_config_files="$ac_config_files src/Makevars R/epiworldR-package.R"


# Use sed to replace the line useDynLib\([a-zA-Z]+ in the NAMESPACE
# file with useDynLib(${epiworldname}

cat >confcache <<\_ACEOF
# This file is a shell script that caches the results of configure
Expand Down Expand Up @@ -4072,6 +4086,7 @@ for ac_config_target in $ac_config_targets
do
case $ac_config_target in
"src/Makevars") CONFIG_FILES="$CONFIG_FILES src/Makevars" ;;
"R/epiworldR-package.R") CONFIG_FILES="$CONFIG_FILES R/epiworldR-package.R" ;;

*) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
esac
Expand Down
13 changes: 12 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,19 @@ if test x"${can_use_openmp}" = x"yes"; then
fi
fi

# Checking if the development version is being built
AC_MSG_CHECKING([whether to build the dev version])
epiworldname="epiworldR"
if test x"${EPI_DEV}" != x"" ; then
epiworldname="epiworldRdev"
fi

## now use all these
AC_SUBST([OPENMP_FLAG], ["${openmp_flag}"])
AC_CONFIG_FILES([src/Makevars])
AC_SUBST([EPIWORLD_NAME], ["${epiworldname}"])
AC_CONFIG_FILES([src/Makevars R/epiworldR-package.R])

# Use sed to replace the line useDynLib\([a-zA-Z]+ in the NAMESPACE
# file with useDynLib(${epiworldname}

AC_OUTPUT
Loading
Loading