-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
145 lines (91 loc) · 3.57 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Module for miscellaneous functions."""
from __future__ import annotations
import os
from typing import TYPE_CHECKING
import numpy as np
if TYPE_CHECKING:
from .person import Person
LOG_PREFIX = "."
def probability(prob):
"""Return True with probability prob."""
if prob < 0 or prob > 1:
raise ValueError(f"Probability (currently {prob}) must be between 0 and 1.")
return np.random.random() < prob
def get_random_int(high) -> int:
"""Return a random integer between 0 and high."""
if high < 0:
raise ValueError("high must be greater than 0")
return np.random.randint(0, high)
class OutputFiles:
"""Class to manage output files."""
def __init__(self):
self.files = {}
def open(self, file_name):
"""Return a file handle for the given file name."""
if not file_name in self.files:
if os.path.exists(file_name):
os.remove(file_name)
# pylint: disable=consider-using-with
self.files[file_name] = open(file_name, "a", encoding="utf-8")
return self.files[file_name]
def __del__(self) -> None:
for _, value in self.files.items():
value.close()
out_files = OutputFiles()
def log_to_file(category: str, rank: int, data: list[int | float | str]):
"""Log data to a file."""
out_file = out_files.open(f"{LOG_PREFIX}/covid_out_{category}_{rank}.csv")
data = ",".join([str(x) for x in data])
print(data, file=out_file, flush=True)
def log_infection(
t: int, x: float, y: float, loc_type: str, rank: int, phase_duration: int
) -> int:
"""Log an infection event."""
# pylint: disable=too-many-arguments
data = [t, x, y, loc_type, rank, phase_duration]
log_to_file("infections", rank, data)
return 1
def log_hospitalisation(t: int, x: float, y: float, age: int, rank: int) -> int:
"""Log a hospitalisation event."""
data = [t, x, y, age]
log_to_file("hospitalisations", rank, data)
return 1
def log_death(t: int, x: float, y: float, age: int, rank: int) -> int:
"""Log a death event."""
data = [t, x, y, age]
log_to_file("deaths", rank, data)
return 1
def log_recovery(t: int, x: float, y: float, age: int, rank: int) -> int:
"""Log a recovery event."""
data = [t, x, y, age]
log_to_file("recoveries", rank, data)
return 1
def calc_dist(x1: float, y1: float, x2: float, y2: float) -> float:
"""Return the distance between two points."""
return (np.abs(x1 - x2) ** 2 + np.abs(y1 - y2) ** 2) ** 0.5
def write_log_headers(rank) -> None:
"""Write the headers for the log files."""
data = ["#time", "x", "y", "location_type", "rank", "incubation_time"]
log_to_file("infections", rank, data)
data = ["#time", "x", "y", "age"]
log_to_file("hospitalisations", rank, data)
data = ["#time", "x", "y", "age"]
log_to_file("deaths", rank, data)
data = ["#time", "x", "y", "age"]
log_to_file("recoveries", rank, data)
def check_vac_eligibility(a: Person) -> bool:
"""Check if an agent is eligible for vaccination."""
if (
a.status == "susceptible"
and a.symptoms_suppressed is False
and a.antivax is False
):
return True
return False
def get_interpolated_lists(interpolated_size: int, data: list[list[float]]) -> list:
"""Return interpolated lists of data."""
interpolated_data = [0.0] * interpolated_size
data = np.asarray(data)
for age in range(interpolated_size):
interpolated_data[age] = np.interp(age, data[:, 0], data[:, 1])
return interpolated_data