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

284 add support for camtrap dp v1.0 #286

Merged
merged 39 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9e231ef
change supported version to v1.0 only
PietrH Nov 3, 2023
22cc1b3
refer to v1 camtrap-dp example instead of old rc
PietrH Nov 3, 2023
1261a5c
use release instead of commit
PietrH Nov 3, 2023
03cfcbb
check for supporting v1.0 not rc.1
PietrH Nov 3, 2023
a3710b0
no longer use taxonID for join as per camtrap-dp v1.0
PietrH Nov 3, 2023
1c4b612
fail earlier on missing scientificName, no longer use taxonID
PietrH Nov 3, 2023
08809e4
update to test for v1.0 `datapackage.json`
PietrH Nov 3, 2023
d75885a
set error expectation to v1.0
PietrH Nov 3, 2023
bbcb3fd
Downconvert taxonID to NA
PietrH Nov 3, 2023
5fda125
`dp_v1` instead of `dp_v1_rc1`
PietrH Nov 3, 2023
89c0202
refer to `v1.0` not release candidate
PietrH Nov 3, 2023
0615366
downconvert `deployments.cameraDepth` by removing it
PietrH Nov 3, 2023
61fcd0d
remove reference to release candidate
PietrH Nov 3, 2023
5f210ef
remove references to release candidate
PietrH Nov 3, 2023
7761d29
also add taxonIDReference if missing when downconverting
PietrH Nov 3, 2023
391c027
remove code from #282
PietrH Nov 3, 2023
8a31e59
remove references to release candidate
PietrH Nov 3, 2023
62c980d
recode captureMethod factor in downconversion
PietrH Nov 3, 2023
0899146
don't depend on forcats
PietrH Nov 3, 2023
d8e89c5
add test for replacing values in media.captureMethod
PietrH Nov 3, 2023
2ea4878
add test for downconverting captureMethod values in package$project
PietrH Nov 3, 2023
dc42fec
convert captureMethod to v0.1.6 values in `package$project`
PietrH Nov 3, 2023
395506d
remove references to release candidate
PietrH Nov 3, 2023
0813325
end file on linebreak
PietrH Nov 3, 2023
fb477ef
split off test, skip if no NA in deployments.csv
PietrH Nov 6, 2023
d703dfb
always keep deploymentGroup info, see comment in #286
PietrH Nov 6, 2023
9d8361a
avoid writeLines so message can be silenced
PietrH Nov 6, 2023
706592e
skip test if there are no NA in deploymentGroups, pass if one row is …
PietrH Nov 6, 2023
6878f41
Update Camtrap DP section in README
peterdesmet Nov 8, 2023
3f2b840
Update URL of Camtrap DP
peterdesmet Nov 8, 2023
48d5d71
Update supported versions message
peterdesmet Nov 8, 2023
b149c32
Change taxonID to character
peterdesmet Nov 8, 2023
ee4cea4
Simplify glue in messages
peterdesmet Nov 8, 2023
1c77116
Simplify check_value(), don't use custom transformer
peterdesmet Nov 8, 2023
868c307
Don't name regexp parameter
peterdesmet Nov 8, 2023
46e40d7
Bump version
peterdesmet Nov 8, 2023
3472788
Correct version
peterdesmet Nov 8, 2023
48f5626
Create NEWS.md
peterdesmet Nov 8, 2023
b6a0310
Remove check on windows-latest + R 3.6.0
damianooldoni Nov 8, 2023
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
8 changes: 4 additions & 4 deletions R/read_camtrap_dp.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ read_camtrap_dp <- function(file = NULL,
package <- frictionless::read_package(file)

# supported versions
supported_versions <- c("0.1.6", "1.0-rc.1")
supported_versions <- c("0.1.6", "1.0")

# get package version
profile <- package$profile
if (profile == "https://raw.githubusercontent.com/tdwg/camtrap-dp/1.0-rc.1/camtrap-dp-profile.json") {
version <- "1.0-rc.1"
if (profile == "https://raw.githubusercontent.com/tdwg/camtrap-dp/1.0/camtrap-dp-profile.json") {
version <- "1.0"
} else {
if (profile == "https://raw.githubusercontent.com/tdwg/camtrap-dp/0.1.6/camtrap-dp-profile.json") {
version <- "0.1.6"
Expand Down Expand Up @@ -152,7 +152,7 @@ read_camtrap_dp <- function(file = NULL,
package <- add_taxonomic_info(package)

# convert to 0.1.6
if (version == "1.0-rc.1") {
if (version == "1.0") {
package <- convert_to_0.1.6(package, version, media = media)
}

Expand Down
85 changes: 59 additions & 26 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ mutate_when_missing <- function(.data,...){
#'
#' This help function adds taxonomic information in `taxonomic` element of
#' metadata to `observations`. Notice that higher classification, i.e. new
#' fields in v1.0-rc.1, are removed.
#' fields in v1.0, are removed.
#'
#' @param package Camera trap data package.
#' @return Camera trap data package with taxonomic related cols added to
Expand All @@ -384,9 +384,8 @@ add_taxonomic_info <- function(package) {
# classification)
taxon_infos <- dplyr::select(
taxon_infos,
dplyr::any_of(c("taxonID",
"taxonIDReference",
"scientificName",
dplyr::all_of("scientificName"),
dplyr::any_of(c("taxonIDReference",
"taxonRank")),
dplyr::starts_with("vernacularNames")
)
Expand All @@ -397,7 +396,7 @@ add_taxonomic_info <- function(package) {
dplyr::left_join(
package$data$observations,
taxon_infos,
by = c("taxonID", "scientificName")
by = c("scientificName")
)
package$data$observations <- observations
}
Expand Down Expand Up @@ -437,13 +436,13 @@ add_speed_radius_angle <- function(obs){
#' @param media If `TRUE` (default), read media records into memory. If `FALSE`,
#' ignore media file to speed up reading larger Camtrap DP packages.
#' @noRd
convert_to_0.1.6 <- function(package, from = "1.0-rc.1", media = TRUE){
convert_to_0.1.6 <- function(package, from = "1.0", media = TRUE){
if (from == "0.1.6") {
message(glue::glue("package's version: {from}. No conversion needed."))
return(package)
}
# check version
supported_versions <- c("1.0-rc.1")
supported_versions <- c("1.0")
assertthat::assert_that(
from %in% supported_versions,
msg = paste0(
peterdesmet marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -461,14 +460,13 @@ convert_to_0.1.6 <- function(package, from = "1.0-rc.1", media = TRUE){
)

# notify about conversion
message(
writeLines(
c(
"The dataset uses Camtrap DP version 1.0-rc.1, it has been converted to 0.1.6.",
"See https://inbo.github.io/camtraptor/#camtrap-dp for details."
)
message(paste0(
peterdesmet marked this conversation as resolved.
Show resolved Hide resolved
collapse = "\n",
c(
"The dataset uses Camtrap DP version 1.0, it has been converted to 0.1.6.",
"See https://inbo.github.io/camtraptor/#camtrap-dp for details."
)
)
))
# convert metadata
package <- convert_metadata_to_0.1.6(package, from)
# convert deployments
Expand All @@ -485,15 +483,15 @@ convert_to_0.1.6 <- function(package, from = "1.0-rc.1", media = TRUE){

#' Convert metadata to Camtrap DP version 0.1.6
#'
#' Convert metadata of a Camtrap DP from version 1.0-rc.1 to 0.1.6 to avoid
#' Convert metadata of a Camtrap DP from version 1.0 to 0.1.6 to avoid
#' breaking changes
#'
#' @param package Camera trap data package object.
#' @param from Character identifying the version of `package`.
#' @return Camera trap data package object with converted `metadata`.
#' @noRd
#' @importFrom dplyr %>% .data
convert_metadata_to_0.1.6 <- function(package, from = "1.0-rc.1"){
convert_metadata_to_0.1.6 <- function(package, from = "1.0"){
authors <- purrr::map_df(package$contributors, unlist)
if ("role" %in% names(authors)) {
deprecated_roles <- c("author", "maintainer")
Expand Down Expand Up @@ -537,20 +535,29 @@ convert_metadata_to_0.1.6 <- function(package, from = "1.0-rc.1"){
dplyr::filter(.data$role == "rightsHolder") %>%
dplyr::slice(1) %>%
dplyr::pull(.data$title)

# downconvert `captureMethod` to values from Camtrap DP version v0.1.6
package$project$captureMethod <-
dplyr::case_match(
.default = package$project$captureMethod,
package$project$captureMethod,
"activityDetection" ~ "motionDetection"
)

return(package)
}

#' Convert deployments to Camtrap DP version 0.1.6
#'
#' Convert deployments of a Camtrap DP from version 1.0-rc.1 to 0.1.6 to avoid
#' Convert deployments of a Camtrap DP from version 1.0 to 0.1.6 to avoid
#' breaking changes
#'
#' @param package Camera trap data package object.
#' @param from Character identifying the version of `package`.
#' @return Camera trap data package object with converted `deployments`.
#' @noRd
#' @importFrom dplyr %>% .data
convert_deployments_to_0.1.6 <- function(package, from = "1.0-rc.1") {
convert_deployments_to_0.1.6 <- function(package, from = "1.0") {

# check deployments slot is present
assertthat::assert_that(
Expand All @@ -572,6 +579,8 @@ convert_deployments_to_0.1.6 <- function(package, from = "1.0-rc.1") {
}
# ignore detectionDistance
deployments$detectionDistance <- NULL
# ignore cameraDepth
deployments$cameraDepth <- NULL
if ("baitUse" %in% names(deployments)) {
# baitUse values in version 0.1.6
bait_uses_old <- c("none", "scent", "food", "visual", "acoustic", "other")
Expand Down Expand Up @@ -615,12 +624,16 @@ convert_deployments_to_0.1.6 <- function(package, from = "1.0-rc.1") {
# map to session and then remove
deployments <- deployments %>%
dplyr::mutate(session = dplyr::case_when(
is.na(.data$session) ~.data$deploymentGroups,
is.na(.data$deploymentGroups) ~ .data$session,
!is.na(.data$deploymentGroups) & !is.na(.data$session) ~
stringr::str_c(.data$session,
.data$deploymentGroups,
sep = " | "))) %>%
sep = " | "),
!is.na(.data$deploymentGroups) & is.na(.data$session) ~
.data$deploymentGroups,
is.na(.data$deploymentGroups) & !is.na(.data$session) ~
.data$session,
# if there is no value for neither deploymentGroups or session:
.default = NA)) %>%
dplyr::select(-"deploymentGroups")
}
if ("array" %in% names(deployments)) {
Expand Down Expand Up @@ -654,7 +667,7 @@ convert_deployments_to_0.1.6 <- function(package, from = "1.0-rc.1") {

#' Convert media to Camtrap DP version 0.1.6
#'
#' Convert media of a Camtrap DP from version 1.0-rc.1 to 0.1.6 to avoid
#' Convert media of a Camtrap DP from version 1.0 to 0.1.6 to avoid
#' breaking changes. Notice that this function `MUST` be run before
#' `convert_observations_to_0.1.6()`.
#'
Expand All @@ -663,7 +676,7 @@ convert_deployments_to_0.1.6 <- function(package, from = "1.0-rc.1") {
#' @return Camera trap data package object with converted `media`.
#' @noRd
#' @importFrom dplyr %>% .data
convert_media_to_0.1.6 <- function(package, from = "1.0-rc.1") {
convert_media_to_0.1.6 <- function(package, from = "1.0") {

# check media slot is present
assertthat::assert_that(
Expand All @@ -689,7 +702,7 @@ convert_media_to_0.1.6 <- function(package, from = "1.0-rc.1") {
event_obs <- observations %>%
dplyr::filter(is.na(.data$mediaID)) %>%
dplyr::select("eventID", "deploymentID", "eventStart", "eventEnd") %>%
# eventID is not anymore required in v1.0-rc1, remove where not present
# eventID is not anymore required in v1.0, remove where not present
dplyr::filter(!is.na(.data$eventID))

# Join on deploymentID and timestamp between eventStart and eventEnd
Expand Down Expand Up @@ -722,21 +735,31 @@ convert_media_to_0.1.6 <- function(package, from = "1.0-rc.1") {
dplyr::mutate("_id" = NA)
}

# convert captureMethod value to v1.6.0 terms
media <- media %>%
dplyr::mutate(
captureMethod = factor(
ifelse(captureMethod == "activityDetection",
"motionDetection",
as.character(captureMethod))
)
)

package$data$media <- media
return(package)
}

#' Convert observations to Camtrap DP version 0.1.6
#'
#' Convert observations of a Camtrap DP from version 1.0-rc.1 to 0.1.6 to avoid
#' Convert observations of a Camtrap DP from version 1.0 to 0.1.6 to avoid
#' breaking changes
#'
#' @param package Camera trap data package object.
#' @param from Character identifying the version of `package`.
#' @return Camera trap data package object with converted `observations`.
#' @noRd
#' @importFrom dplyr %>% .data
convert_observations_to_0.1.6 <- function(package, from = "1.0-rc.1") {
convert_observations_to_0.1.6 <- function(package, from = "1.0") {

# check observations slot is present
assertthat::assert_that(
Expand Down Expand Up @@ -824,6 +847,16 @@ convert_observations_to_0.1.6 <- function(package, from = "1.0-rc.1") {
}
# remove bounding box related cols if present
observations <- observations %>% dplyr::select(-dplyr::starts_with("bbox"))
# add taxonID if missing
if(!"taxonID" %in% colnames(observations)){
observations <- observations %>%
dplyr::mutate(taxonID = NA_integer_)
peterdesmet marked this conversation as resolved.
Show resolved Hide resolved
}
# add taxonIDReference if missing
if(!"taxonIDReference" %in% colnames(observations)){
observations <- observations %>%
dplyr::mutate(taxonIDReference = NA_character_)
}

package$data$observations <- observations
return(package)
Expand Down
Loading
Loading