From a0d50862dd988131b3fb2af8855332cbaaf374fc Mon Sep 17 00:00:00 2001 From: Milo Banks Date: Mon, 24 Jun 2024 14:09:51 -0600 Subject: [PATCH] [readme] add graph to example 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). --- README.md | 69 ++++++++++++++++++++++++++++++++++--------- README.qmd | 80 ++++++++++++++++++++++++++++++++------------------ pyproject.toml | 2 +- 3 files changed, 107 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index f2f3438..c32c6e4 100644 --- a/README.md +++ b/README.md @@ -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%) @@ -77,7 +77,7 @@ covid19.print(False) - Prob. Recovery : 0.1400 - Prob. Transmission : 0.1000 - + And run it and see what we get @@ -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%) @@ -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 - + + +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 diff --git a/README.qmd b/README.qmd index 2d3b951..a29eb62 100644 --- a/README.qmd +++ b/README.qmd @@ -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, @@ -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/ \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index aacfa53..8605e68 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"