Skip to content

Commit

Permalink
Remove reliance on 'plyr' and fix minor typos.
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardlavender committed Jul 12, 2021
1 parent cd5f800 commit 9e2d59b
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 8 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ Author: Edward Lavender
Maintainer: Edward Lavender <[email protected]>
Description: Algorithms which facilitate the use of passive acoustic telemetry (PAT) data for ecological inferences, especially those pertaining to animal space use. This includes functions for processing PAT data, spatial tools such as least-cost pathway calculations, new algorithms for inferring space use and simulations designed to evaluate the efficacy of algorithms for inferring space use. Package development has been motivated by the collection of PAT data for a Critically Endangered benthopelagic elasmobranch off the west coast of Scotland.
Imports:
methods, magrittr, data.table, dplyr, plyr, rlang, lubridate,
methods, magrittr, data.table, dplyr, rlang, lubridate,
purrr, pbapply,
Matrix, raster, rgeos, geosphere, Orcs, cppRouting, gdistance, sp, adehabitatHR,
prettyGraphics, fields,
Tools4ETS
Suggests:
plyr,
glatos,
plotly, animation, scales, viridis,
circular,
Expand Down
7 changes: 3 additions & 4 deletions R/acdc.R
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ acdc_setup_centroids <- function(
#### Define a list of acoustic centroids for each receiver
cat_to_console("... Building a nested list of acoustic centroids. This is the slow step...")

## Define a sequence of centroid radiuss
## Define a sequence of centroid radius
# Around each receiver, we'll create a polygon of this radius
radius_seq <- seq(detection_range, length.out = n_timesteps, by = mobility)

Expand Down Expand Up @@ -423,7 +423,6 @@ acdc_setup_centroids <- function(
}



######################################
######################################
#### .acdc()
Expand Down Expand Up @@ -713,7 +712,7 @@ acdc_setup_centroids <- function(
receiver_2_id <- acoustics$receiver_id[timestep_detection+1]

#### Duration between detections to the nearest 2 minutes (floored):
time_btw_dets <- plyr::round_any(as.numeric(difftime(receiver_2_timestamp, receiver_1_timestamp, units = "mins")), 2)
time_btw_dets <- round_any(as.numeric(difftime(receiver_2_timestamp, receiver_1_timestamp, units = "mins")), 2)


######################################
Expand Down Expand Up @@ -1017,7 +1016,7 @@ acdc_setup_centroids <- function(
#'
#' In outline, the crux of the approach is the recognition that acoustic detections typically occur irregularly, while archival observations occur at regular intervals. Each detection anchors our knowledge of the location of an individual around a particular receiver (assuming that all detections are true detections). As time passes between acoustic detections, our uncertainty about the geographical location of an individual expands around the receiver at which it was detected before shrinking towards the receiver at which it was next detected. During this time, regular depth observations restrict the number of possible locations in which the individual could have been located at each time step.
#'
#' More specifically, when an individual is detected, it must be within some radius – say 800 m – of that receiver. This is the starting acoustic centroid. With a more-refined model of detection probability, it may be possible to predict more precisely where the individual is likely to have been within this centroid (but this approach is not yet implemented). The observed depth at this time further restricts the positions in which the individual could have been, assuming a benthic/demersal lifestyle and a non-homogenous bathymetric landscape. Moving forward in time, a number of depth records may be made before another acoustic detection. During this time, our uncertainty about where the individual could have been gets larger, because it could have moved further away from the receiver, so the acoustic centroids that define this uncertainty expand to a maximum size at the halfway point between acoustic detections. After that, the individual must have started to move towards the receiver at which it was next detected, so these acoustic centroids start to shrink towards that receiver. If the individual was detected by different receivers, the overlap between the centroids of these two receivers at the halfway point defines the set of positions in which the individual could have been at this time. Thereafter, our uncertainty in the individual's location is given by the overlap between the expansion of this centroid region and the contraction of the centroid around the receiver at which it was next detected. Thus, when the individual is detected again, our uncertainty about where it could have been collapses to the detection radius around the next receiver, possibly weighted by a model of detection probability around this receiver (although that is not yet implemented). The rate of change in centroid size depends a movement parameter that describes an average swimming speed, which will depend on some underlying estimated behavioural state (although that is not yet implemented).
#' More specifically, when an individual is detected, it must be within some radius---say 800 m---of that receiver. This is the starting acoustic centroid. With a more-refined model of detection probability, it may be possible to predict more precisely where the individual is likely to have been within this centroid (but this approach is not yet implemented). The observed depth at this time further restricts the positions in which the individual could have been, assuming a benthic/demersal lifestyle and a non-homogeneous bathymetric landscape. Moving forward in time, a number of depth records may be made before another acoustic detection. During this time, our uncertainty about where the individual could have been gets larger, because it could have moved further away from the receiver, so the acoustic centroids that define this uncertainty expand to a maximum size at the halfway point between acoustic detections. After that, the individual must have started to move towards the receiver at which it was next detected, so these acoustic centroids start to shrink towards that receiver. If the individual was detected by different receivers, the overlap between the centroids of these two receivers at the halfway point defines the set of positions in which the individual could have been at this time. Thereafter, our uncertainty in the individual's location is given by the overlap between the expansion of this centroid region and the contraction of the centroid around the receiver at which it was next detected. Thus, when the individual is detected again, our uncertainty about where it could have been collapses to the detection radius around the next receiver, possibly weighted by a model of detection probability around this receiver (although that is not yet implemented). The rate of change in centroid size depends a movement parameter that describes an average swimming speed, which will depend on some underlying estimated behavioural state (although that is not yet implemented).
#'
#' The result is a map that shows where the individual could have spent more or less (or no) time over the time interval under construction. The main limitation of this approach is that reconstructs where the individual could have been, but not where it was. In reality, the individual's current position constrains where it can go next. The ACDCPF/MP algorithms are extensions of this approach that incorporate a movement model for this reason.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/dcpf.R
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ dcpf <- function(archival,
if(nrow(pr_j) > 0) out <- NULL else out <- pr_j
return(pr_j)
})
cells_from_current_to_next <- plyr::compact(cells_from_current_to_next)
cells_from_current_to_next <- compact(cells_from_current_to_next)
cells_from_current_to_next <- do.call(rbind, cells_from_current_to_next)
}

Expand Down
2 changes: 1 addition & 1 deletion R/evals.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ eval_by_kud <-
if(!is.null(add_land)) add_land$x <- array$land
if(!is.null(add_sea)) add_sea$x <- array$sea
add_polys <- list(add_land, add_sea)
add_polys <- plyr::compact(add_polys)
add_polys <- compact(add_polys)
if(length(add_polys) == 0L) add_polys <- NULL
if(!is.null(add_receivers)) add_receivers$x <- array$xy

Expand Down
3 changes: 3 additions & 0 deletions R/spatial_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ mask_io <- function(x, mask, mask_inside = FALSE,...){
#' @param r A \code{\link[raster]{raster}}.
#' @param n An integer that defines the number of parts into which to split the raster.
#' @details The raster (\code{r}) should not only contain NAs.
#' @note This function requires the `plyr' package.
#' @return The function returns a list containing the split raster components.
#'
#' @examples
Expand All @@ -311,6 +312,8 @@ mask_io <- function(x, mask, mask_inside = FALSE,...){
#' @export

split_raster_equally <- function(r, n) {
# Check for plyr
if(!requireNamespace("plyr", quietly = TRUE)) stop("This function requires the 'plyr' package.")
# get total number of non NA grid cells
mean.r <- raster::calc(r, function(x) {
s <- sum(!is.na(x))
Expand Down
29 changes: 29 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@
NULL


######################################
######################################
#### round_any()

#' @title Round to multiple of any number
#' @description Round to multiple of any number.
#' @param x numeric or date-time (POSIXct) vector to round
#' @param accuracy number to round to; for POSIXct objects, a number of seconds
#' @param f rounding function: floor, ceiling or round
#' @source This function and the documentation are taken from the `plyr' package. The function is defined separately in \code{\link[flapper]{flapper}} to reduce reliance on non-default packages.
#' @keywords internal
#'

round_any <- function(x, accuracy, f = round){f(x/accuracy) * accuracy}


######################################
######################################
#### compact()

#' @title Compact a list
#' @description Remove all \code{NULL} entries from a list.
#' @param l A list.
#' @source This function is derived from the \code{plyr::compact()} function. The function is defined separately in \code{\link[flapper]{flapper}} to reduce reliance on non-default packages.
#' @keywords internal

compact <- function(l) l[which(!sapply(l, is.null))]


######################################
######################################
#### check...()
Expand Down
2 changes: 1 addition & 1 deletion man/acdc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/compact.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/round_any.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/split_raster_equally.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9e2d59b

Please sign in to comment.