forked from babybuddy/babybuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8b0f7f
commit af1d179
Showing
12 changed files
with
3,922 additions
and
16 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
core/migrations/0030_weightpercentile_weightpercentile_unique_age_sex.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Generated by Django 4.2.5 on 2023-09-20 10:56 | ||
|
||
from django.db import migrations, models, transaction | ||
from django.utils import dateparse | ||
import csv | ||
|
||
|
||
def add_to_ORM(WeightPercentile: models.Model, alias, filepath: str, sex: str): | ||
with open(filepath) as csvfile: | ||
weight_reader = csv.DictReader(csvfile) | ||
data_list = [] | ||
for row in weight_reader: | ||
data_list.append( | ||
WeightPercentile( | ||
age_in_days=dateparse.parse_duration(f'P{row["Age"]}D'), | ||
sex=sex, | ||
p3_weight=row["P3"], | ||
p15_weight=row["P15"], | ||
p50_weight=row["P50"], | ||
p85_weight=row["P85"], | ||
p97_weight=row["P97"], | ||
) | ||
) | ||
WeightPercentile.objects.using(alias).bulk_create(data_list, batch_size=50) | ||
|
||
|
||
def insert_weight_percentiles(apps, schema_editor): | ||
WeightPercentile: models.Model = apps.get_model("core", "WeightPercentile") | ||
db_alias = schema_editor.connection.alias | ||
with transaction.atomic(): | ||
add_to_ORM( | ||
WeightPercentile, db_alias, "core/migrations/weight_percentile_boys.csv", "boy" | ||
) | ||
with transaction.atomic(): | ||
add_to_ORM( | ||
WeightPercentile, db_alias, "core/migrations/weight_percentile_girls.csv", "girl" | ||
) | ||
|
||
def remove_weight_percentiles(apps, schema_editor): | ||
WeightPercentile: models.Model = apps.get_model("core", "WeightPercentile") | ||
db_alias = schema_editor.connection.alias | ||
with transaction.atomic(): | ||
WeightPercentile.objects.using(db_alias).all().delete() | ||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0029_alter_pumping_options_remove_pumping_time_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="WeightPercentile", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("age_in_days", models.DurationField()), | ||
("p3_weight", models.FloatField()), | ||
("p15_weight", models.FloatField()), | ||
("p50_weight", models.FloatField()), | ||
("p85_weight", models.FloatField()), | ||
("p97_weight", models.FloatField()), | ||
( | ||
"sex", | ||
models.CharField( | ||
choices=[("girl", "Girl"), ("boy", "Boy")], max_length=255 | ||
), | ||
), | ||
], | ||
), | ||
migrations.AddConstraint( | ||
model_name="weightpercentile", | ||
constraint=models.UniqueConstraint( | ||
fields=("age_in_days", "sex"), name="unique_age_sex" | ||
), | ||
), | ||
migrations.RunPython(insert_weight_percentiles, remove_weight_percentiles), | ||
] |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
pkgs.pipenv | ||
pkgs.nodejs_18 | ||
pkgs.nodePackages.gulp | ||
pkgs.sqlite | ||
]; | ||
|
||
# https://devenv.sh/scripts/ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,98 @@ | ||
# -*- coding: utf-8 -*- | ||
from datetime import datetime | ||
from django.utils.translation import gettext as _ | ||
from django.db.models.manager import BaseManager | ||
|
||
import plotly.offline as plotly | ||
import plotly.graph_objs as go | ||
|
||
from reports import utils | ||
|
||
|
||
def weight_change(objects): | ||
def weight_change( | ||
actual_weights: BaseManager, percentile_weights: BaseManager, birthday: datetime | ||
): | ||
""" | ||
Create a graph showing weight over time. | ||
:param objects: a QuerySet of Weight instances. | ||
:param actual_weights: a QuerySet of Weight instances. | ||
:param percentile_weights: a QuerySet of Weight Percentile instances. | ||
:param birthday: a datetime of the child's birthday | ||
:returns: a tuple of the the graph's html and javascript. | ||
""" | ||
objects = objects.order_by("-date") | ||
actual_weights = actual_weights.order_by("-date") | ||
|
||
trace = go.Scatter( | ||
weighing_dates: list[datetime] = list(actual_weights.values_list("date", flat=True)) | ||
measured_weights = list(actual_weights.values_list("weight", flat=True)) | ||
|
||
actual_weights_trace = go.Scatter( | ||
name=_("Weight"), | ||
x=list(objects.values_list("date", flat=True)), | ||
y=list(objects.values_list("weight", flat=True)), | ||
x=weighing_dates, | ||
y=measured_weights, | ||
fill="tozeroy", | ||
mode="lines+markers", | ||
) | ||
|
||
if percentile_weights: | ||
dates = list( | ||
map( | ||
lambda timedelta: birthday + timedelta, | ||
percentile_weights.values_list("age_in_days", flat=True), | ||
) | ||
) | ||
|
||
percentile_weight_3_trace = go.Scatter( | ||
name=_("P3"), | ||
x=dates, | ||
y=list(percentile_weights.values_list("p3_weight", flat=True)), | ||
line={"color": "red"}, | ||
) | ||
percentile_weight_15_trace = go.Scatter( | ||
name=_("P15"), | ||
x=dates, | ||
y=list(percentile_weights.values_list("p15_weight", flat=True)), | ||
line={"color": "orange"}, | ||
) | ||
percentile_weight_50_trace = go.Scatter( | ||
name=_("P50"), | ||
x=dates, | ||
y=list(percentile_weights.values_list("p50_weight", flat=True)), | ||
line={"color": "green"}, | ||
) | ||
percentile_weight_85_trace = go.Scatter( | ||
name=_("P85"), | ||
x=dates, | ||
y=list(percentile_weights.values_list("p85_weight", flat=True)), | ||
line={"color": "orange"}, | ||
) | ||
percentile_weight_97_trace = go.Scatter( | ||
name=_("P97"), | ||
x=dates, | ||
y=list(percentile_weights.values_list("p97_weight", flat=True)), | ||
line={"color": "red"}, | ||
) | ||
|
||
data = [ | ||
actual_weights_trace, | ||
] | ||
layout_args = utils.default_graph_layout_options() | ||
layout_args["barmode"] = "stack" | ||
layout_args["title"] = _("<b>Weight</b>") | ||
layout_args["xaxis"]["title"] = _("Date") | ||
layout_args["xaxis"]["rangeselector"] = utils.rangeselector_date() | ||
layout_args["yaxis"]["title"] = _("Weight") | ||
if percentile_weights: | ||
layout_args["xaxis"]["range"] = [birthday, datetime.now()] | ||
layout_args["yaxis"]["range"] = [0, max(measured_weights) * 1.5] | ||
data.extend( | ||
[ | ||
percentile_weight_97_trace, | ||
percentile_weight_85_trace, | ||
percentile_weight_50_trace, | ||
percentile_weight_15_trace, | ||
percentile_weight_3_trace, | ||
] | ||
) | ||
|
||
fig = go.Figure({"data": [trace], "layout": go.Layout(**layout_args)}) | ||
fig = go.Figure({"data": data, "layout": go.Layout(**layout_args)}) | ||
output = plotly.plot(fig, output_type="div", include_plotlyjs=False) | ||
return utils.split_graph_output(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters