Skip to content

Commit

Permalink
⬆️ support for fixed/topology dependent infection rate in stochastic …
Browse files Browse the repository at this point in the history
…models
  • Loading branch information
GiulioRossetti committed May 10, 2019
1 parent f80b144 commit bb2d264
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 20 deletions.
21 changes: 17 additions & 4 deletions ndlib/models/epidemics/SEIRModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def __init__(self, graph):
"descr": "Recovery rate",
"range": [0, 1],
"optional": False
},
"tp_rate": {
"descr": "Whether if the infection rate depends on the number of infected neighbors",
"range": [0, 1],
"optional": True,
"default": 1
}
},
"nodes": {},
Expand Down Expand Up @@ -67,11 +73,18 @@ def iteration(self, node_status=True):
neighbors = self.graph.predecessors(u)

if u_status == 0: # Susceptible
triggered = 1 if len([v for v in neighbors if self.status[v] == 1]) > 0 else 0

if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 2 # Exposed
self.progress[u] = 0
infected_neighbors = [v for v in neighbors if self.status[v] == 1]
triggered = 1 if len(infected_neighbors) > 0 else 0

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] = 0
else:
if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 2 # Exposed
self.progress[u] = 0

elif u_status == 2:
if self.progress[u] < 1:
Expand Down
20 changes: 16 additions & 4 deletions ndlib/models/epidemics/SEISModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def __init__(self, graph):
"descr": "Recovery rate",
"range": [0, 1],
"optional": False
},
"tp_rate": {
"descr": "Whether if the infection rate depends on the number of infected neighbors",
"range": [0, 1],
"optional": True,
"default": 1
}
},
"nodes": {},
Expand Down Expand Up @@ -80,11 +86,17 @@ def iteration(self, node_status=True):
neighbors = self.graph.predecessors(u)

if u_status == 0: # Susceptible
triggered = 1 if len([v for v in neighbors if self.status[v] == 1]) > 0 else 0
infected_neighbors = [v for v in neighbors if self.status[v] == 1]
triggered = 1 if len(infected_neighbors) > 0 else 0

if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 2 # Exposed
self.progress[u] = 0
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] = 0
else:
if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 2 # Exposed
self.progress[u] = 0

elif u_status == 2:
if self.progress[u] < 1:
Expand Down
21 changes: 16 additions & 5 deletions ndlib/models/epidemics/SIModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ def __init__(self, graph):
"beta": {
"descr": "Infection rate",
"range": "[0,1]",
"optional": False}
"optional": False},
"tp_rate": {
"descr": "Whether if the infection rate depends on the number of infected neighbors",
"range": [0, 1],
"optional": True,
"default": 1
}
},
"nodes": {},
"edges": {},
Expand Down Expand Up @@ -68,10 +74,15 @@ def iteration(self, node_status=True):
neighbors = self.graph.predecessors(u)

if u_status == 0:
triggered = 1 if len([v for v in neighbors if self.status[v] == 1]) > 0 else 0

if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 1
infected_neighbors = [v for v in neighbors if self.status[v] == 1]
triggered = 1 if len(infected_neighbors) > 0 else 0

if self.params['model']['tp_rate'] == 1:
if eventp < 1 - (1 - self.params['model']['beta']) ** len(infected_neighbors):
actual_status[u] = 1
else:
if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 1

delta, node_count, status_delta = self.status_delta(actual_status)
self.status = actual_status
Expand Down
19 changes: 15 additions & 4 deletions ndlib/models/epidemics/SIRModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def __init__(self, graph):
"gamma": {
"descr": "Recovery rate",
"range": [0, 1],
"optional": False
"optional": False},
"tp_rate": {
"descr": "Whether if the infection rate depends on the number of infected neighbors",
"range": [0, 1],
"optional": True,
"default": 1
}
},
"nodes": {},
Expand Down Expand Up @@ -76,10 +81,16 @@ def iteration(self, node_status=True):
neighbors = self.graph.predecessors(u)

if u_status == 0:
triggered = 1 if len([v for v in neighbors if self.status[v] == 1]) > 0 else 0
infected_neighbors = [v for v in neighbors if self.status[v] == 1]
triggered = 1 if len(infected_neighbors) > 0 else 0

if self.params['model']['tp_rate'] == 1:
if eventp < 1 - (1 - self.params['model']['beta']) ** len(infected_neighbors):
actual_status[u] = 1
else:
if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 1

if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 1
elif u_status == 1:
if eventp < self.params['model']['gamma']:
actual_status[u] = 2
Expand Down
18 changes: 15 additions & 3 deletions ndlib/models/epidemics/SISModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def __init__(self, graph):
"descr": "Recovery rate",
"range": [0, 1],
"optional": False
},
"tp_rate": {
"descr": "Whether if the infection rate depends on the number of infected neighbors",
"range": [0, 1],
"optional": True,
"default": 1
}
},
"nodes": {},
Expand Down Expand Up @@ -75,10 +81,16 @@ def iteration(self, node_status=True):
neighbors = self.graph.predecessors(u)

if u_status == 0:
triggered = 1 if len([v for v in neighbors if self.status[v] == 1]) > 0 else 0
infected_neighbors = [v for v in neighbors if self.status[v] == 1]
triggered = 1 if len(infected_neighbors) > 0 else 0

if self.params['model']['tp_rate'] == 1:
if eventp < 1 - (1 - self.params['model']['beta']) ** len(infected_neighbors):
actual_status[u] = 1
else:
if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 1

if eventp < self.params['model']['beta'] * triggered:
actual_status[u] = 1
elif u_status == 1:
if eventp < self.params['model']['lambda']:
actual_status[u] = 0
Expand Down

0 comments on commit bb2d264

Please sign in to comment.