Skip to content

Commit

Permalink
Merge pull request #28 from inbo/27-create-helper-build_taxonomy
Browse files Browse the repository at this point in the history
Create helper `build_taxonomy()`
  • Loading branch information
PietrH authored Mar 20, 2024
2 parents 34ed046 + cccf821 commit b58d8eb
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ BugReports: https://github.com/inbo/camtrapdp/issues
Imports:
cli,
dplyr,
frictionless
frictionless,
purrr
Suggests:
testthat (>= 3.0.0)
Encoding: UTF-8
Expand Down
26 changes: 26 additions & 0 deletions R/build_taxonomy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Build a data frame with taxonomic information
#'
#' Builds a data frame from the `taxonomy` property in a Camera Trap Data
#' Package object.
#'
#' @inheritParams version
#' @return Data frame with the taxonomic information.
#' @noRd
build_taxonomy <- function(x) {
# Extract the taxonomic information
taxonomic_list <- purrr::pluck(x, "taxonomic")

# If there is no taxonomic information, return NULL
if (is.null(taxonomic_list)) {
return(NULL)
}

# Convert list into a data.frame
purrr::map(
taxonomic_list,
purrr::list_flatten,
name_spec = "{outer}.{inner}"
) %>%
purrr::map(as.data.frame) %>%
purrr::list_rbind()
}
144 changes: 144 additions & 0 deletions tests/testthat/test-build_taxonomy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
test_that("build_taxonomy() returns a data frame", {
skip_if_offline()
x <- example_dataset()
expect_s3_class(build_taxonomy(x), "data.frame")
})

test_that("build_taxonomy() returns NULL when there is no taxonomic information", {
skip_if_offline()
x <- example_dataset()
x$taxonomic <- NULL

expect_null(build_taxonomy(x))
})

test_that("build_taxonomy() returns one row per species in $data$observations", {
skip_if_offline()
x <- example_dataset()
number_of_species <-
dplyr::n_distinct(x$data$observations$scientificName, na.rm = TRUE)

expect_identical(
nrow(build_taxonomy(x)),
number_of_species
)
})

test_that("build_taxonomy() returns the expected columns", {
skip_if_offline()
x <- example_dataset()
expect_named(
build_taxonomy(x),
c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng",
"vernacularNames.nld")
)
})

test_that("build_taxonomy() creates a column per language for vernacularName", {
x <- example_dataset()
taxonomy_many_languages <- list(
list(
scientificName = "Anas platyrhynchos",
taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6",
taxonRank = "species",
vernacularNames = list(
eng = "mallard",
nld = "wilde eend",
est = "sinikael-part",
glv = "Laagh Voirrey",
wel = "Hwyaden Wyllt",
afr = "Groenkopeend"
)
)
)
x$taxonomic <- taxonomy_many_languages

# Expect 6 vernacularName columns
expect_length(
dplyr::select(build_taxonomy(x), dplyr::starts_with("vernacularNames.")),
6
)

# Expect the right vernacularName columns
expect_named(
dplyr::select(build_taxonomy(x), dplyr::starts_with("vernacularNames.")),
c(
"vernacularNames.eng", "vernacularNames.nld", "vernacularNames.est",
"vernacularNames.glv", "vernacularNames.wel", "vernacularNames.afr"
)
)
})

test_that("build_taxonomy() can handle missing vernacular names", {
x <- example_dataset()
# Create a taxonomy where the English vernacularName of Anas strepera is not
# provided.
taxonomy_missing_vernacular <- list(
list(
scientificName = "Anas platyrhynchos",
taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6",
taxonRank = "species",
vernacularNames = list(
eng = "mallard",
nld = "wilde eend"
)
),
list(
scientificName = "Anas strepera",
taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGPL",
taxonRank = "species",
vernacularNames = list(
nld = "krakeend"
)
)
)
x$taxonomic <- taxonomy_missing_vernacular

# Check that we still get the `vernacularNames.` prefix
expect_named(
build_taxonomy(x),
c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng",
"vernacularNames.nld")
)
})

test_that("build_taxonomy() fills missing values with NA when a taxonomic field
is only present for some of the records", {
x <- example_dataset()
taxonomy_missing_vernaculars <- list(
list(
scientificName = "Anas platyrhynchos",
taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6",
taxonRank = "species",
vernacularNames = list(
eng = "mallard"
)
),
list(
scientificName = "Anas strepera",
taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGPL",
taxonRank = "species",
vernacularNames = list(
nld = "krakeend"
)
)
)
x$taxonomic <- taxonomy_missing_vernaculars

# Check that we still get the `vernacularNames.` prefix
expect_named(
build_taxonomy(x),
c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng",
"vernacularNames.nld")
)
# Check that the Dutch vernacular name column contains an NA
expect_contains(
dplyr::pull(build_taxonomy(x), vernacularNames.nld),
NA_character_
)
# Check that the English vernacular name column contains an NA
expect_contains(
dplyr::pull(build_taxonomy(x), vernacularNames.eng),
NA_character_
)
})

0 comments on commit b58d8eb

Please sign in to comment.