Repository navigation
Expand file tree
/
Copy pathexponential_growth_model.py
More file actions
100 lines (78 loc) · 3.04 KB
/
Copy pathexponential_growth_model.py
File metadata and controls
100 lines (78 loc) · 3.04 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import chi
import numpy as np
class ExponentialGrowthModel(chi.MechanisticModel):
"""
An exponential growth model.
"""
def __init__(self):
super(ExponentialGrowthModel, self).__init__()
self._has_sensitivities = False
def enable_sensitivities(self, enabled, parameter_names=None):
r"""
Enables the computation of the model output sensitivities to the model
parameters if set to ``True``.
The sensitivities of the model outputs are defined as the partial
derviatives of the ouputs :math:`\bar{y}` with respect to the model
parameters :math:`\psi`
.. math:
\frac{\del \bar{y}}{\del \psi}.
:param enabled: A boolean flag which enables (``True``) / disables
(``False``) the computation of sensitivities.
:type enabled: bool
"""
self._has_sensitivities = bool(enabled)
def has_sensitivities(self):
"""
Returns a boolean indicating whether sensitivities have been enabled.
"""
return self._has_sensitivities
def n_outputs(self):
"""
Returns the number of output dimensions.
By default this is the number of states.
"""
return 1
def n_parameters(self):
"""
Returns the number of parameters in the model.
Parameters of the model are initial state values and structural
parameter values.
"""
return 2
def outputs(self):
"""
Returns the output names of the model.
"""
return ['Tumour volume']
def parameters(self):
"""
Returns the parameter names of the model.
"""
return ['Initial volume', 'Growth rate']
def simulate(self, parameters, times):
"""
Returns the numerical solution of the model outputs (and optionally
the sensitivites) for the specified parameters and times.
The model outputs are returned as a 2 dimensional NumPy array of shape
``(n_outputs, n_times)``. If sensitivities are enabled, a tuple is
returned with the NumPy array of the model outputs and a NumPy array of
the sensitivities of shape ``(n_times, n_outputs, n_parameters)``.
:param parameters: An array-like object with values for the model
parameters.
:type parameters: list, numpy.ndarray
:param times: An array-like object with time points at which the output
values are returned.
:type times: list, numpy.ndarray
:rtype: np.ndarray of shape (n_outputs, n_times) or
(n_times, n_outputs, n_parameters)
"""
y0, growth_rate = parameters
times = np.asarray(times)
# Solve model
y = y0 * np.exp(growth_rate * times)
if not self._has_sensitivities:
return y[np.newaxis, :]
sensitivities = np.empty(shape=(len(times), 1, 2))
sensitivities[:, 0, 0] = np.exp(growth_rate * times)
sensitivities[:, 0, 1] = times * y
return y[np.newaxis, :], sensitivities