Sensors¶
The output-agnostic sensor interface and the default CGM error model. See the CGM error model guide.
Sensor¶
Bases: ABC
Output-agnostic sensor that turns a model output into a measurement.
A sensor observes the model's output trace during replay() and produces a
(typically noisy, sub-sampled) measurement of it. It is deliberately generic:
it knows nothing about glucose or any specific model. Concrete subclasses (e.g.
:class:~sensors.vettoretti19_cgm.Vettoretti19CGM) implement the actual error
model in :meth:measure.
...
Attributes:
| Name | Type | Description |
|---|---|---|
ts |
int
|
Sample time of the sensor, in integration steps. The replay loop takes a
measurement when |
t_offset |
int
|
Extra age of the current sensor, in integration steps (0 when new). Added
to the time elapsed since |
max_lifetime |
int
|
Maximum lifetime of the sensor, in integration steps (10 days at |
connected_at |
int
|
Integration step, in the current replay's step frame, at which the current
sensor was connected. |
Methods:
| Name | Description |
|---|---|
connect_new |
Connects a fresh sensor by (re)sampling its error realization. |
age |
Age of the current sensor at integration step |
measure |
Produces a measurement from the current and past model outputs. |
add_offset |
Ages the current sensor by |
Source code in src/py_replay_bg/sensors/sensor.py
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
ts = 5
instance-attribute
¶
t_offset = 0
instance-attribute
¶
max_lifetime = 1440 * 10
instance-attribute
¶
connected_at = 0
instance-attribute
¶
__init__()
¶
Constructs all the necessary attributes for the Sensor object.
Source code in src/py_replay_bg/sensors/sensor.py
connect_new(connected_at=0)
¶
Connects a fresh sensor by (re)sampling its error realization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connected_at
|
int
|
Integration step at which the sensor is connected. |
: 0
|
Source code in src/py_replay_bg/sensors/sensor.py
age(k)
¶
Age of the current sensor at integration step k.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Integration step, in the current replay's step frame. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The sensor's age in integration steps:
|
Source code in src/py_replay_bg/sensors/sensor.py
measure(value, past_values, t)
abstractmethod
¶
Produces a measurement from the current and past model outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The current model output. This is the value being measured. |
required |
past_values
|
ndarray
|
The series of past model outputs ( |
required |
t
|
float
|
The sensor's age, in days ( |
required |
Returns:
| Type | Description |
|---|---|
float
|
The measurement at the current time. |
Source code in src/py_replay_bg/sensors/sensor.py
add_offset(to_add)
¶
Ages the current sensor by to_add integration steps.
Only meaningful for a sensor carried into a replay with
continue_sensor=True (a fresh replay reconnects the sensor, which
resets t_offset). Use it, for example, to account for a gap between
two replay runs that share the sensor. Back-to-back runs need no offset:
replay() already rebases the sensor's age at the end of a run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_add
|
int
|
The number of integration steps to add to the sensor's age. |
required |
Source code in src/py_replay_bg/sensors/sensor.py
Vettoretti19CGM¶
Bases: Sensor
A CGM sensor based on the error model in Vettoretti et al., Sensors, 2019.
Concrete :class:~sensors.sensor.Sensor whose measurement is a continuous glucose
monitoring (CGM) reading of the model output (interstitial glucose). The error
model adds a time-varying calibration error plus colored noise generated by an
AR(2) process whose parameters are sampled per sensor connection.
...
Attributes:
| Name | Type | Description |
|---|---|---|
cgm_error_parameters |
ndarray
|
Array of the sampled CGM error-model parameters. |
output_noise_sd |
float
|
Output standard deviation of the CGM error model (mg/dl). |
ekm1 |
float
|
Memory term for the colored noise, e(k-1). |
ekm2 |
float
|
Memory term for the colored noise, e(k-2). |
Methods:
| Name | Description |
|---|---|
connect_new |
Connects a new CGM sensor by sampling new error parameters. |
measure |
Produces a CGM measurement of the model output. |
Source code in src/py_replay_bg/sensors/vettoretti19_cgm.py
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
cgm_error_parameters = []
instance-attribute
¶
output_noise_sd = []
instance-attribute
¶
ekm1 = None
instance-attribute
¶
ekm2 = None
instance-attribute
¶
__init__()
¶
Constructs the CGM sensor. The sample time ts (5) is inherited.
Source code in src/py_replay_bg/sensors/vettoretti19_cgm.py
connect_new(connected_at=0)
¶
Connects a new CGM sensor by sampling new error parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connected_at
|
int
|
Integration step at which the sensor is connected. |
: 0
|
Source code in src/py_replay_bg/sensors/vettoretti19_cgm.py
measure(value, past_values, t)
¶
Provides a CGM measure using the model of Vettoretti et al., Sensors, 2019.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The current interstitial glucose concentration (mg/dl). |
required |
past_values
|
ndarray
|
The series of past interstitial glucose concentrations (mg/dl). |
required |
t
|
float
|
The sensor's age, in days ( |
required |
Returns:
| Type | Description |
|---|---|
float
|
The CGM measurement at the current time (mg/dl). |