Skip to content

Commit

Permalink
chart beautified
Browse files Browse the repository at this point in the history
  • Loading branch information
amynickolls committed May 10, 2024
1 parent 3368e94 commit 5e43735
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 17 deletions.
100 changes: 84 additions & 16 deletions dm_regional_app/charts.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,105 @@
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from demand_model.multinomial.predictor import Prediction

from ssda903.datacontainer import DemandModellingDataContainer
from ssda903.population_stats import PopulationStats


def prediction_chart(historic_data: PopulationStats, prediction: Prediction):
def prediction_chart(historic_data: PopulationStats, prediction: Prediction, **kwargs):
# pop start and end dates to visualise reference period
reference_start_date = kwargs.pop("reference_start_date")
reference_end_date = kwargs.pop("reference_end_date")

# dataframe containing total children in prediction
df = prediction.population.unstack().reset_index()
df.columns = ["from", "date", "forecast"]
df = df[["date", "forecast"]].groupby(by="date").sum().reset_index()
df["date"] = pd.to_datetime(df["date"])
df["date"] = pd.to_datetime(df["date"]).dt.date

# dataframe containing total children in historic data
df_hd = historic_data.stock.unstack().reset_index()
df_hd.columns = ["from", "date", "historic"]
df_hd = df_hd[["date", "historic"]].groupby(by="date").sum().reset_index()
df_hd["date"] = pd.to_datetime(df_hd["date"])
df_hd["date"] = pd.to_datetime(df_hd["date"]).dt.date

df = pd.merge(df, df_hd, how="outer", on="date", indicator=True)
df = df.melt(id_vars=["date"], value_vars=["historic", "forecast"])
# dataframe containing upper and lower confidence intervals
df_ci = prediction.variance.unstack().reset_index()
df_ci.columns = ["bin", "date", "variance"]
df_ci = df_ci[["date", "variance"]].groupby(by="date").sum().reset_index()
df_ci["date"] = pd.to_datetime(df_ci["date"]).dt.date
df_ci["upper"] = df["forecast"] + df_ci["variance"]
df_ci["lower"] = df["forecast"] - df_ci["variance"]

# visualise prediction using unstacked dataframe
fig = px.line(
df,
y="value",
x="date",
labels={
"value": "Number of children",
"date": "Date",
},
color="variable",
fig = go.Figure()

# Display confidence interval as filled shape
fig.add_trace(
go.Scatter(
x=df_ci["date"],
y=df_ci["lower"],
line_color="rgba(255,255,255,0)",
name="Confidence interval",
showlegend=False,
)
)

fig.add_trace(
go.Scatter(
x=df_ci["date"],
y=df_ci["upper"],
fill="tonexty",
fillcolor="rgba(0,176,246,0.2)",
line_color="rgba(255,255,255,0)",
name="Confidence interval",
showlegend=True,
)
)

# add forecast for total children
fig.add_trace(
go.Scatter(
x=df["date"],
y=df["forecast"],
name="Forecast",
line=dict(color="black", width=1.5),
)
)

# add historic data for total children
fig.add_trace(
go.Scatter(
x=df_hd["date"],
y=df_hd["historic"],
name="Historic data",
line=dict(color="black", width=1.5, dash="dot"),
)
)
fig.update_layout(title="Prediction")

# add shaded reference period
fig.add_shape(
type="rect",
xref="x",
yref="paper",
x0=reference_start_date,
y0=0,
x1=reference_end_date,
y1=1,
line=dict(
width=0,
),
label=dict(
text="Reference period", textposition="top center", font=dict(size=15)
),
fillcolor="rgba(0,0,255,0.1)",
layer="above",
)

fig.update_layout(
title="Base forecase", xaxis_title="Date", yaxis_title="Number of children"
)
fig.update_yaxes(rangemode="tozero")
fig_html = fig.to_html(full_html=False)
return fig_html

Expand Down
4 changes: 3 additions & 1 deletion dm_regional_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def prediction(request):
)

# build chart
chart = prediction_chart(stats, prediction)
chart = prediction_chart(
stats, prediction, **session_scenario.prediction_parameters
)

return render(
request,
Expand Down

0 comments on commit 5e43735

Please sign in to comment.