Skip to content

Commit

Permalink
adjusted chart added to all table adjustment pages
Browse files Browse the repository at this point in the history
  • Loading branch information
amynickolls committed Jun 5, 2024
1 parent a743bcb commit 5474591
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 10 deletions.
154 changes: 154 additions & 0 deletions dm_regional_app/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,157 @@ def entry_rate_table(data):
df.columns = ["Age Group", "Placement", "Entry rate"]

return df


def compare_forecast(
historic_data: PopulationStats,
base_forecast: Prediction,
adjusted_forecast: 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 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"]).dt.date

# dataframe containing total children in base forecast
df = base_forecast.population.unstack().reset_index()

df.columns = ["from", "date", "forecast"]
df = df[df["from"].apply(lambda x: "Not in care" in x) == False]
df = df[["date", "forecast"]].groupby(by="date").sum().reset_index()
df["date"] = pd.to_datetime(df["date"]).dt.date

# dataframe containing upper and lower confidence intervals for base forecast
df_ci = base_forecast.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"]

# dataframe containing total children in adjusted forecast
df_af = adjusted_forecast.population.unstack().reset_index()

df_af.columns = ["from", "date", "forecast"]
df_af = df_af[df_af["from"].apply(lambda x: "Not in care" in x) == False]
df_af = df_af[["date", "forecast"]].groupby(by="date").sum().reset_index()
df_af["date"] = pd.to_datetime(df_af["date"]).dt.date

# dataframe containing upper and lower confidence intervals for adjusted forecast
df_df_ci = adjusted_forecast.variance.unstack().reset_index()
df_df_ci.columns = ["bin", "date", "variance"]
df_df_ci = df_df_ci[["date", "variance"]].groupby(by="date").sum().reset_index()
df_df_ci["date"] = pd.to_datetime(df_df_ci["date"]).dt.date
df_df_ci["upper"] = df_af["forecast"] + df_df_ci["variance"]
df_df_ci["lower"] = df_af["forecast"] - df_df_ci["variance"]

# visualise prediction using unstacked dataframe
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="Base confidence interval",
showlegend=True,
)
)

# 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="Adjusted confidence interval",
showlegend=True,
)
)

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

# add adjusted forecast for total children
fig.add_trace(
go.Scatter(
x=df["date"],
y=df["forecast"],
name="Base 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"),
)
)

# 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=14)
),
fillcolor="rgba(105,105,105,0.1)",
layer="above",
)

fig.update_layout(
title="Forecast", xaxis_title="Date", yaxis_title="Number of children"
)
fig.update_yaxes(rangemode="tozero")
fig_html = fig.to_html(full_html=False)
return fig_html
5 changes: 3 additions & 2 deletions dm_regional_app/templates/dm_regional_app/views/adjusted.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ <h5 class="card-title">To make adjustments, you can view the table by:<br></h5>
</div>
<div class="card-body">
<p>Once you have adjusted the table you will be able to see the base
forecast alongside your adjusted forecast to compare. You can
continue to edit the table across each of the views.</p>
forecast alongside your adjusted forecast to compare. You can
continue to edit the table across each of the views.</p>
<p>Rates in these tables include any previous adjustments you might have made.</p>
<div class="accordion-group">
<div class="collapse indent" id="entering_care" data-bs-parent="#tables">
<div class="card card-body">
Expand Down
23 changes: 20 additions & 3 deletions dm_regional_app/templates/dm_regional_app/views/entry_rates.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
<h1>Adjust entry rates</h1>
<br>
<p>You may want to edit the number of children per year entering care to give a more accurate
picture of future forecasting or simply to explore what changes you may expect to see. <br><br>
Rates are applied to the daily population of children. For example, a rate of 0.5 to 10-16 Fostering
would mean that every two days a child would enter this placement.<br>
picture of future forecasting or simply to explore what changes you may expect to see. </p>
<p>Rates are applied to the daily population of children. For example, a rate of 0.5 to 10-16 Fostering
would mean that every two days a child would enter this placement.
</p>
<p>Rate multiplication multiplies the initial base rate produced by the model parameters you selected.
</p>
<div class="table-responsive">
<table id="transition-rate" class="table table-hover">
Expand All @@ -34,6 +36,21 @@ <h1>Adjust entry rates</h1>
</table>
</div>


{% if is_post is True %}
<div class="alert alert-primary" role="alert">
{{chart|safe}}

Adjusted forecast includes all adjustments made to rates: entry rates, exit rates, and transition rates.
</div>

<div class="d-flex bd-highlight mb-3">
<div class="p-2 bd-highlight"><a class="btn btn-warning" href="#">Clear all rate adjustments</a></div>
<div class="ms-auto p-2 bd-highlight"><a class="btn btn-primary" href="{% url 'router_handler' %}?next_url_name=adjusted">Return to adjusted forecast</a></div>
</div>

{% endif %}

</div>

{% endblock %}
22 changes: 20 additions & 2 deletions dm_regional_app/templates/dm_regional_app/views/exit_rates.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
<h1>Adjust exit rates</h1>
<br>
<p>You may want to edit the number of children per year exiting care to give a more accurate
picture of future forecasting or simply to explore what changes you may expect to see. <br><br>
picture of future forecasting or simply to explore what changes you may expect to see. </p>
<p>
Rates are applied to the daily population of children. For example, a rate of 0.5 from 10-16 Fostering
would mean that each day, 50% of children in Fostering will leave care.<br>
would mean that each day, 50% of children in Fostering will leave care.
</p>
<p>Rate multiplication multiplies the initial base rate produced by the model parameters you selected.
</p>
<div class="table-responsive">
<table id="transition-rate" class="table table-hover">
Expand All @@ -34,6 +37,21 @@ <h1>Adjust exit rates</h1>
</table>
</div>



{% if is_post is True %}
<div class="alert alert-primary" role="alert">
{{chart|safe}}

Adjusted forecast includes all adjustments made to rates: entry rates, exit rates, and transition rates.
</div>

<div class="d-flex bd-highlight mb-3">
<div class="p-2 bd-highlight"><a class="btn btn-warning" href="#">Clear all rate adjustments</a></div>
<div class="ms-auto p-2 bd-highlight"><a class="btn btn-primary" href="{% url 'router_handler' %}?next_url_name=adjusted">Return to adjusted forecast</a></div>
</div>

{% endif %}

</div>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ <h1>Adjust transition rates</h1>
</table>
</div>


{% if is_post is True %}
<div class="alert alert-primary" role="alert">
{{chart|safe}}

Adjusted forecast includes all adjustments made to rates: entry rates, exit rates, and transition rates.
</div>

<div class="d-flex bd-highlight mb-3">
<div class="p-2 bd-highlight"><a class="btn btn-warning" href="#">Clear all rate adjustments</a></div>
<div class="ms-auto p-2 bd-highlight"><a class="btn btn-primary" href="{% url 'router_handler' %}?next_url_name=adjusted">Return to adjusted forecast</a></div>
</div>

{% endif %}

</div>

{% endblock %}
Loading

0 comments on commit 5474591

Please sign in to comment.