Repository navigation
Conversation
- correct some bug with BD_create set of function - add test - add export function to PM_result
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #384 +/- ##
==========================================
+ Coverage 62.73% 63.15% +0.41%
==========================================
Files 51 52 +1
Lines 22410 22760 +350
==========================================
+ Hits 14059 14373 +314
- Misses 8351 8387 +36
🚀 New features to boost your workflow:
|
|
Please run |
mhovd
left a comment
There was a problem hiding this comment.
Thanks for the PR, Romain.
There are two issues that needs to be fixed:
- There seems to be a duplicated model parser, can you not use the DSL directly?
- There is a lot of unnecessary documentation that is BestDose specific, which should be removed.
Other than that I think the function name and scope is OK. A very cheap option would be to use the $save that exists already on the PM_model, and parse that directly.
| #' @title Derive the administration routes from the equations | ||
| #' @description BestDose needs at least one `{route, compartment}` entry, and that compartment | ||
| #' index is what the engine actually doses into. Infusion tokens (`R[N]`, `RATEIV[N]`) map to IV, | ||
| #' bolus tokens (`B[N]`) to an extravascular depot. The compartment is inferred, the human label is | ||
| #' a guess: confirm it. | ||
| #' | ||
| #' @param equations The equation list from `extractPMequation` | ||
| #' @return A data.frame of routes | ||
|
|
||
| extractBDroutes <- function(equations) { | ||
| text <- tolower(paste(unlist(equations), collapse = " ")) | ||
| indices <- function(pattern) { | ||
| unique(as.integer(stringr::str_match_all(text, pattern)[[1]][, 2])) | ||
| } | ||
|
|
||
| iv <- indices("(?:^|[^a-z0-9_])(?:r|rateiv)\\s*\\[\\s*([0-9]+)\\s*\\]") | ||
| po <- indices("(?:^|[^a-z0-9_])b\\s*\\[\\s*([0-9]+)\\s*\\]") | ||
|
|
||
| if (length(iv) + length(po) == 0) { | ||
| message( | ||
| "No dose input (R[N]/B[N]) found in the equations, defaulting to IV into compartment 1." | ||
| ) | ||
| return(data.frame(route = "IV", compartment = 1L)) | ||
| } | ||
|
|
||
| routes <- data.frame( | ||
| route = c(rep("IV", length(iv)), rep("PO", length(po))), | ||
| compartment = as.integer(c(iv, po)) | ||
| ) | ||
| message( | ||
| "Routes inferred from the equations, check the labels: ", | ||
| paste(routes$route, "->", routes$compartment, collapse = ", ") | ||
| ) | ||
|
|
||
| return(routes) | ||
| } |
There was a problem hiding this comment.
I am not sure about this and other helpers, which appear to mirror the parsing of the PM_model to DSL, and is duplicate code.
I assume you would rather want the DSL directly, accessible from a created model using PM_model$dsl
There was a problem hiding this comment.
indeed, i was not familiar with Pmetrics DSL. This is transfered from the previous compile_model() of Pmetrics. This is what is used by bestdose for the moment. We could directly import dsl model in a near future, but this would require substantial changes in BestDose. Would you mind if we let this like that for the moment ? Then when bestdose ModelLibrary will be uploaded to support full DSL, this will be changed in Pmetrics aswell.
There was a problem hiding this comment.
I think it would be worth the effort to convert those models to the DSL sooner rather than later. It should not take long at all, and I would be willing to help you with it.
|
|
||
| # Starting ranges for the covariates BestDose knows about. These are the clinical bounds shown in | ||
| # the UI, not statistical ones: a Pmetrics fit carries none of this, so anything missing here is | ||
| # filled with a placeholder that needs a review. |
There was a problem hiding this comment.
Not Pmetrics documentation, but BestDose specific
There was a problem hiding this comment.
should i document of just make it @internal ?
| export_bestdose = function(drug_name = "Drug", dir_path = NULL) { | ||
| # create model using functions in BD_create.R | ||
| model_file <- createBDmodel(self, drug_name = drug_name) | ||
| path = ifelse(is.null(dir_path), paste0(drug_name, ".json"), paste0(dir_path, "/", drug_name, ".json")) | ||
|
|
||
| # #export to JSON | ||
| jsonlite::write_json( | ||
| model_file, | ||
| path, | ||
| auto_unbox = TRUE, | ||
| dataframe = "rows", | ||
| null = "null", | ||
| pretty = TRUE | ||
| ) | ||
|
|
There was a problem hiding this comment.
Would it not be enough to have a JSON of the DSL and the support points? Or what else is needed for BestDose?
There was a problem hiding this comment.
Unfortunately, Its not for the moment. This is a planed change for 1.1.0 in bestdose probably. But we are far from it at the moment, hence this adaptation of the code to get everything from model$arg_list instead of model$dsl
There was a problem hiding this comment.
🟡 Changes recommended
Tests and user-facing API/export plumbing need fixes (unexported helper usage in tests, export_bestdose() argument/path portability, and roxygen/NAMESPACE export sync for createBDmodel()).
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (1)
tests/testthat/test-BD_create.R:8
- This test file calls many helpers from
R/BD_create.R(e.g.,bdKey,extractPMBlock) that are not exported. With onlylibrary(Pmetrics)attached, these symbols won’t be found and the tests will fail unless you qualify them (e.g.,Pmetrics:::bdKey) or bind them from the namespace.
library(Pmetrics)
library(testthat)
PM_result <- NPex
- Files reviewed: 3/3 changed files
- Comments generated: 3
- Review effort level: Lite
| #' @title Create a BestDose model list from a PM model object | ||
| #' @description | ||
| #' `r lifecycle::badge("experimental")` | ||
| #' | ||
| #' @param PM_result A PM model result object | ||
| #' @param drug_name The drug the model attaches to | ||
| #' @param model_name The model file name, without the .json extension | ||
| #' @param description Free text description of the model | ||
| #' @param reference Citation for the model | ||
| #' @param reference_url URL for the citation | ||
| #' @return A list containing the description, model and support points formatted for BestDose | ||
| #' | ||
| #' @export | ||
|
|
| export_bestdose = function(drug_name = "Drug", dir_path = NULL) { | ||
| # create model using functions in BD_create.R | ||
| model_file <- createBDmodel(self, drug_name = drug_name) | ||
| path = ifelse(is.null(dir_path), paste0(drug_name, ".json"), paste0(dir_path, "/", drug_name, ".json")) |
| # Checks for BD_create.R — run with testthat after sourcing the exporter: | ||
| # source("docs/BD_create.R"); testthat::test_file("docs/test-BD_create.R") |
There was a problem hiding this comment.
🟡 Changes recommended
As-is, the new public API/export flow is inconsistent with the PR’s documented usage and the tests/docs depend on export/namespace details that are currently missing (e.g., roxygen/NAMESPACE updates and direct calls to non-exported helpers).
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (3)
tests/testthat/test-BD_create.R:2
- The header comment references
docs/BD_create.Randdocs/test-BD_create.R, but this test lives undertests/testthat/and the implementation is inR/BD_create.R, so the suggested manual commands are incorrect.
# Checks for BD_create.R — run with testthat after sourcing the exporter:
# source("docs/BD_create.R"); testthat::test_file("docs/test-BD_create.R")
R/BD_create.R:546
createBDmodel()is tagged with@export, but the generatedNAMESPACEin this branch does not currently export it, so it won't be available to users (and tests referring to it as a public API will fail). The roxygen outputs (at leastNAMESPACE, and likely aman/createBDmodel.Rd) need to be regenerated and committed.
#' @param reference Citation for the model
#' @param reference_url URL for the citation
#' @return A list containing the description, model and support points formatted for BestDose
#'
#' @export
R/PM_result.R:318
- The PR description/example shows
export_bestdose(model_name)(file name + optional path), but the implementation currently takesdrug_nameand uses it for both the JSON drug field and the output filename. This makes it impossible to set a different model filename from the drug name and conflicts with the documented usage; consider exposingmodel_nameexplicitly and usingfile.path()for portable path construction.
#' @param drug_name Name of the drug to use in the exported JSON file. Default is "Drug".
#' @param dir_path Optional path to save the JSON file. If not provided, the file will be saved in the current working directory with the name "{drug_name}.json".
export_bestdose = function(drug_name = "Drug", dir_path = NULL) {
# create model using functions in BD_create.R
- Files reviewed: 3/3 changed files
- Comments generated: 3
- Review effort level: Lite
| # generate data | ||
| library(Pmetrics) | ||
| library(testthat) | ||
| PM_result <- NPex | ||
|
|
||
| PMmodel <- NPex$model$arg_list | ||
|
|
||
| # ================================= | ||
| # Test Helper | ||
| # ================================ |
| stringr::str_replace(x, "<-", "=") | ||
| }) | ||
|
|
||
| # input route replacte "rateiv[n]" or "R[n]" by "r[n]" and "Bolus[n]" or "b[n]" by "B[n]" |
| #' @description | ||
| #' `r lifecycle::badge("experimental")` | ||
| #' export the model in a JSON file format that can be used in the BestDose. | ||
| #' This is an experimental feature and may change in future versions. | ||
| #' | ||
| #' @details | ||
| #' This feature is useful if the user want to use this specific model to perform MIPD. | ||
| #' The file will contains most of the features and covariates, but the user will have to specify | ||
| #' some additional information after importing this file. | ||
| #' |
This PR introduces 3 changes (2 new files and one function update)
1. Create
BD_create.Rin R/Has all the set of function to create any model from a Pmetrics run
classic S3 object
2. Add
export_bestdosetoPM_resultUser can now easily export a bestdose model file from any run
coverage
test-BD_create.Rfile added in tests/testthat