|
3 | 3 | # @author Bruno Goncalves |
4 | 4 | ###################################################### |
5 | 5 |
|
6 | | -from typing import Union |
| 6 | +from typing import Union, Optional |
7 | 7 | import networkx as nx |
8 | 8 | import numpy as np |
9 | 9 | from numpy import linalg |
|
12 | 12 | import matplotlib.pyplot as plt |
13 | 13 | from .EpiModel import EpiModel |
14 | 14 | from collections import Counter |
15 | | -from .utils import * |
| 15 | +from . import utils |
16 | 16 |
|
17 | 17 |
|
18 | 18 | class NetworkEpiModel(EpiModel): |
19 | | - def __init__(self, network, compartments=None): |
| 19 | + def __init__(self, network:nx.Graph, compartments: Optional[str]=None): |
20 | 20 | super(NetworkEpiModel, self).__init__(compartments) |
21 | 21 | self.network = network |
22 | 22 | self.kavg_ = 2 * network.number_of_edges() / network.number_of_nodes() |
23 | 23 | self.spontaneous = {} |
24 | 24 | self.interactions = {} |
25 | | - self.params = {} |
| 25 | + self.params = utils.Parameters() |
26 | 26 |
|
27 | 27 | def integrate(self, timesteps, **kwargs): |
28 | 28 | raise NotImplementedError("Network Models don't support numerical integration") |
29 | 29 |
|
30 | 30 | def add_interaction( |
31 | | - self, source: str, target: str, agent: str, rescale: bool = False, **rates |
| 31 | + self, source: str, target: str, agent: str, rate:Optional[float] = None, rescale: bool = False, **rates |
32 | 32 | ) -> None: |
| 33 | + |
33 | 34 | if rescale: |
34 | | - rate /= self.kavg_ |
| 35 | + if rate: |
| 36 | + rate /= self.kavg_ |
| 37 | + else: |
| 38 | + rates_names = list(rates.keys()) |
| 39 | + rates[rates_names[0]] /= self.kavg_ |
35 | 40 |
|
36 | | - self.params.update(rates) |
37 | | - rate = list(rates.keys())[0] |
38 | 41 | super(NetworkEpiModel, self).add_interaction( |
39 | | - source, target, agent=agent, rate=rate |
| 42 | + source, target, agent=agent, rate=rate, **rates |
40 | 43 | ) |
41 | 44 |
|
| 45 | + if rate is not None: |
| 46 | + count = len(self.params) + 1 |
| 47 | + rate_key = "rate" + str(count) |
| 48 | + else: |
| 49 | + self.params.define_parameters(**rates) |
| 50 | + rates = list(rates.keys()) |
| 51 | + rate_key = rates[0] |
| 52 | + |
42 | 53 | if source not in self.interactions: |
43 | 54 | self.interactions[source] = {} |
44 | 55 |
|
45 | 56 | if target not in self.interactions[source]: |
46 | 57 | self.interactions[source] = {} |
47 | 58 |
|
48 | | - self.interactions[source][agent] = {"target": target, "rate": rate} |
| 59 | + self.interactions[source][agent] = {"target": target, "rate": rate_key} |
| 60 | + |
| 61 | + def add_spontaneous(self, source: str, target: str, rate: Optional[float], **rates): |
| 62 | + super(NetworkEpiModel, self).add_spontaneous(source, target, rate=rate, **rates) |
49 | 63 |
|
50 | | - def add_spontaneous(self, source: str, target: str, **rates): |
51 | | - self.params.update(rates) |
52 | | - rate = list(rates.keys())[0] |
| 64 | + if rate is not None: |
| 65 | + count = len(self.params) + 1 |
| 66 | + rate_key = "rate" + str(count) |
| 67 | + else: |
| 68 | + self.params.define_parameters(**rates) |
| 69 | + rates = list(rates.keys()) |
| 70 | + rate_key = rates[0] |
53 | 71 |
|
54 | | - super(NetworkEpiModel, self).add_spontaneous(source, target, rate=rate) |
55 | 72 | if source not in self.spontaneous: |
56 | 73 | self.spontaneous[source] = {} |
57 | 74 |
|
58 | 75 | if target not in self.spontaneous[source]: |
59 | 76 | self.spontaneous[source] = {} |
60 | 77 |
|
61 | | - self.spontaneous[source][target] = rate |
| 78 | + self.spontaneous[source][target] = rate_key |
62 | 79 |
|
63 | 80 | def simulate(self, timesteps: int, seeds, **kwargs) -> None: |
64 | 81 | """Stochastically simulate the epidemic model""" |
|
0 commit comments