Added the two plot img, plot data to the return data of the fetch return (context). Adapted orders of labels
This commit is contained in:
parent
6e16feb380
commit
8146224392
5 changed files with 41 additions and 24 deletions
|
|
@ -21,14 +21,15 @@ def build_initial_plot(
|
|||
[occupancy_transition_times[-1]] +
|
||||
ventilation_transition_times)
|
||||
|
||||
ventilation_plot: str = form.generate_ventilation_plot(
|
||||
ventilation_transition_times=all_vent_transition_times,
|
||||
vent_plot_img, vent_plot_data = form.generate_ventilation_plot(
|
||||
ventilation_transition_times=ventilation_transition_times,
|
||||
occupancy_transition_times=occupancy_transition_times
|
||||
)
|
||||
|
||||
context = {
|
||||
'CO2_plot': ventilation_plot,
|
||||
'transition_times': [round(el, 2) for el in all_vent_transition_times],
|
||||
'CO2_plot_img': vent_plot_img,
|
||||
'CO2_plot_data': vent_plot_data
|
||||
}
|
||||
|
||||
return context
|
||||
|
|
@ -52,9 +53,12 @@ def build_fitting_results(
|
|||
# predictive CO2 result based on the fitting results.
|
||||
context = dict(CO2model.CO2_fit_params())
|
||||
|
||||
vent_plot_img, vent_plot_data = form.generate_ventilation_plot(ventilation_transition_times=ventilation_transition_times[:-1],
|
||||
predictive_CO2=context['predictive_CO2'])
|
||||
|
||||
# Add the transition times and CO2 plot to the results.
|
||||
context['transition_times'] = ventilation_transition_times
|
||||
context['CO2_plot'] = form.generate_ventilation_plot(ventilation_transition_times=ventilation_transition_times[:-1],
|
||||
predictive_CO2=context['predictive_CO2'])
|
||||
context['CO2_plot_img'] = vent_plot_img
|
||||
context['CO2_plot_data'] = vent_plot_data
|
||||
|
||||
return context
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ class CO2FormData(FormData):
|
|||
"""
|
||||
Perform change point detection using scipy library (find_peaks method) with rolling average of data.
|
||||
Incorporate existing state change candidates and adjust the result accordingly.
|
||||
Returns a list of the detected ventilation state changes, discarding any occupancy state change.
|
||||
Returns a list of the detected ventilation transition times, discarding any occupancy state change.
|
||||
"""
|
||||
times: list = self.CO2_data['times']
|
||||
CO2_values: list = self.CO2_data['CO2']
|
||||
|
|
@ -147,33 +147,46 @@ class CO2FormData(FormData):
|
|||
def generate_ventilation_plot(self,
|
||||
ventilation_transition_times: typing.Optional[list] = None,
|
||||
occupancy_transition_times: typing.Optional[list] = None,
|
||||
predictive_CO2: typing.Optional[list] = None) -> str:
|
||||
predictive_CO2: typing.Optional[list] = None) -> dict:
|
||||
|
||||
# Plot data (x-axis: times; y-axis: CO2 concentrations)
|
||||
times_values: list = self.CO2_data['times']
|
||||
CO2_values: list = self.CO2_data['CO2']
|
||||
|
||||
fig = plt.figure(figsize=(7, 4), dpi=110)
|
||||
plt.plot(times_values, CO2_values, label='Input CO₂')
|
||||
plt.plot(times_values, CO2_values, label='CO₂ Data')
|
||||
|
||||
# Add predictive CO2
|
||||
if (predictive_CO2):
|
||||
plt.plot(times_values, predictive_CO2, label='Predictive CO₂')
|
||||
|
||||
# Add occupancy state changes:
|
||||
if (occupancy_transition_times):
|
||||
for i, time in enumerate(occupancy_transition_times):
|
||||
plt.axvline(x = time, color = 'grey', linewidth=0.5, linestyle='--', label='Occupancy change (from input)' if i == 0 else None)
|
||||
# Add ventilation state changes:
|
||||
# Add ventilation transition times:
|
||||
if (ventilation_transition_times):
|
||||
for i, time in enumerate(ventilation_transition_times):
|
||||
if i == 0:
|
||||
label = 'Ventilation change (detected)' if occupancy_transition_times else 'Ventilation state changes'
|
||||
label = 'Ventilation transition times (suggestion)' if occupancy_transition_times else 'Ventilation transition times'
|
||||
else: label = None
|
||||
plt.axvline(x = time, color = 'red', linewidth=0.5, linestyle='--', label=label)
|
||||
plt.axvline(x = time, color = 'red', linewidth=1, linestyle='--', label=label)
|
||||
|
||||
# Add occupancy changes (UI):
|
||||
if (occupancy_transition_times):
|
||||
for i, time in enumerate(occupancy_transition_times):
|
||||
plt.axvline(x = time, color = 'grey', linewidth=1, linestyle='--', label='Occupancy change (from UI)' if i == 0 else None)
|
||||
|
||||
if (predictive_CO2):
|
||||
plt.plot(times_values, predictive_CO2, label='Predictive CO₂')
|
||||
plt.xlabel('Time of day')
|
||||
plt.ylabel('Concentration (ppm)')
|
||||
plt.legend()
|
||||
return img2base64(_figure2bytes(fig))
|
||||
|
||||
vent_plot_data = {
|
||||
'plot': img2base64(_figure2bytes(fig)),
|
||||
'times': times_values,
|
||||
'CO2': CO2_values,
|
||||
'occ_trans_time': occupancy_transition_times,
|
||||
'vent_trans_time': ventilation_transition_times,
|
||||
'predictive_CO2': predictive_CO2,
|
||||
}
|
||||
|
||||
return img2base64(_figure2bytes(fig)), vent_plot_data
|
||||
|
||||
def population_present_changes(self, infected_presence: models.Interval, exposed_presence: models.Interval) -> typing.List[float]:
|
||||
state_change_times = set(infected_presence.transition_times())
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ def test_integrated_concentration(simple_co2_conc_model):
|
|||
def test_find_change_points(scenario_data, room_volume, max_total_people, start, finish, state_changes, request):
|
||||
'''
|
||||
Specific test of the find_change_points method.
|
||||
Testing the ventilation state changes only.
|
||||
Testing the ventilation transition times only.
|
||||
'''
|
||||
CO2_form_model: CO2FormData = CO2FormData(
|
||||
CO2_data=request.getfixturevalue(scenario_data),
|
||||
|
|
|
|||
|
|
@ -222,7 +222,7 @@ function validateCO2Form() {
|
|||
if (parsedValue.length <= 1) {
|
||||
insertErrorFor(
|
||||
$referenceNode,
|
||||
`'${$ventilationStates.attr('name')}' must have more than one ventilation state change (at least the beggining and end of simulation time).<br />`
|
||||
`'${$ventilationStates.attr('name')}' must have more than one ventilation transition time (at least the beginning and end of simulation time).<br />`
|
||||
);
|
||||
submit = false;
|
||||
}
|
||||
|
|
@ -297,9 +297,9 @@ function displayTransitionTimesHourFormat(start, stop) {
|
|||
|
||||
function displayFittingData(json_response) {
|
||||
$("#DIVCO2_fitting_result").show();
|
||||
$("#CO2_data_plot").attr("src", json_response["CO2_plot"]);
|
||||
$("#CO2_data_plot").attr("src", json_response["CO2_plot_img"]);
|
||||
// Not needed for the form submission
|
||||
delete json_response["CO2_plot"];
|
||||
delete json_response["CO2_plot_img"];
|
||||
delete json_response["predictive_CO2"];
|
||||
// Convert nulls to empty strings in the JSON response
|
||||
if (json_response["room_capacity"] === null) json_response["room_capacity"] = '';
|
||||
|
|
@ -381,7 +381,7 @@ function plotCO2Data(url) {
|
|||
response
|
||||
.json()
|
||||
.then((json_response) => {
|
||||
$("#CO2_data_plot").attr("src", json_response["CO2_plot"])
|
||||
$("#CO2_data_plot").attr("src", json_response["CO2_plot_img"])
|
||||
$("#fitting_ventilation_states").val(`[${json_response["transition_times"]}]`)
|
||||
})
|
||||
.then($("#DIVCO2_fitting_to_submit").show())
|
||||
|
|
|
|||
|
|
@ -349,7 +349,7 @@
|
|||
The dashed lines are suggestions for the ventilation transition times<br>
|
||||
</p>
|
||||
<div id="DIVfitting_ventilation" class="form-group mb-0">
|
||||
<label for="fitting_ventilation_states">Please enter the ventilation state change times, separated by comma - e.g. [8.5, 10, 11.5, 17]. </label>
|
||||
<label for="fitting_ventilation_states">Please enter the ventilation transition times, separated by comma - e.g. [8.5, 10, 11.5, 17]. </label>
|
||||
<div data-tooltip="Default values indicated below correspond to the dashed lines in the above plot - these are only suggestions and can be changed.">
|
||||
<span class="tooltip_text">?</span>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in a new issue