|
| 1 | +from zipfile import ZipFile |
| 2 | +import base64 |
| 3 | +from django.db.models import QuerySet |
| 4 | +from django.template.loader import render_to_string |
| 5 | + |
| 6 | +from qmra.risk_assessment.models import RiskAssessment, RiskAssessmentResult, Inflow, Treatment |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | +from qmra.risk_assessment.plots import risk_plots |
| 10 | + |
| 11 | + |
| 12 | +def inflows_as_df(inflows: QuerySet[Inflow]): |
| 13 | + dfs = [] |
| 14 | + for inflow in inflows.all(): |
| 15 | + dfs += [pd.DataFrame({ |
| 16 | + "Pathogen": [inflow.pathogen], |
| 17 | + "Minimum Concentration": [inflow.min], |
| 18 | + "Maximum Concentration": [inflow.max], |
| 19 | + })] |
| 20 | + return pd.concat(dfs) |
| 21 | + |
| 22 | + |
| 23 | +def treatments_as_df(treatments: QuerySet[Treatment]) -> pd.DataFrame: |
| 24 | + dfs = [] |
| 25 | + for t in treatments.all(): |
| 26 | + dfs += [pd.DataFrame({ |
| 27 | + "Treatment": [t.name] * 3, |
| 28 | + "Pathogen group": ["Viruses", "Bacteria", "Protozoa"], |
| 29 | + "Maximum LRV": [t.viruses_max, t.bacteria_max, t.protozoa_max], |
| 30 | + "Minimum LRV": [t.viruses_min, t.bacteria_min, t.protozoa_min] |
| 31 | + })] |
| 32 | + return pd.concat(dfs) |
| 33 | + |
| 34 | + |
| 35 | +def risk_assessment_result_as_df(pathogen: str, r: RiskAssessmentResult) -> pd.DataFrame: |
| 36 | + return pd.DataFrame({ |
| 37 | + ("", "pathogen"): [pathogen] * 2, |
| 38 | + ("", "stat"): ["Maximum LRV", "Minimum LRV"], |
| 39 | + ("Infection prob.", "min"): [ |
| 40 | + r.infection_maximum_lrv_min, r.infection_minimum_lrv_min |
| 41 | + ], |
| 42 | + ("Infection prob.", "25%"): [ |
| 43 | + r.infection_maximum_lrv_q1, r.infection_minimum_lrv_q1 |
| 44 | + ], |
| 45 | + ("Infection prob.", "50%"): [ |
| 46 | + r.infection_maximum_lrv_median, r.infection_minimum_lrv_median |
| 47 | + ], |
| 48 | + ("Infection prob.", "75%"): [ |
| 49 | + r.infection_maximum_lrv_q3, r.infection_minimum_lrv_q3 |
| 50 | + ], |
| 51 | + ("Infection prob.", "max"): [ |
| 52 | + r.infection_maximum_lrv_max, r.infection_minimum_lrv_max |
| 53 | + ], |
| 54 | + ("DALYs pppy", "min"): [ |
| 55 | + r.dalys_maximum_lrv_min, r.dalys_minimum_lrv_min |
| 56 | + ], |
| 57 | + ("DALYs pppy", "25%"): [ |
| 58 | + r.dalys_maximum_lrv_q1, r.dalys_minimum_lrv_q1 |
| 59 | + ], |
| 60 | + ("DALYs pppy", "50%"): [ |
| 61 | + r.dalys_maximum_lrv_median, r.dalys_minimum_lrv_median |
| 62 | + ], |
| 63 | + ("DALYs pppy", "75%"): [ |
| 64 | + r.dalys_maximum_lrv_q3, r.dalys_minimum_lrv_q3 |
| 65 | + ], |
| 66 | + ("DALYs pppy", "max"): [ |
| 67 | + r.dalys_maximum_lrv_max, r.dalys_minimum_lrv_max |
| 68 | + ], |
| 69 | + }) |
| 70 | + |
| 71 | + |
| 72 | +def results_as_df(results: dict[str, RiskAssessmentResult]) -> pd.DataFrame: |
| 73 | + dfs = [] |
| 74 | + for pathogen, r in results.items(): |
| 75 | + dfs += [risk_assessment_result_as_df(pathogen, r)] |
| 76 | + return pd.concat(dfs) |
| 77 | + |
| 78 | + |
| 79 | +def risk_assessment_as_zip(buffer, risk_assessment: RiskAssessment): |
| 80 | + inflows = inflows_as_df(risk_assessment.inflows) |
| 81 | + treatments = treatments_as_df(risk_assessment.treatments) |
| 82 | + results = results_as_df({r.pathogen: r for r in risk_assessment.results.all()}) |
| 83 | + plots = risk_plots(risk_assessment.results.all(), "png") |
| 84 | + report = render_to_string("assessment-result-export.html", |
| 85 | + context=dict(results=risk_assessment.results.all(), |
| 86 | + infection_risk=risk_assessment.infection_risk, |
| 87 | + risk_plot_data=base64.b64encode(plots[0]).decode("utf-8"), |
| 88 | + daly_plot_data=base64.b64encode(plots[1]).decode("utf-8"))) |
| 89 | + with ZipFile(buffer, mode="w") as archive: |
| 90 | + archive.mkdir("exposure-assessment") |
| 91 | + archive.mkdir("results-plots") |
| 92 | + archive.writestr("exposure-assessment/inflows.csv", inflows.to_csv(sep=",", decimal=".", index=False)) |
| 93 | + archive.writestr("exposure-assessment/treatments.csv", treatments.to_csv(sep=",", decimal=".", index=False)) |
| 94 | + archive.writestr(f"{risk_assessment.name}-result.csv", results.to_csv(sep=",", decimal=".", index=False)) |
| 95 | + archive.writestr(f"{risk_assessment.name}-report.html", report) |
| 96 | + archive.writestr("results-plots/infection-probability.png", plots[0]) |
| 97 | + archive.writestr("results-plots/dalys-pppy.png", plots[1]) |
0 commit comments