Skip to content

Commit

Permalink
children entering care table and page added
Browse files Browse the repository at this point in the history
  • Loading branch information
amynickolls committed May 29, 2024
1 parent 6749731 commit a743bcb
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 6 deletions.
29 changes: 29 additions & 0 deletions dm_regional_app/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,32 @@ def exit_rate_table(data):
df.columns = ["Age Group", "Placement", "Exit rate"]

return df


def entry_rate_table(data):
df = data

df = df.reset_index()
df["to"] = df["index"]
df = df[df["to"].apply(lambda x: "Not in care" in x) == False]

df = df.round(4)

df[["Age Group", "Placement"]] = df["to"].str.split(" - ", expand=True)
df.set_index(["to"], inplace=True)

placement = df.pop("Placement")
df.insert(0, "Placement", placement)

age_group = df.pop("Age Group")
df.insert(0, "Age Group", age_group)

df["Age Group"] = df["Age Group"].mask(df["Age Group"].duplicated(), "")

df = df.drop(["index"], axis=1)

print(df)

df.columns = ["Age Group", "Placement", "Entry rate"]

return df
6 changes: 4 additions & 2 deletions dm_regional_app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ def save(self):
)
data["transition"] = data["transition"].apply(str_to_tuple)
data = data.set_index("transition")
# Converting the index to a MultiIndex
data.index = pd.MultiIndex.from_tuples(data.index, names=["from", "to"])

# if index is tuple, convert to a MultiIndex
if all(isinstance(idx, tuple) for idx in data.index):
data.index = pd.MultiIndex.from_tuples(data.index, names=["from", "to"])
# convert dataframe to series
data = data.squeeze()
return data
18 changes: 15 additions & 3 deletions dm_regional_app/templates/dm_regional_app/views/adjusted.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ <h2>There are no children in the historic dataset that match this filter selecti
<h5 class="card-title">To make adjustments, you can view the table by:<br></h5>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#entering_care" role="button" aria-expanded="false" aria-controls="entering_care">
<span>Children entering<br></span>
<span>care each year</span>
<span>care</span>
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#leaving_care" aria-expanded="false" aria-controls="leaving_care">
<span>Children leaving<br></span>
<span>care each year</span>
<span>care</span>
</button>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#rate_change" aria-expanded="false" aria-controls="rate_change">
<span>Rate of change between<br></span>
Expand All @@ -67,7 +67,19 @@ <h5 class="card-title">To make adjustments, you can view the table by:<br></h5>
<div class="accordion-group">
<div class="collapse indent" id="entering_care" data-bs-parent="#tables">
<div class="card card-body">
Entering care chart placeholder
<div class="table-responsive">
<table id="efficiency-hub-table" class="table table-hover table-sm">
<thead>
{{ entry_rate_table | convert_data_frame_to_html_table_headers | safe}}
</thead>
<tbody>
{{ entry_rate_table | convert_data_frame_to_html_table_rows | safe}}
</tbody>
</table>
<div class="d-flex bd-highlight mb-3">
<div class="ms-auto p-2 bd-highlight"><a class="btn btn-primary" href="entry_rates">Edit this table</a></div>
</div>
</div>
</div>
</div>
<div class="collapse indent" id="leaving_care" data-bs-parent="#tables">
Expand Down
39 changes: 39 additions & 0 deletions dm_regional_app/templates/dm_regional_app/views/entry_rates.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends "dm_regional_app/base.html" %}
{% load crispy_forms_tags %}
{% load table_tags %}


{% block content %}
<br>
<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>
</p>
<div class="table-responsive">
<table id="transition-rate" class="table table-hover">
<form method="post">
{% csrf_token %}
<thead>
{{ entry_rate_table | convert_data_frame_to_html_table_headers_form | safe}}
</thead>
<tbody>
{{ entry_rate_table | convert_data_frame_to_html_table_rows_form:form | safe}}
<tr>
<td colspan="3">
<div class="p-2 bd-highlight"><a class="btn btn-secondary" href="adjusted">Back</a></div>
</td>
<td>
<div class="p-2 bd-highlight"><button type="submit" class="btn btn-primary">Save</button></div>
</td>
</tr>
</tbody>
</form>
</table>
</div>

</div>

{% endblock %}
1 change: 1 addition & 0 deletions dm_regional_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
path("adjusted", views.adjusted, name="adjusted"),
path("transition_rates", views.transition_rates, name="transition_rates"),
path("exit_rates", views.exit_rates, name="exit_rates"),
path("entry_rates", views.entry_rates, name="entry_rates"),
]
8 changes: 7 additions & 1 deletion dm_regional_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ def default(self, obj):


def str_to_tuple(string):
return ast.literal_eval(string)
"""
will convert string to a tuple if possible, otherwise will return string
"""
try:
return ast.literal_eval(string)
except Exception:
return string
68 changes: 68 additions & 0 deletions dm_regional_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.urls import reverse

from dm_regional_app.charts import (
entry_rate_table,
exit_rate_table,
historic_chart,
prediction_chart,
Expand Down Expand Up @@ -76,6 +77,68 @@ def router_handler(request):
return redirect(next_url_name)


@login_required
def entry_rates(request):
if "session_scenario_id" in request.session:
pk = request.session["session_scenario_id"]
session_scenario = get_object_or_404(SessionScenario, pk=pk)
# read data
datacontainer = read_data(source=settings.DATA_SOURCE)

historic_data = apply_filters(
datacontainer.enriched_view, session_scenario.historic_filters
)

# Call predict function
prediction = predict(
data=historic_data, **session_scenario.prediction_parameters
)

entry_rates = entry_rate_table(prediction.entry_rates)

if request.method == "POST":
form = DynamicForm(
request.POST,
dataframe=prediction.entry_rates,
initial_data=session_scenario.adjusted_numbers,
)
if form.is_valid():
data = form.save()

if session_scenario.adjusted_numbers is not None:
# if previous rate adjustments have been made, update old series with new adjustments
rate_adjustments = session_scenario.adjusted_numbers
new_numbers = rate_adjustments.combine_first(data)

session_scenario.adjusted_numbers = new_numbers
session_scenario.save()

else:
session_scenario.adjusted_numbers = data
session_scenario.save()
return redirect("adjusted")

else:
form = DynamicForm(
initial_data=session_scenario.adjusted_numbers,
dataframe=prediction.entry_rates,
)

return render(
request,
"dm_regional_app/views/entry_rates.html",
{
"entry_rate_table": entry_rates,
"form": form,
},
)
else:
next_url_name = "router_handler"
# Construct the URL for the router handler view and append the next_url_name as a query parameter
redirect_url = reverse(next_url_name) + "?next_url_name=" + "transition_rates"
return redirect(redirect_url)


@login_required
def exit_rates(request):
if "session_scenario_id" in request.session:
Expand Down Expand Up @@ -284,6 +347,8 @@ def adjusted(request):
rate_adjustment=session_scenario.adjusted_rates
)

print(prediction.entry_rates)

# build chart
chart = prediction_chart(
stats, prediction, **session_scenario.prediction_parameters
Expand All @@ -293,6 +358,8 @@ def adjusted(request):

exit_rates = exit_rate_table(prediction.transition_rates)

entry_rates = entry_rate_table(prediction.entry_rates)

return render(
request,
"dm_regional_app/views/adjusted.html",
Expand All @@ -303,6 +370,7 @@ def adjusted(request):
"empty_dataframe": empty_dataframe,
"transition_rate_table": transition_rates,
"exit_rate_table": exit_rates,
"entry_rate_table": entry_rates,
},
)
else:
Expand Down
2 changes: 2 additions & 0 deletions ssda903/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def predict(
prediction_start_date: Optional[date] = None,
prediction_end_date: Optional[date] = None,
rate_adjustment: Union[pd.Series, Iterable[pd.Series]] = None,
number_adjustment: Union[pd.Series, Iterable[pd.Series]] = None,
) -> Prediction:
"""
Analyses source between start and end, and then predicts the population at prediction_date.
Expand All @@ -41,6 +42,7 @@ def predict(
),
start_date=prediction_start_date,
rate_adjustment=rate_adjustment,
number_adjustment=number_adjustment,
)
prediction_days = (prediction_end_date - prediction_start_date).days
prediction = predictor.predict(prediction_days, progress=False)
Expand Down

0 comments on commit a743bcb

Please sign in to comment.