Skip to content

Commit 90fb734

Browse files
committed
Added validation __post_init__ check in ExposureModelGroup class and respective tests
1 parent e27f2c2 commit 90fb734

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

caimira/src/caimira/calculator/models/models.py

+9
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,15 @@ class ExposureModelGroup:
19241924
#: The set of exposure models for each exposed population
19251925
exposure_models: typing.Tuple[ExposureModel, ...]
19261926

1927+
def __post_init__(self):
1928+
"""
1929+
Validate that all ExposureModels have the same ConcentrationModel.
1930+
"""
1931+
first_concentration_model = self.exposure_models[0].concentration_model
1932+
for model in self.exposure_models[1:]:
1933+
if model.concentration_model != first_concentration_model:
1934+
raise ValueError("All ExposureModels must have the same ConcentrationModel.")
1935+
19271936
@method_cache
19281937
def _deposited_exposure_list(self) -> typing.List[_VectorisedFloat]:
19291938
"""

caimira/tests/models/test_dynamic_population.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ def test_infection_probability(
194194
npt.assert_almost_equal(base_infection_probability, dynamic_population_exposure_model.infection_probability())
195195

196196

197-
@pytest.mark.skip
198197
def test_dynamic_total_probability_rule(
199198
dynamic_infected_single_exposure_model: models.ExposureModel,
200199
dynamic_population_exposure_model: models.ExposureModel):
@@ -207,27 +206,33 @@ def test_dynamic_total_probability_rule(
207206
dynamic_population_exposure_model.total_probability_rule()
208207

209208

210-
@pytest.mark.skip
211-
def test_dynamic_expected_new_cases(
212-
dynamic_infected_single_exposure_model: models.ExposureModel,
213-
dynamic_population_exposure_model: models.ExposureModel):
209+
def test_exposure_model_group_structure(data_registry, full_exposure_model: models.ExposureModel):
210+
"""
211+
ExposureModels must have the same ConcentrationModel.
212+
In this test the number of infected occupants is different.
213+
"""
214+
another_full_exposure_model = dc_utils.nested_replace(full_exposure_model,
215+
{'concentration_model.infected.number': 2, })
216+
with pytest.raises(ValueError, match=re.escape("All ExposureModels must have the same ConcentrationModel.")):
217+
models.ExposureModelGroup(data_registry, exposure_models=(full_exposure_model, another_full_exposure_model, ))
214218

215-
with pytest.raises(NotImplementedError, match=re.escape("Cannot compute expected new cases "
216-
"with dynamic occupancy")):
217-
dynamic_infected_single_exposure_model.expected_new_cases()
218-
with pytest.raises(NotImplementedError, match=re.escape("Cannot compute expected new cases "
219-
"with dynamic occupancy")):
220-
dynamic_population_exposure_model.expected_new_cases()
221219

220+
def test_exposure_model_group_expected_new_cases(data_registry, full_exposure_model: models.ExposureModel):
221+
"""
222+
ExposureModelGroup expected number of new cases must
223+
be the sum of expected new cases of each ExposureModel.
222224
223-
@pytest.mark.skip
224-
def test_dynamic_reproduction_number(
225-
dynamic_infected_single_exposure_model: models.ExposureModel,
226-
dynamic_population_exposure_model: models.ExposureModel):
225+
In this case, the number of exposed people is changing
226+
between the two ExposureModel groups.
227+
"""
228+
another_full_exposure_model = dc_utils.nested_replace(
229+
full_exposure_model, {'exposed.number': 5, }
230+
)
231+
exposure_model_group = models.ExposureModelGroup(
232+
data_registry=data_registry,
233+
exposure_models=(full_exposure_model, another_full_exposure_model, ),
234+
)
227235

228-
with pytest.raises(NotImplementedError, match=re.escape("Cannot compute reproduction number "
229-
"with dynamic occupancy")):
230-
dynamic_infected_single_exposure_model.reproduction_number()
231-
with pytest.raises(NotImplementedError, match=re.escape("Cannot compute reproduction number "
232-
"with dynamic occupancy")):
233-
dynamic_population_exposure_model.reproduction_number()
236+
assert exposure_model_group.expected_new_cases() == (
237+
full_exposure_model.expected_new_cases() + another_full_exposure_model.expected_new_cases()
238+
)

0 commit comments

Comments
 (0)