|
1 | 1 | # Example model |
2 | 2 |
|
3 | | -This python package provides a simple, standalone executable model. |
| 3 | +This python package provides a simple branching process model intended to be used with the `mrp` (model runner protocol) package. |
4 | 4 |
|
5 | | -To run the model within the `uv` environment: |
| 5 | +This README will describe multiple ways to run the model. |
| 6 | + |
| 7 | +First open the Python interactive shell within the `uv` environment: |
6 | 8 |
|
7 | 9 | ```bash |
8 | 10 | uv sync --all-packages |
9 | | -uv run python -m example_model.example_model packages/example_model/tests/model_input.json |
| 11 | +uv run python |
| 12 | +``` |
| 13 | +The following two lines of code are sufficient to run the example model using the static method (i.e., calling `Binom_BP_Model.simulate()`). |
| 14 | +```python |
| 15 | +from example_model import Binom_BP_Model |
| 16 | +Binom_BP_Model.simulate({"seed": 123, "max_gen": 15, "n": 3, "p": 0.5, "max_infect": 500}) |
10 | 17 | ``` |
| 18 | +This should yield the following output: `[1, 2, 1, 1, 1, 2, 4, 8, 15, 24, 28, 38, 58, 86, 126]` |
| 19 | + |
| 20 | +An equivalent approach is to read the model input from `defaults.json`. (The code here assumes working from the root of the repo.) |
11 | 21 |
|
12 | | -Which will write `model_output.json` with the results of the run. You |
13 | | -can specify a different location with the `-o` command line argument. |
| 22 | +```python |
| 23 | +import json |
| 24 | +from example_model import Binom_BP_Model |
| 25 | +model_inputs = json.load(open("./packages/example_model/defaults.json")) |
| 26 | +Binom_BP_Model.simulate(model_inputs) |
| 27 | +``` |
| 28 | +To use the MRP functionality, create an environment that specifies the inputs and use the `.run()` method: |
| 29 | +```python |
| 30 | +from mrp import Environment |
| 31 | +from example_model import Binom_BP_Model |
| 32 | +model_inputs = {"max_gen": 15, "n": 3, "p": 0.5, "max_infect": 500} |
| 33 | +env = Environment({"input": model_inputs}) |
| 34 | +Binom_BP_Model(env).run() |
| 35 | +``` |
| 36 | +The above examples are very similar to those included in `scripts/direct_runner.py`, which can be run (from the root of the repo) with the following: |
| 37 | +```bash |
| 38 | +uv sync --all-packages |
| 39 | +uv run python scripts/direct_runner.py |
| 40 | +``` |
| 41 | +Additionally, as described in the repo-level README, the model can be run as specified in the `example_model.mrp.toml`, which can be run as follows: |
| 42 | +```bash |
| 43 | +uv sync --all-packages |
| 44 | +uv run mrp run example_model.mrp.toml |
| 45 | +``` |
0 commit comments