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

Local seed #98

Merged
merged 5 commits into from
Sep 9, 2024
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
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ Imports:
stats,
terra,
tidyr,
vegan,
withr
vegan
Suggests:
ggExtra,
ggplot2,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ importFrom(terra,spatSample)
importFrom(terra,vect)
importFrom(tidyr,unnest)
importFrom(vegan,decostand)
importFrom(withr,local_seed)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Update `sample_occurrences_from_raster()`
- Use `lapply()` instead of for-loop
- Randomise points in raster cells.
* Fix issues (#76).
* Fix issues (#37, #76).

# gcube 0.3.0

Expand Down
3 changes: 0 additions & 3 deletions R/apply_manual_sampling_bias.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
#' # Get occurrence points
#' occurrences_sf <- simulate_occurrences(plgn)
#'
#' # Set seed for reproducibility
#' set.seed(123)
#'
#' # Create grid with bias weights
#' grid <- st_make_grid(
#' plgn,
Expand Down
3 changes: 0 additions & 3 deletions R/apply_polygon_sampling_bias.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
#' library(dplyr)
#' library(ggplot2)
#'
#' # Set seed for reproducibility
#' set.seed(123)
#'
#' # Simulate some occurrence data with coordinates and time points
#' num_points <- 10
#' occurrences <- data.frame(
Expand Down
11 changes: 8 additions & 3 deletions R/create_spatial_pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#' The default is `"random"`. `"clustered"` corresponds to a value of 10.
#' See Details.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#' @param n_sim Number of simulations. Each simulation is a different layer in
#' the raster. Default is 1.
#'
Expand All @@ -35,7 +37,6 @@
#' @importFrom stats predict
#' @importFrom terra vect rast rasterize
#' @importFrom gstat vgm gstat
#' @importFrom withr local_seed
#' @importFrom vegan decostand
#'
#' @family occurrence
Expand Down Expand Up @@ -149,7 +150,11 @@ create_spatial_pattern <- function(

# Set seed if provided
if (!is.na(seed)) {
withr::local_seed(seed)
if (exists(".Random.seed", envir = .GlobalEnv)) {
rng_state_old <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", rng_state_old, envir = .GlobalEnv))
}
set.seed(seed)
}

# Use gstat object with vgm model to create spatial pattern
Expand Down
11 changes: 8 additions & 3 deletions R/generate_taxonomy.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#' @param num_phyla Number of phyla to generate. Defaults to 1.
#' @param num_kingdoms Number of kingdoms to generate. Defaults to 1.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#'
#' @details The function works by randomly assigning species to genera, genera
#' to families, families to orders, orders to classes, classes to phyla, and
Expand All @@ -32,7 +34,6 @@
#'
#' @import dplyr
#' @import assertthat
#' @importFrom withr local_seed
#'
#' @family multispecies
#'
Expand Down Expand Up @@ -129,7 +130,11 @@ generate_taxonomy <- function(

# Set seed if provided
if (!is.na(seed)) {
withr::local_seed(seed)
if (exists(".Random.seed", envir = .GlobalEnv)) {
rng_state_old <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", rng_state_old, envir = .GlobalEnv))
}
set.seed(seed)
}

# Assign species to genera
Expand Down
6 changes: 3 additions & 3 deletions R/grid_designation.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#' `"row_names"` (the default), a new column `id` is created where the row names
#' represent the unique IDs.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#' @param aggregate Logical. If `TRUE` (default), returns data cube in
#' aggregated form (grid with the number of observations per grid cell).
#' Otherwise, returns sampled points within the uncertainty circle.
Expand Down Expand Up @@ -51,8 +53,6 @@
#' library(sf)
#' library(dplyr)
#'
#' set.seed(123)
#'
#' # Create four random points
#' n_points <- 4
#' xlim <- c(3841000, 3842000)
Expand Down
12 changes: 8 additions & 4 deletions R/sample_from_binormal_circle.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#' circle. Default is 0.95.
#' See Details.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#'
#' @details A new observation point is sampled from a bivariate Normal
#' distribution with means equal to the X and Y coordinates of its original
Expand All @@ -40,8 +42,6 @@
#' library(sf)
#' library(dplyr)
#'
#' set.seed(123)
#'
#' # Create four random points
#' n_points <- 4
#' xlim <- c(3841000, 3842000)
Expand Down Expand Up @@ -86,7 +86,11 @@ sample_from_binormal_circle <- function(

# Set seed if provided
if (!is.na(seed)) {
withr::local_seed(seed)
if (exists(".Random.seed", envir = .GlobalEnv)) {
rng_state_old <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", rng_state_old, envir = .GlobalEnv))
}
set.seed(seed)
}

# Set uncertainty to zero if column not present in data
Expand Down
12 changes: 8 additions & 4 deletions R/sample_from_uniform_circle.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#' the function will assume no uncertainty (zero meters) around the observation
#' points.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#'
#' @returns An sf object with POINT geometry containing the locations of the
#' sampled occurrences and a `coordinateUncertaintyInMeters` column containing
Expand All @@ -26,8 +28,6 @@
#' @examples
#' library(sf)
#'
#' set.seed(123)
#'
#' # Create four random points
#' n_points <- 4
#' xlim <- c(3841000, 3842000)
Expand Down Expand Up @@ -65,7 +65,11 @@ sample_from_uniform_circle <- function(

# Set seed if provided
if (!is.na(seed)) {
withr::local_seed(seed)
if (exists(".Random.seed", envir = .GlobalEnv)) {
rng_state_old <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", rng_state_old, envir = .GlobalEnv))
}
set.seed(seed)
}

# Set uncertainty to zero if column not present in data
Expand Down
14 changes: 8 additions & 6 deletions R/sample_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
#' Weights can be numeric values between 0 and 1 or positive integers, which
#' will be rescaled to values between 0 and 1.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#'
#' @returns An sf object with POINT geometry containing the locations of the
#' occurrence with detection status. The object includes the following columns:
Expand All @@ -63,7 +65,6 @@
#' @import dplyr
#' @import assertthat
#' @importFrom stats rbinom
#' @importFrom withr local_seed
#' @importFrom rlang .data
#'
#' @family main
Expand All @@ -73,9 +74,6 @@
#' library(sf)
#' library(dplyr)
#'
#' # Set seed for reproducibility
#' set.seed(123)
#'
#' # Simulate some occurrence data with coordinates and time points
#' num_points <- 10
#' occurrences <- data.frame(
Expand Down Expand Up @@ -164,7 +162,11 @@ sample_observations <- function(

# Set seed if provided
if (!is.na(seed)) {
withr::local_seed(seed)
if (exists(".Random.seed", envir = .GlobalEnv)) {
rng_state_old <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", rng_state_old, envir = .GlobalEnv))
}
set.seed(seed)
}

# Add detection probability
Expand Down
10 changes: 8 additions & 2 deletions R/sample_occurrences_from_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#' @param raster A SpatRaster object (see [terra::rast()]).
#' @param time_series A vector with the number of occurrences per time point.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#'
#' @returns An sf object with POINT geometry containing the locations of the
#' simulated occurrences, a `time_point` column indicating the associated
Expand Down Expand Up @@ -110,7 +112,11 @@ sample_occurrences_from_raster <- function(

# Set seed if provided
if (!is.na(seed)) {
withr::local_seed(seed)
if (exists(".Random.seed", envir = .GlobalEnv)) {
rng_state_old <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", rng_state_old, envir = .GlobalEnv))
}
set.seed(seed)
}

occ_pf_list <- lapply(seq_along(time_series), function(t) {
Expand Down
4 changes: 3 additions & 1 deletion R/simulate_occurrences.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#' The default is `"random"`. `"clustered"` corresponds to a value of 10.
#' See `create_spatial_pattern()`.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#'
#' @returns An sf object with POINT geometry containing the locations of the
#' simulated occurrences, a `time_point` column indicating the associated
Expand Down
11 changes: 8 additions & 3 deletions R/simulate_random_walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#' @param sd_step A positive numeric value indicating the standard deviation of
#' the random steps.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#'
#' @returns A vector of integers of length `n_time_points` with the average
#' number of occurrences.
Expand All @@ -19,7 +21,6 @@
#'
#' @import assertthat
#' @importFrom stats rnorm
#' @importFrom withr local_seed
#'
#' @family occurrence
#'
Expand Down Expand Up @@ -62,7 +63,11 @@ simulate_random_walk <- function(

# Set seed if provided
if (!is.na(seed)) {
withr::local_seed(seed)
if (exists(".Random.seed", envir = .GlobalEnv)) {
rng_state_old <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", rng_state_old, envir = .GlobalEnv))
}
set.seed(seed)
}

# Initialize an empty vector to store average abundance values
Expand Down
13 changes: 8 additions & 5 deletions R/simulate_timeseries.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#' occurrences.
#' @param ... Additional arguments to be passed to `temporal_function`.
#' @param seed A positive numeric value setting the seed for random number
#' generation to ensure reproducibility. If `NA` (default), no seed is used.
#' generation to ensure reproducibility. If `NA` (default), then `set.seed()`
#' is not called at all. If not `NA`, then the random number generator state is
#' reset (to the state before calling this function) upon exiting this function.
#'
#' @returns A vector of integers of length `n_time_points` with the number of
#' occurrences.
Expand Down Expand Up @@ -58,9 +60,6 @@
#' return(lambdas)
#' }
#'
#' # Set seed for reproducibility
#' set.seed(123)
#'
#' # Draw n_sim number of occurrences from Poisson distribution using
#' # the custom function
#' n_sim <- 10
Expand Down Expand Up @@ -135,7 +134,11 @@ simulate_timeseries <- function(

# Set seed if provided
if (!is.na(seed)) {
withr::local_seed(seed)
if (exists(".Random.seed", envir = .GlobalEnv)) {
rng_state_old <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", rng_state_old, envir = .GlobalEnv))
}
set.seed(seed)
}

# Check type of temporal_function
Expand Down
14 changes: 1 addition & 13 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,9 @@
},
"sameAs": "https://CRAN.R-project.org/package=vegan"
},
"13": {
"@type": "SoftwareApplication",
"identifier": "withr",
"name": "withr",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=withr"
},
"SystemRequirements": null
},
"fileSize": "549.627KB",
"fileSize": "550.823KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand Down
3 changes: 0 additions & 3 deletions man/apply_manual_sampling_bias.Rd

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

3 changes: 0 additions & 3 deletions man/apply_polygon_sampling_bias.Rd

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

4 changes: 3 additions & 1 deletion man/create_spatial_pattern.Rd

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

Loading