Open-Systems-Pharmacology / R-Toolbox

Other
4 stars 7 forks source link

"IsFormula" is stored in the DCI_Info not correctly #47

Closed PavelBal closed 6 years ago

PavelBal commented 6 years ago

Consider my example from #46 .

The described model is loaded into R and no parameters are initialized:

library(MoBiToolboxForR, quietly=TRUE)

testXML = ".\\QC_R\\testSim.xml"
DCI_Info_test <- initSimulation(testXML, whichInitParam = "none")

Now I want to initialized all species that are NOT formulas. For this, I first get all the species (which turn out to be parameters, and I am getting no species at all, but this is another story described in #46 ...), and iterate through the list while getting the property "IsFormula". If the species in not a formula, I initialize its initial value:

allSpecies = getSpeciesInitialValue(path_id ="*", list(Type = "readonly"), DCI_Info = DCI_Info_test);
initStruct <- list()
for (idx in allSpecies$Index){
  isFormula = getSpeciesInitialValue(path_id ="*", list(Type = "readonly", Property = "IsFormula", Index = idx), DCI_Info=DCI_Info_test)$Value;
  if (!isFormula){
    path_id = getSpeciesInitialValue(path_id ="*", list(Type = "readonly", Property = "Path", Index = idx), DCI_Info=DCI_Info_test)$Value;
    initStruct <- initSpeciesInitialValue(initStruct, path_id, initializeIfFormula = "never")
  }
}

Init struct now consists of two parameters (state variables), one of which was defined by a formula:

initStruct[["InitialValues"]][["Path_ID"]]
        [,1]                                     
        "testSim|Organism|RHS_Param_constantInit"
path_id "testSim|Organism|RHS_Param_formulaInit" 

Now we could think that the formula of "RHS_Param_formulaInit" was optimized to a constant, because it was just "5+6", and everything is fine. Well, it is not, because trying to initialized the model with this init struct leads to an error!

> DCI_Info_test <- initSimulation(testXML, ParamList = initStruct)
Error in initParameterStruct(ParamList, TableArray[[id]], id, DCI_Info) : 
  The SpeciesInitialValue is defined as Formula:  If you want to inititalize this species initial value do not use option 'never' for the function initParameter testSim|Organism|RHS_Param_formulaInit 5+6

Looking at DCI_Info, nether the parameters nor the Species lists have their "isFormula" set to 1 for any of the species/parameters!

> DCI_Info_test[["InputTab"]][["AllParameters"]][["Formula"]]
 [1] "" "" "" "" "" "" "" "" "" "" "" "" ""
attr(,"Name")
[1] "Formula"
attr(,"Description")
[1] ""

> DCI_Info_test[["InputTab"]][["AllSpecies"]][["IsFormula"]]
[1] 0 0
attr(,"MinValue")
[1] 0
attr(,"MaxValue")
[1] 0
attr(,"DefaultValue")
[1] 0
attr(,"Name")
[1] "IsFormula"
attr(,"Description")
[1] ""
Yuri05 commented 6 years ago

Looking at DCI_Info, nether the parameters nor the Species lists have their "isFormula" set to 1 for any of the species/parameters!

This is correct behavior. What happens here:

PavelBal commented 6 years ago

I do not get it. How can I then initialize all parameters/species except those with formulas?

Yuri05 commented 6 years ago

In the Matlab-Toolbox you can do it with allNonFormula This can be done in the same manner in the R-Toolbox (if not implemented yet). And as far as I see it's also implemented in R

PavelBal commented 6 years ago

That does not work for species, only for parameters!

>DCI_Info_test <- initSimulation(testXML, whichInitParam = "all")
>DCI_Info_test$InputTab$VariableSpecies$Path
[1] "testSim|Organism|RHS_Param_constantInit" "testSim|Organism|RHS_Param_formulaInit" 
attr(,"Name")
[1] "Path"
attr(,"Description")
[1] ""

Initializing all creates two variable species. Initializing "allNonFormula" creates NO variable species, although at least "testSim|Organism|RHS_Param_constantInit" should be there.

DCI_Info_test <- initSimulation(testXML, whichInitParam = "allNonFormula") DCI_Info_test$InputTab$VariableSpecies$Path character(0) attr(,"Name") [1] "Path" attr(,"Description") [1] ""

The ouput is also emtpy for a PBPK-Model exported from PK-Sim, so it is not because of "optimization" or whatever.

Yuri05 commented 6 years ago

Might be a bug in the R-Toolbox. Please create an issue for that.

Initializing your model in Matlab with allNonFormulasets _RHS_ParamconstantInit species to variable. Means: if we have an issue here, it's in initSimulation.R