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

Use filter functions/remove args from functions #331

Merged
merged 97 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
7521921
Fix #231
damianooldoni Jul 22, 2024
dee8feb
Rename map_dep() function to map_deployments()
damianooldoni Jul 22, 2024
7e44fd2
Rename get_cam_op()
damianooldoni Jul 22, 2024
ffeb6cc
Rename get_n_species as n_species
damianooldoni Jul 22, 2024
d4ddc4d
Rename get_record_table to camtrapR_recordTable
damianooldoni Jul 22, 2024
9ac0997
Rename R files
damianooldoni Jul 22, 2024
d9976b7
devtools::document()
damianooldoni Jul 22, 2024
86f0404
Remove filtering args in map_deployments()
damianooldoni Jul 22, 2024
769dad4
Add renaming in NEWS.md
damianooldoni Jul 22, 2024
4de9590
Remove deprecated datapkg arg in tests
damianooldoni Jul 22, 2024
b17d4eb
Remove round_coordinates()
damianooldoni Jul 22, 2024
1f65a84
Use purrr::pluck(deployments(x), ...)
damianooldoni Jul 22, 2024
4801760
Fix #330
damianooldoni Jul 22, 2024
489314a
Use purrr::pluck()
damianooldoni Jul 22, 2024
32bcd4c
Replace args with filter_observations()
damianooldoni Jul 22, 2024
05bfe20
Use filter_o*() in vignette
damianooldoni Jul 22, 2024
18db751
Remove library(dplyr) where present only for %>%
damianooldoni Jul 22, 2024
0021f43
Improve style/indentation code
damianooldoni Jul 22, 2024
150258c
Remove obsolete section about applying filter predicates
damianooldoni Jul 22, 2024
084d182
devtools::document()
damianooldoni Jul 22, 2024
6f58ad3
Remove library(dplyr): %>% exported by camtraptor
damianooldoni Jul 23, 2024
2d631fc
Remove sections about filtering by species and life stage
damianooldoni Jul 23, 2024
418a5cb
Remove section about filter predicates
damianooldoni Jul 23, 2024
cf26d96
Update example to get the desired combination of unknown species and …
damianooldoni Jul 23, 2024
a77669f
Avoid filtering by removed args `sex` and `life_stage`
damianooldoni Jul 23, 2024
1fa5f8e
Remove tests of sex and life_stage args
damianooldoni Jul 23, 2024
977adbe
Rename tests file based on new function names
damianooldoni Jul 23, 2024
16b2d19
Use `filter_observations()` internally
damianooldoni Jul 23, 2024
7916806
Use `purrr::pluck()` in `get_n_obs()`
damianooldoni Jul 23, 2024
1045c33
Capitalize comments
damianooldoni Jul 23, 2024
a09c86b
Remove tests for sex and life_stage
damianooldoni Jul 23, 2024
8390b7e
Capitalize comments in some tests
damianooldoni Jul 23, 2024
286f804
Remove args sex and life_stage
damianooldoni Jul 23, 2024
8bbe7d3
Remove sex and life_stage args from documentation
damianooldoni Jul 23, 2024
492962e
devtools::document()
damianooldoni Jul 23, 2024
286fbd3
Correct error in NEWS.md
damianooldoni Jul 23, 2024
8ee4ed5
Add news about filtering functions
damianooldoni Jul 23, 2024
3b62d4c
Add news about `events()`, `taxa()`
damianooldoni Jul 24, 2024
19e2e81
Improve documentation
damianooldoni Jul 24, 2024
08fdbe7
Update examples using filter_observations()
damianooldoni Jul 24, 2024
1d8c85c
Remove sex and life_stage args in map_deployments()
damianooldoni Jul 24, 2024
61b5f2c
Deprecate map_dep and its args sex and life_stage
damianooldoni Jul 24, 2024
a3530aa
Remove outdated information
damianooldoni Jul 24, 2024
4f316f9
Improve news about map_dep(), sex and life_stage
damianooldoni Jul 24, 2024
3113218
Improve news about deprecation get_cam_op()
damianooldoni Jul 24, 2024
69a7d46
Deprecate get_record_table()
damianooldoni Jul 24, 2024
89e6a04
Set deprecation message in Description section
damianooldoni Jul 24, 2024
68630ab
Remove redundant @inheritParams
damianooldoni Jul 24, 2024
1d532e4
Deprecate get_cam_op() and get_n_species()
damianooldoni Jul 24, 2024
dfefc63
Improve family for camtrapR derived functions
damianooldoni Jul 24, 2024
a43ba5e
Improve code style args
damianooldoni Jul 24, 2024
b878ff6
Update NEWS mentioning deprecations
damianooldoni Jul 24, 2024
993d93a
devtools::document()
damianooldoni Jul 24, 2024
cb83d38
Make filter predicates functions defunct
damianooldoni Jul 24, 2024
1b0483e
Add "Function" to news items where missing
damianooldoni Jul 24, 2024
27b8808
Compile documentation - `devtools::document()`
damianooldoni Jul 24, 2024
971f199
Move deprecation of function as first step
damianooldoni Jul 25, 2024
e54060a
Use arg details in `deprecate_warn()`
damianooldoni Jul 25, 2024
286dc00
Remove part related to sex and life_stage in new function
damianooldoni Jul 25, 2024
e10d0da
Solve typo in warning message causing function to crash
damianooldoni Jul 25, 2024
9abe607
Add tests for deprecated functions
damianooldoni Jul 25, 2024
8f523cc
Rename test file as function is renamed
damianooldoni Jul 25, 2024
8e01800
Add tests for deprecation of map_dep()
damianooldoni Jul 25, 2024
5087309
Merge branch 'version-1.0' into rename-functions
damianooldoni Jul 25, 2024
a1043de
fix typo
sannegovaert Aug 1, 2024
8522123
fix typo
sannegovaert Aug 1, 2024
3b9ec39
update parameter names
sannegovaert Aug 1, 2024
0af0179
missing #
sannegovaert Aug 1, 2024
7bcd7a9
fix typo
sannegovaert Aug 1, 2024
2dee77c
fix some tests
sannegovaert Aug 2, 2024
d0e525f
update earliest deployment in tests (new example dataset)
sannegovaert Aug 2, 2024
613f53d
update parameter names
sannegovaert Aug 2, 2024
d54e862
fix test because both an error and warning are returned
sannegovaert Aug 2, 2024
5ecf096
fix test (example update)
sannegovaert Aug 2, 2024
3e90a6e
fix issue "object 'datapkg' not found"
sannegovaert Aug 2, 2024
244515d
fix typo
sannegovaert Aug 2, 2024
8c17d9d
fix test
sannegovaert Aug 2, 2024
595845e
devtools::document()
sannegovaert Aug 2, 2024
674ee72
update parameter names
sannegovaert Aug 2, 2024
d31486b
fix typo
sannegovaert Aug 2, 2024
40403a3
remove test
sannegovaert Aug 2, 2024
15d6334
style and character limit
sannegovaert Aug 5, 2024
7549b2f
update parameter names
sannegovaert Aug 5, 2024
bea171f
Explain acronym RAI in documentation `map_deployments()`
damianooldoni Sep 12, 2024
d09a3bf
Remove publication functions section in pkg website
damianooldoni Sep 12, 2024
7e181a3
Use `lifecycle::expect_deprecated()`
damianooldoni Sep 12, 2024
a7193bf
Remove if statement
damianooldoni Sep 12, 2024
ac9e1c0
Use lifecycle::expect_deprecated()
damianooldoni Sep 12, 2024
6c5d69d
Use `taxa()` instead of `get_species()`
damianooldoni Sep 12, 2024
69f6965
Update deployment ID in test
damianooldoni Sep 12, 2024
25b4b48
Fix unit-test about deprecations
damianooldoni Sep 12, 2024
ab40dc2
Replace "has been" with "is" and use "now"
damianooldoni Sep 12, 2024
6e265c9
Remove redundant news
damianooldoni Sep 12, 2024
0a24cc2
Group deprecations together
damianooldoni Sep 12, 2024
ff7732a
Use `expect_deprecated()`
damianooldoni Sep 12, 2024
879a736
devtools::document()
damianooldoni Sep 12, 2024
8cce504
Use ´expect_deprecated()´
sannegovaert Sep 18, 2024
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
19 changes: 18 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(apply_filter_predicate)
export(calc_animal_pos)
export(camtrapR_cameraOperation)
export(camtrapR_recordTable)
export(check_species)
export(deployments)
export(events)
Expand All @@ -21,12 +25,25 @@ export(get_scientific_name)
export(get_species)
export(locations)
export(map_dep)
export(map_deployments)
export(media)
export(n_species)
export(observations)
export(pred)
export(pred_and)
export(pred_gt)
export(pred_gte)
export(pred_in)
export(pred_lt)
export(pred_lte)
export(pred_na)
export(pred_not)
export(pred_notin)
export(pred_notna)
export(pred_or)
export(read_camtrap_dp)
export(read_camtrapdp)
export(read_wi)
export(round_coordinates)
export(taxa)
importFrom(camtrapdp,deployments)
importFrom(camtrapdp,events)
Expand Down
14 changes: 11 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

This is a major release that updates the internal data model to Camtrap DP 1.0 and drops support for Camtrap DP 0.1.6.

- `read_camtrapdp()` reexports the read functionality from [camtrapdp](https://inbo.github.io/camtrapdp/reference/read_camtrapdp.html) (#298).
It replaces the now deprecated `read_camtrap_dp()` (with underscore) (#298).
- `read_camtrap_dp()` is deprecated. Use `read_camtrapdp()` instead.
- `read_camtrap_dp()` (with underscore) is now deprecated. Use `read_camtrapdp()`, which reexports the read functionality from [camtrapdp](https://inbo.github.io/camtrapdp/reference/read_camtrapdp.html) (#298) (#298).
- Function `get_record_table()` is now deprecated. Use `camtrapR_recordTable()` (#240).
- Function `get_cam_op()` is now deprecated. Use `camtrapR_cameraOperation()` (#239).
- Function `get_n_species()` is now deprecated. Use `n_species()` (#243).
- Function `map_dep()` is now deprecated. Use `map_deployments()` (#231).
- Filter predicates (`apply_filter_predicate()`, `pred()` `pred_not()` `pred_gt()`, `pred_and()`, `pred_or()`, etc.) are now defunct. Use `filter_deployments()` (#316).
- Arguments `sex` and `life_stage` are now deprecated in (deprecated) `map_dep()`. Use `filter_observations()` beforehand.
- The deprecated argument `datapkg` in `read_camtrap_dp()` is removed.
- `write_dwc()` has moved to [camtrapdp](https://inbo.github.io/camtrapdp/reference/write_dwc.html). `write_eml()` is not needed for GBIF processing of Camtrap DPs and has been removed ([camtrapdp#61](https://github.com/inbo/camtrapdp/issues/61)).
- `round_coordinates()` has moved to [camtrapdp](https://inbo.github.io/camtrapdp/reference/round_coordinates.html) (#327).
damianooldoni marked this conversation as resolved.
Show resolved Hide resolved
- `example_dataset()` allows to create an example dataset in Camtrap DP 1.0 format. It reexports the functionality from [camtrapdp](https://inbo.github.io/inbo/camtrapdp/reference/example_dataset.html). The internal dataset `mica` is not present anymore. Raw data files in `inst/extdata` are also removed.
- Functions `deployments()`, `observations()` and `media()` allow to extract the respective tables (#317). They reexports the functionality from camtrapdp, e.g. see [`camtrapdp::deployments()`](https://inbo.github.io/camtrapdp/reference/deployments.html).
- Functions `events()`and `taxa()` allow to extract information about events and taxa from the observations (#317). They reexport the functionalities from camtrapdp, see [`camtrapdp::events()`](https://inbo.github.io/camtrapdp/reference/events.html) and [camtrapdp::taxa()](https://inbo.github.io/camtrapdp/reference/taxa.html).
- Function `locations()` allows to extract information about locations from the deployments (#317). It reexports the functionality from camtrapdp, see [`camtrapdp::locations()`](https://inbo.github.io/camtrapdp/reference/locations.html).
- The first argument of many functions accepts a Camtrap DP object: not `package` anymore, but `x` (#324).
- Functions `filter_deployments()`, `filter_observations()` and `filter_media()` allow to filter at data package level (#315). They reexport the functionalities from camtrapdp. See [`camtrapdp::filter_deployments()`](https://inbo.github.io/camtrapdp/reference/filter_deployments.html), [`camtrapdp::filter_observations()`](https://inbo.github.io/camtrapdp/reference/filter_observations.html) and [`camtrapdp::filter_media()`](https://inbo.github.io/camtrapdp/reference/filter_media.html).

# camtraptor 0.25.0

Expand Down
269 changes: 269 additions & 0 deletions R/camtrapR_cameraOperation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
#' Get camera operation matrix
#'
#' Returns the [camera operation matrix](
#' https://jniedballa.github.io/camtrapR/reference/cameraOperation.html) as
#' returned by [camtrapR::cameraOperation()](
#' https://jniedballa.github.io/camtrapR/reference/cameraOperation.html).
#'
#' The deployment data are by default grouped by `locationName` (station ID in
#' camtrapR jargon) or another column specified by the user via the
#' `station_col` argument. If multiple deployments are linked to same location,
#' daily efforts higher than 1 occur.
#'
#' Partially active days, e.g. the first or the last day of a deployment, result
#' in decimal effort values as in [camtrapR::cameraOperation()](
#' https://jniedballa.github.io/camtrapR/reference/cameraOperation.html).
#'
#' @param station_col Column name to use for identifying the stations. Default:
#' `"locationName"`.
#' @param camera_col Column name of the column specifying Camera ID. Default:
#' `NULL`.
#' @param session_col Column name to use for identifying the session. Default:
#' `NULL`. Use it for creating multi-session / multi-season detection
#' histories.
#' @param use_prefix Logical (`TRUE` or `FALSE`). If `TRUE` the returned row
#' names will start with prefix `"Station"` as returned by
#' [camtrapR::cameraOperation()](
#' https://jniedballa.github.io/camtrapR/reference/cameraOperation.html).
#' Default: `FALSE`.
#' @inheritParams get_species
#' @return A matrix. Row names always indicate the station ID. Column names are
#' dates.
#' @family camtrapR-derived functions
#' @export
#' @examples
#' x <- example_dataset()
#' camtrapR_cameraOperation(x)
#'
#' # Specify column with station names
#' camtrapR_cameraOperation(x, station_col = "locationID")
#'
#' # Specify column with session IDs
#' x_sessions <- x
#' x_sessions$data$deployments <- deployments(x_sessions) %>%
#' dplyr::mutate(session = ifelse(
#' stringr::str_starts(.data$locationName, "B_DL_"),
#' "after2020",
#' "before2020"
#' )
#' )
#' camtrapR_cameraOperation(x_sessions, session_col = "session")
#'
#' # Specify column with camera IDs
#' x_cameras <- x_sessions
#' x_cameras$data$deployments$cameraID <- c(1, 2, 3, 4)
#' camtrapR_cameraOperation(x_cameras, camera_col = "cameraID")
#'
#' # Specify both session and camera IDs
#' camtrapR_cameraOperation(
#' x_cameras,
#' camera_col = "cameraID",
#' session_col = "session"
#' )
#'
#' # Use prefix Station as in camtrapR's camera operation matrix
#' camtrapR_cameraOperation(x, use_prefix = TRUE)
camtrapR_cameraOperation <- function(x,
station_col = "locationName",
camera_col = NULL,
session_col = NULL,
use_prefix = FALSE) {
# Check Camera Trap Data Package
camtrapdp::check_camtrapdp(x)

# 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(deployments(x)),
msg = glue::glue(
"Station column name (`{station_col}`) is not valid: ",
"it must be one of the deployments column names."
)
)

# Check that `station_col` doesn't contain empty values (NA)
n_na <- deployments(x) %>%
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."
)
)

# Check that `station_col` do not contain the reserved words "__SESS_" and
# "__CAM_" (no need to remove NAs beforehand as station_col must not contain
# any NA, see previous check)
assertthat::assert_that(
all(!stringr::str_detect(string = deployments(x)[[station_col]],
pattern = "__SESS_|__CAM_")),
msg = glue::glue(
"Station column name (`{station_col}`) must not contain any of the ",
"reserved words: \"__SESS_\", \"__CAM_\"."
)
)

# Check that `session_col` exists in deployments, if defined, and that its
# values do not contain the reserved words "__SESS_" and "__CAM_"
if (!is.null(session_col)) {
assertthat::assert_that(assertthat::is.string(session_col))
assertthat::assert_that(
session_col %in% names(deployments(x)),
msg = glue::glue(
"Session column name (`{session_col}`) is not valid: ",
"it must be one of the deployments column names."
)
)
session_values <- deployments(x)[[session_col]]
session_values <- session_values[!is.na(session_values)]
assertthat::assert_that(
all(!stringr::str_detect(string = session_values,
pattern = "__SESS_|__CAM_")),
msg = glue::glue(
"Session column name (`{session_col}`) must not contain any of the ",
"reserved words: \"__SESS_\", \"__CAM_\"."
)
)
}

# Check that `camera_col` exists in deployments, if defined, and that its
# values do not contain the reserved words "__SESS_" and "__CAM_"
if (!is.null(camera_col)) {
assertthat::assert_that(assertthat::is.string(camera_col))
assertthat::assert_that(
camera_col %in% names(deployments(x)),
msg = glue::glue(
"Camera column name (`{camera_col}`) is not valid: ",
"it must be one of the deployments column names."
)
)
camera_values <- deployments(x)[[camera_col]]
camera_values <- camera_values[!is.na(camera_values)]
assertthat::assert_that(
all(!stringr::str_detect(string = camera_values,
pattern = "__SESS_|__CAM_")),
msg = glue::glue(
"Camera column name (`{camera_col}`) must not contain any of the ",
"reserved words: \"__SESS_\", \"__CAM_\"."
)
)
}

assertthat::assert_that(
use_prefix %in% c(TRUE, FALSE),
msg = "use_prefix must be TRUE or FALSE."
)

# Extract the deployments
deploys <- deployments(x)

# very first day among all stations
first_day <- min(deploys$deploymentStart)
# very last day among all stations
last_day <- max(deploys$deploymentEnd)

# get sequence with all days from very first day to very last day
days_operations <- seq(
lubridate::date(first_day),
lubridate::date(last_day),
by = "days"
)
# get a string version of this: useful for setting names of final matrix
days_operations_string <- as.character(days_operations)
# convert to datetime as it helps while operating with "+" and "-"
days_operations <- lubridate::as_datetime(days_operations)
# add aux variables, start_day and end_day for each deployment
deploys <-
deploys %>%
dplyr::mutate(
start_day = lubridate::date(.data$deploymentStart),
end_day = lubridate::date(.data$deploymentEnd)
)

# make a operation table per deployment
deployment_operational <- purrr::map(
deploys$deploymentID,
function(x) {
start_day <-
deploys %>%
dplyr::filter(.data$deploymentID == x) %>%
dplyr::pull(start_day)
end_day <-
deploys %>%
dplyr::filter(.data$deploymentID == x) %>%
dplyr::pull(end_day)
operational <- days_operations > start_day & days_operations < end_day
operational[operational == TRUE] <- 1
# edge cases start and end day
deploy_df <-
deploys %>%
dplyr::filter(.data$deploymentID == x)
daily_effort_start <- calc_daily_effort(deploy_df, calc_start = TRUE)
daily_effort_end <- calc_daily_effort(deploy_df, calc_end = TRUE)
operational[days_operations == start_day] <- daily_effort_start
operational[days_operations == end_day] <- daily_effort_end
operational <- dplyr::as_tibble(operational)
names(operational) <- x
return(operational)
}
)
names(deployment_operational) <- deploys$deploymentID

# add session to station names
if (!is.null(session_col)) {
deploys <- deploys %>%
dplyr::mutate(!!station_col := paste(.data[[station_col]],
.data[[session_col]],
sep = "__SESS_")
)
}

# add camera to column names
if (!is.null(camera_col)) {
deploys <- deploys %>%
dplyr::mutate(!!station_col := paste(.data[[station_col]],
.data[[camera_col]],
sep = "__CAM_")
)
}

# get for each station_col which days a deployment was active
camOps <- purrr::map_dfc(
unique(deploys[[station_col]]),
function(loc_name) {
# get deployments linked to the location
deploys_id <-
deploys %>%
dplyr::filter(.data[[station_col]] == loc_name) %>%
dplyr::pull(.data$deploymentID)
# get operational dfs linked to these deployment_ids
dep_dfs <- deployment_operational[
names(deployment_operational) %in% deploys_id
]
dep_op <- dplyr::bind_cols(dep_dfs)
# sum daily effort along all deployments at same location
dep_op <- dplyr::as_tibble(rowSums(dep_op[, names(dep_op)], na.rm = TRUE))
# set locations as station id
names(dep_op) <- loc_name
if (use_prefix == TRUE) {
names(dep_op) <- paste0("Station", names(dep_op))
}
# the 0s should actually be NAs meaning "camera(s) not set up". Notice
# that in the actual stadium of camera trap dp exchange format, 0s as
# returned by camtrapR::cameraOperation()` meaning "camera(s) not
# operational", will never occur.
dep_op[dep_op == 0] <- NA
dep_op[[names(dep_op)]] <- as.numeric(dep_op[[names(dep_op)]])
return(dep_op)
}
)
# transform to matrix
camOps <- as.matrix(camOps)
# add names to rows (days)
rownames(camOps) <- days_operations_string
# transpose to get location name as rows and days as columns and return
t(camOps)
}
Loading