|
| 1 | +import re |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import numpy.testing as npt |
| 5 | +import pytest |
| 6 | +from dataclasses import dataclass |
| 7 | + |
| 8 | +from caimira.calculator.models import models |
| 9 | +from caimira.calculator.store.data_registry import DataRegistry |
| 10 | + |
| 11 | +known_min_background_concentration = 440.44 |
| 12 | + |
| 13 | +@dataclass(frozen=True) |
| 14 | +class KnownConcentrationModelBase(models._ConcentrationModelBase): |
| 15 | + """ |
| 16 | + A _ConcentrationModelBase class where all the class methods are |
| 17 | + redefined with a value taken from new parameters. Useful for testing. |
| 18 | +
|
| 19 | + """ |
| 20 | + known_multiple_populations: models.MultiplePopulations |
| 21 | + |
| 22 | + known_removal_rate: float |
| 23 | + |
| 24 | + known_min_background_concentration: float |
| 25 | + |
| 26 | + known_normalization_factor: float |
| 27 | + |
| 28 | + @property |
| 29 | + def population(self) -> models.MultiplePopulations: |
| 30 | + return self.known_multiple_populations |
| 31 | + |
| 32 | + def removal_rate(self, time: float) -> float: |
| 33 | + return self.known_removal_rate |
| 34 | + |
| 35 | + def min_background_concentration(self) -> float: |
| 36 | + return self.known_min_background_concentration |
| 37 | + |
| 38 | + def normalization_factor(self) -> float: |
| 39 | + return self.known_normalization_factor |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def dummy_multiple_populations() -> models.MultiplePopulations: |
| 44 | + interesting_times1 = models.SpecificInterval(([0.5, 1.], [1.1, 2], [2., 3.]), ) |
| 45 | + population1 = models.Population( |
| 46 | + number=1, |
| 47 | + presence=interesting_times1, |
| 48 | + mask=models.Mask.types['No mask'], |
| 49 | + activity=models.Activity.types['Seated'], |
| 50 | + host_immunity=0., |
| 51 | + ) |
| 52 | + |
| 53 | + interesting_times2 = models.SpecificInterval(([0.4, 1.], [1.1, 2]), ) |
| 54 | + population2 = models.Population( |
| 55 | + number=10, |
| 56 | + presence=interesting_times2, |
| 57 | + mask=models.Mask.types['Cloth'], |
| 58 | + activity=models.Activity.types['Heavy exercise'], |
| 59 | + host_immunity=0., |
| 60 | + ) |
| 61 | + |
| 62 | + interesting_times3 = models.SpecificInterval(([5., 6.],), ) |
| 63 | + population3 = models.Population( |
| 64 | + number=5, |
| 65 | + presence=interesting_times3, |
| 66 | + mask=models.Mask.types['Type I'], |
| 67 | + activity=models.Activity.types['Light activity'], |
| 68 | + host_immunity=0.5, |
| 69 | + ) |
| 70 | + return models.MultiplePopulations([population1, population2, population3]) |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +def simple_dynamic_conc_model(data_registry, dummy_multiple_populations): |
| 74 | + interesting_times = models.SpecificInterval(([0.5, 1.], [1.1, 2], [2., 3.]), ) |
| 75 | + return KnownConcentrationModelBase( |
| 76 | + data_registry, |
| 77 | + room = models.Room(75, models.PiecewiseConstant((0., 24.), (293,))), |
| 78 | + ventilation = models.AirChange(interesting_times, 100), |
| 79 | + known_multiple_populations = dummy_multiple_populations, |
| 80 | + known_removal_rate = 10., |
| 81 | + known_min_background_concentration = known_min_background_concentration, |
| 82 | + known_normalization_factor = 10.) |
| 83 | + |
| 84 | +def test_multiple_populations(simple_dynamic_conc_model): |
| 85 | + assert isinstance(simple_dynamic_conc_model.population, models.MultiplePopulations) |
| 86 | + assert np.all([isinstance(population, models.SimplePopulation) for population in simple_dynamic_conc_model.population.populations]) |
| 87 | + |
| 88 | +def test_state_change_times(simple_dynamic_conc_model): |
| 89 | + expected_transition_times = sorted(set((0.0, 0.4, 0.5, 1., 1.1, 2, 3., 5, 6))) |
| 90 | + result = simple_dynamic_conc_model.state_change_times() |
| 91 | + npt.assert_array_equal(result, expected_transition_times) |
| 92 | + |
| 93 | +def test_first_presence_time(simple_dynamic_conc_model): |
| 94 | + npt.assert_array_equal(simple_dynamic_conc_model._first_presence_time(), 0.4) |
| 95 | + |
| 96 | +def test_concentration(simple_dynamic_conc_model): |
| 97 | + assert isinstance(simple_dynamic_conc_model.concentration(0), float) |
| 98 | + npt.assert_almost_equal(simple_dynamic_conc_model.concentration(0),known_min_background_concentration) |
| 99 | + assert simple_dynamic_conc_model.concentration(0.5) > known_min_background_concentration |
| 100 | + |
0 commit comments