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

Read camtrap dp v1.0 rc.1 #223

Merged
merged 124 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
7bd2106
Ignore and deprecate the media flag
damianooldoni May 8, 2023
0c82aa0
Avoid creating temporary data slot with media NULL
damianooldoni May 8, 2023
7a822c1
Split comment
damianooldoni May 8, 2023
83d2f76
Read version camtrap DP
damianooldoni May 8, 2023
a485821
map metadata
damianooldoni May 8, 2023
5879437
Invert orders lat/lon
damianooldoni May 8, 2023
3a54149
Rename cameraDelay if present
damianooldoni May 8, 2023
cc627ed
Convert baitUse to factor where possible
damianooldoni May 8, 2023
369d995
Rename deploymentTags
damianooldoni May 8, 2023
b814862
Rename deploymentComments
damianooldoni May 8, 2023
aba8242
Remove typo in mutate
damianooldoni May 8, 2023
2173839
Correct typo in commnet
damianooldoni May 9, 2023
4b28d4c
Merge branch 'main' into read-CamtrapDP-v0.1.6-0.6
damianooldoni Jun 9, 2023
3a7c5f1
Aplpy DRY principle while checking reading issues
damianooldoni Jun 9, 2023
1c56593
Use help function - DRY principle
damianooldoni Jun 9, 2023
cee6828
Set back media flag
damianooldoni Jun 9, 2023
88e60d7
Fix #217
damianooldoni Jun 12, 2023
5d33e1e
Fix #218
damianooldoni Jun 12, 2023
7a17f49
Run devtools:document() after fixing #218
damianooldoni Jun 12, 2023
4c2181a
Update check_package using media instead of multimedia
damianooldoni Jun 12, 2023
8903c9c
Detect the version in package$profile
damianooldoni Jun 12, 2023
0e4621f
Check package at the end of reading it
damianooldoni Jun 12, 2023
968b29d
Filter on observationLevel = event
damianooldoni Jun 12, 2023
f6fa02f
Work about baitUse
damianooldoni Jun 12, 2023
3d8904f
ignore detectionDistance
damianooldoni Jun 12, 2023
2fe690c
Map of baitUse
damianooldoni Jun 12, 2023
c4104fa
deprecate and leave empty: sessoin,array _id
damianooldoni Jun 12, 2023
70203a5
Rename eventID favorite and mediaComments in media
damianooldoni Jun 12, 2023
1012434
Rename eventStart and eventEnd
damianooldoni Jun 12, 2023
91103c1
Use help function to get reading issues in media
damianooldoni Jun 13, 2023
acc622b
Use new version number instead of old long link
damianooldoni Jun 13, 2023
b79983a
Set rightsHolder
damianooldoni Jun 13, 2023
d7876ea
Set platform
damianooldoni Jun 13, 2023
ed8e7a7
Rename to old classificationLevel
damianooldoni Jun 13, 2023
60bdba2
Rename to old sequenceInterval
damianooldoni Jun 13, 2023
3d69cf1
Deprecate and remove sequenceInterval if present
damianooldoni Jun 13, 2023
f2af129
Deprecate _id and leave empty
damianooldoni Jun 13, 2023
c000bb0
Remove typo in two deprecation messages
damianooldoni Jun 13, 2023
f0f1fa7
Ignore filePublic if present
damianooldoni Jun 13, 2023
f254633
Add flag arg and check of presence of media
damianooldoni Jun 13, 2023
ddbf3f7
Use flag arg to check package in get_record_table
damianooldoni Jun 13, 2023
a018892
Read observations before media
damianooldoni Jun 15, 2023
be929f1
Specify source of taxon info in comment
damianooldoni Jun 15, 2023
9b78c8b
Remove typo in profile check
damianooldoni Jun 20, 2023
d1a46ac
Remove typo in retrieving contributors
damianooldoni Jun 20, 2023
6a2a9e7
Remove typo while renaming deployments' start-end
damianooldoni Jun 20, 2023
e1bb62b
Correct the mapping of baitUse
damianooldoni Jun 20, 2023
611b9cd
Read example datapackage v1.0-rc1
damianooldoni Jun 20, 2023
ab62517
Correct filtering event-based obs
damianooldoni Jun 20, 2023
e80cc84
Rename eventID if present
damianooldoni Jun 20, 2023
aee81d0
Rename eventStart
damianooldoni Jun 20, 2023
e79d213
Ignore eventEnd
damianooldoni Jun 20, 2023
71ef1c7
Finalize work about baitUse
damianooldoni Jun 20, 2023
8883c8e
Ignore observationLevel
damianooldoni Jun 20, 2023
6cfd1df
Rename cameraSetupType
damianooldoni Jun 20, 2023
3a2f06f
Deprecate and leave countNew empty
damianooldoni Jun 20, 2023
d23e4c3
Rename behavior if present
damianooldoni Jun 20, 2023
0c6aad0
Test reading only the right versions of dps
damianooldoni Jun 20, 2023
8c9ac5d
Rename classificationProbability
damianooldoni Jun 20, 2023
5c1c76f
Add test for renaming behavior
damianooldoni Jun 20, 2023
9ed6136
Test renaming of classificationProbability
damianooldoni Jun 20, 2023
6e73a17
Remove typo in check_reading_issues for obs
damianooldoni Jun 20, 2023
e1bf42c
Rename observationsComments and test it
damianooldoni Jun 20, 2023
a6faa1e
Return errors from help function about readr issues
damianooldoni Jun 20, 2023
d5c396f
Update reading function: big dump
damianooldoni Jun 20, 2023
85fd69f
Update tests: big dump
damianooldoni Jun 20, 2023
6aabe92
Set patch for speed-radius-angle in version related if()
damianooldoni Jun 27, 2023
5497f1f
Rename individualSpeed
damianooldoni Jun 27, 2023
ff88a76
Merge branch 'main' into read-CamtrapDP-v1.0-rc.1
damianooldoni Jul 13, 2023
9950bbe
Improve text and length lines
damianooldoni Jul 13, 2023
48f99b0
Change position where first version datapackage is made
damianooldoni Jul 13, 2023
b676db1
solve conflict
damianooldoni Jul 13, 2023
f753a33
Merge branch 'main' into read-CamtrapDP-v1.0-rc.1
damianooldoni Jul 13, 2023
f215fc0
Add dot in error message
damianooldoni Jul 14, 2023
fff6755
Move patch for #185 in help function
damianooldoni Jul 14, 2023
93253a1
Refactor read function: conversion in help functions
damianooldoni Jul 14, 2023
d80df4d
Add some warnings in conversion metadata
damianooldoni Jul 17, 2023
4067425
Remove help column in deployments
damianooldoni Jul 17, 2023
a2facdf
Map deploymentGroups to `session`
damianooldoni Jul 17, 2023
3d23745
Use standard order Roxygen fields for function doc
damianooldoni Jul 17, 2023
b05f4de
Define final order columns deployments df
damianooldoni Jul 17, 2023
316d37c
Remove and improve order some cols in media
damianooldoni Jul 17, 2023
7635ad4
Use right var to pass version number in warning
damianooldoni Jul 17, 2023
a55ac24
Improve tests for reading function
damianooldoni Jul 17, 2023
e018efc
Add sequenceID if not present in obs
damianooldoni Jul 17, 2023
792868d
Relocate sequenceID in obs
damianooldoni Jul 17, 2023
0be728a
Relocate countNew
damianooldoni Jul 17, 2023
3c5825d
Add note about reading both versions camtrap DP
damianooldoni Jul 17, 2023
3cc7786
Fix some tests
damianooldoni Jul 17, 2023
28eeb8b
Run devtools::document()
damianooldoni Jul 17, 2023
241325c
Improve description convert_media_to_0.1.6
damianooldoni Jul 17, 2023
e283f95
Avoid some global variable notes while check package
damianooldoni Jul 17, 2023
fda35af
Add assertion to check media is not NULL
damianooldoni Jul 17, 2023
7a1b2ea
Solve bug while checking package without media
damianooldoni Jul 17, 2023
0f915f1
Specify default value of flag media in documentation
damianooldoni Jul 17, 2023
e398226
Remove bug flag media
damianooldoni Jul 17, 2023
2b13b4d
Remove bounding box cols while reading
damianooldoni Jul 18, 2023
4050770
Avoid using superseded one_of() function
damianooldoni Jul 18, 2023
f0c2c65
Move chunk code adding taxonomic info in help function
damianooldoni Jul 18, 2023
1cad8fe
Order columns via help functions
damianooldoni Jul 18, 2023
9e5f241
Check package at the end
damianooldoni Jul 18, 2023
6a0640e
Ignore observationTags
damianooldoni Jul 18, 2023
851110e
Add test about absence higher taxonomy in observations
damianooldoni Jul 18, 2023
3fe7670
Change default of media flag in `check_package()`
damianooldoni Jul 20, 2023
8951e3b
Check presence of `data$media` only if needed
damianooldoni Jul 20, 2023
34fb081
Use frictionless::resources()
damianooldoni Jul 20, 2023
0aea192
Check that the needed resources are present
damianooldoni Jul 20, 2023
10e0650
Improve message if checking resources fails
damianooldoni Jul 20, 2023
9a537ed
Replace isTRUE(media) with media in if() statement
damianooldoni Jul 20, 2023
42e6435
Merge branch 'main' into read-CamtrapDP-v1.0-rc.1
damianooldoni Jul 20, 2023
f6ae98d
Avoid check media flag in check_package()
damianooldoni Jul 20, 2023
a91a02a
Add section instead of a note
damianooldoni Jul 20, 2023
8cb8a56
Improve message about removing media-based obs
damianooldoni Jul 20, 2023
47ff332
Improve message about ignoring media.filePublic
damianooldoni Jul 20, 2023
139a61f
Add message about ignoring observationTags
damianooldoni Jul 20, 2023
feff15b
Add message about ignoring bbox* columns
damianooldoni Jul 20, 2023
c60797c
Add sentences while reading package with new version
damianooldoni Jul 20, 2023
a6997e6
Revert commits about messages for ignored terms
damianooldoni Jul 20, 2023
8c84826
Add preliminary check as mentioned in #258
damianooldoni Jul 20, 2023
bf242db
Add tests based on #258
damianooldoni Jul 20, 2023
f33c4e9
Test again specific error message
damianooldoni Jul 20, 2023
33b88d9
Update Camtrap DP description
peterdesmet Jul 20, 2023
d8c0ad7
Merge branch 'read-CamtrapDP-v1.0-rc.1' of https://github.com/inbo/ca…
peterdesmet Jul 20, 2023
17184f6
Run devtools::build_readme()
damianooldoni Jul 20, 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
14 changes: 14 additions & 0 deletions R/get_cam_op.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ get_cam_op <- function(package = NULL,
# check camera trap data package
package <- check_package(package, datapkg, "get_cam_op")

# Check that station_col is a single string
assertthat::assert_that(assertthat::is.string(station_col))
# Check that station_col is one of the columns in deployments
assertthat::assert_that(
station_col %in% names(package$data$deployments),
Expand All @@ -59,6 +61,18 @@ get_cam_op <- function(package = NULL,
"it must be one of the deployments column names."
)
)

# Check that station_col doesn't contain empty values (NA)
n_na <- package$data$deployments %>%
dplyr::filter(is.na(.data[[station_col]])) %>%
nrow()
assertthat::assert_that(
n_na == 0,
msg = glue::glue(
"Column `{station_col}` must be non-empty: ",
"{n_na} NAs found."
)
)

assertthat::assert_that(
use_prefix %in% c(TRUE, FALSE),
Expand Down
8 changes: 4 additions & 4 deletions R/get_custom_effort.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Get custom effort
#'
#' Gets the custom effort (deployment duration) for a custom time window and a
#' specific time interval such as day or month.
#' The custom effort is also calculated over all deployments, although
#' filtering predicates can be applied as well.
#' specific time interval such as day or month. The custom effort is also
peterdesmet marked this conversation as resolved.
Show resolved Hide resolved
#' calculated over all deployments, although filtering predicates can be applied
#' as well. This function calls `get_cam_op()` internally.
#'
#' @param package Camera trap data package object, as returned by
#' `read_camtrap_dp()`.
Expand Down Expand Up @@ -122,7 +122,7 @@ get_custom_effort <- function(package = NULL,
deployments <- package$data$deployments

# camera operation matrix with filter(s) on deployments
cam_op <- get_cam_op(package, ...)
cam_op <- get_cam_op(package, ..., station_col = "deploymentID")

# Sum effort over all deployments for each day (in day units)
sum_effort <- colSums(cam_op, na.rm = TRUE, dims = 1)
Expand Down
2 changes: 1 addition & 1 deletion R/get_record_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ get_record_table <- function(package = NULL,
removeDuplicateRecords = TRUE,
datapkg = lifecycle::deprecated()) {
# check data package
package <- check_package(package, datapkg, "get_record_table")
package <- check_package(package, datapkg, "get_record_table", media = TRUE)
peterdesmet marked this conversation as resolved.
Show resolved Hide resolved

# check stationCol is a valid column name
assertthat::assert_that(
Expand Down
2 changes: 1 addition & 1 deletion R/get_species.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' get_species(mica)
get_species <- function(package = NULL, datapkg = lifecycle::deprecated()) {
# Check camera trap data package
package <- check_package(package, datapkg, "get_species")
package <- check_package(package, datapkg, "get_species", media = FALSE)

# Get taxonomic information from package metadata
if (!"taxonomic" %in% names(package)) {
Expand Down
4 changes: 2 additions & 2 deletions R/map_dep.R
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ map_dep <- function(package = NULL,
)
} else if (feature == "rai") {
feat_df <- get_rai(package, species = species, sex = sex, life_stage = life_stage, ...)
feat_df <- feat_df %>% dplyr::rename(n = rai)
feat_df <- feat_df %>% dplyr::rename(n = "rai")
} else if (feature == "rai_individuals") {
feat_df <- get_rai_individuals(
package,
Expand All @@ -537,7 +537,7 @@ map_dep <- function(package = NULL,
effort_unit <- "hour" # default value of get_effort()
}
feat_df <- get_effort(package, unit = effort_unit, ...)
feat_df <- feat_df %>% dplyr::rename(n = effort)
feat_df <- feat_df %>% dplyr::rename(n = "effort")
}

# define title legend
Expand Down
163 changes: 90 additions & 73 deletions R/read_camtrap_dp.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#' `observations` data frame.
#'
#' @param file Path or URL to a `datapackage.json` file.
#' @param media If `TRUE`, read media records into memory. If `FALSE`, ignore
#' media file to speed up reading larger Camtrap DP packages.
#' @param media If `TRUE` (default), read media records into memory. If `FALSE`,
#' ignore media file to speed up reading larger Camtrap DP packages.
#' @param path Path to the directory containing the datapackage. Use `file`
#' with path or URL to a `datapackage.json` file instead.
#' @return List describing a Data Package (as returned by
Expand All @@ -24,7 +24,10 @@
#' @examples
#' \dontrun{
#' # Read Camtrap DP package
#' camtrap_dp_file <- system.file("extdata", "mica", "datapackage.json", package = "camtraptor")
#' camtrap_dp_file <- system.file(
#' "extdata", "mica", "datapackage.json",
#' package = "camtraptor"
#' )
#' muskrat_coypu <- read_camtrap_dp(camtrap_dp_file)
#'
#' # Read Camtrap DP package and ignore media file
Expand All @@ -46,8 +49,9 @@
read_camtrap_dp <- function(file = NULL,
media = TRUE,
path = lifecycle::deprecated()) {
# check path (deprecated)
warning_detail <- paste(
"Use parameter `file` containing the path or URL to the `datapackage.json`",
"Use argument `file` containing the path or URL to the `datapackage.json`",
"file. The use of parameter `path` with path to the local directory is ",
"deprecated since version 0.6.0."
)
Expand All @@ -58,7 +62,6 @@ read_camtrap_dp <- function(file = NULL,
details = warning_detail
)
}

# define the right file value
if (lifecycle::is_present(path)) {
file <- file.path(path, "datapackage.json")
Expand All @@ -67,91 +70,105 @@ read_camtrap_dp <- function(file = NULL,
if (dir.exists(file)) {
file <- file.path(file, "datapackage.json")
}

# check media
# check media arg
assertthat::assert_that(
media %in% c(TRUE, FALSE),
msg = "`media` must be a logical: TRUE or FALSE"
)
# read files

# read package (metadata)
package <- frictionless::read_package(file)
deployments <- frictionless::read_resource(package, "deployments")
issues_deployments <- readr::problems(deployments)
if (nrow(issues_deployments) > 0) {
warning(glue::glue(
"One or more parsing issues occurred while reading deployments. ",
"See `?read_camtrap_dp()` for examples on how to use ",
"`readr::problems()`."
))
}
observations <- frictionless::read_resource(package, "observations")
issues_observations <- readr::problems(observations)
if (nrow(issues_observations) > 0) {
warning(glue::glue(
"One or more parsing issues occurred while reading observations. ",
"See `?read_camtrap_dp()` for examples on how to use ",
"`readr::problems()`."
))

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

# 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"
} else {
if (profile == "https://raw.githubusercontent.com/tdwg/camtrap-dp/0.1.6/camtrap-dp-profile.json") {
version <- "0.1.6"
} else {
version <- profile
}
}

# patch for non-standard values speed, radius, angle
# see https://github.com/inbo/camtraptor/issues/185
obs_col_names <- names(observations)
if (all(c("X22", "X23", "X24") %in% names(observations))) {
observations <- observations %>%
dplyr::rename(speed = "X22", radius = "X23", angle = "X24")
message(
paste("Three extra fields in `observations` interpreted as `speed`,",
"`radius` and `angle`."
)
# check version is supported
assertthat::assert_that(
version %in% supported_versions,
msg = paste0(
glue::glue("Version {version} "),
"is not supported. Supported versions: ",
glue::glue_collapse(glue::glue("{supported_versions}"),
sep = " ",
last = " and "),
".")
)

# get resource names
resource_names <- frictionless::resources(package)
#check needed resources are present
resources_to_read <- c("deployments", "media", "observations")
assertthat::assert_that(
all(resources_to_read %in% resource_names),
msg = glue::glue(
"One or more resources among ",
glue::glue_collapse(resources_to_read, sep = ", ", last = " and "),
" is missing."
)
)

# read deployments
deployments <- frictionless::read_resource(package, "deployments")
issues_deployments <- check_reading_issues(deployments, "deployments")
# read observations (needed to create sequenceID in media)
observations <- frictionless::read_resource(package, "observations")
issues_observations <- check_reading_issues(observations, "observations")

if (version == "0.1.6"){
observations <- add_speed_radius_angle(observations)
}

# create first version datapackage with resources in data element
# create first version datapackage with resources in data slot
data <- list(
"deployments" = deployments,
"media" = NULL,
"observations" = observations
)

package$data <- data
# get taxonomic info
taxon_infos <- get_species(package)
# add vernacular names to observations
if (!is.null(taxon_infos)) {
cols_taxon_infos <- names(taxon_infos)
observations <-
dplyr::left_join(
observations,
taxon_infos,
by = c("taxonID", "scientificName")
)
observations <-
observations %>%
dplyr::relocate(dplyr::one_of(cols_taxon_infos), .after = "cameraSetup")
# Inherit parsing issues from reading
attr(observations, which = "problems") <- issues_observations
package$data$observations <- observations

# read media if needed
if (media) {
media_df <- frictionless::read_resource(package, "media")
issues_media <- check_reading_issues(media_df, "media")
data$media <- media_df
}
if (media == TRUE) {
media <- frictionless::read_resource(package, "media")
issues_media <- readr::problems(media)
if (nrow(issues_media) > 0) {
warning(glue::glue(
"One or more parsing issues occurred while reading media. ",
"See `?read_camtrap_dp()` for examples on how to use ",
"`readr::problems()`."
))
}

package$data <- data
package <- check_package(package, media = media)

package <- add_taxonomic_info(package)

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

# return list resources
if (is.data.frame(media)) {
data <- list(
"deployments" = deployments,
"media" = media,
"observations" = observations
)
package$data <- data

# order columns
package$data$deployments <- order_cols_deployments(package$data$deployments)
package$data$observations <- order_cols_observations(
package$data$observations
)
if (!is.null(package$data$media)) {
package$data$media <- order_cols_media(package$data$media)
}
package

package <- check_package(package, media = media)

# Inherit parsing issues from reading
attr(package$data$observations, which = "problems") <- issues_observations

return(package)
}
4 changes: 2 additions & 2 deletions R/write_eml.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ write_eml <- function(package,
taxonomy <- dplyr::filter(taxonomy, .data$taxonRank == "species")
}
sci_names <-
dplyr::rename(taxonomy, Species = scientificName) %>%
dplyr::select(Species)
dplyr::rename(taxonomy, Species = "scientificName") %>%
dplyr::select("Species")

eml$dataset$coverage <- EML::set_coverage(
begin = package$temporal$start,
Expand Down
Loading