Skip to content

Commit

Permalink
modified to capture stdout and stderr to log in using system2() being…
Browse files Browse the repository at this point in the history
… applicable in Windows as well in flepimop/main_scripts/inference_main.R
  • Loading branch information
kjsato committed Aug 7, 2024
1 parent 6c432fe commit 70ef527
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions flepimop/main_scripts/inference_main.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ foreach(seir_modifiers_scenario = seir_modifiers_scenarios) %:%
if (nchar(opt$ground_truth_end) > 0) {
ground_truth_end_text <- c("--ground_truth_end", opt$ground_truth_end)
}

err <- system(
paste(
opt$rpath,
#log_file <- paste0("log_inference_slot_", config$name, "_", opt$run_id, "_", flepi_slot, ".txt")
log_file2 <- paste0("model_output/", config$name, "/", opt$run_id, "/log/log_inference_slot_", flepi_slot, ".txt")

if (!dir.exists(dirname(log_file2))) {
dir.create(dirname(log_file2), recursive = TRUE, showWarnings = TRUE)
}
command <- c(
# opt$rpath,
file.path(opt$flepi_path, "flepimop", "main_scripts","inference_slot.R"),
"-c", opt$config,
"-u", opt$run_id,
Expand All @@ -152,7 +156,13 @@ foreach(seir_modifiers_scenario = seir_modifiers_scenarios) %:%
#paste("2>&1 | tee log_inference_slot_",config$name,"_",opt$run_id, "_", flepi_slot, ".txt", sep=""), # works on Mac only, not windows
#paste("2>&1 | tee model_output/",config$name,"/",opt$run_id,"/log/log_inference_slot", flepi_slot, ".txt", sep=""), # doesn't work because config$name needs to be combined with scenarios to generate the folder name, and, because this command seems to only be able to pipe output to pre-existing folders
sep = " ")
)
err <- tryCatch({
system2(command = opt$rpath, args = command, stdout = log_file2, stderr = log_file2)
}, error = function(e) {
message <- paste("Error in slot", flepi_slot, ":", e$message)
writeLines(message, con = log_file2)
return(1) # Return non-zero to indicate error
})
if(err != 0){quit("no")}
}
parallel::stopCluster(cl)

0 comments on commit 70ef527

Please sign in to comment.