Skip to content

Commit 8f168c8

Browse files
authored
Merge pull request #46 from lcossu/snack_overrides
Custom rate of appearance class
2 parents ee9f35e + 848e961 commit 8f168c8

22 files changed

+308
-25
lines changed

docs/.vuepress/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export default defineUserConfig({
5050
text: 'The CGM error model',
5151
link: 'documentation/cgm_model.md'
5252
},
53+
{
54+
text: 'Custom Glucose Rate of Appearance Model',
55+
link: 'documentation/custom_ra.md'
56+
},
5357
{
5458
text: 'Twinning Procedure',
5559
link: 'documentation/twinning_procedure.md'

docs/documentation/choosing_blueprint.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ More details can be found in [The ReplayBG Object](./replaybg_object.md) page.
5353
The single-meal blueprint is composed of four main subsystems: subcutaneous insulin absorption, oral
5454
glucose absorption, glucose-insulin kinetics, CGM sensor error.
5555

56-
!["Single-meal blueprint"](https://i.postimg.cc/QtJ6zd2x/replaybg-single-meal.jpg "Single-meal blueprint")
56+
!["Single-meal blueprint"](images/single-meal.jpg "Single-meal blueprint")
5757

5858
#### Subcutaneous Insulin Absorption Subsystem
5959
The subsystem of subcutaneous insulin absorption system is composed of three compartments and describes the
@@ -192,7 +192,7 @@ More details can be found in [The ReplayBG object](./replaybg_object.md) page.
192192
The multi-meal blueprint is an expanded version of the single-meal one. It is composed of four main subsystems:
193193
subcutaneous insulin absorption, oral glucose absorption, glucose-insulin kinetics, CGM sensor error.
194194

195-
!["Multi-meal blueprint"](https://i.postimg.cc/fy3wKpPC/replaybg-multi-meal.jpg "Multi-meal blueprint")
195+
!["Multi-meal blueprint"](images/multi-meal.jpg "Multi-meal blueprint")
196196

197197

198198
#### Subcutaneous Insulin Absorption Subsystem

docs/documentation/custom_ra.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
sidebar: heading
3+
---
4+
5+
# Custom Rate of Appearance (Ra) Model
6+
7+
The Rate of Appearance (Ra) model is a crucial component in glucose metabolism simulations, representing the rate at which glucose enters the bloodstream after carbohydrate ingestion. In ReplayBG, users can implement custom Ra models to better simulate specific physiological responses.
8+
9+
## Model abstract class
10+
The Ra model is implemented as an abstract class `CustomRaBase`, which defines the interface for all Ra model implementations. This class can be extended by specific Ra model implementations.
11+
12+
The interface of the abstract class includes the following method:
13+
- `simulate_forcing_ra(self, time: np.ndarray, time_index: int) -> float:`: Simulates the Rate of Appearance at a given time index. It takes as input the time array and the current time index, returning the Ra value in mg/kg/min.
14+
- `get_events(self)-> np.ndarray`: Returns the array of events (e.g., meal times and quantities) that influence the Ra simulation.
15+
16+
::: warning
17+
The model shoud inherit from `CustomRaBase` and implement the required methods.
18+
:::
19+
20+
::: tip REMARK
21+
The Ra model should output values in **mg/kg/min** to ensure compatibility with the ReplayBG framework.
22+
All the logic of equations, states and parameters should be implemented within the custom class.
23+
:::
24+
25+
## How to set the Ra Model to use during replays
26+
The replay method of the `ReplayBG` class accepts a `custom_ra` parameter, which can be set to an instance of the custom Ra model to be used.
27+
28+
```python
29+
import MyCustomRaModel
30+
custom_ra = MyCustomRaModel(params)
31+
results = rbg.replay(..., custom_ra=custom_ra)
32+
```
33+
34+
## Example of custom Ra Model reproducing the snack model of ReplayBG
35+
36+
This example shows how to use a custom Ra model in a ReplayBG simulation. The example uses a simple Ra model that simulates a snack ingestion event.
37+
The full example is available in the `examples` folder of the ReplayBG repository.
38+
39+
```python
40+
from py_replay_bg.replay.custom_ra import CustomRaBase
41+
42+
class CustomRa(CustomRaBase):
43+
44+
def __init__(self, CHO, k_empt, k_abs, f=1.0, beta=0, bw=70.0):
45+
super().__init__()
46+
self.dt = 1.0 # time step in minutes
47+
self.CHO = CHO * 1000 / bw # np.array of CHO at each timestamp converted to mg/(kg*min)
48+
self.k_empt = k_empt
49+
self.k_abs = k_abs
50+
self.f = f
51+
self.beta = int(beta) # delay in number of time steps
52+
self.Q_sto1 = 0.0
53+
self.Q_sto2 = 0.0
54+
self.Q_gut = 0.0
55+
self.bw = bw # body weight in kg
56+
57+
def simulate_forcing_ra(self, time: np.ndarray, time_index: int) -> float:
58+
# Apply delay (beta) in indices
59+
cho_idx = (time_index - self.beta)
60+
CHO_input = self.CHO[cho_idx - 1] if cho_idx >= 0 else 0.0
61+
62+
dQ_sto1 = -self.k_empt * self.Q_sto1 + CHO_input
63+
dQ_sto2 = self.k_empt * self.Q_sto1 - self.k_empt * self.Q_sto2
64+
dQ_gut = self.k_empt * self.Q_sto2 - self.k_abs * self.Q_gut
65+
66+
self.Q_sto1 += dQ_sto1 * self.dt
67+
self.Q_sto2 += dQ_sto2 * self.dt
68+
self.Q_gut += dQ_gut * self.dt
69+
70+
Ra = self.f * self.k_abs * self.Q_gut
71+
return Ra
72+
73+
def get_events(self):
74+
return self.CHO
75+
```
132 KB
Loading
129 KB
Loading
758 KB
Loading
584 KB
Loading
1 MB
Loading
938 KB
Loading
609 KB
Loading

0 commit comments

Comments
 (0)