Skip to content

Commit 6b23f09

Browse files
committed
postprocessing: add model_prefix argument to save TF scripts to model-specific directory / Fix cat_time
1 parent 5d72106 commit 6b23f09

File tree

7 files changed

+84
-65
lines changed

7 files changed

+84
-65
lines changed

R/general_cat_time.R

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
#'
4040
#' cat_time("\n\nTime now", n_lines = 2L, level = 1L)
4141
#'
42-
#' cat_time("time now", date = TRUE, bold = TRUE, red = TRUE)
42+
#' cat_time(
43+
#' "\ntime now", date = TRUE, bold = TRUE, red = TRUE,
44+
#' n_lines = 2L, level = 1L)
4345
#'
4446
#' # The use of levels
4547
#' {
@@ -96,15 +98,8 @@ cat_time <- function(
9698
if (NLinesBefore > 0) {
9799
text <- paste0(strrep("\n", NLinesBefore), text)
98100
}
99-
cat(Now, ...)
100-
cat(rep("\n", n_lines))
101+
text <- paste0(text, Now)
101102
} else {
102-
if (bold) {
103-
text <- crayon::bold(text)
104-
}
105-
if (red) {
106-
text <- crayon::red(text)
107-
}
108103
if (level > 0) {
109104
Prefix <- rep(" >>>", each = level) %>%
110105
paste(collapse = "") %>%
@@ -115,8 +110,19 @@ cat_time <- function(
115110
if (NLinesBefore > 0) {
116111
text <- paste0(strrep("\n", NLinesBefore), text)
117112
}
113+
text <- paste0(text, Now2)
114+
115+
}
118116

119-
cat(paste0(text, Now2), ...)
120-
cat(rep("\n", n_lines))
117+
if (bold) {
118+
text <- crayon::bold(text)
121119
}
120+
if (red) {
121+
text <- crayon::red(text)
122+
}
123+
124+
cat(text, ...)
125+
cat(rep("\n", n_lines))
126+
127+
return(invisible(NULL))
122128
}

R/mod_postprocess.R

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#' less than or equal to the maximum job limit of the HPC environment.
2323
#' @param working_directory Character. Optionally sets the working directory in
2424
#' batch scripts to this path. If `NULL`, the directory remains unchanged.
25+
#' @param model_prefix Character. Prefix for the model name. A directory named
26+
#' `model_prefix_TF` is created in the `model_dir` to store the TensorFlow
27+
#' running commands. Defaults to `NULL`. This can not be `NULL`.
2528
#' @param partition_name Character. Name of the partition to submit the SLURM
2629
#' jobs to. Default is `small-g`.
2730
#' @param LF_runtime,VP_runtime Character. Time limit for latent factor
@@ -78,11 +81,11 @@
7881
#' - for <u>variance partitioning</u>, the function matches all files with
7982
#' the pattern ` "VP_.+Command.txt"` (created by [variance_partitioning_compute]
8083
#' and merges their contents into a single file
81-
#' (`TF_postprocess/VP_Commands.txt`). Then, it prepares a SLURM script for
82-
#' variance partitioning computations (`TF_postprocess/VP_SLURM.slurm`).
84+
#' (`model_prefix_TF/VP_Commands.txt`). Then, it prepares a SLURM script for
85+
#' variance partitioning computations (`model_prefix_TF/VP_SLURM.slurm`).
8386
#' - for <u>latent factor predictions</u>, the function matches all files
8487
#' with the pattern `"^LF_NewSites_Commands_.+.txt|^LF_RC_Commands_.+txt"` and
85-
#' split their contents into multiple scripts at the `TF_postprocess` directory
88+
#' split their contents into multiple scripts at the `model_prefix_TF` directory
8689
#' for processing as a batch job. The function prepares a SLURM script for
8790
#' latent factor predictions (`LF_SLURM.slurm`).
8891
#'
@@ -198,7 +201,7 @@ mod_postprocess_1_CPU <- function(
198201
# ****************************************************************
199202

200203
IASDT.R::record_arguments(
201-
out_path = IASDT.R::path(model_dir, "Args_Mod_Postprocess_1_CPU.RData"))
204+
out_path = IASDT.R::path(model_dir, "Args_mod_postprocess_1_CPU.RData"))
202205

203206
# ****************************************************************
204207

@@ -224,9 +227,9 @@ mod_postprocess_1_CPU <- function(
224227
# ****************************************************************
225228

226229
# Check unsuccessful models -----
227-
230+
228231
IASDT.R::info_chunk(
229-
"\tCheck unsuccessful models",
232+
"Check unsuccessful models", level = 1,
230233
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
231234

232235
IASDT.R::mod_SLURM_refit(
@@ -240,9 +243,9 @@ mod_postprocess_1_CPU <- function(
240243
# ****************************************************************
241244

242245
IASDT.R::info_chunk(
243-
"\tMerge chains and saving RData files",
246+
"Merge chains and saving RData/qs2 files", level = 1,
244247
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
245-
248+
246249
IASDT.R::mod_merge_chains(
247250
model_dir = model_dir, n_cores = n_cores, from_JSON = from_JSON)
248251

@@ -251,9 +254,9 @@ mod_postprocess_1_CPU <- function(
251254
# ****************************************************************
252255

253256
IASDT.R::info_chunk(
254-
"\tConvergence of all model variants",
257+
"Convergence of all model variants", level = 1,
255258
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
256-
259+
257260
IASDT.R::convergence_plot_all(
258261
model_dir = model_dir, n_omega = n_omega, n_cores = n_cores,
259262
margin_type = "histogram")
@@ -265,7 +268,7 @@ mod_postprocess_1_CPU <- function(
265268

266269
# Path of selected model -----
267270
IASDT.R::info_chunk(
268-
"\tPath of selected model",
271+
"Path of selected model", level = 1,
269272
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
270273

271274
path_model <- IASDT.R::path(
@@ -293,9 +296,9 @@ mod_postprocess_1_CPU <- function(
293296

294297
# Gelman_Plot -----
295298
IASDT.R::info_chunk(
296-
"\tGelman_Plot",
299+
"Gelman_Plot", level = 1,
297300
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
298-
301+
299302
IASDT.R::plot_gelman(
300303
path_coda = path_coda, alpha = TRUE, beta = TRUE, omega = TRUE, rho = TRUE,
301304
n_omega = n_omega, env_file = env_file)
@@ -306,7 +309,7 @@ mod_postprocess_1_CPU <- function(
306309

307310
# Convergence plots ----
308311
IASDT.R::info_chunk(
309-
"\tConvergence plots",
312+
"Convergence plots", level = 1,
310313
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
311314

312315
IASDT.R::convergence_plot(
@@ -320,7 +323,7 @@ mod_postprocess_1_CPU <- function(
320323

321324
# Model summary ------
322325
IASDT.R::info_chunk(
323-
"\tModel summary",
326+
"Model summary", level = 1,
324327
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
325328

326329
IASDT.R::mod_summary(path_coda = path_coda, env_file = env_file)
@@ -331,7 +334,7 @@ mod_postprocess_1_CPU <- function(
331334

332335
# Plotting model parameters - Omega -----
333336
IASDT.R::info_chunk(
334-
"\tPlotting model parameters - Omega",
337+
"Plotting model parameters - Omega", level = 1,
335338
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
336339

337340
IASDT.R::mod_heatmap_omega(
@@ -344,7 +347,7 @@ mod_postprocess_1_CPU <- function(
344347

345348
# Plotting model parameters - Beta -----
346349
IASDT.R::info_chunk(
347-
"\tPlotting model parameters - Beta",
350+
"Plotting model parameters - Beta", level = 1,
348351
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
349352

350353
IASDT.R::mod_heatmap_beta(
@@ -358,7 +361,7 @@ mod_postprocess_1_CPU <- function(
358361
# Prepare input data for cross-validation -------
359362

360363
IASDT.R::info_chunk(
361-
"\tPrepare input data for cross-validation",
364+
"Prepare input data for cross-validation", level = 1,
362365
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
363366

364367
IASDT.R::mod_CV_fit(
@@ -372,8 +375,9 @@ mod_postprocess_1_CPU <- function(
372375

373376
# latent factors of the response curves -------
374377
IASDT.R::info_chunk(
375-
"\tPrepare scripts for predicting latent factors of the response curves",
376-
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
378+
"Prepare scripts for predicting latent factors of the response curves",
379+
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE,
380+
level = 1)
377381

378382
IASDT.R::resp_curv_prepare_data(
379383
path_model = path_model, n_grid = n_grid, n_cores = n_cores,
@@ -387,10 +391,11 @@ mod_postprocess_1_CPU <- function(
387391
# ****************************************************************
388392

389393
# latent factors for new sampling units -------
390-
394+
391395
IASDT.R::info_chunk(
392-
"\tPrepare scripts for predicting latent factors for new sampling units",
393-
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
396+
"Prepare scripts for predicting latent factors for new sampling units",
397+
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE,
398+
level = 1)
394399

395400
IASDT.R::predict_maps(
396401
path_model = path_model, hab_abb = hab_abb, env_file = env_file,
@@ -404,7 +409,7 @@ mod_postprocess_1_CPU <- function(
404409

405410
# Prepare scripts for computing variance partitioning -------
406411
IASDT.R::info_chunk(
407-
"\tPrepare scripts for computing variance partitioning",
412+
"Prepare scripts for computing variance partitioning", level = 1,
408413
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
409414

410415
IASDT.R::variance_partitioning_compute(
@@ -438,7 +443,7 @@ mod_postprocess_1_CPU <- function(
438443

439444
mod_prepare_TF <- function(
440445
n_batch_files = 210L, env_file = ".env", working_directory = NULL,
441-
partition_name = "small-g", LF_runtime = "01:00:00",
446+
partition_name = "small-g", LF_runtime = "01:00:00", model_prefix = NULL,
442447
VP_runtime = "01:30:00") {
443448

444449
# ****************************************************************
@@ -460,7 +465,7 @@ mod_prepare_TF <- function(
460465
IASDT.R::check_args(
461466
args_all = AllArgs, args_type = "character",
462467
args_to_check = c(
463-
"LF_runtime", "VP_runtime", "partition_name", "env_file"))
468+
"LF_runtime", "VP_runtime", "partition_name", "env_file", "model_prefix"))
464469
IASDT.R::check_args(
465470
args_all = AllArgs, args_type = "numeric", args_to_check = "n_batch_files")
466471
rm(AllArgs, envir = environment())
@@ -480,12 +485,12 @@ mod_prepare_TF <- function(
480485
rm(EnvVars2Read, envir = environment())
481486

482487
IASDT.R::record_arguments(
483-
out_path = IASDT.R::path(path_model, "Args_Mod_Prep_TF.RData"))
488+
out_path = IASDT.R::path(path_model, "Args_mod_prep_TF.RData"))
484489

485490
# ****************************************************************
486491

487492
# Path to store TF commands
488-
Path_TF <- IASDT.R::path(path_model, "TF_postprocess")
493+
Path_TF <- IASDT.R::path(path_model, paste0(model_prefix, "_TF"))
489494
# Path to store log files
490495
Path_Log <- IASDT.R::normalize_path(IASDT.R::path(Path_TF, "log"))
491496
fs::dir_create(c(Path_TF, Path_Log))
@@ -705,7 +710,7 @@ mod_prepare_TF <- function(
705710
paste0("#SBATCH --array=1-", n_batch_files),
706711
"",
707712
"# Define directories",
708-
paste0('OutputDir="', IASDT.R::path(path_model, "TF_postprocess"), '"'),
713+
paste0('OutputDir="', Path_TF, '"'),
709714
"",
710715
"# Find all the split files and sort them explicitly",
711716
paste0(
@@ -918,7 +923,7 @@ mod_postprocess_2_CPU <- function(
918923

919924
# Path of selected model -----
920925
IASDT.R::info_chunk(
921-
"\tPath of selected model",
926+
"Path of selected model", level = 1,
922927
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
923928

924929
path_model <- IASDT.R::path(
@@ -944,7 +949,7 @@ mod_postprocess_2_CPU <- function(
944949

945950
# Prepare response curve data -----
946951
IASDT.R::info_chunk(
947-
"\tPrepare response curve data",
952+
"Prepare response curve data", level = 1,
948953
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
949954

950955
IASDT.R::resp_curv_prepare_data(
@@ -961,7 +966,7 @@ mod_postprocess_2_CPU <- function(
961966

962967
# Plotting response curves - species richness -----
963968
IASDT.R::info_chunk(
964-
"\tPlotting response curves - species richness",
969+
"Plotting response curves - species richness", level = 1,
965970
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
966971

967972
IASDT.R::resp_curv_plot_SR(
@@ -973,7 +978,7 @@ mod_postprocess_2_CPU <- function(
973978

974979
# Plotting response curves - species -----
975980
IASDT.R::info_chunk(
976-
"\tPlotting response curves - species",
981+
"Plotting response curves - species", level = 1,
977982
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
978983

979984
IASDT.R::resp_curv_plot_species(
@@ -985,9 +990,9 @@ mod_postprocess_2_CPU <- function(
985990

986991
# Plotting - all species together -----
987992
IASDT.R::info_chunk(
988-
"\tPlotting response curves - all species together",
993+
"Plotting response curves - all species together", level = 1,
989994
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
990-
995+
991996
IASDT.R::resp_curv_plot_species_all(
992997
model_dir = model_dir, n_cores = RC_n_cores)
993998

@@ -997,8 +1002,9 @@ mod_postprocess_2_CPU <- function(
9971002

9981003
# Predicting habitat suitability across different climate options -------
9991004
IASDT.R::info_chunk(
1000-
"\tPredicting habitat suitability across different climate options",
1001-
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
1005+
"Predicting habitat suitability across different climate options",
1006+
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE,
1007+
level = 1)
10021008

10031009
IASDT.R::predict_maps(
10041010
path_model = path_model, hab_abb = hab_abb, env_file = env_file,
@@ -1017,7 +1023,7 @@ mod_postprocess_2_CPU <- function(
10171023

10181024
# Compute variance partitioning ------
10191025
IASDT.R::info_chunk(
1020-
"\tCompute variance partitioning",
1026+
"Compute variance partitioning", level = 1,
10211027
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
10221028

10231029
IASDT.R::variance_partitioning_compute(
@@ -1031,7 +1037,7 @@ mod_postprocess_2_CPU <- function(
10311037

10321038
# Plot Variance partitioning ------
10331039
IASDT.R::info_chunk(
1034-
"\tPlot Variance partitioning",
1040+
"Plot Variance partitioning", level = 1,
10351041
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
10361042

10371043
IASDT.R::variance_partitioning_plot(
@@ -1043,7 +1049,7 @@ mod_postprocess_2_CPU <- function(
10431049

10441050
# Plot species & SR predictions as JPEG ------
10451051
IASDT.R::info_chunk(
1046-
"\tPlot species & SR predictions as JPEG",
1052+
"Plot species & SR predictions as JPEG", level = 1,
10471053
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
10481054

10491055
IASDT.R::plot_prediction(
@@ -1053,7 +1059,7 @@ mod_postprocess_2_CPU <- function(
10531059

10541060
# Plot latent factors as JPEG ------
10551061
IASDT.R::info_chunk(
1056-
"\tPlot latent factors as JPEG",
1062+
"Plot latent factors as JPEG", level = 1,
10571063
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
10581064

10591065
IASDT.R::plot_latent_factor(path_model = path_model, env_file = env_file)
@@ -1062,7 +1068,7 @@ mod_postprocess_2_CPU <- function(
10621068

10631069
# Plot explanatory Power ------
10641070
IASDT.R::info_chunk(
1065-
"\tPlot explanatory Power",
1071+
"Plot explanatory Power", level = 1,
10661072
line_char = "+", repetitions = 60, red = TRUE, bold = TRUE, time = FALSE)
10671073

10681074
IASDT.R::plot_evaluation(model_dir = model_dir, env_file = env_file)

R/mod_predict_maps.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,8 @@ predict_maps <- function(
772772
model_name, " (", ID, "/", nrow(Prediction_Options), ")")
773773
cat("\n")
774774
IASDT.R::info_chunk(
775-
paste0("\t", MSG), n_separators = 1, line_char = "-", repetitions = 70,
776-
red = TRUE, bold = TRUE, time = FALSE)
775+
MSG, n_separators = 1, line_char = "-", repetitions = 70,
776+
red = TRUE, bold = TRUE, time = FALSE, level = 1)
777777

778778
if (DoClamp) {
779779

@@ -1123,7 +1123,7 @@ predict_maps <- function(
11231123
# Predicting ------
11241124

11251125
IASDT.R::info_chunk(
1126-
paste0("\t", "Making spatial predictions"), n_separators = 2,
1126+
"Making spatial predictions", n_separators = 2, level = 1,
11271127
line_char = "*", repetitions = 70, red = TRUE, bold = TRUE, time = FALSE)
11281128

11291129
Grid10 <- terra::unwrap(IASDT.R::load_as(Path_GridR))

0 commit comments

Comments
 (0)