Skip to content

Commit

Permalink
added predictive CO2 line and time labels
Browse files Browse the repository at this point in the history
  • Loading branch information
lrdossan committed Aug 9, 2023
1 parent 7a5c894 commit 9b1cbed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
25 changes: 21 additions & 4 deletions caimira/apps/calculator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,21 +398,33 @@ def find_change_points_with_pelt(self, CO2_data: dict):
result_set.add(times[CO2_values.index(max(CO2_np[segment]))])
return list(result_set)

def generate_ventilation_plot(self, CO2_data: dict, transition_times: typing.Optional[list] = None, ventilation_values: typing.Optional[list] = None):
def generate_ventilation_plot(self, CO2_data: dict,
transition_times: typing.Optional[list] = None,
ventilation_values: typing.Optional[list] = None,
predictive_CO2: typing.Optional[list] = None):
times = CO2_data['times']
CO2_data = CO2_data['CO2']

def time_to_hour(time: float) -> str:
minutes = int(np.floor((time % 1) * 60))
minutes_to_display = f'0{minutes}' if minutes < 10 else minutes
return f'{int(np.floor(time))}:{minutes_to_display}'

fig = plt.figure(figsize=(7, 4), dpi=110)
plt.plot(times, CO2_data)
plt.plot(times, CO2_data, label='Input CO₂')

if (transition_times):
for index, time in enumerate(transition_times):
plt.axvline(x = time, color = 'grey', linewidth=0.5, linestyle='--')
plt.text(x = time + 0.04, y = max(predictive_CO2) if predictive_CO2 else max(CO2_data), s=time_to_hour(time))
if ventilation_values:
y_location = (CO2_data[min(range(len(times)), key=lambda i: abs(times[i]-time))])
plt.text(x = time + 0.04, y = y_location, s="{:.2g}".format(ventilation_values[index]))
if (predictive_CO2):
plt.plot(times, predictive_CO2, label='Predictive CO₂')
plt.xlabel('Time of day')
plt.ylabel('Concentration (ppm)')
plt.legend(loc='lower right')
return img2base64(_figure2bytes(fig))

async def post(self, endpoint: str) -> None:
Expand Down Expand Up @@ -443,9 +455,14 @@ async def post(self, endpoint: str) -> None:
report = await asyncio.wrap_future(report_task)

result = dict(report.CO2_fit_params())
ventilation_transition_times = report.ventilation_transition_times

result['fitting_ventilation_type'] = form.fitting_ventilation_type
result['transition_times'] = report.ventilation_transition_times
result['CO2_plot'] = self.generate_ventilation_plot(form.CO2_data, report.ventilation_transition_times[:-1], result['ventilation_values'])
result['transition_times'] = ventilation_transition_times
result['CO2_plot'] = self.generate_ventilation_plot(form.CO2_data,
ventilation_transition_times[:-1],
result['ventilation_values'],
result['predictive_CO2'])
self.finish(result)


Expand Down
4 changes: 2 additions & 2 deletions caimira/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,8 @@ def fun(x):

exhalation_rate = res_dict['x'][0]
ventilation_values = res_dict['x'][1:]

return {"exhalation_rate": exhalation_rate, "ventilation_values": list(ventilation_values)}
predictive_CO2 = self.CO2_concentrations_from_params(exhalation_rate=exhalation_rate, ventilation_values=ventilation_values)
return {"exhalation_rate": exhalation_rate, "ventilation_values": list(ventilation_values), 'predictive_CO2': list(predictive_CO2)}


@dataclass(frozen=True)
Expand Down

0 comments on commit 9b1cbed

Please sign in to comment.