-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from BiologicalRecordsCentre/dev
Dev
- Loading branch information
Showing
17 changed files
with
375 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
#' Defines detection using a custom function | ||
#' Simulate Detection Process | ||
#' | ||
#' @param simulation_object a SimulationObject | ||
#' @param fun a function that takes the simulation object and returns a simulation object with data in detection slot | ||
#' @param ... other parameters for the user supplied function fun | ||
#' @return A SimulationObject with a detection | ||
#' @examples | ||
#' \dontrun{ | ||
#' sim_effort_fun(simulation_object, fun, ...) | ||
#' } | ||
sim_detect <- function(simulation_object, fun, ...) { | ||
check_fun(fun) | ||
#' This function simulates the detection process in a biodiversity study using a specified function to extract the realised state. | ||
#' | ||
#' @param simulation_object A `SimulationObject` that contains the state of the environment, target, effort, and other relevant simulation information. | ||
#' @param realised_extract_fun A function to extract the realised state used in \code{\link[=sim_extract]{sim_extract()}}. The default is `mean`. | ||
#' | ||
#' @return A `SimulationObject` with the detection information added. | ||
sim_detect <- function(simulation_object, realised_extract_fun = mean) { | ||
check_fun(realised_extract_fun) | ||
|
||
simulation_object_original <- simulation_object | ||
simulation_object <- read_sim_obj_rasters(simulation_object) | ||
|
||
# apply the function | ||
detections <- fun(simulation_object, ...) | ||
extracted <- sim_extract(simulation_object,realised_extract_fun=realised_extract_fun) | ||
|
||
|
||
# validity checks | ||
fun_args <- as.list(match.call()) | ||
simulation_object_original@metadata[["detect"]] <- fun_args[3:length(fun_args)] | ||
|
||
simulation_object_original@detect <- detections | ||
simulation_object_original@detect <- extracted | ||
simulation_object_original@hash <- hash_sim_obj(simulation_object_original) | ||
simulation_object_original | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#' Extract Environmental, Suitability, and Realised Values at Sampling Locations | ||
#' | ||
#' This function extracts values from the environmental state, target suitability, and realised target state | ||
#' at the locations defined by the sampling effort in a `SimulationObject`. | ||
#' | ||
#' @param simulation_object A `SimulationObject` containing the environmental state, target suitability, | ||
#' realised target state, and sampling effort. | ||
#' @param fun fun argument passed to \link[terra]{extract} for extracting realised values. | ||
#' @param ... Additional arguments passed to \link[terra]{extract} for extracting realised values. | ||
#' | ||
#' @return A `SimulationObject` with updated `@effort` slot containing extracted values for environmental | ||
#' state, target suitability, and realised target state at each sampling location. | ||
#' | ||
#' @details | ||
#' This function extracts the values from the environmental state (`@state_env`), target suitability | ||
#' (`@state_target_suitability`), and realised target state (`@state_target_realised`) at the sampling locations | ||
#' defined in the `@effort` slot of the input `SimulationObject`. The extracted values are added as columns to the `@effort` slot. | ||
#' | ||
#' @export | ||
sim_extract <- function(simulation_object,realised_extract_fun=mean) { | ||
#get the effort | ||
effort_sf <- simulation_object@effort | ||
|
||
#get values from env, suitability, realised | ||
extracted_values <- terra::extract(simulation_object@state_env,effort_sf,fun = mean) | ||
effort_sf[,names(extracted_values)] <- extracted_values | ||
|
||
#loop through each target | ||
targets_sf <- list() | ||
for (target in names(simulation_object@state_target_suitability)){ | ||
target_sf <- effort_sf | ||
target_sf$target <- target | ||
|
||
#extract suitability | ||
extracted_values <- terra::extract(simulation_object@state_target_suitability[target],effort_sf,ID=F,fun = mean) | ||
target_sf[,"target_suitability"] <- extracted_values | ||
|
||
#target realised | ||
extracted_values <- terra::extract(simulation_object@state_target_realised[target],effort_sf,ID=F,fun = mean) | ||
target_sf[,"target_realised"] <- extracted_values | ||
|
||
#extract realised | ||
extracted_values <- terra::extract(simulation_object@state_target_realised[target],effort_sf,ID=F,fun = realised_extract_fun) | ||
target_sf[,"target_detected"] <- extracted_values | ||
|
||
targets_sf[[target]] <- target_sf | ||
} | ||
|
||
effort_sf <- do.call(rbind,targets_sf) | ||
rownames(effort_sf) <- NULL | ||
|
||
effort_sf_extracted <- effort_sf | ||
effort_sf_extracted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
#' Defines reporting using a custom function | ||
#' #' Defines reporting using a custom function | ||
#' #' | ||
#' #' @param simulation_object a SimulationObject | ||
#' #' @param fun a function that takes the simulation object and returns a simulation object with data in reporting slot | ||
#' #' @param ... other parameters for the user supplied function fun | ||
#' #' @return A SimulationObject with reporting | ||
#' #' @examples | ||
#' #' \dontrun{ | ||
#' #' sim_report_fun(simulation_object, fun, ...) | ||
#' #' } | ||
#' sim_report <- function(simulation_object, fun, ...) { | ||
#' check_fun(fun) | ||
#' simulation_object_original <- simulation_object | ||
#' simulation_object <- read_sim_obj_rasters(simulation_object) | ||
#' | ||
#' @param simulation_object a SimulationObject | ||
#' @param fun a function that takes the simulation object and returns a simulation object with data in reporting slot | ||
#' @param ... other parameters for the user supplied function fun | ||
#' @return A SimulationObject with reporting | ||
#' @examples | ||
#' \dontrun{ | ||
#' sim_report_fun(simulation_object, fun, ...) | ||
#' # apply the function | ||
#' report <- fun(simulation_object, ...) | ||
#' | ||
#' # validity checks | ||
#' fun_args <- as.list(match.call()) | ||
#' simulation_object_original@metadata[["report"]] <- fun_args[3:length(fun_args)] | ||
#' | ||
#' simulation_object_original@report <- report | ||
#' simulation_object_original@hash <- hash_sim_obj(simulation_object_original) | ||
#' simulation_object_original | ||
#' } | ||
sim_report <- function(simulation_object, fun, ...) { | ||
check_fun(fun) | ||
simulation_object_original <- simulation_object | ||
simulation_object <- read_sim_obj_rasters(simulation_object) | ||
|
||
# apply the function | ||
report <- fun(simulation_object, ...) | ||
|
||
# validity checks | ||
fun_args <- as.list(match.call()) | ||
simulation_object_original@metadata[["report"]] <- fun_args[3:length(fun_args)] | ||
|
||
simulation_object_original@report <- report | ||
simulation_object_original@hash <- hash_sim_obj(simulation_object_original) | ||
simulation_object_original | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.