Open-Systems-Pharmacology / OSPSuite-R

R package for the OSPSuite
https://www.open-systems-pharmacology.org/OSPSuite-R/
Other
29 stars 12 forks source link

Add new argument `stateVariableParamsOrPaths` to the function `createSimulationBatch` #1400

Open PavelBal opened 7 months ago

PavelBal commented 7 months ago

Add a new argument stateVariableParams to the function, so createSimulationBatch(simulation, parametersOrPaths = NULL, stateVariableParamsOrPaths = NULL, moleculesOrPaths = NULL).

PavelBal commented 7 months ago
createSimulationBatch <- function(simulation, parametersOrPaths = NULL, moleculesOrPaths = NULL,
                                  stateVariableParametersOrPaths = NULL) {
  validateIsOfType(simulation, "Simulation")
  validateIsOfType(parametersOrPaths, c("Parameter", "character"), nullAllowed = TRUE)
  validateIsOfType(stateVariableParametersOrPaths, c("Parameter", "character"), nullAllowed = TRUE)
  validateIsOfType(moleculesOrPaths, c("Molecule", "character"), nullAllowed = TRUE)

  if ((length(parametersOrPaths) + length(moleculesOrPaths) + length(stateVariableParametersOrPaths)) == 0) {
    stop(messages$errorSimulationBatchNothingToVary)
  }

  variableParameters <- c(parametersOrPaths)

  if (isOfType(variableParameters, "Parameter")) {
    variableParameters <- unlist(lapply(variableParameters, function(x) x$path), use.names = FALSE)
  }

  variableMolecules <- c(moleculesOrPaths)

  if (isOfType(variableMolecules, "Quantity")) {
    variableMolecules <- unlist(lapply(variableMolecules, function(x) x$path), use.names = FALSE)
  }

  # Add state variable parameters to the molecules list as internally such parameters
  # are treated as molecules
  if (isOfType(stateVariableParametersOrPaths, "Parameter")) {
    variableMolecules <- c(variableMolecules, unlist(lapply(stateVariableParametersOrPaths, function(x) x$path), use.names = FALSE))
  } else {
    variableMolecules <- c(variableMolecules, stateVariableParametersOrPaths)
  }

Documentation:

#' @param stateVariableParametersOrPaths Parameter instances (element or vector)
#' typically retrieved using `getAllParametersMatching` or parameter path (element or vector of strings)
#' of parameters that are defined by an ODE that will be varied in the simulation. (optional)
PavelBal commented 7 months ago

I realized this is not that straightforward. SimulationBatch$addRunValues() supports adding values for parameters and initial values of molecules, and RHS params should be in the molecules list. If we want to extend the createSimulationBatch function by the argument for state variable parameters, addRunValues should support this too, but this is implemented in .NET. IMO too much overhead for now.

Yuri05 commented 7 months ago

I wonder if we need this extension or rather it should be handled internally.

E.g. the createSimulationBatchfunction could check for every parameter (path) if the corresponding parameter has an RHS or not. If yes: insert parameter path into variableMoleculesand not into variableParameters. Then no interface modification is required and no changes in the OSPSuite.Core https://github.com/Open-Systems-Pharmacology/OSPSuite-R/blob/03e8f5aed10f1a783b0fdc1621d03f6e6c416248/R/utilities-simulation.R#L338-L366

PavelBal commented 7 months ago

Doable... Then we would have to track the idx of parameters that are ODE based to add the initial values of those parameters to the molecules IC list... Sounds error-prone.