Skip to content

Commit

Permalink
[readme] add graph to example
Browse files Browse the repository at this point in the history
Added a graph to the example in the `qmd` file. Sadly, this can't be
displayed in the README (only in editor or in generated HTML).
  • Loading branch information
IsaccBarker committed Jun 24, 2024
1 parent 6a90e10 commit a0d5086
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 44 deletions.
69 changes: 55 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ covid19.print(False)
Last run elapsed t : -
Rewiring : off

Global actions:
(none)
Global events:
- Update infected individuals (runs daily)

Virus(es):
- covid-19 (baseline prevalence: 1.00%)
Expand All @@ -77,7 +77,7 @@ covid19.print(False)
- Prob. Recovery : 0.1400
- Prob. Transmission : 0.1000

<epiworldpy._core.ModelSEIRCONN at 0x7fe01a91bcb0>
<epiworldpy._core.ModelSEIRCONN at 0x10899ebb0>

And run it and see what we get

Expand All @@ -100,12 +100,12 @@ covid19.print(False)
Number of entities : 0
Days (duration) : 100 (of 100)
Number of viruses : 1
Last run elapsed t : 105.00ms
Last run speed : 9.47 million agents x day / second
Last run elapsed t : 14.00ms
Last run speed : 71.15 million agents x day / second
Rewiring : off

Global actions:
(none)
Global events:
- Update infected individuals (runs daily)

Virus(es):
- covid-19 (baseline prevalence: 1.00%)
Expand All @@ -120,17 +120,58 @@ covid19.print(False)
- Prob. Transmission : 0.1000

Distribution of the population at time 100:
- (0) Susceptible : 9900 -> 8454
- (1) Exposed : 100 -> 140
- (2) Infected : 0 -> 135
- (3) Recovered : 0 -> 1271
- (0) Susceptible : 9900 -> 7275
- (1) Exposed : 100 -> 269
- (2) Infected : 0 -> 292
- (3) Recovered : 0 -> 2164

Transition Probabilities:
- Susceptible 1.00 0.00 0.00 0.00
- Exposed 0.00 0.86 0.14 0.00
- Infected 0.00 0.00 0.85 0.15
- Exposed 0.00 0.85 0.15 0.00
- Infected 0.00 0.00 0.86 0.14
- Recovered 0.00 0.00 0.00 1.00

<epiworldpy._core.ModelSEIRCONN at 0x7fe01a91bcb0>
<epiworldpy._core.ModelSEIRCONN at 0x10899ebb0>

Let’s visualize the resulting time series

``` python
import numpy as np
import matplotlib.pyplot as plt

# Get the data from the database
history = covid19.getDb().getHistTotal()

# Extract unique states and dates
unique_states = np.unique(history['states'])
unique_dates = np.unique(history['dates'])

# Initialize a dictionary to store time series data for each state
time_series_data = {state: [] for state in unique_states}

# Populate the time series data for each state
for state in unique_states:
for date in unique_dates:
# Get the count for the current state and date
mask = (history['states'] == state) & (history['dates'] == date)
count = history['counts'][mask][0]
time_series_data[state].append(count)

# Start the plotting!
plt.figure(figsize=(10, 6))

for state in unique_states:
plt.plot(unique_dates, time_series_data[state], marker='o', label=state)

plt.xlabel('Time')
plt.ylabel('Count')
plt.title('Time Series of States')
plt.legend()
plt.grid(True)
plt.show()
```

![The data resulting from the COVID-19 SEIR model
run](README_files/figure-commonmark/series-visualization-output-1.png)

# Acknowledgements
80 changes: 51 additions & 29 deletions README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,26 @@ This is a python wrapper of the [`epiworld c++` library][epiworld-git], an ABM
simulation engine. This is possible using the
[`pybind11`][] library (which rocks!).

[`pybind11`]: https://pybind11.readthedocs.io/en/stable/

The `epiworld` module is already
[implemented in R](https://github.com/UofUEpiBio/epiworldR){target="_blank"}.


[epiworld-git]: https://github.com/UofUEpiBio/epiworld
[gitter-badge]: https://badges.gitter.im/pybind/Lobby.svg
[gitter-link]: https://gitter.im/pybind/Lobby
[actions-badge]: https://github.com/UofUEpiBio/epiworldpy/workflows/Tests/badge.svg
[actions-conda-link]: https://github.com/UofUEpiBio/epiworldpy/actions?query=workflow%3AConda
[actions-conda-badge]: https://github.com/UofUEpiBio/epiworldpy/actions/workflows/conda.yml/badge.svg
[actions-pip-link]: https://github.com/UofUEpiBio/epiworldpy/actions?query=workflow%3APip
[actions-pip-badge]: https://github.com/UofUEpiBio/epiworldpy/actions/workflows/pip.yml/badge.svg
[actions-wheels-link]: https://github.com/UofUEpiBio/epiworldpy/actions?query=workflow%3AWheels
[actions-wheels-badge]: https://github.com/UofUEpiBio/epiworldpy/workflows/Wheels/badge.svg

# Installation


- clone this repository
- `pip install ./epiworldpy`


# Examples

## Base setup
## Basic

Here we show how to create a `SEIR` object and add terms to it. We will use the following data:

```{python}
# Loading the module
import epiworldpy as m
covid19 = m.ModelSEIR(
import epiworldpy as epiworld
# Create a SEIR model (susceptible, exposed, infectious, recovered), representing COVID-19.
covid19 = epiworld.ModelSEIR(
name = 'covid-19',
n = 10000,
prevalence = .01,
Expand All @@ -57,24 +43,60 @@ covid19 = m.ModelSEIR(
recovery_rate = 0.14
)
```

We can now take a look at the model

```{python}
# Creating the object
# Taking a look
covid19.print(False)
```

And run it and see what we get
Let's run it and to see what we get:

```{python}
# Run for 100 days with a seed of 223.
covid19.run(100, 223)
# Print an overview.
covid19.print(False)
```

[`cibuildwheel`]: https://cibuildwheel.readthedocs.io
We can know visualize the resulting time series:

```{python}
#| label: series-visualization
#| fig-cap: "The data resulting from the COVID-19 SEIR model run"
import numpy as np
import matplotlib.pyplot as plt
# Get the data from the database
history = covid19.getDb().getHistTotal()
# Extract unique states and dates
unique_states = np.unique(history['states'])
unique_dates = np.unique(history['dates'])
# Initialize a dictionary to store time series data for each state
time_series_data = {state: [] for state in unique_states}
# Populate the time series data for each state
for state in unique_states:
for date in unique_dates:
# Get the count for the current state and date
mask = (history['states'] == state) & (history['dates'] == date)
count = history['counts'][mask][0]
time_series_data[state].append(count)
# Start the plotting!
plt.figure(figsize=(10, 6))
for state in unique_states:
plt.plot(unique_dates, time_series_data[state], marker='o', label=state)
plt.xlabel('Day')
plt.ylabel('Count')
plt.yscale('log')
plt.title('COVID-19 SEIR Model Data')
plt.legend()
plt.grid(True)
plt.show()
```

# Acknowledgements

[`pybind11`]: https://pybind11.readthedocs.io/en/stable/
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["scikit-build-core>=0.3.3", "pybind11"]
requires = ["scikit-build-core>=0.3.3", "pybind11", "matplotlib>=3.5.0"]
build-backend = "scikit_build_core.build"


Expand Down

0 comments on commit a0d5086

Please sign in to comment.