Skip to content

Commit 4fad553

Browse files
committed
minor fixes and wip user exposures, sources & treatments
1 parent 7e8810e commit 4fad553

15 files changed

+814
-81
lines changed

qmra/risk_assessment/forms.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from qmra.risk_assessment.models import Inflow, DefaultTreatments, Treatment, \
99
RiskAssessment
10+
from qmra.user.models import User
1011

1112

1213
def _zero_if_none(x): return x if x is not None else 0
@@ -27,7 +28,7 @@ class Meta:
2728
def __init__(self, *args, **kwargs):
2829
super().__init__(*args, **kwargs)
2930
self.fields["source_name"].label = "Select a source water type to add pathogen concentrations"
30-
self.fields['exposure_name'].widget.attrs['min'] = 0
31+
self.fields['events_per_year'].widget.attrs['min'] = 0
3132
self.fields['volume_per_event'].widget.attrs['min'] = 0
3233
self.fields['volume_per_event'].label = "Volume per event in liters"
3334
self.helper = FormHelper(self)
@@ -39,6 +40,17 @@ def __init__(self, *args, **kwargs):
3940
# Row("source_name", css_id="source-form")
4041
)
4142

43+
def set_user(self, user: User):
44+
self.fields["exposure_name"].widget.choices = [
45+
["Your Exposures", [(e.name, e.name) for e in user.exposures.all()]],
46+
*self.fields["exposure_name"].widget.choices
47+
]
48+
self.fields["source_name"].widget.choices = [
49+
["Your Sources", [(s.name, s.name) for s in user.sources.all()]],
50+
*self.fields["source_name"].widget.choices
51+
]
52+
return self
53+
4254
def clean(self):
4355
cleaned_data = super().clean()
4456
if cleaned_data["events_per_year"] <= 0:
@@ -139,7 +151,7 @@ def __init__(self, *args, **kwargs):
139151
self.helper.form_tag = False
140152
self.helper.disable_csrf = True
141153
self.helper.label_class = "text-muted small"
142-
self.fields['name'].choices = DefaultTreatments.choices()
154+
# self.fields['name'].choices = DefaultTreatments.choices()
143155
self.fields['name'].label = ""
144156
self.fields['bacteria_min'].label = ""
145157
self.fields['bacteria_max'].label = ""
@@ -201,6 +213,13 @@ def __init__(self, *args, **kwargs):
201213
"select_treatment",
202214
)
203215

216+
def set_user(self, user: User):
217+
self.fields["select_treatment"].choices = [
218+
*self.fields["select_treatment"].choices,
219+
*[(t.name, t.name) for t in user.treatments.all()]
220+
]
221+
return self
222+
204223

205224
class TreatmentFormSet(TreatmentFormSetBase):
206225

@@ -210,3 +229,10 @@ def __init__(self, *args, **kwargs):
210229
self.helper.form_tag = False
211230
if not kwargs.get("queryset", False):
212231
self.queryset = Treatment.objects.none()
232+
233+
def set_user(self, user: User):
234+
self.form.base_fields["name"].choices = [
235+
*self.form.base_fields['name'].choices,
236+
*[(t.name, t.name) for t in user.treatments.all()]
237+
]
238+
return self
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Generated by Django 5.0.6 on 2025-02-05 06:54
2+
3+
import django.db.models.deletion
4+
import uuid
5+
from django.conf import settings
6+
from django.db import migrations, models
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('risk_assessment', '0002_alter_inflow_pathogen_and_more'),
13+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='UserExposure',
19+
fields=[
20+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
21+
('name', models.TextField()),
22+
('description', models.TextField()),
23+
('events_per_year', models.IntegerField()),
24+
('volume_per_event', models.FloatField()),
25+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='exposures', to=settings.AUTH_USER_MODEL)),
26+
],
27+
),
28+
migrations.CreateModel(
29+
name='UserSource',
30+
fields=[
31+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
32+
('name', models.TextField(max_length=64)),
33+
('rotavirus_min', models.FloatField(blank=True, null=True)),
34+
('rotavirus_max', models.FloatField(blank=True, null=True)),
35+
('campylobacter_min', models.FloatField(blank=True, null=True)),
36+
('campylobacter_max', models.FloatField(blank=True, null=True)),
37+
('cryptosporidium_min', models.FloatField(blank=True, null=True)),
38+
('cryptosporidium_max', models.FloatField(blank=True, null=True)),
39+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sources', to=settings.AUTH_USER_MODEL)),
40+
],
41+
),
42+
migrations.CreateModel(
43+
name='UserTreatment',
44+
fields=[
45+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
46+
('name', models.TextField(max_length=64)),
47+
('bacteria_min', models.FloatField(blank=True, null=True)),
48+
('bacteria_max', models.FloatField(blank=True, null=True)),
49+
('viruses_min', models.FloatField(blank=True, null=True)),
50+
('viruses_max', models.FloatField(blank=True, null=True)),
51+
('protozoa_min', models.FloatField(blank=True, null=True)),
52+
('protozoa_max', models.FloatField(blank=True, null=True)),
53+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='treatments', to=settings.AUTH_USER_MODEL)),
54+
],
55+
),
56+
]

qmra/risk_assessment/plots.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
RISK_CATEGORY_BG_COLORS = dict(
77
none='#E2FBAC', min='#FFDDB5', max='#FFECF4'
88
)
9-
MAX_COLOR_SEQ = ["#FF0532",
10-
"#FF506F",
11-
"#FF8DA2"
12-
]
9+
MAX_COLOR_SEQ = [
10+
"hsl(359, 100%, 40%)",
11+
"hsl(359, 100%, 60%)",
12+
"hsl(359, 100%, 80%)",
13+
]
1314

1415
MIN_COLOR_SEQ = [
15-
"#FF873F",
16-
"#FFA570",
17-
"#ED5500"
16+
"hsl(23, 100%, 50%)",
17+
"hsl(23, 100%, 70%)",
18+
"hsl(23, 100%, 90%)",
1819
]
1920

2021
NONE_COLOR_SEQ = [
21-
"#1B6638",
22-
"#46A16A",
23-
"#88D0A5"
22+
"hsl(144, 100%, 40%)",
23+
"hsl(144, 100%, 60%)",
24+
"hsl(144, 100%, 80%)",
2425
]
2526

2627
COLOR_SEQS = dict(min=MIN_COLOR_SEQ, max=MAX_COLOR_SEQ, none=NONE_COLOR_SEQ)
@@ -40,6 +41,7 @@ def risk_plots(risk_assessment_results, output_type="div"):
4041
name=r.pathogen,
4142
marker=dict(color=COLOR_SEQS[r.infection_risk][i % 3])
4243
))
44+
# infection_prob_fig.add_annotation(text=r.pathogen, showarrow=False, xref="paper", yref="paper", x=(i+1)/7, y=0)
4345
dalys_fig.add_trace(go.Box(
4446
x=["Minimum LRV", "Maximum LRV"],
4547
lowerfence=[r.dalys_minimum_lrv_min, r.dalys_maximum_lrv_min],
@@ -50,6 +52,7 @@ def risk_plots(risk_assessment_results, output_type="div"):
5052
name=r.pathogen,
5153
marker=dict(color=COLOR_SEQS[r.dalys_risk][i % 3])
5254
))
55+
# dalys_fig.add_annotation(text=r.pathogen, showarrow=False, xref="paper", yref="paper", x=(i+1)/7, y=0)
5356

5457
infection_prob_fig.update_layout(
5558
boxmode='group',
@@ -60,15 +63,18 @@ def risk_plots(risk_assessment_results, output_type="div"):
6063
xaxis=dict(title="", showgrid=False),
6164
yaxis=dict(title="Probability of infection per year",
6265
showgrid=False),
63-
margin=dict(l=0, r=0, t=30, b=30),
66+
margin=dict(l=(int(output_type == "png") * 30), r=(int(output_type == "png") * 30), t=30, b=30),
6467
legend=dict(
6568
orientation="h",
6669
yanchor="top",
6770
xanchor="center",
6871
x=0.5,
6972
)
7073
)
71-
infection_prob_fig.update_yaxes(type="log")
74+
infection_prob_fig.update_yaxes(type="log",
75+
showexponent='all',
76+
exponentformat='power'
77+
)
7278
infection_prob_fig.add_hline(y=0.0001, line_dash="dashdot",
7379
label=dict(
7480
text="tolerable level",
@@ -90,15 +96,18 @@ def risk_plots(risk_assessment_results, output_type="div"):
9096
plot_bgcolor="#F6F6FF",
9197
xaxis=dict(title="", showgrid=False),
9298
yaxis=dict(title="DALYs pppy", showgrid=False),
93-
margin=dict(l=0, r=0, t=30, b=30),
99+
margin=dict(l=(int(output_type == "png") * 30), r=(int(output_type == "png") * 30), t=30, b=30),
94100
legend=dict(
95101
orientation="h",
96102
yanchor="top",
97103
xanchor="center",
98104
x=0.5,
99105
)
100106
)
101-
dalys_fig.update_yaxes(type="log")
107+
dalys_fig.update_yaxes(type="log",
108+
showexponent='all',
109+
exponentformat='power'
110+
)
102111
dalys_fig.add_hline(y=0.000001, line_dash="dashdot",
103112
label=dict(
104113
text="tolerable level",

0 commit comments

Comments
 (0)