Skip to content

Commit

Permalink
Merge branch 'feature/CO2_profile' into 'master'
Browse files Browse the repository at this point in the history
CO₂ Concentration Plot

Closes #307 and #297

See merge request caimira/caimira!428
  • Loading branch information
andrejhenriques committed Jun 16, 2023
2 parents 564a6d0 + 90642c5 commit ac1aa84
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 108 deletions.
2 changes: 1 addition & 1 deletion caimira/apps/calculator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# calculator version. If the calculator needs to make breaking changes (e.g. change
# form attributes) then it can also increase its MAJOR version without needing to
# increase the overall CAiMIRA version (found at ``caimira.__version__``).
__version__ = "4.10"
__version__ = "4.11"

LOG = logging.getLogger(__name__)

Expand Down
34 changes: 31 additions & 3 deletions caimira/apps/calculator/model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ def get_activity_mins(population):

if total_percentage != 100:
raise ValueError(f'The sum of all respiratory activities should be 100. Got {total_percentage}.')


def build_mc_model(self) -> mc.ExposureModel:
def initialize_room(self) -> models.Room:
# Initializes room with volume either given directly or as product of area and height
if self.volume_type == 'room_volume_explicit':
volume = self.room_volume
Expand All @@ -323,7 +322,10 @@ def build_mc_model(self) -> mc.ExposureModel:
humidity = float(self.humidity)
inside_temp = self.inside_temp

room = models.Room(volume=volume, inside_temp=models.PiecewiseConstant((0, 24), (inside_temp,)), humidity=humidity)
return models.Room(volume=volume, inside_temp=models.PiecewiseConstant((0, 24), (inside_temp,)), humidity=humidity)

def build_mc_model(self) -> mc.ExposureModel:
room = self.initialize_room()

infected_population = self.infected_population()

Expand Down Expand Up @@ -357,6 +359,32 @@ def build_mc_model(self) -> mc.ExposureModel:
def build_model(self, sample_size=DEFAULT_MC_SAMPLE_SIZE) -> models.ExposureModel:
return self.build_mc_model().build_model(size=sample_size)

def build_CO2_model(self, sample_size=DEFAULT_MC_SAMPLE_SIZE) -> models.CO2ConcentrationModel:
infected_population: models.InfectedPopulation = self.infected_population().build_model(sample_size)
exposed_population: models.Population = self.exposed_population().build_model(sample_size)

state_change_times = set(infected_population.presence_interval().transition_times())
state_change_times.update(exposed_population.presence_interval().transition_times())
transition_times = sorted(state_change_times)

total_people = [infected_population.people_present(stop) + exposed_population.people_present(stop)
for _, stop in zip(transition_times[:-1], transition_times[1:])]

population = mc.Population(
number=models.IntPiecewiseConstant(transition_times=tuple(transition_times), values=tuple(total_people)),
presence=None,
mask=models.Mask.types[self.mask_type],
activity=activity_distributions[ACTIVITIES[ACTIVITY_TYPES.index(self.activity_type)]['activity']],
host_immunity=0.,
)

# Builds a CO2 concentration model based on model inputs
return mc.CO2ConcentrationModel(
room=self.initialize_room(),
ventilation=self.ventilation(),
CO2_emitters=population,
).build_model(size=sample_size)

def tz_name_and_utc_offset(self) -> typing.Tuple[str, float]:
"""
Return the timezone name (e.g. CET), and offset, in hours, that need to
Expand Down
7 changes: 7 additions & 0 deletions caimira/apps/calculator/report_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def calculate_report_data(form: FormData, model: models.ExposureModel) -> typing
for time1, time2 in zip(times[:-1], times[1:])
])

CO2_model: models.CO2ConcentrationModel = form.build_CO2_model()
CO2_concentrations = {'CO₂': {'concentrations': [
np.array(CO2_model.concentration(float(time))).mean()
for time in times
]}}

prob = np.array(model.infection_probability())
prob_dist_count, prob_dist_bins = np.histogram(prob/100, bins=100, density=True)
prob_probabilistic_exposure = np.array(model.total_probability_rule()).mean()
Expand All @@ -158,6 +164,7 @@ def calculate_report_data(form: FormData, model: models.ExposureModel) -> typing
"prob_probabilistic_exposure": prob_probabilistic_exposure,
"expected_new_cases": expected_new_cases,
"uncertainties_plot_src": uncertainties_plot_src,
"CO2_concentrations": CO2_concentrations,
}


Expand Down
Loading

0 comments on commit ac1aa84

Please sign in to comment.