Skip to content

Commit

Permalink
Removed dependencies on srr and geosphere
Browse files Browse the repository at this point in the history
  • Loading branch information
japilo committed Sep 19, 2024
1 parent cd526ea commit a757d1f
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 73 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ URL: https://github.com/GlobalEcologyLab/poems, https://globalecologylab.github.
BugReports: https://github.com/GlobalEcologyLab/poems/issues
Encoding: UTF-8
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE, roclets = c ("namespace", "rd", "srr::srr_stats_roclet"))
Roxygen: list(markdown = TRUE)
Imports:
abc (>= 2.1),
doParallel (>= 1.0.16),
foreach (>= 1.5.1),
gdistance (>= 1.3.6),
geosphere (>= 1.5.10),
fossil (>= 0.4.0),
lhs (>= 1.1.1),
metRology (>= 0.9.28.1),
R6 (>= 2.5.0),
Expand Down Expand Up @@ -62,7 +62,6 @@ Collate:
'population_transformation.R'
'population_transitions.R'
'population_simulator.R'
'srr-stats-standards.R'
Suggests:
knitr,
rmarkdown,
Expand Down
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ importFrom(foreach,foreach)
importFrom(gdistance,costDistance)
importFrom(gdistance,geoCorrection)
importFrom(gdistance,transition)
importFrom(geosphere,distGeo)
importFrom(geosphere,distm)
importFrom(fossil,earth.dist)
importFrom(lhs,randomLHS)
importFrom(metRology,qtri)
importFrom(qs,qread)
Expand Down
10 changes: 4 additions & 6 deletions R/DispersalFriction.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#' R6 class representing a dispersal friction.
#' @srrstats {G4.0} WHen it writes to disk, this function writes .RData files
#' only and does not accept any other specification of file type/suffix.
#'
#'
#' @description
#' \code{\link[R6:R6Class]{R6}} class functionality for modeling sea, ice and other
#' frictional barriers to dispersal within a spatially-explicit population model. The
Expand Down Expand Up @@ -108,18 +106,18 @@ DispersalFriction <- R6Class("DispersalFriction",
dispersal_indices <- as.matrix(dispersal_indices)
}

# Convert to matrix if array
# Convert to matrix if array
if (is.array(dispersal_indices)) {
dispersal_indices <- as.matrix(dispersal_indices)
}

# Ensure dispersal indices are correctly set and are consistent with coordinates
if (is.null(dispersal_indices) || !all(is.integer(dispersal_indices)) ||
if (is.null(dispersal_indices) || !all(is.integer(dispersal_indices)) ||
!all(dispersal_indices >= 1) ||
!is.matrix(dispersal_indices) || ncol(dispersal_indices) != 2 ||
nrow(dispersal_indices) > self$region$region_cells^2 ||
max(dispersal_indices) > self$region$region_cells) {
stop("Dispersal indices must be a two-column matrix representing the
stop("Dispersal indices must be a two-column matrix representing the
target and source coordinate index for each in-range migration, or a
data.frame or array that can be converted to such a two-column matrix", call. = FALSE)
}
Expand Down
5 changes: 2 additions & 3 deletions R/DispersalGenerator.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
#' ))
#'
#' @importFrom R6 R6Class
#' @importFrom geosphere distm
#' @importFrom geosphere distGeo
#' @importFrom fossil earth.dist
#' @import raster
#' @include Generator.R
#' @include DispersalTemplate.R
Expand Down Expand Up @@ -202,7 +201,7 @@ DispersalGenerator <- R6Class("DispersalGenerator",
}
if (!self$region$use_raster || (is.logical(use_longlat) && use_longlat) ||
length(grep("longlat", as.character(raster::crs(self$region$region_raster)), fixed = TRUE)) > 0) {
return(distm(coordinates, coordinates, fun = distGeo) / self$distance_scale)
return(earth.dist(coordinates, dist = FALSE)*1000 / self$distance_scale)
} else { # assume coordinates in meters
if (is.na(raster::crs(self$region$region_raster))) {
warning("No coordinate reference system (CRS) specified: assuming coordinates are in meters", call. = FALSE)
Expand Down
8 changes: 3 additions & 5 deletions R/GenericManager.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#' R6 class representing a generic manager.
#' @srrstats {G4.0} When a results file suffix is not provided, this class
#' defaults to .RData.
#'
#'
#' @description
#' \code{\link[R6:R6Class]{R6}} class to represent a generic (abstract) manager for
#' generating or processing simulation results, as well as optionally generating values
#' via generators.
#'
#' @examples
#'
#' @examples
#' generic_manager <- GenericManager$new(
#' attr1 = 22:23,
#' results_filename_attributes = c("attr1", "example")
Expand Down
2 changes: 0 additions & 2 deletions R/ResultsManager.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#' R6 class representing a results manager.
#' @srrstats {G4.0} When a results file suffix is not provided, this class
#' fills it in with .RData.
#'
#' @description
#' \code{\link[R6:R6Class]{R6}} class to represent a manager for generating summary
Expand Down
2 changes: 0 additions & 2 deletions R/SimulationManager.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#' R6 class representing a simulation manager.
#' @srrstats {G4.0} When a results file suffix is not provided, this class
#' fills it in with .RData.
#'
#' @description
#' \code{\link[R6:R6Class]{R6}} class to represent a manager for running multiple model
Expand Down
4 changes: 1 addition & 3 deletions R/SpatialCorrelation.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
#' env_corr$calculate_distance_matrix() # km
#'
#' @importFrom R6 R6Class
#' @importFrom geosphere distm
#' @importFrom geosphere distGeo
#' @include SpatialModel.R
#' @export SpatialCorrelation

Expand Down Expand Up @@ -91,7 +89,7 @@ SpatialCorrelation <- R6Class("SpatialCorrelation",
}
if (!self$region$use_raster || (is.logical(use_longlat) && use_longlat) ||
length(grep("longlat", as.character(raster::crs(self$region$region_raster)), fixed = TRUE)) > 0) {
return(distm(coordinates, coordinates, fun = distGeo) / self$distance_scale)
return(earth.dist(coordinates, dist = FALSE)*1000 / self$distance_scale)
} else { # assume coordinates in meters
if (is.na(raster::crs(self$region$region_raster))) {
warning("No coordinate reference system (CRS) specified: assuming coordinates are in meters", call. = FALSE)
Expand Down
6 changes: 1 addition & 5 deletions R/Validator.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#' R6 class representing a pattern-oriented validator.
#'
#' @srrstats {G1.3} We clearly define here what pattern-oriented validation is.
#' @srrstats {G4.0} When validation diagnostics are written to disk, they are
#' always written as .pdf files. No alternative file suffix is accepted.
#'
#' @description
#' \code{\link[R6:R6Class]{R6}} class for pattern-oriented validation and simulation
#' model ensemble selection. Pattern-oriented validation is a statistical
#' approach to compare patterns generated in simulations against observed
#' empirical patterns.
#'
#'
#' The class wraps functionality for the validation approach,
#' typically utilizing an external library, the default being the approximate Bayesian
#' computation (ABC) \code{\link[abc:abc]{abc}} library, and includes methods for
Expand Down
8 changes: 0 additions & 8 deletions R/srr-stats-standards.R

This file was deleted.

12 changes: 0 additions & 12 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ knitr::opts_chunk$set(
)
```

```{r srr-tags, eval = FALSE, echo = FALSE}
#' General statistical software standards addressed in documentation outside of .R files
#'
#' @srrstats {G1.0} References from the literature for this package are given in the References section of this document.
#' @srrstats {G1.1} The README explains how this package relates to other algorithms.
#' @srrstats {G1.2} A life-cycle statement is provided in the CONTRIBUTING.md document.
#' @srrstats {G1.3} Here we define the statistical term "pattern-oriented modeling."
#' @srrstats {G1.4} All documentation of functions and classes is done with roxygen2 tags.
#' There are no non-exported functions or classes.
#' @srrstats {SP2.1} `poems` uses `sf` and not `sp`.
```

# poems: Pattern-oriented ensemble modeling system (for spatially explicit populations) <img src='man/figures/logo.svg' align="right" height="125" />

<!-- badges: start -->
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test_dispersal_friction.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,52 +85,52 @@ test_that("distance multiplier calculation errors", {
)
expect_error(
dispersal_friction$calculate_distance_multipliers(dispersal_indices = "wrong"),
"Dispersal indices must be a two-column matrix representing the
"Dispersal indices must be a two-column matrix representing the
target and source coordinate index for each in-range migration, or a
data.frame or array that can be converted to such a two-column matrix"
)
expect_error(
dispersal_friction$calculate_distance_multipliers(dispersal_indices = dispersal_indices - 1),
"Dispersal indices must be a two-column matrix representing the
"Dispersal indices must be a two-column matrix representing the
target and source coordinate index for each in-range migration, or a
data.frame or array that can be converted to such a two-column matrix"
)
expect_error(
dispersal_friction$calculate_distance_multipliers(dispersal_indices = dispersal_indices + 1),
"Dispersal indices must be a two-column matrix representing the
"Dispersal indices must be a two-column matrix representing the
target and source coordinate index for each in-range migration, or a
data.frame or array that can be converted to such a two-column matrix"
)
expect_error(
dispersal_friction$calculate_distance_multipliers(dispersal_indices = dispersal_indices / 3),
"Dispersal indices must be a two-column matrix representing the
"Dispersal indices must be a two-column matrix representing the
target and source coordinate index for each in-range migration, or a
data.frame or array that can be converted to such a two-column matrix"
)
expect_error(
dispersal_friction$calculate_distance_multipliers(dispersal_indices = as.vector(dispersal_indices)),
"Dispersal indices must be a two-column matrix representing the
"Dispersal indices must be a two-column matrix representing the
target and source coordinate index for each in-range migration, or a
data.frame or array that can be converted to such a two-column matrix"
)
expect_error(
dispersal_friction$calculate_distance_multipliers(dispersal_indices = cbind(dispersal_indices, dispersal_indices[, 1])),
"Dispersal indices must be a two-column matrix representing the
"Dispersal indices must be a two-column matrix representing the
target and source coordinate index for each in-range migration, or a
data.frame or array that can be converted to such a two-column matrix"
)
dispersal_indices <- which(distance_matrix > 0, arr.ind = TRUE)
expect_error(
dispersal_friction$calculate_distance_multipliers(dispersal_indices = rbind(dispersal_indices, dispersal_indices)),
"Dispersal indices must be a two-column matrix representing the
"Dispersal indices must be a two-column matrix representing the
target and source coordinate index for each in-range migration, or a
data.frame or array that can be converted to such a two-column matrix"
)
})

test_that("distance multiplier calculations", {
coordinates <- data.frame(x = rep(1:4, 4), y = rep(1:4, each = 4))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates) / 1000
dispersal_indices <- which(distance_matrix > 1 & distance_matrix <= 350, arr.ind = TRUE)
colnames(dispersal_indices) <- c("target_pop", "source_pop")
conductance_matrix <- array(1, c(16, 10))
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test_dispersal_generator.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_that("initialization and parameter setting", {
test_that("calculate distance matrix and classes", {
# Region with longlat coordinates
coordinates <- data.frame(x = rep(1:4, 4), y = rep(1:4, each = 4))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo)
distance_matrix <- fossil::earth.dist(coordinates, dist = FALSE)*1000
dispersal_gen <- DispersalGenerator$new()
expect_error(
dispersal_gen$calculate_distance_matrix(),
Expand Down Expand Up @@ -78,7 +78,7 @@ test_that("calculate distance matrix and classes", {

test_that("calculate distance data", {
coordinates <- data.frame(x = rep(1:4, 4), y = rep(1:4, each = 4))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
dispersal_gen <- DispersalGenerator$new()
# Manually calculate expected distance data (no dispersal friction)
distance_data <- which(distance_matrix > 1 & distance_matrix <= 400, arr.ind = TRUE)
Expand Down Expand Up @@ -121,7 +121,7 @@ test_that("calculate distance data", {

test_that("calculate dispersals", {
coordinates <- data.frame(x = rep(1:4, 4), y = rep(1:4, each = 4))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
dispersal_gen <- DispersalGenerator$new(coordinates = coordinates, distance_classes = seq(100, 400, 20))
# Pre-calculation required
expect_equal(
Expand Down Expand Up @@ -167,7 +167,7 @@ test_that("calculate dispersals", {

test_that("connect dispersal friction object", {
coordinates <- data.frame(x = rep(1:4, 4), y = rep(1:4, each = 4))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
dispersal_gen <- DispersalGenerator$new(coordinates = coordinates, distance_classes = seq(100, 400, 20))
dispersal_gen$calculate_distance_data(distance_matrix = distance_matrix)
# Errors, warnings, consistency checks
Expand Down Expand Up @@ -232,7 +232,7 @@ test_that("connect dispersal friction object", {
test_that("calculate distance data with dispersal friction object", {
# Region and conductance values with longlat coordinates
coordinates <- data.frame(x = rep(1:4, 4), y = rep(1:4, each = 4))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
# Distance data base as before
dispersal_gen <- DispersalGenerator$new(coordinates = coordinates, distance_classes = seq(100, 400, 20))
dispersal_gen$calculate_distance_data(distance_matrix = distance_matrix)
Expand Down Expand Up @@ -279,7 +279,7 @@ test_that("calculate distance data with dispersal friction object", {

test_that("calculate dispersals with dispersal friction", {
coordinates <- data.frame(x = rep(1:4, 4), y = rep(1:4, each = 4))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
conductance_matrix <- array(1, c(16, 10))
conductance_matrix[1, 1] <- 0.5
conductance_matrix[c(2, 3, 5, 6, 7, 9, 10, 11), 2] <- 0 # isolate coordinate (1, 1)
Expand Down Expand Up @@ -333,7 +333,7 @@ test_that("calculate dispersals with dispersal friction", {
region = region, distance_classes = seq(100, 400, 20),
proportion = 0.4, breadth = 110, max_distance = 300
)
distance_matrix <- geosphere::distm(region$coordinates, region$coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
distance_matrix[which(distance_matrix < 1)] <- 0 # ensure actual zero distance for self-referenced cells
dispersal_gen$calculate_distance_data(distance_matrix = distance_matrix)
dispersal_gen$calculate_dispersals()
Expand All @@ -353,7 +353,7 @@ test_that("calculate dispersals with dispersal friction", {

test_that("cloning and generation", {
coordinates <- data.frame(x = rep(1:4, 4), y = rep(1:4, each = 4))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
distance_matrix[which(distance_matrix < 1)] <- 0 # ensure actual zero distance for self-referenced cells
dispersal_gen <- DispersalGenerator$new(coordinates = coordinates, distance_classes = seq(100, 400, 20))
expect_equal(dispersal_gen$generative_requirements, list(dispersal_data = "default"))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_population_dispersal_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ test_that("density dependent dispersal", {
distance_scale = 1000,
distance_classes = seq(100, 400, 20)
)
distance_matrix <- geosphere::distm(region$coordinates, region$coordinates, fun = geosphere::distGeo) / 1000
distance_matrix <- fossil::earth.dist(coordinates, dist = F)
distance_matrix[which(distance_matrix < 1)] <- 0 # ensure actual zero distance for self-referenced cells
dispersal_gen$calculate_distance_data(distance_matrix = distance_matrix)
dispersal_gen$calculate_dispersals(type = "matrix")
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test_spatial_correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test_that("initialization and parameter setting", {
test_that("calculate distance matrix", {
# Latitude/longitude coordinates
coordinates1 <- array(c(1:4, 4:1), c(7, 2))
distance_matrix1 <- geosphere::distm(coordinates1, coordinates1, fun = geosphere::distGeo)
distance_matrix1 <- fossil::earth.dist(coordinates, dist = FALSE)*1000
spatial_correlation <- SpatialCorrelation$new()
expect_error(
spatial_correlation$calculate_distance_matrix(),
Expand Down Expand Up @@ -84,7 +84,7 @@ test_that("calculate distance matrix", {
test_that("calculate correlations", {
# Default threshold (0.0000001)
coordinates <- array(c(1:4, 4:1), c(7, 2))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000 # km
distance_matrix <- fossil::earth.dist(coordinates, dist = FALSE) # km
correlation_matrix <- 0.6 * exp(-1 * distance_matrix / 200)
diag(correlation_matrix) <- 1
spatial_correlation <- SpatialCorrelation$new(correlation_amplitude = 0.6, correlation_breadth = 200)
Expand Down Expand Up @@ -119,7 +119,7 @@ test_that("calculate correlations", {

test_that("calculate Cholesky decomposition", {
coordinates <- array(c(1:4, 4:1), c(7, 2))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000 # km
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
correlation_matrix <- 0.6 * exp(-1 * distance_matrix / 200)
diag(correlation_matrix) <- 1
spatial_correlation <- SpatialCorrelation$new(
Expand All @@ -137,7 +137,7 @@ test_that("calculate Cholesky decomposition", {

test_that("calculate compact decomposition", {
coordinates <- array(c(1:4, 4:1), c(7, 2))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000 # km
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
correlation_matrix <- 0.6 * exp(-1 * distance_matrix / 32)
diag(correlation_matrix) <- 1
spatial_correlation <- SpatialCorrelation$new(
Expand All @@ -158,7 +158,7 @@ test_that("calculate compact decomposition", {

test_that("generate correlated normal deviates", {
coordinates <- array(c(1:4, 4:1), c(7, 2))
distance_matrix <- geosphere::distm(coordinates, coordinates, fun = geosphere::distGeo) / 1000 # km
distance_matrix <- fossil::earth.dist(coordinates, dist = F) # km
correlation_matrix <- 0.6 * exp(-1 * distance_matrix / 32)
diag(correlation_matrix) <- 1
spatial_correlation <- SpatialCorrelation$new(
Expand Down

0 comments on commit a757d1f

Please sign in to comment.