Skip to content

Commit

Permalink
Rename get_cam_op()
Browse files Browse the repository at this point in the history
See #239
  • Loading branch information
damianooldoni committed Jul 22, 2024
1 parent dee8feb commit 7e44fd2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 deletions.
14 changes: 7 additions & 7 deletions R/get_cam_op.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
#' library(dplyr)
#'
#' x <- example_dataset()
#' get_cam_op(x)
#' camtrapR_cameraOperation(x)
#'
#' # Specify column with station names
#' get_cam_op(x, station_col = "locationID")
#' camtrapR_cameraOperation(x, station_col = "locationID")
#'
#' # Specify column with session IDs
#' x_sessions <- x
Expand All @@ -49,19 +49,19 @@
#' "before2020"
#' )
#' )
#' get_cam_op(x_sessions, session_col = "session")
#' 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)
#' get_cam_op(x_cameras, camera_col = "cameraID")
#' camtrapR_cameraOperation(x_cameras, camera_col = "cameraID")
#'
#' # Specify both session and camera IDs
#' get_cam_op(x_cameras, camera_col = "cameraID", session_col = "session")
#' camtrapR_cameraOperation(x_cameras, camera_col = "cameraID", session_col = "session")
#'
#' # Use prefix Station as in camtrapR's camera operation matrix
#' get_cam_op(x, use_prefix = TRUE)
get_cam_op <- function(x,
#' camtrapR_cameraOperation(x, use_prefix = TRUE)
camtrapR_cameraOperation <- function(x,
station_col = "locationName",
camera_col = NULL,
session_col = NULL,
Expand Down
4 changes: 2 additions & 2 deletions R/get_custom_effort.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Gets the custom effort (deployment duration) for a custom time window and a
#' specific time interval such as day, week, month or year. The custom effort is
#' also calculated over all deployments. This function calls `get_cam_op()`
#' also calculated over all deployments. This function calls `camtrapR_cameraOperation()`
#' internally.
#'
#' @param start Start date. Default: `NULL`. If `NULL` the earliest start date
Expand Down Expand Up @@ -95,7 +95,7 @@ get_custom_effort <- function(x,
deployments <- deployments(x)

# Camera operation matrix with filter(s) on deployments
cam_op <- get_cam_op(x, station_col = "deploymentID")
cam_op <- camtrapR_cameraOperation(x, 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
70 changes: 35 additions & 35 deletions tests/testthat/test-get_cam_op.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ test_that("input camtrap dp is checked properly", {
skip_if_offline()
x <- example_dataset()
# Character instead of datapackage
expect_error(get_cam_op("aaa"))
expect_error(camtrapR_cameraOperation("aaa"))
# Numeric instead of datapackage
expect_error(get_cam_op(1))
expect_error(camtrapR_cameraOperation(1))
# Station_col is not NA
expect_error(
get_cam_op(x, station_col = NA),
camtrapR_cameraOperation(x, station_col = NA),
"station_col is not a string (a length one character vector).",
fixed = TRUE)
# Station_col is length 1
expect_error(
get_cam_op(x, station_col = c("locationID","locationName")),
camtrapR_cameraOperation(x, station_col = c("locationID","locationName")),
"station_col is not a string (a length one character vector).",
fixed = TRUE)
# Station_col value is not a column of deployments
expect_error(
get_cam_op(x, station_col = "bla"),
camtrapR_cameraOperation(x, station_col = "bla"),
paste0(
"Station column name (`bla`) is not valid: ",
"it must be one of the deployments column names."
Expand All @@ -27,22 +27,22 @@ test_that("input camtrap dp is checked properly", {
# Column specified by station_col contains empty values
x_empty_location_name <- x
x_empty_location_name$data$deployments$locationName[2:3] <- NA
expect_error(get_cam_op(x_empty_location_name),
expect_error(camtrapR_cameraOperation(x_empty_location_name),
"Column `locationName` must be non-empty: 2 NAs found."
)
# Camera_col is not NA
expect_error(
get_cam_op(x, camera_col = NA),
camtrapR_cameraOperation(x, camera_col = NA),
"camera_col is not a string (a length one character vector).",
fixed = TRUE)
# Camera_col is length 1
expect_error(
get_cam_op(x, camera_col = c("locationID","locationName")),
camtrapR_cameraOperation(x, camera_col = c("locationID","locationName")),
"camera_col is not a string (a length one character vector).",
fixed = TRUE)
# Station_col value is not a column of deployments
expect_error(
get_cam_op(x, camera_col = "bla"),
camtrapR_cameraOperation(x, camera_col = "bla"),
paste0(
"Camera column name (`bla`) is not valid: ",
"it must be one of the deployments column names."
Expand All @@ -51,39 +51,39 @@ test_that("input camtrap dp is checked properly", {
)
# Session_col is not NA
expect_error(
get_cam_op(x, session_col = NA),
camtrapR_cameraOperation(x, session_col = NA),
"session_col is not a string (a length one character vector).",
fixed = TRUE)
# Session_col is length 1
expect_error(
get_cam_op(x, session_col = c("locationID","locationName")),
camtrapR_cameraOperation(x, session_col = c("locationID","locationName")),
"session_col is not a string (a length one character vector).",
fixed = TRUE)
# Session_col value is not a column of deployments
expect_error(
get_cam_op(x, session_col = "bla"),
camtrapR_cameraOperation(x, session_col = "bla"),
paste0(
"Session column name (`bla`) is not valid: ",
"it must be one of the deployments column names."
),
fixed = TRUE
)
# use_prefix must be TRUE or FALSE
expect_error(get_cam_op(x, use_prefix = "bla"))
expect_error(get_cam_op(x, use_prefix = NA))
expect_error(camtrapR_cameraOperation(x, use_prefix = "bla"))
expect_error(camtrapR_cameraOperation(x, use_prefix = NA))
})

test_that("output is a matrix", {
skip_if_offline()
x <- example_dataset()
cam_op_matrix <- get_cam_op(x)
cam_op_matrix <- camtrapR_cameraOperation(x)
expect_true(is.matrix(cam_op_matrix))
})

test_that("output matrix has locations as rownames", {
skip_if_offline()
x <- example_dataset()
cam_op_matrix <- get_cam_op(x)
cam_op_matrix <- camtrapR_cameraOperation(x)
locations <- deployments(x)$locationName
n_locations <- length(deployments(x)$locationName)
expect_identical(nrow(cam_op_matrix), n_locations)
Expand All @@ -101,7 +101,7 @@ test_that("output matrix has sessions addded to locations as rownames", {
"before2020"
)
)
cam_op_matrix <- get_cam_op(x_sessions, session_col = "session")
cam_op_matrix <- camtrapR_cameraOperation(x_sessions, session_col = "session")
locations_sessions <- paste(deployments(x_sessions)$locationName,
deployments(x_sessions)$session,
sep = "__SESS_"
Expand All @@ -116,7 +116,7 @@ test_that("output matrix has camera IDs addded to locations as rownames", {
x <- example_dataset()
x_cameras <- x
x_cameras$data$deployments$cameraID <- c(1, 2, 3, 4)
cam_op_matrix <- get_cam_op(x_cameras, camera_col = "cameraID")
cam_op_matrix <- camtrapR_cameraOperation(x_cameras, camera_col = "cameraID")
locations_cameras <- paste(deployments(x_sessions)$locationName,
deployments(x_sessions)$cameraID,
sep = "__CAM_"
Expand All @@ -133,7 +133,7 @@ test_that(
x_sess_cam <- x
x_sess_cam$data$deployments$cameraID <- c(1, 2, 3, 4)
x_sess_cam$data$deployments$session <- c(1, 2, 3, 4)
cam_op_matrix <- get_cam_op(x_sess_cam,
cam_op_matrix <- camtrapR_cameraOperation(x_sess_cam,
camera_col = "cameraID",
session_col = "session"
)
Expand All @@ -157,14 +157,14 @@ test_that(
x <- example_dataset()
x_sess <- x
x_sess$data$deployments$session <- c("1__SESS_1")
expect_error(get_cam_op(x_sess, session_col = "session"),
expect_error(camtrapR_cameraOperation(x_sess, session_col = "session"),
paste0("Session column name (`session`) must not contain any ",
"of the reserved words: \"__SESS_\", \"__CAM_\"."),
fixed = TRUE
)
x_sess <- x
x_sess$data$deployments$cameraID <- paste0(c(1,2,3,4), "__SESS_")
expect_error(get_cam_op(x_sess, camera_col = "cameraID"),
expect_error(camtrapR_cameraOperation(x_sess, camera_col = "cameraID"),
paste0("Camera column name (`cameraID`) must not contain any ",
"of the reserved words: \"__SESS_\", \"__CAM_\"."),
fixed = TRUE
Expand All @@ -175,7 +175,7 @@ test_that(
deployments(x_sess)$locationName[1]
)
expect_error(
get_cam_op(x_sess),
camtrapR_cameraOperation(x_sess),
paste0("Station column name (`locationName`) must not contain any ",
"of the reserved words: \"__SESS_\", \"__CAM_\"."),
fixed = TRUE
Expand All @@ -190,14 +190,14 @@ test_that(
x <- example_dataset()
x_cam <- x
x_cam$data$deployments$session[1] <- c("1__CAM_1")
expect_error(get_cam_op(x_cam, session_col = "session"),
expect_error(camtrapR_cameraOperation(x_cam, session_col = "session"),
paste0("Session column name (`session`) must not contain any ",
"of the reserved words: \"__SESS_\", \"__CAM_\"."),
fixed = TRUE
)
x_cam <- x
x_cam$data$deployments$cameraID <- paste0(c(1,2,3,4), "__CAM_")
expect_error(get_cam_op(x_cam, camera_col = "cameraID"),
expect_error(camtrapR_cameraOperation(x_cam, camera_col = "cameraID"),
paste0("Camera column name (`cameraID`) must not contain any ",
"of the reserved words: \"__SESS_\", \"__CAM_\"."),
fixed = TRUE
Expand All @@ -208,7 +208,7 @@ test_that(
deployments(x_cam)$locationName[1]
)
expect_error(
get_cam_op(x_cam),
camtrapR_cameraOperation(x_cam),
paste0("Station column name (`locationName`) must not contain any ",
"of the reserved words: \"__SESS_\", \"__CAM_\"."),
fixed = TRUE
Expand All @@ -219,7 +219,7 @@ test_that(
test_that("output matrix has Station prefix in rownames", {
skip_if_offline()
x <- example_dataset()
cam_op_matrix <- get_cam_op(x, use_prefix = TRUE)
cam_op_matrix <- camtrapR_cameraOperation(x, use_prefix = TRUE)
locations <- paste0("Station", deployments(x)$locationName)
n_locations <- length(deployments(x)$locationName)
expect_identical(nrow(cam_op_matrix), n_locations)
Expand All @@ -229,7 +229,7 @@ test_that("output matrix has Station prefix in rownames", {
test_that("output matrix has specified location column as rownames", {
skip_if_offline()
x <- example_dataset()
cam_op_matrix <- get_cam_op(x, station_col = "locationID")
cam_op_matrix <- camtrapR_cameraOperation(x, station_col = "locationID")
locations <- deployments(x)$locationID
n_locations <- length(deployments(x)$locationID)
expect_identical(nrow(cam_op_matrix), n_locations)
Expand All @@ -240,7 +240,7 @@ test_that("output matrix has specified location column as rownames", {
test_that("output matrix has all deployment days as colnames", {
skip_if_offline()
x <- example_dataset()
cam_op_matrix <- get_cam_op(x)
cam_op_matrix <- camtrapR_cameraOperation(x)
days_activity <- seq(as.Date(min(deployments(x)$start)),
as.Date(max(deployments(x)$end)),
by = "days"
Expand All @@ -254,7 +254,7 @@ test_that("output matrix has all deployment days as colnames", {
test_that("daily effort is > 0 for fully active days, NA for inactive days", {
skip_if_offline()
x <- example_dataset()
cam_op_matrix <- get_cam_op(x)
cam_op_matrix <- camtrapR_cameraOperation(x)
location <- deployments(x)$locationName[4]
deployment_start <- deployments(x) %>%
dplyr::filter(locationName == location) %>%
Expand All @@ -280,7 +280,7 @@ test_that("daily effort is > 0 for fully active days, NA for inactive days", {
test_that("daily effort is > 0 and < 1 for partial active days (start/end)", {
skip_if_offline()
x <- example_dataset()
cam_op_matrix <- get_cam_op(x)
cam_op_matrix <- camtrapR_cameraOperation(x)
location <- deployments(x)$locationName[4]
start <- as.character(as.Date(deployments(x)$start[4]))
end <- as.character(as.Date(deployments(x)$end[4]))
Expand All @@ -299,7 +299,7 @@ test_that(
x1$data$deployments$start[2] <- lubridate::as_datetime("2020-07-30 21:00:00")
x1$data$deployments$end[2] <- lubridate::as_datetime("2020-08-07 21:00:00")
x1$data$deployments$locationName[2] <- deployments(x1)$locationName[1]
cam_op_matrix <- get_cam_op(x1)
cam_op_matrix <- camtrapR_cameraOperation(x1)

first_full_day_two_deps <- as.character(
as.Date(deployments(x1)$start[2]) + lubridate::ddays(1)
Expand All @@ -323,8 +323,8 @@ test_that(
x <- example_dataset()
x1 <- x
x1$data$deployments$locationName[2] <- deployments(x1)$locationName[1]
cam_op_matrix1 <- get_cam_op(x1)
cam_op_matrix <- get_cam_op(x)
cam_op_matrix1 <- camtrapR_cameraOperation(x1)
cam_op_matrix <- camtrapR_cameraOperation(x)
start_date1 <- as.character(as.Date(deployments(x)$start[1]))
start_date2 <- as.character(as.Date(deployments(x)$start[2]))
end_date1 <- as.character(as.Date(deployments(x)$end[1]))
Expand Down Expand Up @@ -353,9 +353,9 @@ test_that("Argument datapkg is deprecated: warning returned", {
expect_warning(
rlang::with_options(
lifecycle_verbosity = "warning",
get_cam_op(datapkg = x)
camtrapR_cameraOperation(datapkg = x)
),
paste0("The `datapkg` argument of `get_cam_op()` is deprecated ",
paste0("The `datapkg` argument of `camtrapR_cameraOperation()` is deprecated ",
"as of camtraptor 0.16.0."
),
fixed = TRUE
Expand Down
16 changes: 8 additions & 8 deletions vignettes/camera-operation-matrix.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ It contains camera trap data about musk rat and coypu. We will use this variable
You can create a camera operation matrix by passing a camera trap data package:

```{r basic_usage}
get_cam_op(x)
camtrapR_cameraOperation(x)
```

The function will read the `deployments` slot of the data package to create the matrix.
Expand All @@ -58,7 +58,7 @@ x1 <- x
x1$data$deployments$locationName <- deployments(x1)$locationName[1]
x1$data$deployments$start <- deployments(x1)$start[1]
x1$data$deployments$end <- deployments(x1)$deployments$end[1]
multiple_deploys_matrix <- get_cam_op(x1)
multiple_deploys_matrix <- camtrapR_cameraOperation(x1)
multiple_deploys_matrix
```

Expand All @@ -75,7 +75,7 @@ deployments(x2) %>% dplyr::select(locationName, start, end)
This results in the following camera operation matrix:

```{r camOp_two_deploys_two_periods}
two_deploys_two_periods_matrix <- get_cam_op(x2)
two_deploys_two_periods_matrix <- camtrapR_cameraOperation(x2)
days_to_show <- seq(as.Date(deployments(x2)$start[3]),
as.Date(deployments(x2)$end[4]),
by = "days")
Expand All @@ -87,14 +87,14 @@ two_deploys_two_periods_matrix[3,as.character(days_to_show)]
You can specify the column containing the station names instead of using the default `locationName`. In the example below we use the column `locationID`. A small preview of the matrix is shown:

```{r cam_op_with_locationID}
cam_op_with_locationID <- get_cam_op(x, station_col = "locationID")
cam_op_with_locationID <- camtrapR_cameraOperation(x, station_col = "locationID")
cam_op_with_locationID[1:4, 1:2]
```

You can also decide to use prefix `"Station"` in the station names as done by camtrapR's `cameraOperation()` by setting `use_prefix = TRUE`. A small preview of the entire matrix is shown:

```{r use_prefix}
cam_op_with_prefix <- get_cam_op(x, use_prefix = TRUE)
cam_op_with_prefix <- camtrapR_cameraOperation(x, use_prefix = TRUE)
cam_op_with_prefix[1:4,1:2]
```

Expand All @@ -105,7 +105,7 @@ You can specify the column containing the camera IDs to be added to the station
```{r add_camera_IDs}
x_cameras <- x
x_cameras$data$deployments$cameraID <- c(1, 2, 3, 4)
cam_op_with_camera_ids <- get_cam_op(x_cameras, camera_col = "cameraID")
cam_op_with_camera_ids <- camtrapR_cameraOperation(x_cameras, camera_col = "cameraID")
row.names(cam_op_with_camera_ids)
```

Expand All @@ -114,15 +114,15 @@ You cans also add the session IDs using `session_col` argument, following the ca
```{r add_session_IDs}
x_sessions <- x
x_sessions$data$deployments$session <- c(1, 2, 3, 4)
cam_op_with_session_ids <- get_cam_op(x_sessions, session_col = "session")
cam_op_with_session_ids <- camtrapR_cameraOperation(x_sessions, session_col = "session")
row.names(cam_op_with_session_ids)
```

To use both camera and session IDs, the camtrapR's convention `Station__SESS_SessionID__CAM_CameraID` is followed:

```{r add_session_camera_IDs}
x_sessions$data$deployments$cameraID <- c(1, 2, 3, 4)
cam_op_with_session_and_camera_ids <- get_cam_op(
cam_op_with_session_and_camera_ids <- camtrapR_cameraOperation(
x_sessions,
camera_col = "cameraID",
session_col = "session"
Expand Down

0 comments on commit 7e44fd2

Please sign in to comment.