Skip to content

Commit

Permalink
Make conversion of parameters (N, times) to integers explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
bodkan committed Apr 20, 2023
1 parent 3211bac commit b7e89e4
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 3 deletions.
28 changes: 26 additions & 2 deletions R/interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#' \code{NULL} value), the population will be allowed to occupy the entire
#' landscape.
#'
#' Note that because slendr models have to accomodate both SLiM and msprime
#' back ends, population sizes and split times are rounded to the nearest
#' integer value.
#'
#' @param name Name of the population
#' @param time Time of the population's first appearance
#' @param N Number of individuals at the time of first appearance
Expand Down Expand Up @@ -55,8 +59,8 @@ population <- function(name, time, N, parent = NULL, map = FALSE,
if (!is.character(name) || length(name) != 1)
stop("A population name must be a character scalar value", call. = FALSE)

N <- round(N)
time <- round(time)
N <- as.integer(round(N))
time <- as.integer(round(time))

if (time < 1) stop("Split time must be a non-negative number", call. = FALSE)
if (N < 1) stop("Population size must be a non-negative number", call. = FALSE)
Expand Down Expand Up @@ -282,6 +286,10 @@ move <- function(pop, trajectory, end, start, overlap = 0.8, snapshots = NULL,
#' Expands the spatial population range by a specified distance in a given
#' time-window
#'
#' Note that because slendr models have to accomodate both SLiM and msprime
#' back ends, population sizes and times of events are rounded to the nearest
#' integer value.
#'
#' @param pop Object of the class \code{slendr_pop}
#' @param by How many units of distance to expand by?
#' @param start,end When does the expansion start/end?
Expand All @@ -308,6 +316,10 @@ move <- function(pop, trajectory, end, start, overlap = 0.8, snapshots = NULL,
expand_range <- function(pop, by, end, start, overlap = 0.8, snapshots = NULL,
polygon = NULL, lock = FALSE, verbose = TRUE) {
if (!has_map(pop)) stop("This operation is only allowed for spatial models", call. = FALSE)

start <- as.integer(round(start))
end <- as.integer(round(end))

shrink_or_expand(pop, by, end, start, overlap, snapshots, polygon, lock, verbose)
}

Expand All @@ -317,6 +329,10 @@ expand_range <- function(pop, by, end, start, overlap = 0.8, snapshots = NULL,
#' Shrinks the spatial population range by a specified distance in a given
#' time-window
#'
#' Note that because slendr models have to accomodate both SLiM and msprime
#' back ends, population sizes and split times are rounded to the nearest
#' integer value.
#'
#' @param pop Object of the class \code{slendr_pop}
#' @param by How many units of distance to shrink by?
#' @param start,end When does the boundary shrinking start/end?
Expand Down Expand Up @@ -437,6 +453,10 @@ set_range <- function(pop, time, center = NULL, radius = NULL,
#' specified time period until it reaches \code{N} individuals. If \code{N} is
#' smaller, the population will shrink exponentially.
#'
#' Note that because slendr models have to accomodate both SLiM and msprime
#' back ends, population sizes and split times are rounded to the nearest
#' integer value.
#'
#' @param pop Object of the class \code{slendr_pop}
#' @param N Population size after the change
#' @param how How to change the population size (options are \code{"step"} or
Expand All @@ -456,6 +476,10 @@ set_range <- function(pop, time, center = NULL, radius = NULL,
resize <- function(pop, N, how, time, end = NULL) {
if (N < 1) stop("resize(): Only positive, non-zero population sizes are allowed", call. = FALSE)

N <- as.integer(round(N))
time <- as.integer(round(time))
if (!is.null(end)) end <- as.integer(round(end))

if (!how %in% c("step", "exponential"))
stop("resize(): Only 'step' or 'exponential' are allowed as arguments for the 'how' parameter", call. = FALSE)

Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ seconds, but if you don't want to wait, you can set `snapshots = N` manually.")
if (lock) {
areas <- as.numeric(sapply(inter_regions, sf::st_area))
area_changes <- areas[-1] / areas[-length(areas)]
new_N <- round(cumprod(area_changes) * prev_N)
new_N <- as.integer(round(cumprod(area_changes) * prev_N))
prev_N <- c(prev_N, new_N[-length(new_N)])
times <- sapply(inter_regions[-1], `[[`, "time")
changes <- lapply(seq_len(length(new_N)), function(i) {
Expand Down
Binary file modified docs/reference/Rplot001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions docs/reference/expand_range.html

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

3 changes: 3 additions & 0 deletions docs/reference/population.html

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

3 changes: 3 additions & 0 deletions docs/reference/resize.html

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

6 changes: 6 additions & 0 deletions docs/reference/shrink_range.html

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

5 changes: 5 additions & 0 deletions man/expand_range.Rd

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

4 changes: 4 additions & 0 deletions man/population.Rd

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

4 changes: 4 additions & 0 deletions man/resize.Rd

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

5 changes: 5 additions & 0 deletions man/shrink_range.Rd

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

0 comments on commit b7e89e4

Please sign in to comment.