Finds any .tif files in pred_dir
and writes them to .png files. Includes the
retrieval and addition to the map of: various SDM metrics; and the original
presence points.
Usage
png_from_preds(
pred_dir,
full_run_dir = NULL,
trim = TRUE,
force_new = FALSE,
do_gc = TRUE,
...
)
Arguments
- trim
Logical. Trim NA values from outside (using
terra::trim()
)- force_new
Logical. If .png file already exists, recreate it?
- do_gc
Logical. Run
base::rm(list = ls)
andbase::gc()
at end of function? Useful when running SDMs for many, many taxa, especially if done in parallel.- ...
Passed to
fs::dir_ls()
- prep
Character or named list. If character, the path to an existing
prep.rds
. Otherwise, the result of a call toprep_sdm()
with return_val = "object".- full_run
Character or named list. If character, the path to an existing
full_run.rds
. Otherwise, the result of a call torun_full_sdm
() with return_val = "object".- out_dir
Character. Name of directory into which
.pngs
s will be saved. Will be created if it does not exist.
Examples
# setup -------
out_dir <- file.path(system.file(package = "envSDM"), "examples")
env_dat <- system.file("ex/bio.tif", package = "predicts")
# data ------
data <- fs::path(system.file(package = "envSDM"), "examples") |>
fs::dir_ls(regexp = "prep\\.rds$"
, recurse = TRUE
) |>
tibble::enframe(name = NULL, value = "prep") |>
dplyr::mutate(taxa = gsub("\\.rds", "", basename(dirname(prep)))
, tune = gsub("prep", "tune", prep)
, out_dir = fs::path(out_dir, taxa, "combo")
)
# Best combo--------
## run full SDM --------
purrr::pmap(list(data$prep
, data$tune
, data$out_dir
)
, \(a, b, c) run_full_sdm(prep = a
, tune = b
, out_dir = c
, use_metric = "combo"
# passed to tune_sdm via dots
, metrics_df = envSDM::sdm_metrics
)
)
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> [[1]]
#> [[1]]$full_run_file
#> H:/temp/nige/RtmpAZgTQZ/temp_libpath362426b1106f/envSDM/examples/acaule/combo/full_run.rds
#>
#>
#> [[2]]
#> [[2]]$full_run_file
#> H:/temp/nige/RtmpAZgTQZ/temp_libpath362426b1106f/envSDM/examples/bradypus/combo/full_run.rds
#>
#>
## predict -------
purrr::pwalk(list(data$prep
, data$out_dir
)
, \(a, b) predict_sdm(prep = a
, full_run = fs::path(b, "full_run.rds")
, out_dir = b
, predictors = env_dat
, check_tifs = TRUE
#, force_new = TRUE
)
)
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Error in pmap(.l, .f, ..., .progress = .progress): ℹ In index: 1.
#> Caused by error in `predict_sdm()`:
#> ! object 'thresh_file' not found
## .pngs -------
if(FALSE) {
# not working for binary 'thresh' rasters (due to an issue with number of 'classes"?):
# Error in if (any(na.omit(x) < min(breaks)) && show.warnings) warning("Values have found that are less than the lowest break", :
# missing value where TRUE/FALSE needed
purrr::walk2(data$out_dir
, data$out_dir
, \(x, y) png_from_preds(pred_dir = x
, tune_dir = y
, trim = FALSE
, recurse = 1
)
)
}
## visualise-------
### mask -------
purrr::walk(data$out_dir
, \(x) fs::path(x, "pred.tif") %>%
terra::rast() %>%
terra::trim() %>%
terra::plot()
)
# Best auc--------
## run full SDM --------
data <- data %>%
dplyr::mutate(out_dir = gsub("combo", "auc_po", out_dir))
purrr::pmap(list(data$prep
, data$tune
, data$out_dir
)
, \(a, b, c) run_full_sdm(prep = a
, tune = b
, out_dir = c
, use_metric = "auc_po"
# passed to tune_sdm via dots
, metrics_df = envSDM::sdm_metrics
)
)
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> [[1]]
#> [[1]]$full_run_file
#> H:/temp/nige/RtmpAZgTQZ/temp_libpath362426b1106f/envSDM/examples/acaule/auc_po/full_run.rds
#>
#>
#> [[2]]
#> [[2]]$full_run_file
#> H:/temp/nige/RtmpAZgTQZ/temp_libpath362426b1106f/envSDM/examples/bradypus/auc_po/full_run.rds
#>
#>
## predict -------
purrr::pwalk(list(data$prep
, data$out_dir
)
, \(a, b) predict_sdm(prep = a
, full_run = fs::path(b, "full_run.rds")
, out_dir = b
, predictors = env_dat
, is_env_pred = FALSE
, check_tifs = TRUE
)
)
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Warning: Missing `trust` will be set to FALSE by default for RDS in 2.0.0.
#> Error in pmap(.l, .f, ..., .progress = .progress): ℹ In index: 1.
#> Caused by error in `predict_sdm()`:
#> ! object 'thresh_file' not found
## visualise-------
### mask -------
purrr::walk(data$out_dir
, \(x) fs::path(x, "pred.tif") %>%
terra::rast() %>%
terra::trim() %>%
terra::plot()
)