-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubmit-models.R
160 lines (139 loc) · 4.62 KB
/
submit-models.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#########################################################
# S3 dispatches for submitting multiple models in batch
#########################################################
#' Submit models to be run in batch
#'
#' Submits a group of models to be run in batch by calling out to `bbi` in as
#' few external calls as possible (see "Details").
#'
#' @details The number of `bbi` calls to make is determined by the number of
#' distinct sets of `bbi` arguments passed to the submission calls, either
#' explicitly through `.bbi_args`, as specified in the `bbi_args` field of the
#' model YAML, or specified globally in `bbi.yaml`.
#'
#' @template nonmem-mod-ext
#'
#' @seealso [submit_model()]
#' @param .mods The model objects to submit.
#' @inheritParams submit_model
#' @export
submit_models <- function(
.mods,
.bbi_args = NULL,
.mode = getOption("bbr.bbi_exe_mode"),
...,
.overwrite = NULL,
.config_path = NULL,
.wait = TRUE,
.dry_run=FALSE
) {
UseMethod("submit_models")
}
#' @describeIn submit_models Takes a list of `bbi_base_model` objects.
#' @importFrom purrr map map_lgl
#' @export
submit_models.list <- function(
.mods,
.bbi_args = NULL,
.mode = getOption("bbr.bbi_exe_mode"),
...,
.overwrite = NULL,
.config_path = NULL,
.wait = TRUE,
.dry_run=FALSE
) {
# check that each element is a model object
check_model_object_list(.mods)
# check that all are the same type of model object
all_model_types <- map(.mods, function(.x) { .x[[YAML_MOD_TYPE]] })
uniq_model_types <- all_model_types %>% unlist() %>% unique()
if (length(uniq_model_types) != 1) {
stop(paste(
glue("Passed vector `.mods` must contain all the same type of models, but found {length(uniq_model_types)} different classes of model:"),
paste(uniq_model_types, collapse = ", ")
))
}
.model_type <- uniq_model_types
# submit models
class(.mods) <- paste0("bbi_", .model_type, "_models")
submit_models(.mods,
.bbi_args = .bbi_args,
.mode = .mode,
...,
.overwrite = .overwrite,
.config_path = .config_path,
.wait = .wait,
.dry_run = .dry_run)
}
#' @export
submit_models.default <- function(.mods, ...) {
rlang::abort(
c(
paste("Unsupported model type: ", class(.mods)),
"Did you mean `submit_model` (no trailing 's')?"
)
)
}
#' @importFrom stringr str_detect
#' @importFrom tools file_path_sans_ext
#' @importFrom purrr map
#' @importFrom rlang %||%
#' @export
submit_models.bbi_nonmem_models <- function(.mods,
.bbi_args = NULL,
.mode = getOption("bbr.bbi_exe_mode"),
...,
.overwrite = NULL,
.config_path = NULL,
.wait = TRUE,
.dry_run = FALSE) {
check_model_object_list(.mods, NM_MOD_CLASS)
# check against YAML
for (.mod in .mods) { check_yaml_in_sync(.mod) }
# check for valid .mode arg
check_mode_argument(.mode)
# get unique sets of params
if (!is.null(.overwrite)) {
checkmate::assert_logical(.overwrite)
.bbi_args[["overwrite"]] <- .overwrite
}
param_list <- build_bbi_param_list(.mods, .bbi_args)
# a .run is a group of models that can be passed in a single bbi call
cmd_args_list <- map(param_list, function(.run) {
cmd_args <- c(
"nonmem",
"run",
.mode,
purrr::map_chr(.run[["models"]], get_model_path),
.run[["bbi_args"]]
)
model_dir <- get_model_working_directory(.run[["models"]][[1L]])
.path_exists <- file_exists(.config_path %||% file.path(model_dir, "bbi.yaml"))
if(!.path_exists){
stop(paste("No bbi configuration was found in the execution directory.",
"Please run `bbi_init()` with the appropriate directory to continue."))
}
if (!is.null(.config_path)) {
cmd_args <- c(
cmd_args,
sprintf("--config=%s", normalizePath(.config_path))
)
}
return(list(cmd_args = cmd_args, model_dir = model_dir))
})
message(glue("Submitting {length(.mods)} models with {length(cmd_args_list)} unique configurations."))
if (.dry_run) {
# construct fake res object
return(map(
cmd_args_list,
function(.run) { bbi_dry_run(.run$cmd_args, .run$model_dir) }
))
}
# launch models
res_list <- map(
cmd_args_list,
function(.run) { bbi_exec(.run$cmd_args, .wait = .wait, .dir = .run$model_dir, ...) },
...
)
return(res_list)
}