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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 | class SubgroupParams:
"""Class to read and collect Interaction matrix information. Allows for reading of subgroups from generic bins"""
# AgeYoungAdult = 18
# AgeAdult = 18
# AgeOldAdult = 65
PossibleLocs = [
"pub",
"grocery",
"cinema",
"city_transport",
"inter_city_transport",
"gym",
"care_home",
"student_dorm",
"boarding_school",
"university",
"school",
"household",
"company",
"communal",
"distribution_center",
"e_voucher",
"female_communal",
"isolation_unit",
"n_f_distribution_center",
"pump_latrine",
"religious",
"play_group",
"learning_center",
"hospital",
"shelter",
"informal_work",
"sexual_encounter",
]
def __init__(self, params=None) -> None:
if params is None:
self.params = params
self.specs = None
else:
self.params = params
self.specs = params.keys()
def subgroup_bins(self, spec):
"""
Args:
spec:
"""
return self.params[spec]["bins"]
def subgroup_type(self, spec):
"""
Args:
spec:
"""
return self.params[spec]["type"]
def subgroup_labels(self, spec):
"""
Args:
spec:
"""
if spec not in self.params.keys():
if spec not in self.PossibleLocs:
print(f"{spec} not defined in interaction yaml or defualt options")
return list(["default"])
else:
Bins, Type = get_defaults(spec)
logger.info(
f"{spec} interaction bins not specified. Using default values {Bins}"
)
self.params[spec] = {"bins": Bins, "type": Type}
if (
"bins" not in self.params[spec].keys()
or "type" not in self.params[spec].keys()
):
Bins, Type = get_defaults(spec)
logger.info(
f"{spec} interaction bins not specified. Using default values {Bins}"
)
self.params[spec]["bins"] = Bins
self.params[spec]["type"] = Type
elif spec in [
"learning_center",
"hospital",
"shelter",
"university",
"school",
"care_home",
"student_dorm",
"boarding_school",
"household",
"company",
]:
Bins, Type = get_defaults(spec)
if self.params[spec]["bins"] != Bins:
logger.info(f"{spec} interaction bins need default values for methods.")
self.params[spec]["bins"] = Bins
self.params[spec]["type"] = Type
if self.subgroup_type(spec) == "Age": # Make dummy names for N age bins
Nbins = len(self.params[spec]["bins"]) - 1
return list(itertools.islice(self.excel_cols(), Nbins))
elif self.subgroup_type(spec) == "Discrete":
return list(self.params[spec]["bins"]) # Already have our names!
# def kids_indexes(self, spec):
# if self.subgroup_type(spec) == "Age": #Make dummy names for N age bins
# index = sum(np.array(self.params[spec]["bins"]) < self.AgeAdult)
# return np.arange(0, index, 1)
# else:
# return np.array([]) #Empty list of bin indexes
# def adults_indexes(self, spec):
# if self.subgroup_type(spec) == "Age": #Make dummy names for N age bins
# index = sum(np.array(self.params[spec]["bins"]) < self.AgeAdult)
# return np.arange(index, len(self.params[spec]["bins"])-1, 1)
# else:
# return np.array([]) #Empty list of bin indexes
def excel_cols(self):
"""Generate generic string labels in form ["A", "B", "C", ... , "Z", "AA", "AB", .... ]"""
n = 1
while True:
yield from (
"".join(group)
for group in itertools.product(string.ascii_uppercase, repeat=n)
)
n += 1
@classmethod
def from_disease_config(cls, disease_config: DiseaseConfig) -> "SubgroupParams":
"""Initialise SubgroupParams using data from an existing DiseaseConfig object.
Args:
disease_config (DiseaseConfig): The disease-specific configuration object, which already contains the interaction data.
Returns:
SubgroupParams: SubgroupParams class instance.
"""
# Extract the contact matrices from DiseaseConfig
contact_matrices = disease_config.interaction_manager.contact_matrices
# Create and return SubgroupParams
return cls(params=contact_matrices)
|