Skip to content

Commit

Permalink
added validation for dynamic infected population
Browse files Browse the repository at this point in the history
  • Loading branch information
lrdossan committed Jul 19, 2024
1 parent f1e22ad commit 6ff2694
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions caimira/apps/calculator/model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def generate_precise_activity_expiration(self) -> typing.Tuple[typing.Any, ...]:
return (self.precise_activity['physical_activity'], respiratory_dict)

def generate_dynamic_occupancy(self, dynamic_occupancy: typing.List[typing.Dict[str, typing.Any]]):
### Data format validation ###
##### Data format validation #####
for occupancy in dynamic_occupancy:
# Check if each occupancy entry is a dictionary
if not isinstance(occupancy, typing.Dict):
Expand All @@ -426,7 +426,7 @@ def generate_dynamic_occupancy(self, dynamic_occupancy: typing.List[typing.Dict[
raise TypeError(f'Unable to fetch "total_people" key. Got "{dict_keys[0]}".')
else:
value = occupancy["total_people"]
# Check if the total_people value is a non-negative integer
# Check if the value is a non-negative integer
if not isinstance(value, int):
raise ValueError(f"Total number of people should be integer. Got {value}.")
elif not value >= 0:
Expand Down Expand Up @@ -454,7 +454,7 @@ def generate_dynamic_occupancy(self, dynamic_occupancy: typing.List[typing.Dict[
unique_transition_times_sorted = np.array(sorted(set(transition_times)))

if len(values) != len(unique_transition_times_sorted) - 1:
raise ValueError("Cannot compute dynamic occupancy with the inputs provided.")
raise ValueError("Cannot compute dynamic occupancy with the provided inputs.")

population_occupancy: models.IntPiecewiseConstant = models.IntPiecewiseConstant(
transition_times=tuple(unique_transition_times_sorted),
Expand Down Expand Up @@ -482,6 +482,12 @@ def infected_population(self) -> mc.InfectedPopulation:
# If dynamic occupancy is defined, the generator will parse and validate the
# respective input to a format readable by the model - IntPiecewiseConstant.
infected_occupancy, infected_presence = self.generate_dynamic_occupancy(self.dynamic_infected_occupancy)
# If exposed population is static, defined from the "total_people" input, validate
# if every occurency of infected population is less or equal than it.
if isinstance(self.dynamic_exposed_occupancy, typing.List) and len(self.dynamic_exposed_occupancy) == 0:
for infected_people in infected_occupancy.values:
if infected_people >= self.total_people:
raise ValueError('Number of infected people cannot be greater or equal to the number of total people.')
else:
# The number of exposed occupants is the total number of occupants
# minus the number of infected occupants.
Expand Down

0 comments on commit 6ff2694

Please sign in to comment.