Updated tests and model post_init method

This commit is contained in:
Luis Aleixo 2022-10-20 14:19:49 +02:00
parent 24ef358571
commit ecf99b34cc
2 changed files with 29 additions and 17 deletions

View file

@ -1335,11 +1335,15 @@ class ExposureModel:
The IVRR is the unique term in the exponential of the concentration formula, therefore
there is a check for the diameter-independent elements of the infectious_virus_removal_rate method.
"""
infected_population = self.concentration_model.infected
if isinstance(infected_population, InfectedPopulation) and not np.isscalar(infected_population.expiration.diameter) and not (
all(np.isscalar(self.concentration_model.virus.decay_constant(self.concentration_model.room.humidity, self.concentration_model.room.inside_temp.value(time)) +
self.concentration_model.ventilation.air_exchange(self.concentration_model.room, time)) for time in self.concentration_model.state_change_times())):
raise ValueError("If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant can be arrays at the same time.")
c_model = self.concentration_model
# Check if the diameter is vectorised.
if (isinstance(c_model.infected, InfectedPopulation) and not np.isscalar(c_model.infected.expiration.diameter)
# Check if the diameter-independent elements of the infectious_virus_removal_rate method are vectorised.
and not (
all(np.isscalar(c_model.virus.decay_constant(c_model.room.humidity, c_model.room.inside_temp.value(time)) +
c_model.ventilation.air_exchange(c_model.room, time)) for time in c_model.state_change_times()))):
raise ValueError("If the diameter is an array, none of the ventilation parameters, "
"room volume or virus decay constant can be arrays at the same time.")
def long_range_fraction_deposited(self) -> _VectorisedFloat:

View file

@ -312,8 +312,8 @@ def test_probabilistic_exposure_probability(exposed_population, cm,
@pytest.mark.parametrize(
"active, outside_temp, window_height, opening_length", [
[models.PeriodicInterval(period=120, duration=120), models.PiecewiseConstant((0., 24.),
(np.array([293., 300.]),)), 1., 1.,], # Verify (ventilation) outside_temp vectorisation.
[models.PeriodicInterval(period=120, duration=120), models.PiecewiseConstant((0., 12, 24.),
(np.array([293., 300.]), np.array([305., 310.]),)), 1., 1.,], # Verify (ventilation) outside_temp vectorisation.
[models.PeriodicInterval(period=120, duration=120), models.PiecewiseConstant((0., 24.), (293.,)),
np.array([1., 0.5]), 1.], # Verify (ventilation) window_height vectorisation.
[models.PeriodicInterval(period=120, duration=120), models.PiecewiseConstant((0., 24.), (293.,)),
@ -328,7 +328,8 @@ def test_diameter_vectorisation_window_opening(diameter_dependent_model, sr_mode
window_height=window_height,
opening_length=opening_length)
)
with pytest.raises(ValueError, match="If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant can be arrays at the same time."):
with pytest.raises(ValueError, match="If the diameter is an array, none of the ventilation parameters, "
"room volume or virus decay constant can be arrays at the same time."):
models.ExposureModel(concentration, sr_model, populations[0], cases_model)
@ -341,40 +342,47 @@ def test_diameter_vectorisation_hinged_window(diameter_dependent_model, sr_model
opening_length=1.,
window_width=np.array([1., 0.5]))
)
with pytest.raises(ValueError, match="If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant can be arrays at the same time."):
with pytest.raises(ValueError, match="If the diameter is an array, none of the ventilation parameters, "
"room volume or virus decay constant can be arrays at the same time."):
models.ExposureModel(concentration, sr_model, populations[0], cases_model)
def test_diameter_vectorisation_HEPA_filter(diameter_dependent_model, sr_model, cases_model):
# Verify (ventilation) q_air_mech vectorisation.
concentration = replace(diameter_dependent_model,
ventilation = models.HEPAFilter(active=models.PeriodicInterval(period=120, duration=120), q_air_mech=np.array([0.5, 1.]))
ventilation = models.HEPAFilter(active=models.PeriodicInterval(period=120, duration=120),
q_air_mech=np.array([0.5, 1.]))
)
with pytest.raises(ValueError, match="If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant can be arrays at the same time."):
with pytest.raises(ValueError, match="If the diameter is an array, none of the ventilation parameters, "
"room volume or virus decay constant can be arrays at the same time."):
models.ExposureModel(concentration, sr_model, populations[1], cases_model)
def test_diameter_vectorisation_air_change(diameter_dependent_model, sr_model, cases_model):
# Verify (ventilation) air_exch vectorisation.
concentration = replace(diameter_dependent_model,
ventilation = models.AirChange(active=models.PeriodicInterval(period=120, duration=120), air_exch=np.array([0.5, 1.]))
ventilation = models.AirChange(active=models.PeriodicInterval(period=120, duration=120),
air_exch=np.array([0.5, 1.]))
)
with pytest.raises(ValueError, match="If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant can be arrays at the same time."):
with pytest.raises(ValueError, match="If the diameter is an array, none of the ventilation parameters, "
"room volume or virus decay constant can be arrays at the same time."):
models.ExposureModel(concentration, sr_model, populations[2], cases_model)
@pytest.mark.parametrize(
"inside_temp, humidity, error_message", [
[models.PiecewiseConstant((0., 24.), (np.array([293., 300.]),)), 0.3,
"If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant can be arrays at the same time."], # Verify room inside_temp vectorisation
[models.PiecewiseConstant((0., 12, 24.), (np.array([293., 300.]), np.array([305., 310.]))), 0.3,
"If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant "
"can be arrays at the same time."], # Verify room inside_temp vectorisation
[models.PiecewiseConstant((0., 24.), (293.,)), np.array([0.3, 0.5]),
"If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant can be arrays at the same time."], # Verify room humidity vectorisation
"If the diameter is an array, none of the ventilation parameters, room volume or virus decay constant "
"can be arrays at the same time."], # Verify room humidity vectorisation
]
)
def test_diameter_vectorisation_room(diameter_dependent_model, sr_model, cases_model, inside_temp, humidity, error_message):
concentration = replace(diameter_dependent_model,
room = models.Room(volume=50, inside_temp=inside_temp, humidity=humidity))
# The vectorised volume is not considered in the air_exchange method for the AirChange class.
# The Room volume is not considered in the air_exchange method for the AirChange class.
with pytest.raises(ValueError, match=error_message):
models.ExposureModel(concentration, sr_model, populations[0], cases_model)