Skip to content

Commit

Permalink
Merge pull request #7 from A2-ai/vignette_updates
Browse files Browse the repository at this point in the history
Vignette updates
  • Loading branch information
mduncans authored Aug 29, 2024
2 parents 016bd28 + 40220e4 commit a4bd234
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 100 deletions.
69 changes: 28 additions & 41 deletions R/attach.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,57 @@
#' slurmtools_options_message()
#' }
slurmtools_options_message <- function() {
set_header <- cli::rule(
left = cli::style_bold("Set slurmtools options")
)

unset_header <- cli::rule(
left = cli::style_bold("Needed slurmtools options")
)

set_options <- c()
unset_options <- c()
print_set <- FALSE
print_unset <- FALSE

# Check for each used options
tmpl_path <- getOption('slurmtools.slurm_job_template_path')
if (is.null(tmpl_path)) {
unset_options <- c(unset_options, "option('slurmtools.slurm_job_template_path') is not set.")
print_unset <- TRUE
} else {
set_options <- c(set_options, paste("slurmtools.slurm_jon_template_path:", tmpl_path))
print_set <- TRUE
}

root <- getOption('slurmtools.submission_root')
if (is.null(root)) {
unset_options <- c(unset_options, "option('slurmtools.submission_root') is not set.")
print_unset <- TRUE
} else {
set_options <- c(set_options, paste("slurmtools.submission_root:", root))
print_set <- TRUE
}

set_bullets <- paste0(
cli::col_green(cli::symbol$tick), " ", set_options, collapse = "\n"
)
bbi_config <- getOption("slurmtools.bbi_config_path")
if (is.null(bbi_config)) {
unset_options <- c(unset_options, "option('slurmtools.bbi_config_path') is not set.")
} else {
set_options <- c(set_options, paste("slurmtools.bbi_config_path:", bbi_config))
}

unset_bullets <- paste0(
cli::col_red(cli::symbol$cross), " ", unset_options, collapse = "\n"
)
#format .onAttach message
msg <- ""
if (length(set_options)) {
msg <- paste0(
msg,
cli::rule(
left = cli::style_bold("Set slurmtools options")), "\n",
paste0(
cli::col_green(cli::symbol$tick), " ", set_options, collapse = "\n"), "\n"
)
}

unset <- paste0(
cli::col_cyan(cli::symbol$info), " ",
cli::format_inline("Please set all options for job submission defaults to work.")
)
if (print_set) {
if (print_unset) {
if (length(unset_options)) {
msg <- paste0(
msg,
cli::rule(
left = cli::style_bold("Needed slurmtools options")), "\n",
paste0(
set_header, "\n",
set_bullets, "\n",
unset_header, "\n",
unset_bullets, "\n",
unset
)
} else {
cli::col_red(cli::symbol$cross), " ", unset_options, collapse = "\n"), "\n",
paste0(
set_header, "\n",
set_bullets, "\n"
cli::col_cyan(cli::symbol$info), " ",
cli::format_inline("Please set all options for job submission defaults to work.")
)
}
} else {
paste0(
unset_header, "\n",
unset_bullets, "\n",
unset
)
}

msg
}

7 changes: 5 additions & 2 deletions R/nmm-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @param level What level to log at. Default is info. Available options are Trace, Debug, Info, Warn, Fatal
#' @param email if alert is set to Slack, this should be the email associated with slack to get messages sent directly to you.
#' @param threads number of threads if running a parallel job. Default is 1
#' @param topic the ntfy.sh topic to send alerts to.
#'
#' @return none
#' @keywords internal
Expand All @@ -30,7 +31,8 @@ generate_nmm_config <- function(
alert = "None",
level = "Debug",
email = "",
threads = 1
threads = 1,
topic = ""
) {
if (model_number == "") {
model_number <- basename(.mod$absolute_model_path)
Expand Down Expand Up @@ -60,7 +62,8 @@ generate_nmm_config <- function(
alert = alert,
level = level,
email = email,
threads = threads
threads = threads,
topic = topic
)

config_toml_path <- paste0(.mod$absolute_model_path, ".toml")
Expand Down
17 changes: 16 additions & 1 deletion R/submit-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param ... arguments to pass to processx::run
#' @param slurm_job_template_path path to slurm job template
#' @param submission_root directory to track job submission scripts and output
#' @param bbi_config_path path to bbi.yaml file for bbi configuration
#' @param slurm_template_opts choose slurm template
#'
#' @export
Expand All @@ -20,6 +21,7 @@ submit_nonmem_model <-
...,
slurm_job_template_path = getOption('slurmtools.slurm_job_template_path'),
submission_root = getOption('slurmtools.submission_root'),
bbi_config_path = getOption('slurmtools.bbi_config_path'),
slurm_template_opts = list()) {

if (is.null(partition)) {
Expand Down Expand Up @@ -58,14 +60,27 @@ submit_nonmem_model <-
rlang::warn(sprintf("config.toml file not found, if submitting the job with nmm this is required. Please run generate_nmm_config()"))
}

if (is.null(slurm_template_opts$nmm_exe_path)) {
nmm_exe_path <- Sys.which("nmm")
} else {
nmm_exe_path <- slurm_template_opts$nmm_exe_path
}
if (is.null(slurm_template_opts$bbi_exe_path)) {
bbi_exe_path <- Sys.which("bbi")
} else {
bbi_exe_path <- slurm_template_opts$bbi_exe_path
}

default_template_list = list(
partition = partition,
parallel = parallel,
ncpu = ncpu,
job_name = sprintf("%s-nonmem-run", basename(.mod$absolute_model_path)),
bbi_exe_path = bbi_exe_path,
bbi_config_path = bbi_config_path,
model_path = .mod$absolute_model_path,
config_toml_path = config_toml_path,
nmm_exe_path = Sys.which("nmm")
nmm_exe_path = nmm_exe_path
)

template_list = c(
Expand Down
5 changes: 4 additions & 1 deletion man/generate_nmm_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/submit_nonmem_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions vignettes/Running-nonmem.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ install.packages("slurmtools")
library(slurmtools)
```

We are given a message when loading slurmtools that some options are not set and that default job submission will not work without them. These options are used for default arguments in the `submit_nonmem_model` function. Running `?submit_nonmem_model` we can see the documentation![Help view for submit_nonmem_model function](data/images/submit_nonmem_model_help.png)
We are given a message when loading slurmtools that some options are not set and that default job submission will not work without them. These options are used for default arguments in the `submit_nonmem_model` function. Running `?submit_nonmem_model` we can see the documentation

![Help view for `submit_nonmem_model` function](data/images/submit_nonmem_model_help.png)

This function uses the inputs to populate a template Bash shell script that submits the NONMEM job to slurm. A default template file is supplied with the Project Starter and it can be modified to do additional tasks as long as they are possible within Bash.

Expand All @@ -56,6 +58,8 @@ list(
parallel = parallel,
ncpu = ncpu,
job_name = sprintf("%s-nonmem-run", basename(.mod$absolute_model_path)),
bbi_exe_path = Sys.which("bbi"),
bbi_config_path = bbi_config_path,
model_path = .mod$absolute_model_path,
config_toml_path = config_toml_path,
nmm_exe_path = Sys.which("nmm")
Expand All @@ -70,6 +74,10 @@ list(

- `job_name` is created from the `.mod` argument supplied to `submit_nonmem_model`

- `bbi_exe_path` is determined via \`Sys.which("bbi")

- `bbi_config_path` is determined via getOption("slurmtools.bbi_config_path")

- `model_path` is determined from the `.mod` argument supplied to `submit_nonmem_model`

- `config_toml_path` is determined from the `.mod` argument supplied to `submit_nonmem_model` and is requried to use `nmm` (NONMEM monitor)
Expand All @@ -80,7 +88,7 @@ If you need to feed more arguments to the template you simply supply them in the

## Submitting a NONMEM job with `bbi`

To submit a NONMEM job, we need to supply either the path to a mod file or create a model object from `bbr`, and supply a `slurm-template.tmpl` file. To use `bbi` we also need a `bbi.yaml` file, which I've also supplied in `/model/nonmem/bbi.yaml`.
To submit a NONMEM job, we need to supply either the path to a mod file or create a model object from `bbr`, and supply a `slurm-template.tmpl` file. To use `bbi` we also need a `bbi.yaml` file, which I've also supplied in `/model/nonmem/bbi.yaml` (and is also supplied with the R project starter).

Here is an example of a template file that will call `bbi`:

Expand All @@ -105,9 +113,13 @@ Here is an example of a template file that will call `bbi`:
{{/parallel}}
```

This file will call `bbi` to run our supplied model (`{{model_path}}.mod`) if `ncpu > 1` then parallel will be true and the code between `{{#parallel}}` and `{{/parallel}}` will be populated. if `ncpu = 1` then parallel will be false and the code between `{{^parallel}}` and `{{/parallel}}` will be populated. Notice we'll have to supply the `bbi_exe_path` for it to start the NONMEM run because `submit_nonmem_model` does not supply that variable to the template by default.
This file will call `bbi` to run our supplied model (`{{model_path}}.mod`) if `ncpu > 1` then parallel will be true and the code between `{{#parallel}}` and `{{/parallel}}` will be populated. if `ncpu = 1` then parallel will be false and the code between `{{^parallel}}` and `{{/parallel}}` will be populated. By default, `submit_nonmem_model` will inject `Sys.which("bbi")` into the template, so if `bbi` is not on your path we'll have to supply the `bbi_exe_path` for it to start the NONMEM run.

We will use a few different template files with different functionality so we'll inject those template file paths to `submit_nonmem_model`. However, we'll use the `submission-log` directory for the output, so we'll set that option so `submit_nonmem_model` default can be used. The slurm template files are saved in `~/model/nonmem/` Additionally, there is a simple NONMEM control stream in `1001.mod` in the same directory that we can use for testing.
```{r}
Sys.which("bbi")
```

We will use a few different template files with different functionality so we'll inject those template file paths to `submit_nonmem_model`. However, we'll use the `submission-log` directory for the output, so we'll set that option as well as `bbi_config_path` so `submit_nonmem_model` defaults can be used. The slurm template files are saved in `~/model/nonmem/` Additionally, there is a simple NONMEM control stream in `1001.mod` in the same directory that we can use for testing.

```{r}
library(bbr)
Expand All @@ -116,9 +128,10 @@ library(here)
nonmem = file.path(here::here(), "vignettes", "model", "nonmem")
options('slurmtools.submission_root' = file.path(nonmem, "submission-log"))
options('slurmtools.bbi_config_path' = file.path(nonmem, "bbi.yaml"))
```

To create the bbr model object, we need to have both `1001.mod` and `1001.yaml` which contains metadata about the model in the supplied directory (`/model/nonmem/`).
To create the `bbr` model object, we need to have both `1001.mod` and `1001.yaml` which contains metadata about the model in the supplied directory (`./model/nonmem/`). We'll check for mod_number.yaml and if it exists, read in the model otherwise create it and then read it.

```{r}
mod_number <- "1001"
Expand All @@ -130,21 +143,18 @@ if (file.exists(file.path(nonmem, paste0(mod_number, ".yaml")))) {
}
```

We can now submit the job and point to the template file in `model/nonmem/slurm-job-bbi.tmpl`
We can now submit the job and point to the template file in `model/nonmem/slurm-job-bbi.tmpl`.

```{r}
submission <- slurmtools::submit_nonmem_model(
mod,
slurm_job_template_path = file.path(nonmem, "slurm-job-bbi.tmpl"),
slurm_template_opts = list(
bbi_exe_path = Sys.which("bbi"),
bbi_config_path = file.path(nonmem, "bbi.yaml"))
)
submission
```

Notice we get a Warning about a config.toml file not being found. I'll expand on that later, but for now we see a `status` with an exit code of 0 suggesting a successful command, and the `stdout` gives us the batch job number. We can use `slurmtools::get_slurm_jobs()` to monitor the status of the job. Here, I've filtered the results to only show the job I just submitted above.
Notice we get a Warning about a config.toml file not being found. I'll expand on that later, but for now we see a `status` with an exit code of 0 suggesting a successful command, and the `stdout` gives us the batch job number. We can use `slurmtools::get_slurm_jobs()` to monitor the status of the job. Here, we can supply the user = "matthews" argument to filter to just the jobs I've submitted.

```{r}
slurmtools::get_slurm_jobs(user = 'matthews')
Expand Down Expand Up @@ -202,8 +212,6 @@ submission_ntfy <- slurmtools::submit_nonmem_model(
slurm_job_template_path = file.path(nonmem, "slurm-job-bbi-ntfy.tmpl"),
overwrite = TRUE,
slurm_template_opts = list(
bbi_exe_path = Sys.which("bbi"),
bbi_config_path = file.path(nonmem, "bbi.yaml"),
ntfy = "ntfy_demo")
)
Expand Down
Loading

0 comments on commit a4bd234

Please sign in to comment.