Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuos time #187

Merged
merged 5 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/reference/models/epidemics/SEIR.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
****
SEIR
****
*********
SEIR (DT)
*********


In the SEIR model [#]_, during the course of an epidemics, a node is allowed to change its status from **Susceptible** (S) to **Exposed** (E) to **Infected** (I), then to **Removed** (R).
Expand All @@ -9,6 +9,7 @@ The model is instantiated on a graph having a non-empty set of infected nodes.

SEIR assumes that if, during a generic iteration, a susceptible node comes into contact with an infected one, it becomes infected after an exposition period with probability beta, than it can switch to removed with probability gamma (the only transition allowed are S→E→I→R).

This implementation assumes discrete time dynamics for the E->I and I->R transitions.

--------
Statuses
Expand Down
108 changes: 108 additions & 0 deletions docs/reference/models/epidemics/SEIR_ct.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
*********
SEIR (CT)
*********


In the SEIR model [#]_, during the course of an epidemics, a node is allowed to change its status from **Susceptible** (S) to **Exposed** (E) to **Infected** (I), then to **Removed** (R).

The model is instantiated on a graph having a non-empty set of infected nodes.

SEIR assumes that if, during a generic iteration, a susceptible node comes into contact with an infected one, it becomes infected after an exposition period with probability beta, than it can switch to removed with probability gamma (the only transition allowed are S→E→I→R).

This implementation assumes continuous time dynamics for the E->I and I->R transitions.

--------
Statuses
--------

During the simulation a node can experience the following statuses:

=========== ====
Name Code
=========== ====
Susceptible 0
Infected 1
Exposed 2
Removed 3
=========== ====

----------
Parameters
----------

===== ===== =============== ======= ========= =====================
Name Type Value Type Default Mandatory Description
===== ===== =============== ======= ========= =====================
beta Model float in [0, 1] True Infection probability
gamma Model float in [0, 1] True Removal probability
alpha Model float in [0, 1] True Latent period
===== ===== =============== ======= ========= =====================

The initial infection status can be defined via:

- **fraction_infected**: Model Parameter, float in [0, 1]
- **Infected**: Status Parameter, set of nodes

The two options are mutually exclusive and the latter takes precedence over the former.

-------
Methods
-------

The following class methods are made available to configure, describe and execute the simulation:

^^^^^^^^^
Configure
^^^^^^^^^
.. autoclass:: ndlib.models.epidemics.SEIR_ct_Model.SEIRctModel
.. automethod:: ndlib.models.epidemics.SEIR_ct_Model.SEIRctModel.__init__(graph)

.. automethod:: ndlib.models.epidemics.SEIR_ct_Model.SEIRctModel.set_initial_status(self, configuration)
.. automethod:: ndlib.models.epidemics.SEIR_ct_Model.SEIRctModel.reset(self)

^^^^^^^^
Describe
^^^^^^^^

.. automethod:: ndlib.models.epidemics.SEIR_ct_Model.SEIRctModel.get_info(self)
.. automethod:: ndlib.models.epidemics.SEIR_ct_Model.SEIRctModel.get_status_map(self)

^^^^^^^^^^^^^^^^^^
Execute Simulation
^^^^^^^^^^^^^^^^^^
.. automethod:: ndlib.models.epidemics.SEIR_ct_Model.SEIRctModel.iteration(self)
.. automethod:: ndlib.models.epidemics.SEIR_ct_Model.SEIRctModel.iteration_bunch(self, bunch_size)


-------
Example
-------

In the code below is shown an example of instantiation and execution of an SEIR simulation on a random graph: we set the initial set of infected nodes as % of the overall population, a probability of infection of 1%, a removal probability of 0.5% and an incubation period of 5% (e.g. 20 iterations).

.. code-block:: python

import networkx as nx
import ndlib.models.ModelConfig as mc
import ndlib.models.epidemics as ep

# Network topology
g = nx.erdos_renyi_graph(1000, 0.1)

# Model selection
model = ep.SEIRctModel(g)

# Model Configuration
cfg = mc.Configuration()
cfg.add_model_parameter('beta', 0.01)
cfg.add_model_parameter('gamma', 0.005)
cfg.add_model_parameter('alpha', 0.05)
cfg.add_model_parameter("fraction_infected", 0.05)
model.set_initial_status(cfg)

# Simulation execution
iterations = model.iteration_bunch(200)



.. [#] J.L. Aron and I.B. Schwartz. Seasonality and period-doubling bifurcations in an epidemic model. Journal Theoretical Biology, 110:665-679, 1984
7 changes: 4 additions & 3 deletions docs/reference/models/epidemics/SEIS.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
****
SEIS
****
*********
SEIS (DT)
*********


In the SEIS model, during the course of an epidemics, a node is allowed to change its status from **Susceptible** (S) to **Exposed** (E) to **Infected** (I), then again to **Susceptible** (S).
Expand All @@ -9,6 +9,7 @@ The model is instantiated on a graph having a non-empty set of infected nodes.

SEIS assumes that if, during a generic iteration, a susceptible node comes into contact with an infected one, it becomes infected after an exposition period with probability beta, than it can switch back to susceptible with probability lambda (the only transition allowed are S→E→I→S).

This implementation assumes discrete time dynamics for the E->I and I->S transitions.

--------
Statuses
Expand Down
106 changes: 106 additions & 0 deletions docs/reference/models/epidemics/SEIS_ct.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
*********
SEIS (CT)
*********


In the SEIS model, during the course of an epidemics, a node is allowed to change its status from **Susceptible** (S) to **Exposed** (E) to **Infected** (I), then again to **Susceptible** (S).

The model is instantiated on a graph having a non-empty set of infected nodes.

SEIS assumes that if, during a generic iteration, a susceptible node comes into contact with an infected one, it becomes infected after an exposition period with probability beta, than it can switch back to susceptible with probability lambda (the only transition allowed are S→E→I→S).

This implementation assumes continuous time dynamics for the E->I and I->S transitions.

--------
Statuses
--------

During the simulation a node can experience the following statuses:

=========== ====
Name Code
=========== ====
Susceptible 0
Infected 1
Exposed 2
=========== ====

----------
Parameters
----------

====== ===== =============== ======= ========= =====================
Name Type Value Type Default Mandatory Description
====== ===== =============== ======= ========= =====================
beta Model float in [0, 1] True Infection probability
lambda Model float in [0, 1] True Removal probability
alpha Model float in [0, 1] True Latent period
====== ===== =============== ======= ========= =====================

The initial infection status can be defined via:

- **fraction_infected**: Model Parameter, float in [0, 1]
- **Infected**: Status Parameter, set of nodes

The two options are mutually exclusive and the latter takes precedence over the former.

-------
Methods
-------

The following class methods are made available to configure, describe and execute the simulation:

^^^^^^^^^
Configure
^^^^^^^^^
.. autoclass:: ndlib.models.epidemics.SEIS_ct_Model.SEISctModel
.. automethod:: ndlib.models.epidemics.SEIS_ct_Model.SEISctModel.__init__(graph)

.. automethod:: ndlib.models.epidemics.SEIS_ct_Model.SEISctModel.set_initial_status(self, configuration)
.. automethod:: ndlib.models.epidemics.SEIS_ct_Model.SEISctModel.reset(self)

^^^^^^^^
Describe
^^^^^^^^

.. automethod:: ndlib.models.epidemics.SEIS_ct_Model.SEISctModel.get_info(self)
.. automethod:: ndlib.models.epidemics.SEIS_ct_Model.SEISctModel.get_status_map(self)

^^^^^^^^^^^^^^^^^^
Execute Simulation
^^^^^^^^^^^^^^^^^^
.. automethod:: ndlib.models.epidemics.SEIS_ct_Model.SEISctModel.iteration(self)
.. automethod:: ndlib.models.epidemics.SEIS_ct_Model.SEISctModel.iteration_bunch(self, bunch_size)


-------
Example
-------

In the code below is shown an example of instantiation and execution of an SEIS simulation on a random graph:
we set the initial set of infected nodes as 5% of the overall population, a probability of infection of 1%, a removal probability of 0.5% and an latent period of 5% (e.g. 20 iterations).

.. code-block:: python

import networkx as nx
import ndlib.models.ModelConfig as mc
import ndlib.models.epidemics as ep

# Network topology
g = nx.erdos_renyi_graph(1000, 0.1)

# Model selection
model = ep.SEISctModel(g)

# Model Configuration
cfg = mc.Configuration()
cfg.add_model_parameter('beta', 0.01)
cfg.add_model_parameter('lambda', 0.005)
cfg.add_model_parameter('alpha', 0.05)
cfg.add_model_parameter("fraction_infected", 0.05)
model.set_initial_status(cfg)

# Simulation execution
iterations = model.iteration_bunch(200)


2 changes: 2 additions & 0 deletions docs/reference/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ In ``NDlib`` are implemented the following **Epidemic** models:
models/epidemics/SIS.rst
models/epidemics/SIR.rst
models/epidemics/SEIR.rst
models/epidemics/SEIR_ct.rst
models/epidemics/SEIS.rst
models/epidemics/SEIS_ct.rst
models/epidemics/SWIR.rst
models/epidemics/Threshold.rst
models/epidemics/GeneralisedThreshold.rst
Expand Down
7 changes: 1 addition & 6 deletions ndlib/models/epidemics/SEIRModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ def __init__(self, graph, seed=None):
"edges": {},
}

self.progress = {}

def iteration(self, node_status=True):
self.clean_initial_status(self.available_statuses.values())

Expand Down Expand Up @@ -79,18 +77,15 @@ def iteration(self, node_status=True):
if self.params['model']['tp_rate'] == 1:
if eventp < 1 - (1 - self.params['model']['beta']) ** len(infected_neighbors):
actual_status[u] = 2 # Exposed
self.progress[u] = self.actual_iteration # save time of exposure t_i
else:
if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 2 # Exposed
self.progress[u] = self.actual_iteration # save time of exposure t_i

elif u_status == 2:

# apply prob. of infection, after (t - t_i)
if eventp < 1 - np.exp(- (self.actual_iteration - self.progress[u]) * self.params['model']['alpha']):
if eventp < self.params['model']['alpha']:
actual_status[u] = 1 # Infected
del self.progress[u]

elif u_status == 1:
if eventp < self.params['model']['gamma']:
Expand Down