Skip to content

Commit

Permalink
extracted CO2 logic to a dedicated route
Browse files Browse the repository at this point in the history
  • Loading branch information
lrdossan committed Sep 3, 2024
1 parent 41ea92c commit ebde0e1
Show file tree
Hide file tree
Showing 16 changed files with 692 additions and 682 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ python -m cern_caimira.apps.calculator
To run with a specific template theme created:

```
python -m cern_caimira.apps.calculator --theme=ui/apps/templates/{theme}
python -m cern_caimira.apps.calculator --theme=cern_caimira/src/cern_caimira/apps/templates/{theme}
```

To run the entire app in a different `APPLICATION_ROOT` path:
Expand Down Expand Up @@ -168,9 +168,11 @@ Running with Visual Studio Code (VSCode):

### Running the tests

Make sure you are in the root directory of the project. Then:

```
pip install -e .[test]
pytest ./caimira
pip install -e .[test]
python -m pytest
```

### Running the profiler
Expand Down
7 changes: 5 additions & 2 deletions caimira/src/caimira/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@
from tornado.options import define, options
import logging

from caimira.api.routes.report_routes import ReportHandler
from caimira.api.routes.report_routes import VirusReportHandler, CO2ReportHandler

define("port", default=8088, help="Port to listen on", type=int)

logging.basicConfig(format="%(message)s", level=logging.INFO)


class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/report", ReportHandler),
(r"/co2_report", CO2ReportHandler),
(r"/virus_report", VirusReportHandler),
]
settings = dict(
debug=True,
)
super(Application, self).__init__(handlers, **settings)


if __name__ == "__main__":
app = Application()
app.listen(options.port)
Expand Down
26 changes: 26 additions & 0 deletions caimira/src/caimira/api/controller/co2_report_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from caimira.calculator.validators.co2.co2_validator import CO2FormData
from caimira.calculator.store.data_registry import DataRegistry


def generate_form_obj(form_data, data_registry):
return CO2FormData.from_dict(form_data=form_data, data_registry=data_registry)


def generate_model(form_obj, data_registry):
sample_size = data_registry.monte_carlo['sample_size']
return form_obj.build_model(sample_size=sample_size)


def generate_report(model):
return dict(model.CO2_fit_params())


def submit_CO2_form(form_data):
data_registry = DataRegistry()

form_obj = generate_form_obj(
form_data=form_data, data_registry=data_registry)
model = generate_model(form_obj=form_obj, data_registry=data_registry)
report_data = generate_report(model=model)

return report_data
30 changes: 0 additions & 30 deletions caimira/src/caimira/api/controller/report_controller.py

This file was deleted.

38 changes: 38 additions & 0 deletions caimira/src/caimira/api/controller/virus_report_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import concurrent.futures
import functools

from caimira.calculator.validators.virus.virus_validator import VirusFormData
from caimira.calculator.store.data_registry import DataRegistry
import caimira.calculator.report.virus_report_data as rg


def generate_form_obj(form_data, data_registry):
return VirusFormData.from_dict(
form_data=form_data,
data_registry=data_registry,
)


def generate_model(form_obj, data_registry):
sample_size = data_registry.monte_carlo['sample_size']
return form_obj.build_model(sample_size=sample_size)


def generate_report_results(form_obj, model):
return rg.calculate_report_data(
form=form_obj,
model=model,
executor_factory=functools.partial(
concurrent.futures.ThreadPoolExecutor, None, # TODO define report_parallelism
),
)


def submit_virus_form(form_data):
data_registry = DataRegistry

form_obj = generate_form_obj(form_data=form_data, data_registry=data_registry)
model = generate_model(form_obj=form_obj, data_registry=data_registry)
report_data = generate_report_results(form_obj=form_obj, model=model)

return report_data
39 changes: 35 additions & 4 deletions caimira/src/caimira/api/routes/report_routes.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import json
import traceback
import tornado.web
import sys

from caimira.api.controller.report_controller import submit_virus_form
from caimira.api.controller.virus_report_controller import submit_virus_form
from caimira.api.controller.co2_report_controller import submit_CO2_form

class ReportHandler(tornado.web.RequestHandler):

class BaseReportHandler(tornado.web.RedirectHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")

def write_error(self, status_code, **kwargs):
self.set_status(status_code)
self.write({"message": kwargs.get('exc_info')[1].__str__()})


class VirusReportHandler(BaseReportHandler):
def post(self):
try:
form_data = json.loads(self.request.body)
Expand All @@ -24,5 +33,27 @@ def post(self):
self.write(response_data)
except Exception as e:
traceback.print_exc()
self.set_status(400)
self.write({"message": str(e)})
self.write_error(status_code=400, exc_info=sys.exc_info())


class CO2ReportHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")

def post(self):
try:
form_data = json.loads(self.request.body)
report_data = submit_CO2_form(form_data)

response_data = {
"status": "success",
"message": "Results generated successfully",
"report_data": report_data,
}

self.write(response_data)
except Exception as e:
traceback.print_exc()
self.write_error(status_code=400, exc_info=sys.exc_info())
60 changes: 60 additions & 0 deletions caimira/src/caimira/calculator/report/co2_report_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from caimira.calculator.validators.co2.co2_validator import CO2FormData
from caimira.calculator.models.models import CO2DataModel


def build_initial_plot(
form: CO2FormData,
) -> dict:
'''
Initial plot with the suggested ventilation state changes.
This method receives the form input and returns the CO2
plot with the respective transition times.
'''
CO2model: CO2DataModel = form.build_model()

occupancy_transition_times = list(CO2model.occupancy.transition_times)

ventilation_transition_times: list = form.find_change_points()
# The entire ventilation changes consider the initial and final occupancy state change
all_vent_transition_times: list = sorted(
[occupancy_transition_times[0]] +
[occupancy_transition_times[-1]] +
ventilation_transition_times)

ventilation_plot: str = form.generate_ventilation_plot(
ventilation_transition_times=all_vent_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],
}

return context


def build_fitting_results(
form: CO2FormData,
) -> dict:
'''
Final fitting results with the respective predictive CO2.
This method receives the form input and returns the fitting results
along with the CO2 plot with the predictive CO2.
'''
CO2model: CO2DataModel = form.build_model()

# Ventilation times after user manipulation from the suggested ventilation state change times.
ventilation_transition_times = list(CO2model.ventilation_transition_times)

# The result of the following method is a dict with the results of the fitting
# algorithm, namely the breathing rate and ACH values. It also returns the
# predictive CO2 result based on the fitting results.
context = dict(CO2model.CO2_fit_params())

# 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'])

return context

This file was deleted.

Loading

0 comments on commit ebde0e1

Please sign in to comment.