Model¶
The physiological ODE models (Numba @jitclass). Each is one half of a
blueprint.
SingleMealT1DModel¶
A physiological model of glucose-insulin dynamics for a single-meal T1D scenario.
The model is a system of ODEs integrated with Backward Euler at a one-minute
resolution. It chains a meal gut-absorption sub-system, a subcutaneous insulin
pharmacokinetic sub-system, an insulin-action compartment and a glucose
sub-system, and exposes the interstitial glucose as the observable output.
The class is compiled with Numba's @jitclass; all parameters are passed
in through Numba typed dicts.
...
Attributes:
| Name | Type | Description |
|---|---|---|
u2ss |
float
|
Steady-state basal insulin (the basal insulin rate at equilibrium). |
f |
float
|
Fraction of intestinal absorption that appears in plasma. |
VG |
float
|
Glucose distribution volume. |
VI |
float
|
Insulin distribution volume. |
alpha |
int
|
Rate governing the lag between plasma and interstitial glucose. |
SI |
float
|
Insulin sensitivity. |
SG |
float
|
Glucose effectiveness. |
Gb |
float
|
Basal (target) glucose concentration. |
p2 |
float
|
Rate constant of the remote insulin (action) compartment. |
r1, r2 |
float
|
Parameters of the non-symmetric hypoglycaemia risk function. |
ka2, kd, ke |
float
|
Insulin pharmacokinetic rate constants. |
tau |
int
|
Integer delay (minutes) applied to the subcutaneous insulin input. |
kabs |
float
|
Meal gut-absorption rate constant. |
kempt |
float
|
Gastric emptying rate constant. |
beta |
int
|
Meal announcement delay (integer minutes). |
Ipb |
float
|
Basal plasma insulin derived from |
G, X, Qsto1, Qsto2, Qgut, Isc1, Isc2, Ip, IG |
ndarray
|
State trajectories (one entry per integration step) for plasma glucose, insulin action, the two gastric and the gut compartments, the two subcutaneous insulin compartments, plasma insulin and interstitial glucose. |
theta0 |
Dict
|
The initial parameter dict the model was constructed with. |
theta_prev |
Dict
|
Parameters of the previous segment, used to scale carry-over initial conditions across segments. |
x0 |
Dict
|
Initial-condition dict (carry-over state from a previous segment). |
tsteps |
int
|
Number of integration steps. |
n_u |
int
|
Number of input channels (5: meal, bolus, basal, forcing_ip, forcing_ra). |
u |
ndarray
|
Input history buffer used to retrieve delayed inputs during |
previous_ra |
ndarray
|
Rate of appearance carried over from the previous segment. |
Methods:
| Name | Description |
|---|---|
reset |
Resets parameters and re-initialises the state arrays. |
step |
Advances the model by one minute. |
output |
Returns the model output (interstitial glucose) at step |
get_final_x0 |
Returns the end-of-segment state as a typed dict. |
get_theta |
Returns the current parameters as a typed dict. |
Source code in src/py_replay_bg/model/single_meal_t1d.py
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 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 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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | |
theta0 = theta0
instance-attribute
¶
x0 = x0
instance-attribute
¶
tsteps = np.int16(tsteps)
instance-attribute
¶
n_u = np.int16(5)
instance-attribute
¶
u2ss = np.float64(u2ss)
instance-attribute
¶
theta_prev = theta_prev
instance-attribute
¶
__init__(u2ss, theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64), x0=Dict.empty(key_type=(types.unicode_type), value_type=float64), tsteps=1440, theta_prev=Dict.empty(key_type=(types.unicode_type), value_type=float64))
¶
Constructs the model and allocates the state arrays.
Construction never has real data to seed the cold start from — that
only becomes available once the model is handed to replay(),
twin() or similar, which re-seed it via reset()'s g0/u0
(see reset). So this always starts from the steady state (or
x0), never from g0/u0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u2ss
|
float
|
Steady-state basal insulin (the basal insulin rate at equilibrium). |
required |
theta0
|
Dict
|
Initial model parameters. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
x0
|
Dict
|
Initial conditions (carry-over state). Missing entries fall back to the steady state. |
empty(key_type=unicode_type, value_type=float64)
|
tsteps
|
int
|
Number of integration steps to allocate. |
: 1440
|
theta_prev
|
Dict
|
Parameters of the previous segment, used to scale the carry-over initial conditions. Empty for a cold start. |
empty(key_type=unicode_type, value_type=float64)
|
Source code in src/py_replay_bg/model/single_meal_t1d.py
reset(theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64), g0=np.nan, u0=np.empty(0, dtype=(np.float64)))
¶
Resets all model parameters and re-initialises the state arrays.
Parameters are set to those provided in theta0, or to default values
when not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta0
|
Dict
|
The model parameters to set. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
g0
|
float
|
The first observed glucose value for the segment being simulated.
Pass it whenever real data backs this reset, for example every
candidate-parameter reset in a twinning optimisation loop, or a
one-off reset before replaying/plotting against recorded data.
When given, it seeds both |
: numpy.nan
|
u0
|
ndarray
|
The full t=0 input row (length |
: empty array
|
Source code in src/py_replay_bg/model/single_meal_t1d.py
_reset_theta(theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64))
¶
Sets the model parameters from theta0, falling back to defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta0
|
Dict
|
The model parameters to set. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
Source code in src/py_replay_bg/model/single_meal_t1d.py
_reset_x0(g0=np.nan)
¶
Initialises the state arrays from x0, falling back to the steady state.
Carry-over insulin and insulin-action initial conditions are rescaled by
the ratio between the current and previous segment parameters when
theta_prev is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g0
|
float
|
First observed glucose value to seed |
: numpy.nan
|
Source code in src/py_replay_bg/model/single_meal_t1d.py
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
_reset_u(u0=np.empty(0, dtype=(np.float64)))
¶
Allocates the input history buffer and seeds the t=0 input row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u0
|
ndarray
|
The real t=0 input row (length |
: empty array
|
Source code in src/py_replay_bg/model/single_meal_t1d.py
step(u, t)
¶
Advances the model by one minute using Backward Euler integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
ndarray
|
The input vector for this step (5 channels: meal, bolus, basal, forcing_ip, forcing_ra). |
required |
t
|
int
|
The integration step index (minute) to compute. |
required |
Source code in src/py_replay_bg/model/single_meal_t1d.py
output(t)
¶
Returns the model output at time step t.
The observable output is the interstitial glucose concentration, which corresponds to the signal measured by a continuous glucose monitor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
int
|
Time step index (integer minute). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interstitial glucose concentration at time |
Source code in src/py_replay_bg/model/single_meal_t1d.py
get_final_x0()
¶
Returns the final state of the model as a Numba typed dict.
Reads the last time-step of every state array and packs the values
into a typed dict suitable for passing as x0 to a subsequent
segment's reset() call.
Returns:
| Type | Description |
|---|---|
Dict
|
A typed dict (unicode -> float64) with keys matching the |
Source code in src/py_replay_bg/model/single_meal_t1d.py
get_theta()
¶
Returns the current model parameters as a Numba typed dict.
Packs all parameters set by :meth:_reset_theta into a typed dict
suitable for passing as theta_prev to a subsequent segment's
__init__ call.
Returns:
| Type | Description |
|---|---|
Dict
|
A typed dict (unicode -> float64) with keys matching the
|
Source code in src/py_replay_bg/model/single_meal_t1d.py
_override_stomach()
¶
Converts carry-over meal compartments into a forcing rate of appearance.
Evolves the previous segment's meal gut compartments forward (with no new
meal input) under the previous parameters, accumulates the resulting rate
of appearance into previous_ra, and zeroes the meal entries of x0
so the carry-over is handled entirely through the forcing term.
Source code in src/py_replay_bg/model/single_meal_t1d.py
MultiMealT1DModel¶
A physiological model of glucose-insulin dynamics for a multi-meal T1D scenario.
The model is a system of ODEs integrated with Backward Euler at a one-minute
resolution. Compared with the single-meal variant it keeps a separate gut
sub-system per meal label (breakfast B, lunch L, dinner D, snack
S, hypo-treatment H) and uses a time-of-day-dependent insulin
sensitivity (SI_B, SI_L, SI_D). The interstitial glucose is the
observable output. The class is compiled with Numba's @jitclass; all
parameters are passed in through Numba typed dicts.
...
Attributes:
| Name | Type | Description |
|---|---|---|
u2ss |
float
|
Steady-state basal insulin (the basal insulin rate at equilibrium). |
f |
float
|
Fraction of intestinal absorption that appears in plasma. |
VG |
float
|
Glucose distribution volume. |
VI |
float
|
Insulin distribution volume. |
alpha |
int
|
Rate governing the lag between plasma and interstitial glucose. |
SI_B, SI_L, SI_D |
float
|
Insulin sensitivity for the breakfast, lunch and dinner/night windows. |
SG |
float
|
Glucose effectiveness. |
Gb |
float
|
Basal (target) glucose concentration. |
p2 |
float
|
Rate constant of the remote insulin (action) compartment. |
r1, r2 |
float
|
Parameters of the non-symmetric hypoglycaemia risk function. |
ka2, kd, ke |
float
|
Insulin pharmacokinetic rate constants. |
tau |
int
|
Integer delay (minutes) applied to the subcutaneous insulin input. |
kabs_B, kabs_L, kabs_D, kabs_S, kabs_H |
float
|
Per-meal gut-absorption rate constants. |
kempt |
float
|
Gastric emptying rate constant shared across meal slots. |
beta_B, beta_L, beta_D, beta_S |
int
|
Per-meal announcement delays (integer minutes). |
Ipb |
float
|
Basal plasma insulin derived from |
G, X, Isc1, Isc2, Ip, IG |
ndarray
|
State trajectories for plasma glucose, insulin action, the two subcutaneous insulin compartments, plasma insulin and interstitial glucose. |
Qsto1_*, Qsto2_*, Qgut_* |
ndarray
|
Per-meal gastric (1, 2) and gut compartment trajectories, one set per meal label. |
theta0 |
Dict
|
The initial parameter dict the model was constructed with. |
theta_prev |
Dict
|
Parameters of the previous segment, used to scale carry-over initial conditions across segments. |
x0 |
Dict
|
Initial-condition dict (carry-over state from a previous segment). |
tsteps |
int
|
Number of integration steps. |
t_start |
int
|
Minute-of-day at which this segment starts (used when chaining segments;
the per-step SI is selected from the |
n_u |
int
|
Number of input channels (10). |
u |
ndarray
|
Input history buffer used to retrieve delayed inputs during |
previous_ra |
ndarray
|
Rate of appearance carried over from the previous segment. |
Methods:
| Name | Description |
|---|---|
reset |
Resets parameters and re-initialises the state arrays. |
step |
Advances the model by one minute. |
output |
Returns the model output (interstitial glucose) at step |
get_final_x0 |
Returns the end-of-segment state as a typed dict. |
get_theta |
Returns the current parameters as a typed dict. |
Source code in src/py_replay_bg/model/multi_meal_t1d.py
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 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 | |
theta0 = theta0
instance-attribute
¶
x0 = x0
instance-attribute
¶
tsteps = np.int16(tsteps)
instance-attribute
¶
t_start = np.int16(t_start)
instance-attribute
¶
n_u = np.int16(10)
instance-attribute
¶
u2ss = u2ss
instance-attribute
¶
theta_prev = theta_prev
instance-attribute
¶
__init__(u2ss, theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64), x0=Dict.empty(key_type=(types.unicode_type), value_type=float64), tsteps=1440, t_start=240, theta_prev=Dict.empty(key_type=(types.unicode_type), value_type=float64))
¶
Constructs the model and allocates the state arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u2ss
|
float
|
Steady-state basal insulin (the basal insulin rate at equilibrium). |
required |
theta0
|
Dict
|
Initial model parameters. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
x0
|
Dict
|
Initial conditions (carry-over state). Missing entries fall back to the steady state. |
empty(key_type=unicode_type, value_type=float64)
|
tsteps
|
int
|
Number of integration steps to allocate. |
: 1440
|
t_start
|
int
|
Minute-of-day at which this segment starts, driving the time-of-day insulin sensitivity selection. |
: 240
|
theta_prev
|
Dict
|
Parameters of the previous segment, used to scale the carry-over initial conditions. Empty for a cold start. |
empty(key_type=unicode_type, value_type=float64)
|
Source code in src/py_replay_bg/model/multi_meal_t1d.py
reset(theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64), g0=np.nan, u0=np.empty(0, dtype=(np.float64)))
¶
Resets all model parameters and re-initialises the state arrays.
Parameters are set to those provided in theta0, or to default values
when not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta0
|
Dict
|
The model parameters to set. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
g0
|
float
|
The first observed glucose value for the segment being simulated.
Pass it whenever real data backs this reset, for example every
candidate-parameter reset in a twinning optimisation loop, or a
one-off reset before replaying/plotting against recorded data.
When given, it seeds both |
: numpy.nan
|
u0
|
ndarray
|
The full t=0 input row (length |
: empty array
|
Source code in src/py_replay_bg/model/multi_meal_t1d.py
_reset_theta(theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64))
¶
Sets the model parameters from theta0, falling back to defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta0
|
Dict
|
The model parameters to set. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
Source code in src/py_replay_bg/model/multi_meal_t1d.py
_reset_x0(g0=np.nan)
¶
Initialises the state arrays from x0, falling back to the steady state.
Carry-over insulin and insulin-action initial conditions are rescaled by
the ratio between the current and previous segment parameters when
theta_prev is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g0
|
float
|
First observed glucose value to seed |
: numpy.nan
|
Source code in src/py_replay_bg/model/multi_meal_t1d.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
_reset_u(u0=np.empty(0, dtype=(np.float64)))
¶
Allocates the input history buffer and seeds the t=0 input row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u0
|
ndarray
|
The real t=0 input row (length |
: empty array
|
Source code in src/py_replay_bg/model/multi_meal_t1d.py
step(u, t)
¶
Advances the model by one minute using Backward Euler integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
ndarray
|
The input vector for this step (10 channels: the five meal labels B/L/D/S/H, bolus, basal, hour-of-day, forcing_ip and forcing_ra). |
required |
t
|
int
|
The integration step index (minute) to compute. |
required |
Source code in src/py_replay_bg/model/multi_meal_t1d.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | |
output(t)
¶
Returns the model output at time step t.
The observable output is the interstitial glucose concentration, which corresponds to the signal measured by a continuous glucose monitor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
int
|
Time step index (integer minute). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interstitial glucose concentration at time |
Source code in src/py_replay_bg/model/multi_meal_t1d.py
get_final_x0()
¶
Returns the final state of the model as a Numba typed dict.
Reads the last time-step of every state array and packs the values
into a typed dict suitable for passing as x0 to a subsequent
segment's reset() call.
Returns:
| Type | Description |
|---|---|
Dict
|
A typed dict (unicode -> float64) with keys matching the |
Source code in src/py_replay_bg/model/multi_meal_t1d.py
get_theta()
¶
Returns the current model parameters as a Numba typed dict.
Packs all parameters set by :meth:_reset_theta into a typed dict
suitable for passing as theta0 to a subsequent reset() call.
Returns:
| Type | Description |
|---|---|
Dict
|
A typed dict (unicode -> float64) with keys matching the
|
Source code in src/py_replay_bg/model/multi_meal_t1d.py
_override_stomach()
¶
Converts carry-over meal compartments into a forcing rate of appearance.
Evolves the previous segment's per-meal gut compartments forward (with no
new meal input) under the previous parameters, accumulates the resulting
rate of appearance into previous_ra, and zeroes the meal entries of
x0 so the carry-over is handled entirely through the forcing term.
Source code in src/py_replay_bg/model/multi_meal_t1d.py
MultiMealExtendedT1DModel¶
An extended multi-meal physiological model of glucose-insulin dynamics for T1D.
The model is a system of ODEs integrated with Backward Euler at a one-minute
resolution. It extends :class:~model.multi_meal_t1d.MultiMealT1DModel with
second-occurrence meal slots (B2, L2, S2) and a second breakfast
insulin sensitivity SI_B2 so that simulations spanning more than one day
can be twinned in a single segment. The interstitial glucose is the
observable output. The class is compiled with Numba's @jitclass; all
parameters are passed in through Numba typed dicts.
...
Attributes:
| Name | Type | Description |
|---|---|---|
u2ss |
float
|
Steady-state basal insulin (the basal insulin rate at equilibrium). |
f |
float
|
Fraction of intestinal absorption that appears in plasma. |
VG |
float
|
Glucose distribution volume. |
VI |
float
|
Insulin distribution volume. |
alpha |
int
|
Rate governing the lag between plasma and interstitial glucose. |
SI_B, SI_L, SI_D, SI_B2 |
float
|
Insulin sensitivity for the breakfast, lunch, dinner/night and second-day breakfast windows. |
SG |
float
|
Glucose effectiveness. |
Gb |
float
|
Basal (target) glucose concentration. |
p2 |
float
|
Rate constant of the remote insulin (action) compartment. |
r1, r2 |
float
|
Parameters of the non-symmetric hypoglycaemia risk function. |
ka2, kd, ke |
float
|
Insulin pharmacokinetic rate constants. |
tau |
int
|
Integer delay (minutes) applied to the subcutaneous insulin input. |
kabs_B, kabs_L, kabs_D, kabs_S, kabs_H, kabs_B2, kabs_L2, kabs_S2 |
float
|
Per-meal gut-absorption rate constants (including second occurrences). |
kempt |
float
|
Gastric emptying rate constant shared across meal slots. |
beta_B, beta_L, beta_D, beta_S, beta_B2, beta_L2, beta_S2 |
int
|
Per-meal announcement delays (integer minutes). |
Ipb |
float
|
Basal plasma insulin derived from |
G, X, Isc1, Isc2, Ip, IG |
ndarray
|
State trajectories for plasma glucose, insulin action, the two subcutaneous insulin compartments, plasma insulin and interstitial glucose. |
Qsto1_*, Qsto2_*, Qgut_* |
ndarray
|
Per-meal gastric (1, 2) and gut compartment trajectories, one set per meal label (including second occurrences). |
theta0 |
Dict
|
The initial parameter dict the model was constructed with. |
theta_prev |
Dict
|
Parameters of the previous segment, used to scale carry-over initial conditions across segments. |
x0 |
Dict
|
Initial-condition dict (carry-over state from a previous segment). |
tsteps |
int
|
Number of integration steps. |
t_start |
int
|
Minute-of-day at which this segment starts (used to switch to |
n_u |
int
|
Number of input channels (13). |
u |
ndarray
|
Input history buffer used to retrieve delayed inputs during |
previous_ra |
ndarray
|
Rate of appearance carried over from the previous segment. |
Methods:
| Name | Description |
|---|---|
reset |
Resets parameters and re-initialises the state arrays. |
step |
Advances the model by one minute. |
output |
Returns the model output (interstitial glucose) at step |
get_final_x0 |
Returns the end-of-segment state as a typed dict. |
get_theta |
Returns the current parameters as a typed dict. |
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
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 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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 | |
theta0 = theta0
instance-attribute
¶
x0 = x0
instance-attribute
¶
tsteps = np.int16(tsteps)
instance-attribute
¶
t_start = np.int16(t_start)
instance-attribute
¶
n_u = np.int16(13)
instance-attribute
¶
u2ss = u2ss
instance-attribute
¶
theta_prev = theta_prev
instance-attribute
¶
__init__(u2ss, theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64), x0=Dict.empty(key_type=(types.unicode_type), value_type=float64), tsteps=1440, t_start=240, theta_prev=Dict.empty(key_type=(types.unicode_type), value_type=float64))
¶
Constructs the model and allocates the state arrays.
Construction never has real data to seed the cold start from — that
only becomes available once the model is handed to replay(),
twin() or similar, which re-seed it via reset()'s g0/u0
(see reset). So this always starts from the steady state (or
x0), never from g0/u0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u2ss
|
float
|
Steady-state basal insulin (the basal insulin rate at equilibrium). |
required |
theta0
|
Dict
|
Initial model parameters. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
x0
|
Dict
|
Initial conditions (carry-over state). Missing entries fall back to the steady state. |
empty(key_type=unicode_type, value_type=float64)
|
tsteps
|
int
|
Number of integration steps to allocate. |
: 1440
|
t_start
|
int
|
Minute-of-day at which this segment starts, driving the time-of-day insulin sensitivity selection. |
: 240
|
theta_prev
|
Dict
|
Parameters of the previous segment, used to scale the carry-over initial conditions. Empty for a cold start. |
empty(key_type=unicode_type, value_type=float64)
|
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
reset(theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64), g0=np.nan, u0=np.empty(0, dtype=(np.float64)))
¶
Resets all model parameters and re-initialises the state arrays.
Parameters are set to those provided in theta0, or to default values
when not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta0
|
Dict
|
The model parameters to set. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
g0
|
float
|
The first observed glucose value for the segment being simulated.
Pass it whenever real data backs this reset, for example every
candidate-parameter reset in a twinning optimisation loop, or a
one-off reset before replaying/plotting against recorded data.
When given, it seeds both |
: numpy.nan
|
u0
|
ndarray
|
The full t=0 input row (length |
: empty array
|
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
_reset_theta(theta0=Dict.empty(key_type=(types.unicode_type), value_type=float64))
¶
Sets the model parameters from theta0, falling back to defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta0
|
Dict
|
The model parameters to set. Missing entries fall back to default values. |
empty(key_type=unicode_type, value_type=float64)
|
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
_reset_x0(g0=np.nan)
¶
Initialises the state arrays from x0, falling back to the steady state.
Carry-over insulin and insulin-action initial conditions are rescaled by
the ratio between the current and previous segment parameters when
theta_prev is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g0
|
float
|
First observed glucose value to seed |
: numpy.nan
|
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
_reset_u(u0=np.empty(0, dtype=(np.float64)))
¶
Allocates the input history buffer and seeds the t=0 input row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u0
|
ndarray
|
The real t=0 input row (length |
: empty array
|
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
step(u, t)
¶
Advances the model by one minute using Backward Euler integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
ndarray
|
The input vector for this step (13 channels: the eight meal slots B/L/D/S/H/B2/L2/S2, bolus, basal, hour-of-day, forcing_ip and forcing_ra). |
required |
t
|
int
|
The integration step index (minute) to compute. |
required |
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | |
output(t)
¶
Returns the model output at time step t.
The observable output is the interstitial glucose concentration, which corresponds to the signal measured by a continuous glucose monitor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
int
|
Time step index (integer minute). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interstitial glucose concentration at time |
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
get_final_x0()
¶
Returns the final state of the model as a Numba typed dict.
Reads the last time-step of every state array and packs the values
into a typed dict suitable for passing as x0 to a subsequent
segment's reset() call.
Returns:
| Type | Description |
|---|---|
Dict
|
A typed dict (unicode -> float64) with keys matching the |
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
get_theta()
¶
Returns the current model parameters as a Numba typed dict.
Packs all parameters set by :meth:_reset_theta into a typed dict
suitable for passing as theta0 to a subsequent reset() call.
Returns:
| Type | Description |
|---|---|
Dict
|
A typed dict (unicode -> float64) with keys matching the
|
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
_override_stomach()
¶
Converts carry-over meal compartments into a forcing rate of appearance.
Evolves the previous segment's per-meal gut compartments forward (with no
new meal input) under the previous parameters, accumulates the resulting
rate of appearance into previous_ra, and zeroes the meal entries of
x0 so the carry-over is handled entirely through the forcing term.
Source code in src/py_replay_bg/model/multi_meal_extended_t1d.py
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 | |