Skip to content

Quickstart

Open In Colab

Note

This very simple tutorial will guide you through the most basic functionalities of **pyepidemics**, ie building and manipulating a SIR model

Developer import
%matplotlib inline
%load_ext autoreload
%autoreload 2

# Developer import
import sys
sys.path.append("../../")
On Google Colab

Uncomment the following line to install the library locally

# !pip install pyepidemics
Verify the library is correctly installed
import pyepidemics

Building a SIR model

Quickstart

Simply imports the pre-coded SIR model from the bank of models

from pyepidemics.models import SIR

N = 1000 # Thousand persons
beta = 3/4 # One person contaminates 3/4 person per day
gamma = 1/4 # One person stay infected for 4 days

sir = SIR(N,beta,gamma)

You can then make predictions using this model

states = sir.solve()

states is a custom object, ie a Pandas DataFrame with more features adapted to our case

type(states)
pyepidemics.models.states.CompartmentStates

Among those features are visualization with simple but efficient functions like .show()

states.show(plotly = False)

Yet states is also a DataFrame you can use and manipulate as usual where the population in each compartment is stored

states.head()
S I R
0 999.000000 1.000000 0.000000
1 998.028875 1.646935 0.324190
2 996.432306 2.709836 0.857858
3 993.812926 4.451810 1.735264
4 989.530091 7.295041 3.174868

For example you can find the date of the epidemic peak easily with :

states["I"].idxmax()
15

You can also visualize the epidemic curves in proportions (curves can be reordered or selected using Pandas indexing)

states[["I","R","S"]].show_evolution_norm(plotly = False)

To go further

You can learn more about those models in other tutorials like:

Advanced users

Visualization with plotly

Plotly is a super nice visualization library to access dynamic plots. You can easily visualize with plotly using plotly=True with other visualization features :

states.show(plotly = True)

You can separate on each row (respectively column with the argument facet_column) to make it more readable

states.show(facet_row = "compartment")

You can also group epidemic curves together using the argument group, which is super useful to visualize together curves of the same scale.

states.show(group = {"Infections":["I"],"Population":["S","R"]})

There are other arguments like return_plot to get the plotly figure, which you can use in Dash or Streamlit apps.

Controlling the solve() function

When you solve the differential equation it implicitly use I0=1, ie starting with S0 = N-1 and I0 =1, you can change that behavior by passing an init_state to the solve equation as shown below

This will be interpreted as init_state = {"S":N-100,"I":100,"R":0}

sir.solve(init_state = 100).show(plotly = False)

Which is equivalent to the following expressions

sir.solve(init_state = (900,100,0))
sir.solve(init_state = {"S":900,"I":100,"R":0})
sir.solve(init_state = {"S":900,"I":100}) # Any compartment not initialized is implicitly = 0
S I R
0 900.000000 100.000000 0.000000
1 820.473669 148.688683 30.837648
2 718.404927 206.474611 75.120462
3 601.974672 263.965528 134.059800
4 485.051804 308.901834 206.046362
... ... ... ...
96 52.442809 0.000004 947.557187
97 52.442809 0.000003 947.557188
98 52.442809 0.000002 947.557189
99 52.442809 0.000002 947.557189
100 52.442809 0.000002 947.557190

101 rows × 3 columns