Threshold a previously predicted SDM
Usage
thresh_sdm(
pred_file,
this_taxa = stringr::str_extract(pred_file, "[[:alpha:]]+\\s[[:alpha:]]+"),
threshold,
thresh_file = gsub("__pred__", "__thresh__", pred_file),
terra_options = NULL,
force_new = FALSE,
do_gc = FALSE,
check_tifs = TRUE
)
Arguments
- pred_file
Character. File path of predicted sdm to threshold.
- threshold
Numeric. > 0 and < 1. Threshold to apply to the raster stored in the file at
pred_file
. Often this value will be available within the result of a call totune_sdm()
. e.g.mod <- rio::import("tune.rds")
and thenmod$e[[1]]@thresholds$max_spec_sens
- thresh_file
Character. Name to give the output threshold. Defaults to
gsub("pred", "thresh", pred_file)
- terra_options
Passed to
terra::terraOptions()
. e.g. list(memfrac = 0.6)- force_new
Logical. If output files already exist, should they be remade?
- do_gc
Logical. Run
base::rm(list = ls)
andbase::gc()
at end of function? Useful to keep RAM use down when running SDMs for many, many taxa, especially if done in parallel.- check_tifs
Logical. Check if any output
.tif
files error onterra::rast()
and delete them if they do. Useful after a crash during pred_file.
Examples
# setup -------
out_dir <- file.path(system.file(package = "envSDM"), "examples")
# data ------
extract_thresh <- function(tune, metric, thresh_type = "max_spec_sens") {
tune |>
dplyr::filter(!!rlang::ensym(metric) == max(!!rlang::ensym(metric))) |>
dplyr::pull(!!rlang::ensym(thresh_type))
}
data <- fs::path(system.file(package = "envSDM"), "examples") |>
fs::dir_ls(regexp = "pred\\.tif"
, recurse = TRUE
) |>
tibble::enframe(name = NULL, value = "pred") |>
dplyr::mutate(out_dir = dirname(pred)
, taxa = basename(dirname(out_dir))
, metric = basename(out_dir)
, tune = fs::dir_ls(dirname(out_dir), regexp = "tune.rds")
, tune_mean = purrr::map(tune, \(x) rio::import(x)$tune_mean |> dplyr::select(algo, tune_args, auc_po, combo, max_spec_sens))
, thresh = purrr::map2_dbl(tune_mean
, metric
, extract_thresh
)
)
#> Warning: There were 4 warnings in `dplyr::mutate()`.
#> The first warning was:
#> ℹ In argument: `tune_mean = purrr::map(...)`.
#> Caused by warning:
#> ! Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> ℹ Run dplyr::last_dplyr_warnings() to see the 3 remaining warnings.
## thresh -------
purrr::pwalk(list(data$pred
, data$thresh
, data$taxa
)
, \(a, b, c) thresh_sdm(pred_file = a
, threshold = b
, this_taxa = c
, thresh_file = "thresh.tif"
, force_new = TRUE
)
)
## visualise-------
### threshold -------
purrr::walk(data$out_dir
, \(x) fs::path(x, "thresh.tif") %>%
terra::rast() %>%
terra::trim() %>%
terra::plot()
)