From df77bdd1b596c2a133d970137d9fa9a2f3cdecc7 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Mon, 15 May 2023 15:53:57 +0200 Subject: [PATCH] tests for incidence rate --- .../tests/models/test_dynamic_population.py | 69 +++++++++++++++---- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/caimira/tests/models/test_dynamic_population.py b/caimira/tests/models/test_dynamic_population.py index e5bf9805..2bf4a8ac 100644 --- a/caimira/tests/models/test_dynamic_population.py +++ b/caimira/tests/models/test_dynamic_population.py @@ -27,12 +27,16 @@ def full_exposure_model(): short_range=(), exposed=models.Population( number=10, - presence=models.SpecificInterval(((8, 12), (13, 17), )), + presence=models.SpecificInterval(((8, 12), (13, 17), )), mask=models.Mask.types['No mask'], activity=models.Activity.types['Seated'], host_immunity=0. ), - geographical_data=(), + geographical_data=models.Cases( + geographic_population=50_000, + geographic_cases=52, + ascertainment_bias=1, + ), ) @@ -51,11 +55,37 @@ def baseline_infected_population_number(): @pytest.fixture -def dynamic_single_exposure_model(full_exposure_model, baseline_infected_population_number): +def baseline_exposed_population_number(): + return models.Population( + number=models.IntPiecewiseConstant( + (8, 12, 13, 17), (10, 0, 10)), + presence=None, + mask=models.Mask.types['No mask'], + activity=models.Activity.types['Seated'], + host_immunity=0., + ) + + +@pytest.fixture +def dynamic_infected_single_exposure_model(full_exposure_model, baseline_infected_population_number): return dc_utils.nested_replace(full_exposure_model, {'concentration_model.infected': baseline_infected_population_number, }) +@pytest.fixture +def dynamic_exposed_single_exposure_model(full_exposure_model, baseline_exposed_population_number): + return dc_utils.nested_replace(full_exposure_model, + {'exposed': baseline_exposed_population_number, }) + + +@pytest.fixture +def dynamic_population_exposure_model(full_exposure_model, baseline_infected_population_number ,baseline_exposed_population_number): + return dc_utils.nested_replace(full_exposure_model, { + 'concentration_model.infected': baseline_infected_population_number, + 'exposed': baseline_exposed_population_number, + }) + + @pytest.mark.parametrize( "time", [4., 8., 10., 12., 13., 14., 16., 20., 24.], @@ -91,16 +121,16 @@ def test_population_number(full_exposure_model: models.ExposureModel, [4., 8., 10., 12., 13., 14., 16., 20., 24.], ) def test_concentration_model_dynamic_population(full_exposure_model: models.ExposureModel, - dynamic_single_exposure_model: models.ExposureModel, + dynamic_infected_single_exposure_model: models.ExposureModel, time: float): - assert full_exposure_model.concentration(time) == dynamic_single_exposure_model.concentration(time) + assert full_exposure_model.concentration(time) == dynamic_infected_single_exposure_model.concentration(time) @pytest.mark.parametrize("number_of_infected",[1, 2, 3, 4, 5]) @pytest.mark.parametrize("time",[9., 12.5, 16.]) def test_linearity_with_number_of_infected(full_exposure_model: models.ExposureModel, - dynamic_single_exposure_model: models.ExposureModel, + dynamic_infected_single_exposure_model: models.ExposureModel, time: float, number_of_infected: int): @@ -112,8 +142,8 @@ def test_linearity_with_number_of_infected(full_exposure_model: models.ExposureM } ) - npt.assert_almost_equal(static_multiple_exposure_model.concentration(time), dynamic_single_exposure_model.concentration(time) * number_of_infected) - npt.assert_almost_equal(static_multiple_exposure_model.deposited_exposure(), dynamic_single_exposure_model.deposited_exposure() * number_of_infected) + npt.assert_almost_equal(static_multiple_exposure_model.concentration(time), dynamic_infected_single_exposure_model.concentration(time) * number_of_infected) + npt.assert_almost_equal(static_multiple_exposure_model.deposited_exposure(), dynamic_infected_single_exposure_model.deposited_exposure() * number_of_infected) @pytest.mark.parametrize( @@ -171,10 +201,19 @@ def test_dynamic_dose(full_exposure_model, time): npt.assert_almost_equal(dynamic_exposure, np.sum(static_exposure)) -def test_dynamic_total_probability_rule(dynamic_single_exposure_model: models.ExposureModel): - with pytest.raises( - NotImplementedError, - match=re.escape("Cannot compute total probability " - "(including incidence rate) with dynamic occupancy") - ): - dynamic_single_exposure_model.total_probability_rule() +def test_dynamic_total_probability_rule( + full_exposure_model: models.ExposureModel, + dynamic_infected_single_exposure_model: models.ExposureModel, + dynamic_exposed_single_exposure_model: models.ExposureModel, + dynamic_population_exposure_model: models.ExposureModel): + + full_model_total_prob_rule = full_exposure_model.total_probability_rule() + npt.assert_almost_equal(full_model_total_prob_rule, + dynamic_population_exposure_model.dynamic_total_probability_rule()) + + npt.assert_almost_equal(full_model_total_prob_rule, + dynamic_infected_single_exposure_model.dynamic_total_probability_rule()) + + npt.assert_almost_equal(full_model_total_prob_rule, + dynamic_exposed_single_exposure_model.dynamic_total_probability_rule()) + \ No newline at end of file