Utils¶
Helper utilities: typed-dict conversion, saving/loading, AGATA analysis and the plotting functions.
Typed-dict helpers¶
Convert a mapping to a Numba typed dictionary with float64 values.
This helper normalizes a Python dict-like object into a
numba.typed.Dict with keys of type unicode_type and values of type
float64. It is useful when passing parameter dictionaries into Numba-
compiled code or APIs that expect typed containers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
mapping
|
A mapping or mapping-like object to convert. If |
: None
|
strict
|
bool
|
If |
: False
|
Returns:
| Type | Description |
|---|---|
Dict
|
A typed dictionary with |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/py_replay_bg/utils/numba_dicts.py
Saving and loading results¶
Pickles results to <path>/<prefix>_<name>.pkl, creating path if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict
|
The dictionary of objects to persist. |
required |
path
|
str
|
Directory where the file will be written. Created if missing. |
required |
name
|
str or None
|
Base file name (with or without the |
required |
prefix
|
str
|
Prefix always prepended to the file name as |
required |
Returns:
| Type | Description |
|---|---|
str
|
The full path of the file written. |
Source code in src/py_replay_bg/utils/save_results.py
Loads results previously pickled by :func:save_results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Directory where the file lives. |
required |
name
|
str
|
File name (with or without the |
required |
prefix
|
str
|
Prefix used when the file was saved (e.g. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The unpickled results dictionary. |
Source code in src/py_replay_bg/utils/load_results.py
AGATA analysis¶
Analyze the outcome of a :meth:ReplayBG.twin run with AGATA.
Simulates the model forward with the estimated parameters to obtain the fitted (predicted) glucose trace, then:
- runs :meth:
py_agata.Agata.analyze_glucose_profileon that trace, and - computes the comprehensive
py_agata.errormetric set (rmse,mard,cod, Clarke EGA zones,g_rmse) against the reference CGM that was fitted (rbg_data.y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
twin_results
|
dict
|
The dict returned by :meth: |
required |
rbg_data
|
object
|
The data object passed to :meth: |
required |
model
|
object
|
A model instance implementing |
required |
integration_ts
|
int
|
Minutes represented by one integration step ( |
: 1
|
glycemic_target
|
str
|
Glycemic target set forwarded to :class: |
: "diabetes"
|
verbose
|
bool
|
When |
: False
|
path
|
str or None
|
When given, the analysis is added under the |
: None
|
save_name
|
str or None
|
File name for the re-saved pickle. |
: None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict with keys |
Source code in src/py_replay_bg/utils/agata_analysis.py
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
Analyze the glucose traces of a :meth:ReplayBG.replay run with AGATA.
Runs :meth:py_agata.Agata.analyze_glucose_profile on the replayed model
output and, when the replay used a sensor, also on the sensor
measurement trace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replay_results
|
dict
|
The dict returned by :meth: |
required |
ts
|
int
|
AGATA glucose sampling cadence in minutes. The |
: 5
|
integration_ts
|
int
|
Minutes represented by one integration step ( |
: 1
|
glycemic_target
|
str
|
Glycemic target set forwarded to :class: |
: "diabetes"
|
verbose
|
bool
|
When |
: False
|
path
|
str or None
|
When given, the analysis is added to |
: None
|
save_name
|
str or None
|
File name for the re-saved pickle (see
:func: |
: None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict with key |
Source code in src/py_replay_bg/utils/agata_analysis.py
Plotting¶
Plot the outcome of a ReplayBG.twin() run.
Twinning fits the model parameters to the observed data; this utility shows
how well the fitted model reproduces them. The model is simulated forward
with the estimated theta (the same reset/step/output pass the
twinner uses for the likelihood), and the resulting trace is compared against
the observations that were fitted.
The figure follows the same rationale as :func:plot_replay: a vertical
stack of subplots sharing a common time axis.
- Fit (first subplot): the simulated model
outputfor the estimatedthetaas a continuous line and the observed data (rbg_data.yat the non-missing indicesrbg_data.y_idxs) as markers. Each value inthresholdsis drawn as a dashed horizontal line spanning the whole width, acting as a visual reference level, and the span between the outermost levels is shaded. - Inputs (one subplot per input group): the inputs that drove the
fit, labelled via
data_to_input. By default every channel gets its own subplot; passinput_groupsto overlay channels together.
The utility is model-agnostic: nothing about the meaning of the output or
inputs is assumed. Channel names come straight from data_to_input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rbg_data
|
object
|
The data object passed to :meth: |
required |
model
|
object
|
A model instance implementing the model interface contract
( |
required |
theta
|
dict or None
|
The estimated parameters as returned in |
: None
|
thresholds
|
list of float or None
|
Optional reference levels drawn as dashed horizontal lines on the fit subplot. Two or more levels also shade the span between the outermost. |
: None
|
input_groups
|
list of list of int or None
|
Optional list of channel-index groups. Each group becomes one subplot
with its channels overlaid. |
: None
|
mask_inputs
|
list of int or None
|
Optional list of channel indices to hide. Masked channels are dropped
from the (default or explicit) |
: None
|
ts_min
|
float
|
Minutes represented by one integration step, used to scale the time axis. |
: 1.0
|
output_label
|
str
|
Legend label for the simulated fit line. |
: "Fit"
|
observation_label
|
str
|
Legend label for the observed-data markers. |
: "Data"
|
figsize
|
tuple of float or None
|
Optional figure size. Defaults to a height that grows with the number of subplots. |
: None
|
hover
|
bool
|
Whether to attach the interactive readout: hovering any subplot drops a vertical cursor across the whole stack and reports the values at that minute. Ignored on non-interactive backends. |
: True
|
Returns:
| Type | Description |
|---|---|
Figure
|
The assembled figure. |
Source code in src/py_replay_bg/utils/plot_twinning.py
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
Plot several x0-chained twin() segments as one continuous figure.
The "interval" workflow fits one day, carries its final state into the next
(via x0/theta_prev), and repeats. Each day is a separate twin over
its own rbg_data and fitted theta; this utility glues them back
together — laying the segments end to end on a single time axis — so the
multi-day fit reads as one trace. It is the multi-segment counterpart of
:func:plot_twinning and produces the same panel layout:
- Fit (first subplot): every segment's simulated
outputdrawn as one continuous line, with the observed data of all segments as markers. - Inputs (one subplot per input group): the glued input channels.
A dotted vertical separator marks each join between consecutive days.
Each segment must expose the same input-channel layout (data_to_input);
the layout and the observation cadence (yts) are taken from the first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segments
|
list of dict
|
The per-day segments in chronological order. Each is a mapping with keys
|
required |
thresholds
|
list of float or None
|
Optional reference levels drawn as dashed horizontal lines on the fit subplot. Two or more levels also shade the span between the outermost. |
: None
|
input_groups
|
list of list of int or None
|
Optional list of channel-index groups. Each group becomes one subplot
with its channels overlaid. |
: None
|
mask_inputs
|
list of int or None
|
Optional list of channel indices to hide, e.g. the |
: None
|
ts_min
|
float
|
Minutes represented by one integration step, used to scale the time axis. |
: 1.0
|
output_label
|
str
|
Legend label for the simulated fit line. |
: "Fit"
|
observation_label
|
str
|
Legend label for the observed-data markers. |
: "Data"
|
figsize
|
tuple of float or None
|
Optional figure size. Defaults to a height that grows with the number of subplots. |
: None
|
hover
|
bool
|
Whether to attach the interactive readout. Ignored on non-interactive backends. |
: True
|
Returns:
| Type | Description |
|---|---|
Figure
|
The assembled figure. |
Source code in src/py_replay_bg/utils/plot_twinning.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
Plot the outcome of a ReplayBG.replay() run.
The figure is a vertical stack of subplots sharing a common time axis:
- Output (first subplot): the replayed
outputas a continuous line and, when a sensor was used, the replayedmeasurementas markers. Each value inthresholdsis drawn as a dashed horizontal line spanning the whole width, acting as a visual reference level, and the span between the outermost levels is shaded. - Inputs (one subplot per input group): the applied
inputchannels, labelled viadata_to_input. By default every channel gets its own subplot; passinput_groupsto overlay channels together. - Callback actions (one subplot per distinct callback): the values a callback logged, stemmed at the integration minute of each action.
The utility is model-agnostic: nothing about the meaning of the output,
inputs, or actions is assumed. Channel names come straight from
data_to_input and callback labels from the action log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replay_results
|
dict
|
The dict returned by :meth: |
required |
thresholds
|
list of float or None
|
Optional reference levels drawn as dashed horizontal lines on the output subplot. Two or more levels also shade the span between the outermost. |
: None
|
input_groups
|
list of list of int or None
|
Optional list of channel-index groups. Each group becomes one subplot
with its channels overlaid. |
: None
|
mask_inputs
|
list of int or None
|
Optional list of channel indices to hide. Masked channels are dropped
from the (default or explicit) |
: None
|
action_field
|
str or None
|
Name of the scalar field to plot for each callback action. |
: None
|
ts_min
|
float
|
Minutes represented by one integration step, used to scale the time axis. |
: 1.0
|
output_label
|
str
|
Legend label for the replayed output line. |
: "Output"
|
measurement_label
|
str
|
Legend label for the replayed measurement markers. |
: "Measurement"
|
figsize
|
tuple of float or None
|
Optional figure size. Defaults to a height that grows with the number of subplots. |
: None
|
hover
|
bool
|
Whether to attach the interactive readout: hovering any subplot drops a vertical cursor across the whole stack and reports the values at that minute. Ignored on non-interactive backends. |
: True
|
Returns:
| Type | Description |
|---|---|
Figure
|
The assembled figure. |
Source code in src/py_replay_bg/utils/plot_replay.py
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 | |
Plot several x0-chained replay() segments as one continuous figure.
The "interval" workflow replays one day, carries its final state into the
next (via x0/theta_prev), and repeats. Each day is a separate replay;
this utility glues them back together — laying the segments end to end on a
single time axis — so the multi-day replay reads as one trace. It is the
multi-segment counterpart of :func:plot_replay and produces the same panel
layout (output, inputs, one panel per distinct callback), with a dotted
vertical separator marking each join between consecutive days.
Each segment must expose the same input-channel layout (data_to_input),
taken from the first. Measurement markers are shown only when every segment
carries a sensor measurement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replay_results_list
|
list of dict
|
The per-day dicts returned by :meth: |
required |
thresholds
|
list of float or None
|
Optional reference levels drawn as dashed horizontal lines on the output subplot. Two or more levels also shade the span between the outermost. |
: None
|
input_groups
|
list of list of int or None
|
Optional list of channel-index groups, each becoming one overlaid
subplot. |
: None
|
mask_inputs
|
list of int or None
|
Optional list of channel indices to hide, e.g. the |
: None
|
action_field
|
str or None
|
Name of the scalar field to plot for each callback action, as in
:func: |
: None
|
ts_min
|
float
|
Minutes represented by one integration step, used to scale the time axis. |
: 1.0
|
output_label
|
str
|
Legend label for the replayed output line. |
: "Output"
|
measurement_label
|
str
|
Legend label for the replayed measurement markers. |
: "Measurement"
|
figsize
|
tuple of float or None
|
Optional figure size. Defaults to a height that grows with the number of subplots. |
: None
|
hover
|
bool
|
Whether to attach the interactive readout. Ignored on non-interactive backends. |
: True
|
Returns:
| Type | Description |
|---|---|
Figure
|
The assembled figure. |
Source code in src/py_replay_bg/utils/plot_replay.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
Plots the optimization history from a twinning run.
The figure follows the same rationale as :func:plot_replay and
:func:plot_twinning: a vertical stack of subplots sharing a common x axis,
here the function evaluation rather than time.
- Parameter trajectory: each unknown parameter's value per evaluation.
- Log-prior, log-likelihood, log-posterior: the three terms the twinner is optimising, one per subplot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
history
|
dict
|
Dict with keys |
required |
param_names
|
list of str or None
|
Optional list of parameter names for the theta legend. Defaults to
|
: None
|
figsize
|
tuple of float or None
|
Optional figure size. Defaults to a height that grows with the number of subplots. |
: None
|
hover
|
bool
|
Whether to attach the interactive readout: hovering any subplot drops a vertical cursor across the whole stack and reports the values at that evaluation. Ignored on non-interactive backends. |
: True
|
Returns:
| Type | Description |
|---|---|
Figure
|
The assembled figure. |