Skip to content

Commit

Permalink
Merge pull request #92 from b-cubed-eu/simple-mapping
Browse files Browse the repository at this point in the history
Simple mapping
  • Loading branch information
wlangera authored Jul 26, 2024
2 parents c91b7aa + 09c78d3 commit acb3a51
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "gcube: Simulating Biodiversity Data Cubes",
"version": "0.2.0",
"version": "0.3.0",
"license": "MIT",
"upload_type": "software",
"description": "<p>Simulation framework for biodiversity data cubes.<\/p>",
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ abstract: "Simulation framework for biodiversity data cubes."
identifiers:
- type: url
value: https://b-cubed-eu.github.io/gcube/
version: 0.2.0
version: 0.3.0
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gcube
Title: Simulating Biodiversity Data Cubes
Version: 0.2.0
Version: 0.3.0
Authors@R: c(
person("Ward", "Langeraert", , "ward.langeraert@inbo.be", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-5900-8109", affiliation = "Research Institute for Nature and Forest (INBO)")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# gcube 0.3.0

* `generate_taxonomy()` also creates species key.
* Fix bug for mapping simulate occurrences without specified temporal function.

# gcube 0.2.0

* Improve function checks with `stopifnot()` and the [assertthat](https://CRAN.R-project.org/package=assertthat) package.
Expand Down
10 changes: 7 additions & 3 deletions R/generate_taxonomy.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ generate_taxonomy <- function(

# Generate species names
species_df$species <- paste0("species", seq_len(num_species))

# Generate species keys
species_df$species_key <- seq_len(num_species)
} else {
# Generate species names
species_df <- data.frame(species = paste0("species", seq_len(num_species)))
species_df <- data.frame(species = paste0("species", seq_len(num_species)),
species_key = seq_len(num_species))
}

# Check if number of species is smaller than number of genera is smaller than
Expand Down Expand Up @@ -170,8 +174,8 @@ generate_taxonomy <- function(
dplyr::left_join(orders_to_classes, by = "order") %>%
dplyr::left_join(classes_to_phyla, by = "class") %>%
dplyr::left_join(phyla_to_kingdoms, by = "phylum") %>%
dplyr::select("species", "genus", "family", "order", "class", "phylum",
"kingdom", everything())
dplyr::select("species", "species_key", "genus", "family", "order", "class",
"phylum", "kingdom", everything())

return(taxonomy)
}
3 changes: 2 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ get_function_arguments <- function(f, df) {
f_args <- f_args_raw[f_args_raw != "..."]

# Also get argument names of temporal_function if necessary
if (identical(f, simulate_occurrences)) {
if (identical(f, simulate_occurrences) &&
"temporal_function" %in% colnames(df)) {
temp_f_args <- sapply(df$temporal_function, function(f) {
if (is.function(f)) methods::formalArgs(f)
})
Expand Down
6 changes: 3 additions & 3 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/b-cubed-eu/gcube",
"issueTracker": "https://github.com/b-cubed-eu/gcube/issues",
"license": "https://spdx.org/licenses/MIT",
"version": "0.2.0",
"version": "0.3.0",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -346,7 +346,7 @@
},
"SystemRequirements": null
},
"fileSize": "524.384KB",
"fileSize": "549.025KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand All @@ -358,7 +358,7 @@
"familyName": "Langeraert"
}
},
"name": "gcube: Simulating Biodiversity Data Cubes. Version 0.2.0",
"name": "gcube: Simulating Biodiversity Data Cubes. Version 0.3.0",
"url": "https://b-cubed-eu.github.io/gcube/"
}
],
Expand Down
4 changes: 2 additions & 2 deletions inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ citHeader("To cite `gcube` in publications please use:")
# begin checklist entry
bibentry(
bibtype = "Manual",
title = "gcube: Simulating Biodiversity Data Cubes. Version 0.2.0",
title = "gcube: Simulating Biodiversity Data Cubes. Version 0.3.0",
author = c( author = c(person(given = "Ward", family = "Langeraert"))),
year = 2024,
url = "https://b-cubed-eu.github.io/gcube/",
abstract = "Simulation framework for biodiversity data cubes.",
textVersion = "Langeraert, Ward (2024) gcube: Simulating Biodiversity Data Cubes. Version 0.2.0. https://b-cubed-eu.github.io/gcube/",
textVersion = "Langeraert, Ward (2024) gcube: Simulating Biodiversity Data Cubes. Version 0.3.0. https://b-cubed-eu.github.io/gcube/",
keywords = "simulation; data cubes; B-Cubed; biodiversity; Monte-Carlo",
)
# end checklist entry
5 changes: 2 additions & 3 deletions tests/testthat/test-generate_taxonomy.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Prepare example data
n_spec <- 5
colnames_tax <- c("species", "genus", "family", "order", "class", "phylum",
"kingdom")
colnames_tax <- c("species", "species_key", "genus", "family", "order", "class",
"phylum", "kingdom")

# Example dataframe
existing_df <- data.frame(
Expand All @@ -11,7 +11,6 @@ existing_df <- data.frame(


## Unit tests

test_that("generate_taxonomy function works correctly", {
# Is the output format correct?
# Testing with default values
Expand Down
25 changes: 24 additions & 1 deletion tests/testthat/test-map_grid_designation.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plgn <- st_polygon(list(cbind(c(5, 10, 8, 2, 3, 5), c(2, 1, 7, 9, 5, 2))))

# Create grid
cube_grid <- st_make_grid(
st_buffer(plgn, 25),
st_buffer(plgn, 50),
n = c(20, 20),
square = TRUE) %>%
st_sf()
Expand Down Expand Up @@ -106,6 +106,29 @@ test_that("map_grid_designation works with simple column names", {
expect_equal(occ_cube_unnested_test, occ_cube_unnested)
})

test_that("map_grid_designation works with pipes", {
occ_cube_piped <- tibble(
species = c("species1", "species2", "species3"),
plgn = rep(list(plgn), 3),
grid = rep(list(cube_grid), 3),
seed = 123
) %>%
map_simulate_occurrences() %>%
map_sample_observations() %>%
map_filter_observations() %>%
map_add_coordinate_uncertainty() %>%
map_grid_designation(nested = FALSE)

# Is the occurrence_cube_df column removed?
expect_false("occurrence_cube_df" %in% colnames(occ_cube_piped))
# Do we have the expected columns?
expect_equal(
sort(colnames(occ_cube_piped)),
sort(c("species", "plgn", "grid", "seed", "occurrences",
"observations_total", "observations", "time_point", "id", "n",
"min_coord_uncertainty", "geometry")))
})

test_that("map_grid_designation works with arg_list for renaming columns", {
# Test with arg_list
occ_cube_nested <- map_grid_designation(df = obs_uncertainty2,
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-map_simulate_occurrences.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ plgn <- st_polygon(list(cbind(c(5, 10, 8, 2, 3, 5), c(2, 1, 7, 9, 5, 2))))

# Specify dataframe for 3 species with custom function arguments
# Dataframe with column names equal to arguments
species_dataset_df <- tibble(
plgn = rep(list(plgn), 3),
n_time_points = rep(6, 3),
seed = 123)

species_dataset_df1 <- tibble(
taxonID = c("species1", "species2", "species3"),
plgn = rep(list(plgn), 3),
Expand All @@ -21,6 +26,19 @@ species_dataset_df2 <- species_dataset_df1 %>%


## Unit tests
test_that("map_simulate_occurrences works with simple column names", {
result <- map_simulate_occurrences(df = species_dataset_df)

# Are previous column names retained and one extra column name created?
expect_true("occurrences" %in% colnames(result))
expect_equal(sort(c(colnames(species_dataset_df), "occurrences")),
sort(colnames(result)))
# Is the new column a list-column?
expect_true(inherits(result$occurrences, "list"))
# Is the output of the function an sf object for each species (each row)?
expect_true(all(sapply(result$occurrences, inherits, "sf")))
})


test_that("map_simulate_occurrences works with simple column names", {
# Test with nested is TRUE
Expand Down

0 comments on commit acb3a51

Please sign in to comment.