Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Grid of models.
3"""
4import numpy
5from ..data import load_grid_image
6from ..data.image_helper import reduce_image
9class GridMapSIR:
10 """
11 Implements a grid of models.
13 :param model: epidemic model like :class:`CovidSIR
14 <aftercovid.model.CovidSIR>`
15 :param name: picture name, must be a binary image
16 :param grid_size: new size of the image
17 :param gamma: propagation of the epidemics to the neighborhood
18 """
20 def __init__(self, model, name="france_bin.bmp", grid_size=(10, 12),
21 gamma=0.1):
22 self.raw_grid = load_grid_image(name)
23 self.grid = reduce_image(self.raw_grid, grid_size)
24 self.model = model
25 self.gamma = gamma
26 self._init()
28 def _init(self):
29 """
30 Initializes the grid.
31 """
32 self.grid_ = numpy.empty(self.grid.shape).tolist()
33 edges = []
34 for i in range(self.grid.shape[0]):
35 for j in range(self.grid.shape[1]):
36 if self.grid[i, j] == 0:
37 self.grid_[i][j] = None
38 continue
39 self.grid_[i][j] = self.model.copy()
41 for ii in range(-1, 2):
42 for jj in range(-1, 2):
43 if (i + ii >= 0 and j + jj >= 0 and
44 i + ii < self.grid.shape[0] and
45 j + jj < self.grid.shape[1] and
46 self.grid[ii, jj] == 1):
47 edges.append(((i, j), (i + ii, j + jj)))
48 self.edges_ = edges
50 def __getitem__(self, name):
51 """
52 Returns the sum of the metrics all over a grid.
54 :param name: quantity name
55 :return: float
56 """
57 g = self.metric(name)
58 return g.sum()
60 def metric(self, metric="S"):
61 """
62 Returns one metric for all models on the grid.
64 :param name: metric name
65 :return: numpy array
66 """
67 res = numpy.zeros(self.grid.shape, dtype=numpy.float64)
68 for i in range(self.grid.shape[0]):
69 for j in range(self.grid.shape[1]):
70 if not self.grid[i, j]:
71 continue
72 res[i, j] = self.grid_[i][j][metric]
73 return res