Skip to content

Commit dd9567f

Browse files
committed
add and use + post-process cross-validated models
1 parent 2778bbc commit dd9567f

File tree

132 files changed

+2185
-1641
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+2185
-1641
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Package: IASDT.R
33
Title: Modelling the distribution of invasive alien plant species in
44
Europe
55
Version: 0.1.04
6-
Date: 2025-04-23
6+
Date: 2025-04-25
77
Authors@R:
88
person("Ahmed", "El-Gabbas", , "ahmed.el-gabbas@ufz.de", role = c("aut", "cre"),
99
comment = c(ORCID = "0000-0003-2225-088X"))

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export(loaded_packages)
8585
export(mod_CV_evaluate)
8686
export(mod_CV_fit)
8787
export(mod_CV_postprocess_1_CPU)
88+
export(mod_CV_postprocess_2_CPU)
8889
export(mod_CV_prepare)
8990
export(mod_SLURM)
9091
export(mod_SLURM_refit)
@@ -153,6 +154,7 @@ export(source_silent)
153154
export(split_df_to_chunks)
154155
export(split_raster)
155156
export(split_vector)
157+
export(stop_ctx)
156158
export(system_command)
157159
export(text_to_coordinates)
158160
export(variance_partitioning_compute)

R/DWF_CHELSA_prepare.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ CHELSA_prepare <- function(
3333
rm(AllArgs, envir = environment())
3434

3535
if (n_cores < 1) {
36-
stop("`n_cores` must be a positive integer.", call. = FALSE)
36+
IASDT.R::stop_ctx(
37+
"`n_cores` must be a positive integer.", n_cores = n_cores)
3738
}
3839

3940
# # ..................................................................... ###

R/DWF_CHELSA_process.R

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ CHELSA_process <- function(
9090
rm(AllArgs, CharArgs, LogicArgs, NumericArgs, envir = environment())
9191

9292
if (n_cores < 1 || download_n_cores < 1) {
93-
stop("`n_cores` must be a positive integer.", call. = FALSE)
93+
IASDT.R::stop_ctx(
94+
"`n_cores` must be a positive integer.",
95+
n_cores = n_cores, download_n_cores = download_n_cores)
9496
}
9597

9698
# # ..................................................................... ###
@@ -108,9 +110,8 @@ CHELSA_process <- function(
108110
IASDT.R::cat_time("Environment variables")
109111

110112
if (!file.exists(env_file)) {
111-
stop(
112-
"Path to environment variables: ", env_file, " was not found",
113-
call. = FALSE)
113+
IASDT.R::stop_ctx(
114+
"Path to environment variables was not found", env_file = env_file)
114115
}
115116

116117
EnvVars2Read <- tibble::tribble(
@@ -189,10 +190,10 @@ CHELSA_process <- function(
189190
x = dplyr::pull(CHELSA_Data_Checked, "InputOkay"),
190191
file = IASDT.R::path(Path_CHELSA_Out, "ProblematicTiffs.txt"))
191192

192-
stop(
193-
"Not all input tiff files are available and valid. ",
194-
"Check `ProblematicTiffs.txt`", call. = FALSE
195-
)
193+
IASDT.R::stop_ctx(
194+
paste0(
195+
"Not all input tiff files are available and valid. ",
196+
"Check `ProblematicTiffs.txt`"))
196197
}
197198

198199
# CHELSA files that will not be processed
@@ -330,9 +331,11 @@ CHELSA_process <- function(
330331
x = CHELSA2Process$Path_Down,
331332
file = IASDT.R::path(Path_CHELSA_Out, "FailedProcessing.txt"))
332333

333-
stop(
334-
"\n >> ", nrow(CHELSA2Process), " files failed to process.\n",
335-
" >> Check `FailedProcessing.txt` for more details", call. = FALSE)
334+
IASDT.R::stop_ctx(
335+
paste0(
336+
"\n >> ", nrow(CHELSA2Process), " files failed to process.\n",
337+
" >> Check `FailedProcessing.txt` for more details"),
338+
CHELSA2Process = CHELSA2Process)
336339
}
337340

338341
IASDT.R::cat_time("All tiff files were processed", level = 1)

R/DWF_CHELSA_project.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ CHELSA_project <- function(
2727
rm(AllArgs, envir = environment())
2828

2929
if (!inherits(metadata, "tbl_df")) {
30-
stop("Input metadata has to be a tibble", call. = FALSE)
30+
IASDT.R::stop_ctx(
31+
"Input metadata has to be a tibble", class_metadata = class(metadata))
3132
}
3233

3334
if (nrow(metadata) != 1) {
34-
stop("Input metadata has to be a single-row tibble", call. = FALSE)
35+
IASDT.R::stop_ctx(
36+
"Input metadata has to be a single-row tibble",
37+
metadata = metadata, class_metadata = class(metadata),
38+
nrow_metadata = nrow(metadata))
3539
}
3640

3741
# # ..................................................................... ###
@@ -43,11 +47,12 @@ CHELSA_project <- function(
4347
# # ..................................................................... ###
4448

4549
if (is.null(metadata)) {
46-
stop("Input metadata can not be `NULL`", call. = FALSE)
50+
IASDT.R::stop_ctx("Input metadata can not be `NULL`", metadata = metadata)
4751
}
4852

4953
if (!file.exists(metadata$Path_Down)) {
50-
stop("Input file does not exist: ", metadata$Path_Down, call. = FALSE)
54+
IASDT.R::stop_ctx(
55+
"Input file does not exist", path = metadata$Path_Down)
5156
}
5257

5358
# Set `GTIFF_SRS_SOURCE` configuration option to EPSG to use
@@ -72,8 +77,8 @@ CHELSA_project <- function(
7277

7378
GridR <- IASDT.R::path(Path_Grid, "Grid_10_Land_Crop.RData")
7479
if (!file.exists(GridR)) {
75-
stop(
76-
"Path for the Europe boundaries does not exist: ", GridR, call. = FALSE)
80+
IASDT.R::stop_ctx(
81+
"Path for the Europe boundaries does not exist", GridR = GridR)
7782
}
7883
GridR <- terra::unwrap(IASDT.R::load_as(GridR))
7984

R/DWF_CLC_plot.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ CLC_plot <- function(
3838

3939
if (is.null(CLC_name) || is.null(EU_map) || is.null(crosswalk) ||
4040
is.null(path_JPEG) || is.null(path_JPEG_free)) {
41-
stop(
42-
"`CLC_name`, `EU_map`, `crosswalk`, `path_JPEG`, and ",
43-
"`path_JPEG_free` can not be empty", call. = FALSE)
41+
IASDT.R::stop_ctx(
42+
paste0(
43+
"`CLC_name`, `EU_map`, `crosswalk`, `path_JPEG`, and ",
44+
"`path_JPEG_free` can not be empty"),
45+
CLC_name = CLC_name, EU_map = EU_map, crosswalk = crosswalk,
46+
path_JPEG = path_JPEG, path_JPEG_free = path_JPEG_free)
4447
}
4548

4649
CLC_MapR <- dplyr::filter(CLC_map, Name == CLC_name) %>%
@@ -261,11 +264,11 @@ CLC_plot <- function(
261264

262265
# nolint start
263266
CommonLegend <- cowplot::get_legend(
264-
(ggplot2::ggplot() +
267+
(ggplot2::ggplot() +
265268
tidyterra::geom_spatraster(
266269
data = terra::rast(CLC_MapR[[1]]), maxcell = terra::ncell(CLC_MapR)) +
267270
paletteer::scale_fill_paletteer_c(
268-
na.value = "transparent", palette = "viridis::plasma",
271+
na.value = "transparent", palette = "viridis::plasma",
269272
limits = c(0, 100)) +
270273
ggplot2::theme(
271274
legend.box.margin = ggplot2::margin(0, 0, 0, 0),

R/DWF_CLC_process.R

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#' <https://doi.org/10.2909/960998c1-1870-4e82-8051-6485205ebbac>
3131

3232
CLC_process <- function(
33-
env_file = ".env", min_land_percent = 15, plot_CLC = TRUE) {
33+
env_file = ".env", min_land_percent = 15, plot_CLC = TRUE) {
3434
# # ..................................................................... ###
3535

3636
.StartTime <- lubridate::now(tzone = "CET")
@@ -46,21 +46,19 @@ CLC_process <- function(
4646
EU_Bound <- Value <- Country <- Country2 <- NULL
4747

4848
if (is.null(env_file)) {
49-
stop("env_file can not be empty", call. = FALSE)
49+
IASDT.R::stop_ctx("env_file can not be empty", env_file = env_file)
5050
}
5151

5252
if (!is.numeric(min_land_percent) ||
5353
!dplyr::between(min_land_percent, 0, 100)) {
54-
stop(
54+
IASDT.R::stop_ctx(
5555
"`min_land_percent` must be a numeric value between 0 and 100.",
56-
call. = FALSE)
56+
min_land_percent = min_land_percent)
5757
}
5858

5959
if (!file.exists(env_file)) {
60-
stop(
61-
"Path to environment variables (`env_file`): ", env_file,
62-
" was not found",
63-
call. = FALSE)
60+
IASDT.R::stop_ctx(
61+
"Path to environment variables was not found", env_file = env_file)
6462
}
6563

6664
# # ..................................................................... ###
@@ -106,7 +104,7 @@ CLC_process <- function(
106104
.x = requiredPaths,
107105
.f = function(path) {
108106
if (!file.exists(path)) {
109-
stop("Required path does not exist: ", path, call. = FALSE)
107+
IASDT.R::stop_ctx("Required path does not exist: ", path = path)
110108
}
111109
}
112110
)
@@ -627,15 +625,19 @@ CLC_get_percentage <- function(
627625

628626
if (is.null(CLC_type) || is.null(CLC_crossWalk) || is.null(CLC_fracs_r) ||
629627
is.null(path_tif) || is.null(path_RData)) {
630-
stop("None of the input parameters can be empty", call. = FALSE)
628+
IASDT.R::stop_ctx(
629+
"None of the input parameters can be empty",
630+
CLC_type = CLC_type, CLC_crossWalk = CLC_crossWalk,
631+
CLC_fracs_r = CLC_fracs_r, path_tif = path_tif, path_RData = path_RData)
631632
}
632633

633634
if (!(
634635
CLC_type %in% c("SynHab", "CLC_L1", "CLC_L2", "CLC_L3", "EUNIS_2019"))) {
635-
stop(
636-
"CLC_type has to be one of SynHab, CLC_L1, CLC_L2, ",
637-
"CLC_L3, and EUNIS_2019",
638-
call. = FALSE)
636+
IASDT.R::stop_ctx(
637+
paste0(
638+
"CLC_type has to be one of SynHab, CLC_L1, CLC_L2, ",
639+
"CLC_L3, and EUNIS_2019"),
640+
CLC_type = CLC_type)
639641
}
640642
# # ..................................................................... ###
641643

@@ -721,15 +723,21 @@ CLC_get_majority <- function(
721723
if (is.null(CLC_type) || is.null(CLC_majority) || is.null(path_tif) ||
722724
is.null(path_tif_crop) || is.null(path_RData) || is.null(Grid_10_Land) ||
723725
is.null(Grid_10_Land_Crop)) {
724-
stop("None of the input parameters can be empty", call. = FALSE)
726+
IASDT.R::stop_ctx(
727+
"None of the input parameters can be empty",
728+
CLC_type = CLC_type, CLC_majority = CLC_majority,
729+
path_tif = path_tif, path_tif_crop = path_tif_crop,
730+
path_RData = path_RData, Grid_10_Land = Grid_10_Land,
731+
Grid_10_Land_Crop = Grid_10_Land_Crop)
725732
}
726733

727734
if (!(
728735
CLC_type %in% c("SynHab", "CLC_L1", "CLC_L2", "CLC_L3", "EUNIS_2019"))) {
729-
stop(
730-
"CLC_type has to be one of SynHab, CLC_L1, CLC_L2, ",
731-
"CLC_L3, and EUNIS_2019",
732-
call. = FALSE)
736+
IASDT.R::stop_ctx(
737+
paste0(
738+
"CLC_type has to be one of SynHab, CLC_L1, CLC_L2, ",
739+
"CLC_L3, and EUNIS_2019"),
740+
CLC_type = CLC_type)
733741
}
734742

735743
# # ..................................................................... ###

R/DWF_EASIN_download.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ EASIN_download <- function(
1616
# # ..................................................................... ###
1717

1818
if (is.null(species_key)) {
19-
stop("species_key cannot be NULL", call. = FALSE)
19+
IASDT.R::stop_ctx("species_key cannot be NULL", species_key = species_key)
2020
}
2121

2222
Path_EASIN <- NULL

R/DWF_EASIN_plot.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ EASIN_plot <- function(env_file = ".env") {
7474
SummaryMapsMissing <- !file.exists(PathSummaryMaps)
7575

7676
if (any(SummaryMapsMissing)) {
77-
stop(
78-
"The following input files are missing: \n",
79-
paste0(
80-
" >> ", PathSummaryMaps[which(SummaryMapsMissing)], collapse = "\n") ,
81-
call. = FALSE)
77+
IASDT.R::stop_ctx(
78+
"Missing summary input files",
79+
missing_files = PathSummaryMaps[which(SummaryMapsMissing)])
8280
}
8381

8482
# # |||||||||||||||||||||||||||||||||||

R/DWF_EASIN_process.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,23 @@ EASIN_process <- function(
150150
## Grid - raster ----
151151
GridR <- IASDT.R::path(Path_Grid, "Grid_10_Land_Crop.RData")
152152
if (!file.exists(GridR)) {
153-
stop("Path for the reference grid does not exist: ", GridR, call. = FALSE)
153+
IASDT.R::stop_ctx(
154+
"Path for the reference grid does not exist", GridR = GridR)
154155
}
155156

156157
## Grid - sf ----
157158
GridSf <- IASDT.R::path(Path_Grid_Ref, "Grid_10_sf.RData")
158159
if (!file.exists(GridSf)) {
159-
stop("Path for the reference grid does not exist: ", GridSf, call. = FALSE)
160+
IASDT.R::stop_ctx(
161+
"Path for the reference grid does not exist", GridSf = GridSf)
160162
}
161163

162164
## Grid - sf - study area ----
163165
# Grid ID overlapping with study area
164166
LandGrids <- IASDT.R::path(Path_Grid, "Grid_10_Land_sf.RData")
165167
if (!file.exists(LandGrids)) {
166-
stop(
167-
"Path for the reference grid does not exist: ", LandGrids, call. = FALSE)
168+
IASDT.R::stop_ctx(
169+
"Path for the reference grid does not exist", LandGrids = LandGrids)
168170
}
169171

170172
## Species list ----

0 commit comments

Comments
 (0)