Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to not run simulations when converting snapshots #1488

Merged
merged 17 commits into from
Sep 23, 2024
Merged
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions R/snapshots.R
Original file line number Diff line number Diff line change
@@ -95,6 +95,8 @@ runSimulationsFromSnapshot <- function(..., output = ".", exportCSV = TRUE, expo
#' @param ... character strings, path to files or a directory containing files to convert
#' @param format, character string, target format either "snapshot" or "project".
#' @param output character string, path to the output directory where to write the converted files
#' @param runSimulations logical, whether to run simulations during conversion (default = FALSE).
#' Only when converting from snapshot to project.
#'
#' @return NULL
#' @export
@@ -104,7 +106,7 @@ runSimulationsFromSnapshot <- function(..., output = ".", exportCSV = TRUE, expo
#' convertSnapshot("path/to/snapshot.json", format = "project")
#' convertSnapshot("path/to/project.pksim5", format = "snapshot")
#' }
convertSnapshot <- function(..., format, output = ".") {
convertSnapshot <- function(..., format, output = ".", runSimulations = FALSE) {
rlang::arg_match(arg = format, values = c("snapshot", "project"))

initPKSim()
@@ -115,6 +117,12 @@ convertSnapshot <- function(..., format, output = ".") {
SnapshotRunOptions$set(name = "InputFolder", value = temp_dir)
SnapshotRunOptions$set(name = "OutputFolder", value = normalizePath(output))

if (isTRUE(runSimulations)) {
SnapshotRunOptions$set(name = "RunSimulations", value = TRUE)
} else {
SnapshotRunOptions$set(name = "RunSimulations", value = FALSE)
}

if (format == "project") {
SnapshotRunOptions$set("ExportMode", 0L)
nfiles <- length(list.files(temp_dir, pattern = ".json"))
@@ -124,12 +132,25 @@ convertSnapshot <- function(..., format, output = ".") {
}

cli::cli_process_start(
msg = "Converting {nfiles} files{?s} to {format} format",
msg = "Converting {nfiles} file{?s} to {format} format",
msg_done = "Conversion completed",
msg_failed = "An error occured while converting files"
)

rSharp::callStatic("PKSim.R.Api", "RunSnapshot", SnapshotRunOptions)
tryCatch(
{
invisible(rSharp::callStatic("PKSim.R.Api", "RunSnapshot", SnapshotRunOptions))
},
error = function(e) {
message <- stringr::str_extract(as.character(e), "(?<=Message: )[^\\n]*")
Copy link
Member

Choose a reason for hiding this comment

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

Some error messages won't have a message line of format "Message: *". In this case str_extract() returns in NA resulting in

Error in `convertSnapshot()`:
! NA
✖ An error occured while converting files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I added an if case to handle this.


if (is.na(message){
message <- e
})

cli::cli_abort(message = message, call = rlang::caller_env(n = 4))
}
)
}


360 changes: 180 additions & 180 deletions inst/lib/ConsoleApp.deps.json

Large diffs are not rendered by default.

Binary file modified inst/lib/ConsoleApp.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Assets.Images.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Assets.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Core.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Autofac.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Castle.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Export.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Import.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Reporting.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Serialization.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Presentation.Serialization.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Presentation.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.R.dll
Binary file not shown.
Binary file modified inst/lib/PKSim.Assets.Images.dll
Binary file not shown.
Binary file modified inst/lib/PKSim.Assets.dll
Binary file not shown.
Binary file modified inst/lib/PKSim.CLI.Core.dll
Binary file not shown.
Binary file modified inst/lib/PKSim.Core.dll
Binary file not shown.
Binary file modified inst/lib/PKSim.Infrastructure.dll
Binary file not shown.
Binary file modified inst/lib/PKSim.Presentation.dll
Binary file not shown.
Binary file modified inst/lib/PKSim.R.dll
Binary file not shown.
330 changes: 2 additions & 328 deletions tests/data/test_snapshot.json
Original file line number Diff line number Diff line change
@@ -364,74 +364,7 @@
}
}
],
"HasResults": true,
"IndividualAnalyses": [
{
"Axes": [
{
"Unit": "h",
"Dimension": "Time",
"Type": "X",
"GridLines": false,
"Visible": true,
"DefaultColor": "#FFFFFF",
"DefaultLineStyle": "None",
"Scaling": "Linear",
"NumberMode": "Normal"
},
{
"Unit": "µmol/l",
"Dimension": "Concentration (molar)",
"Type": "Y",
"GridLines": false,
"Visible": true,
"DefaultColor": "#FFFFFF",
"DefaultLineStyle": "Solid",
"Scaling": "Log",
"NumberMode": "Normal"
}
],
"Curves": [
{
"Name": "Generic_compound-Venous Blood-Plasma-Concentration",
"X": "Time",
"Y": "Simulation - IV + Weibull - Default tolerance|Organism|VenousBlood|Plasma|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#FF0000",
"LegendIndex": 1
}
},
{
"Name": "Generic_compound-Skin-Intracellular-Concentration",
"X": "Time",
"Y": "Simulation - IV + Weibull - Default tolerance|Organism|Skin|Intracellular|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#0000FF",
"LegendIndex": 2
}
}
],
"Name": "Time Profile Analysis",
"FontAndSize": {
"Fonts": {
"AxisSize": 10,
"LegendSize": 8,
"TitleSize": 16,
"DescriptionSize": 12,
"OriginSize": 8,
"FontFamilyName": "Microsoft Sans Serif",
"WatermarkSize": 32
}
},
"Settings": {
"SideMarginsEnabled": true,
"LegendPosition": "RightInside",
"BackColor": "#FFFFFF",
"DiagramBackColor": "#FFFFFF"
},
"OriginText": "Comparison advanced admin with weibull\nSimulation - IV + Weibull - Default tolerance\n2024-08-30 09:51"
}
]
"HasResults": false
},
{
"Name": "Simulation - IV only - Default tolerance",
@@ -511,266 +444,7 @@
}
}
],
"HasResults": true,
"IndividualAnalyses": [
{
"Axes": [
{
"Unit": "h",
"Dimension": "Time",
"Type": "X",
"GridLines": false,
"Visible": true,
"DefaultColor": "#FFFFFF",
"DefaultLineStyle": "None",
"Scaling": "Linear",
"NumberMode": "Normal"
},
{
"Unit": "µmol/l",
"Dimension": "Concentration (molar)",
"Type": "Y",
"GridLines": false,
"Visible": true,
"DefaultColor": "#FFFFFF",
"DefaultLineStyle": "Solid",
"Scaling": "Log",
"NumberMode": "Normal"
}
],
"Curves": [
{
"Name": "Generic_compound-Skin-Intracellular-Concentration",
"X": "Time",
"Y": "Simulation - IV only - Default tolerance|Organism|Skin|Intracellular|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#FF0000",
"LegendIndex": 1
}
},
{
"Name": "Generic_compound-Venous Blood-Plasma-Concentration",
"X": "Time",
"Y": "Simulation - IV only - Default tolerance|Organism|VenousBlood|Plasma|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#0000FF",
"LegendIndex": 2
}
}
],
"Name": "Time Profile Analysis",
"FontAndSize": {
"Fonts": {
"AxisSize": 10,
"LegendSize": 8,
"TitleSize": 16,
"DescriptionSize": 12,
"OriginSize": 8,
"FontFamilyName": "Microsoft Sans Serif",
"WatermarkSize": 32
}
},
"Settings": {
"SideMarginsEnabled": true,
"LegendPosition": "RightInside",
"BackColor": "#FFFFFF",
"DiagramBackColor": "#FFFFFF"
},
"OriginText": "Comparison advanced admin with weibull\nSimulation - IV only - Default tolerance\n2024-08-30 09:51"
}
]
}
],
"SimulationComparisons": [
{
"Name": "Simulation Comparison - Tolerance reduced",
"Simulations": [
"Simulation - IV + Weibull - Tolerance reduced",
"Simulation - IV only - Tolerance reduced"
],
"IndividualComparison": {
"Axes": [
{
"Unit": "h",
"Dimension": "Time",
"Type": "X",
"GridLines": false,
"Visible": true,
"Min": 15.53561,
"Max": 25.05864,
"DefaultColor": "#FFFFFF",
"DefaultLineStyle": "None",
"Scaling": "Linear",
"NumberMode": "Normal"
},
{
"Unit": "µmol/l",
"Dimension": "Concentration (molar)",
"Type": "Y",
"GridLines": false,
"Visible": true,
"Min": 5.034298E-07,
"Max": 0.3479374,
"DefaultColor": "#FFFFFF",
"DefaultLineStyle": "Solid",
"Scaling": "Log",
"NumberMode": "Normal"
}
],
"Curves": [
{
"Name": "Simulation - IV only-Generic_compound-Venous Blood-Plasma-Concentration",
"X": "Time",
"Y": "Simulation - IV only - Tolerance reduced|Organism|VenousBlood|Plasma|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#800080",
"LegendIndex": 2
}
},
{
"Name": "Simulation - IV only-Generic_compound-Skin-Intracellular-Concentration",
"X": "Time",
"Y": "Simulation - IV only - Tolerance reduced|Organism|Skin|Intracellular|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#0000FF",
"LegendIndex": 3
}
},
{
"Name": "Simulation - IV + Weibull-Generic_compound-Venous Blood-Plasma-Concentration",
"X": "Time",
"Y": "Simulation - IV + Weibull - Tolerance reduced|Organism|VenousBlood|Plasma|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#FF00FF",
"LegendIndex": 1,
"LineStyle": "Dash"
}
},
{
"Name": "Simulation - IV + Weibull-Generic_compound-Skin-Intracellular-Concentration",
"X": "Time",
"Y": "Simulation - IV + Weibull - Tolerance reduced|Organism|Skin|Intracellular|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#008000",
"LegendIndex": 2,
"LineStyle": "Dash"
}
}
],
"Name": "Simulation Comparison - Tolerance reduced",
"FontAndSize": {
"Fonts": {
"AxisSize": 10,
"LegendSize": 8,
"TitleSize": 16,
"DescriptionSize": 12,
"OriginSize": 8,
"FontFamilyName": "Microsoft Sans Serif",
"WatermarkSize": 32
}
},
"Settings": {
"SideMarginsEnabled": true,
"LegendPosition": "RightInside",
"BackColor": "#FFFFFF",
"DiagramBackColor": "#FFFFFF"
}
}
},
{
"Name": "Simulation Comparison - Default tolerance",
"Simulations": [
"Simulation - IV only - Default tolerance",
"Simulation - IV + Weibull - Default tolerance"
],
"IndividualComparison": {
"Axes": [
{
"Unit": "h",
"Dimension": "Time",
"Type": "X",
"GridLines": false,
"Visible": true,
"Min": 15.75082,
"Max": 23.63288,
"DefaultColor": "#FFFFFF",
"DefaultLineStyle": "None",
"Scaling": "Linear",
"NumberMode": "Normal"
},
{
"Unit": "µmol/l",
"Dimension": "Concentration (molar)",
"Type": "Y",
"GridLines": false,
"Visible": true,
"Min": 4.803477E-07,
"Max": 0.3479376,
"DefaultColor": "#FFFFFF",
"DefaultLineStyle": "Solid",
"Scaling": "Log",
"NumberMode": "Normal"
}
],
"Curves": [
{
"Name": "Simulation - IV only - Default tolerance-Generic_compound-Skin-Intracellular-Concentration",
"X": "Time",
"Y": "Simulation - IV only - Default tolerance|Organism|Skin|Intracellular|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#0000FF",
"LegendIndex": 1
}
},
{
"Name": "Simulation - IV only - Default tolerance-Generic_compound-Venous Blood-Plasma-Concentration",
"X": "Time",
"Y": "Simulation - IV only - Default tolerance|Organism|VenousBlood|Plasma|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#800080",
"LegendIndex": 2
}
},
{
"Name": "Simulation - IV + Weibull - Default tolerance-Generic_compound-Venous Blood-Plasma-Concentration",
"X": "Time",
"Y": "Simulation - IV + Weibull - Default tolerance|Organism|VenousBlood|Plasma|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#FF00FF",
"LegendIndex": 1,
"LineStyle": "Dash"
}
},
{
"Name": "Simulation - IV + Weibull - Default tolerance-Generic_compound-Skin-Intracellular-Concentration",
"X": "Time",
"Y": "Simulation - IV + Weibull - Default tolerance|Organism|Skin|Intracellular|Generic_compound|Concentration in container",
"CurveOptions": {
"Color": "#008000",
"LegendIndex": 2,
"LineStyle": "Dash"
}
}
],
"Name": "Simulation Comparison - Default tolerance",
"FontAndSize": {
"Fonts": {
"AxisSize": 10,
"LegendSize": 8,
"TitleSize": 16,
"DescriptionSize": 12,
"OriginSize": 8,
"FontFamilyName": "Microsoft Sans Serif",
"WatermarkSize": 32
}
},
"Settings": {
"SideMarginsEnabled": true,
"LegendPosition": "RightInside",
"BackColor": "#FFFFFF",
"DiagramBackColor": "#FFFFFF"
}
}
"HasResults": false
}
]
}
9 changes: 9 additions & 0 deletions tests/testthat/test-snapshots.R
Original file line number Diff line number Diff line change
@@ -40,6 +40,15 @@ test_that("Convert project to snapshot works", {
expect_length(list.files(temp_dir, pattern = ".json"), 1)
})

test_that("RunSimulations argument is supported", {
path <- getTestDataFilePath("test_snapshot.json")
temp_dir <- withr::local_tempdir()
expect_no_error({
convertSnapshot(path, output = temp_dir, format = "project", runSimulations = TRUE)
convertSnapshot(path, output = temp_dir, format = "project", runSimulations = FALSE)
})
})

test_that("gather files handles one file path", {
# create a temporary file
temp_file <- withr::local_tempfile(fileext = ".json", lines = "content")