|
| 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 | +``` |
0 commit comments