Skip to content

Create BDexport function#300

Open
R-garreau wants to merge 3 commits intobestdosefrom
create-BDexport-function
Open

Create BDexport function#300
R-garreau wants to merge 3 commits intobestdosefrom
create-BDexport-function

Conversation

@R-garreau
Copy link
Copy Markdown

  • Added function to export Pmetrics run to BestDose model in R/BD_create.R
  • added function to export BestDose model in R6 PM_results

[function call]

# load data after optimizing
run <- PM_load(1, "path")

# user can export by calling this function
run$BDexport("vancomycin", "path")

# this will create the bestdose model file called vancomycin.json in the dir given in path

[coverage]

Coverage: 100.00%
R/BD_create.R: 100.00%

[edit]

2026-03-05

  • renamed function to BDexport()
  • compartment are now being calculated based on the number of ode
  • Improve clarity (debugging and spelling issues)

@mnneely it is now ready for merge.

- correct some spelling errors
- move error management using cli_abort
- compartment number is no based on ode number in file
- 100% coverage
- renamed export -> BDexport
- better name management
- return invisible message for user when exporting
@R-garreau R-garreau requested a review from Copilot March 5, 2026 09:29
@R-garreau R-garreau changed the title Create b dexport function Create BDexport function Mar 5, 2026
@mhovd
Copy link
Copy Markdown
Collaborator

mhovd commented Mar 5, 2026

I am assuming this BDExport is targeted at the BestDose GUI, and not the BestDose functionality in Pmetrics, right?
As such, would it be possible to have that code in the BestDose GUI package instead?

My perspective here is package separation, i.e. not blending dependencies with integration. If it is possible to achieve the same functionality by separating the code clearly between packages, that is always preferred. What do you think, @R-garreau ?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an experimental export path from a PM_result run into a BestDose-compatible JSON model file, backed by new helper functions and unit tests.

Changes:

  • Introduce BDexport() on PM_result to write a BestDose JSON model file to disk.
  • Add R/BD_create.R helper functions to convert Pmetrics model components into the BestDose schema.
  • Add testthat coverage for the BestDose export helpers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
R/BD_create.R New helper functions to translate Pmetrics model/run objects into a BestDose JSON model list.
R/PM_result.R Adds BDexport() method to export a run’s model as JSON.
tests/testthat/test-BD_create.R Adds unit tests for the BestDose conversion helpers.
Comments suppressed due to low confidence (1)

R/PM_result.R:331

  • BDexport() writes to dir_path without validating that the directory exists (or creating it). If the user passes a non-existent path, writeLines() will fail with a fairly opaque base error. Consider checking dir.exists(dir_path) (and either dir.create(..., recursive = TRUE) or cli::cli_abort() with a clear message) before writing the JSON.
      file_name <- if (is.null(dir_path)) {
        paste0(drug_name, ".json")
      } else {
        file.path(dir_path, paste0(drug_name, ".json"))
      }
      
      # #export to JSON
      jsonlite::toJSON(model_list, pretty = TRUE, auto_unbox = TRUE, null = "null") |>
      writeLines(con = file_name)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread R/BD_create.R
Comment thread tests/testthat/test-BD_create.R
Comment thread tests/testthat/test-BD_create.R
Comment thread tests/testthat/test-BD_create.R
@R-garreau
Copy link
Copy Markdown
Author

I am assuming this BDExport is targeted at the BestDose GUI, and not the BestDose functionality in Pmetrics, right? As such, would it be possible to have that code in the BestDose GUI package instead?

My perspective here is package separation, i.e. not blending dependencies with integration. If it is possible to achieve the same functionality by separating the code clearly between packages, that is always preferred. What do you think, @R-garreau ?

I agree about package separation but the goal here is simply to transform your final run in BestDose model file.
You can then add this file to the BestDose software. And only then it will trigger all internal code to check and import model.

This is more a Pmetrics functionality for me

@mhovd
Copy link
Copy Markdown
Collaborator

mhovd commented Mar 12, 2026

I agree about package separation but the goal here is simply to transform your final run in BestDose model file.

Ideally, BestDose should be able to read the file that is given when you use PM_model$save(). And if we need to export additional data or fields to make that happen, I would prefer that solution to a separate function call to an "external" (but not really) package. The export by save() should be well-defined and standardized with a good metadata model, which we can tailor to BestDose. But anything more is blurring the lines between the packages.

@mhovd
Copy link
Copy Markdown
Collaborator

mhovd commented Apr 8, 2026

── Building function reference ─────────────────────────────────────────────────
Error in build_reference_index():
! In _pkgdown.yml, 3 topics missing from index: "PM_bestdose",
"PM_bestdose_problem", and "bestdose".
ℹ Either add to the reference index, or use @keywords internal to drop from
the index.

Can you fix this issue before it is merged?
Basically just either add it to the pkgdown.yml so that it is added to the documentation, or add @keywords internal to drop it from the index, like the message suggests.

@R-garreau
Copy link
Copy Markdown
Author

── Building function reference ─────────────────────────────────────────────────
Error in build_reference_index():
! In _pkgdown.yml, 3 topics missing from index: "PM_bestdose",
"PM_bestdose_problem", and "bestdose".
ℹ Either add to the reference index, or use @keywords internal to drop from
the index.

Can you fix this issue before it is merged? Basically just either add it to the pkgdown.yml so that it is added to the documentation, or add @keywords internal to drop it from the index, like the message suggests.

This is not changed i made, it comes from the bestdose branch.
I will wait for Jullian/Michael to merge BestDose code into Pmetrics main since all function name changed.
And then i'll update this before merge

@mnneely
Copy link
Copy Markdown
Contributor

mnneely commented Apr 9, 2026

I've fixed the pkgdown error and merged bestdose into BD-Michael, which was the branch that had my changes. I'll create a PR to merge BD-Michael into main.

@R-garreau
Copy link
Copy Markdown
Author

@mnneely I've seen that you latest merge into BD-Michael recorrected the issue adress to Pmetrics_rust instead of Pmetrics, see DESCRIPTION

@mnneely
Copy link
Copy Markdown
Contributor

mnneely commented Apr 9, 2026

Yes, I know thx. It will be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants