-
Notifications
You must be signed in to change notification settings - Fork 11
/
unit-conversion.Rmd
77 lines (57 loc) · 2.66 KB
/
unit-conversion.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
title: "Dimensions and Units"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Dimensions and Units}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.showtext = TRUE
)
```
## Unit conversion
Every entity - a molecule, a parameter, or an observer - has a certain dimension, like *Amount*, *Concentration*, or *Volume*. The dimension is a property of an entity:
```{r dimension}
library(ospsuite)
# Load a simulation
simFilePath <- system.file("extdata", "Aciclovir.pkml", package = "ospsuite")
sim <- loadSimulation(simFilePath)
# Get the parameter volume of the liver.
livParam <- getParameter("Organism|Liver|Volume", sim)
print(livParam)
# Dimension of the parameter
livParam$dimension
```
The values of a certain dimension can be presented in different units - for example, *l* or *ml* for the dimension *Volume*, or *mol* and *µmol* for the dimension *Amount*. The list of all available units for an entity can be obtained using `allUnits()` method:
```{r allUnits}
# Dimension of the parameter
livParam$dimension
# Units of the parameter
livParam$allUnits
```
Internally, `OSPS` works with the **base units**, and all the values that are shown or passed to functions are in base units by default.
These base units are often different from the units that are displayed by default in PK-Sim (and MoBi).
The list of base and default display units can be found in the [documentation](https://docs.open-systems-pharmacology.org/appendix/appendix).
As an example, the parameter **BMI** is given in the default unit `kg/dm²`, while the default display unit is the more convenient `kg/m²`.
The `{ospuite}` package provides a set of methods for conversion between different units. The methods `toUnit()`, `toBaseUnit()`, and `toDisplayUnit()` require the quantity to get the correct dimension and units; however, it does not change the value of the quantity!
```{r unitConversion}
# Get the BMI parameter
heightParam <- getParameter("Organism|Height", sim)
print(heightParam)
# Print the base and the default display units
heightParam$unit
heightParam$displayUnit
# Convert the value from the base into the default display unit
toDisplayUnit(quantity = heightParam, values = heightParam$value)
# Convert the value to the base unit, that can be used. e.g. for setting new parameter value
toBaseUnit(quantity = heightParam, values = 180, unit = "cm")
liverVolume <- getParameter("Organism|Liver|Volume", sim)
print(liverVolume)
liverVolume$allUnits
# Convert from base volume unit to µl
toUnit(quantity = liverVolume, values = c(1, 2, 3, 4), targetUnit = "ml")
```