Skip to content

Commit

Permalink
transition rates and leaving care working together
Browse files Browse the repository at this point in the history
  • Loading branch information
amynickolls committed May 28, 2024
1 parent ae9fce4 commit 6749731
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 33 deletions.
15 changes: 12 additions & 3 deletions dm_regional_app/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,18 @@ def exit_rate_table(data):
df.set_index(["from", "to"], inplace=True)
df = df.round(4)

placement = df.pop("From")
df.insert(0, "From", placement)
df[["Age Group", "Placement"]] = df["From"].str.split(" - ", expand=True)

df.columns = ["Placement", "Exit rate"]
placement = df.pop("Placement")
df.insert(0, "Placement", placement)

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

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

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

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

return df
31 changes: 20 additions & 11 deletions dm_regional_app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,28 @@ def __init__(self, *args, **kwargs):
self.initialize_fields(initial_data)

def initialize_fields(self, initial_data):
for index in self.dataframe.index:
field_name = str(index)
initial_value = None

# Attempt to get the initial value using the multiindex
try:
initial_value = initial_data.loc[index]
except KeyError:
# adjusted rates will be None if user has not changed these before, so check
if initial_data is not None:
for index in self.dataframe.index:
field_name = str(index)
initial_value = None

self.fields[field_name] = forms.FloatField(
required=False, initial=initial_value
)
# Attempt to get the initial value using the multiindex
try:
initial_value = initial_data.loc[index]
except KeyError:
initial_value = None

self.fields[field_name] = forms.FloatField(
required=False, initial=initial_value
)
else:
for index in self.dataframe.index:
field_name = str(index)
initial_value = None
self.fields[field_name] = forms.FloatField(
required=False, initial=initial_value
)

def save(self):
transition = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Generated by Django 4.2.10 on 2024-05-28 10:18

from django.db import migrations, models

import dm_regional_app.utils


class Migration(migrations.Migration):
dependencies = [
("dm_regional_app", "0005_alter_savedscenario_adjusted_costs_and_more"),
]

operations = [
migrations.AlterField(
model_name="savedscenario",
name="adjusted_costs",
field=models.JSONField(
decoder=dm_regional_app.utils.DateAwareJSONDecoder,
encoder=dm_regional_app.utils.SeriesAwareJSONEncoder,
null=True,
),
),
migrations.AlterField(
model_name="savedscenario",
name="adjusted_numbers",
field=models.JSONField(
decoder=dm_regional_app.utils.DateAwareJSONDecoder,
encoder=dm_regional_app.utils.SeriesAwareJSONEncoder,
null=True,
),
),
migrations.AlterField(
model_name="savedscenario",
name="adjusted_rates",
field=models.JSONField(
decoder=dm_regional_app.utils.DateAwareJSONDecoder,
encoder=dm_regional_app.utils.SeriesAwareJSONEncoder,
null=True,
),
),
migrations.AlterField(
model_name="sessionscenario",
name="adjusted_costs",
field=models.JSONField(
decoder=dm_regional_app.utils.DateAwareJSONDecoder,
encoder=dm_regional_app.utils.SeriesAwareJSONEncoder,
null=True,
),
),
migrations.AlterField(
model_name="sessionscenario",
name="adjusted_numbers",
field=models.JSONField(
decoder=dm_regional_app.utils.DateAwareJSONDecoder,
encoder=dm_regional_app.utils.SeriesAwareJSONEncoder,
null=True,
),
),
migrations.AlterField(
model_name="sessionscenario",
name="adjusted_rates",
field=models.JSONField(
decoder=dm_regional_app.utils.DateAwareJSONDecoder,
encoder=dm_regional_app.utils.SeriesAwareJSONEncoder,
null=True,
),
),
]
6 changes: 3 additions & 3 deletions dm_regional_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ class AbstractScenario(models.Model):
encoder=SeriesAwareJSONEncoder, decoder=DateAwareJSONDecoder
)
adjusted_rates = models.JSONField(
encoder=SeriesAwareJSONEncoder, decoder=DateAwareJSONDecoder
encoder=SeriesAwareJSONEncoder, decoder=DateAwareJSONDecoder, null=True
)
adjusted_numbers = models.JSONField(
encoder=SeriesAwareJSONEncoder, decoder=DateAwareJSONDecoder
encoder=SeriesAwareJSONEncoder, decoder=DateAwareJSONDecoder, null=True
)
historic_stock = models.JSONField(
encoder=SeriesAwareJSONEncoder, decoder=DateAwareJSONDecoder
)
adjusted_costs = models.JSONField(
encoder=SeriesAwareJSONEncoder, decoder=DateAwareJSONDecoder
encoder=SeriesAwareJSONEncoder, decoder=DateAwareJSONDecoder, null=True
)

class Meta:
Expand Down
10 changes: 5 additions & 5 deletions dm_regional_app/templates/dm_regional_app/views/exit_rates.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

{% block content %}
<br>
<h1>Adjust transition rates</h1>
<h1>Adjust exit rates</h1>
<br>
<p>You may want to edit the number of children per year entering a placement type to give a more accurate
<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>
Rates are applied to the daily population of children. For example, a rate of 0.5 from Fostering to Residential means
that each day, 50% of children in Fostering will move to Residential.<br>
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>
</p>
<div class="table-responsive">
<table id="transition-rate" class="table table-hover">
Expand All @@ -22,7 +22,7 @@ <h1>Adjust transition rates</h1>
<tbody>
{{ exit_rate_table | convert_data_frame_to_html_table_rows_form:form | safe}}
<tr>
<td colspan="2">
<td colspan="3">
<div class="p-2 bd-highlight"><a class="btn btn-secondary" href="adjusted">Back</a></div>
</td>
<td>
Expand Down
40 changes: 29 additions & 11 deletions dm_regional_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,30 @@ def router_handler(request):
"uasc": "all",
}

# this is stand in code to be edited when prediction page is improved
prediction_filters = {"buckets": []}
prediction_parameters = {
"reference_start_date": datacontainer.start_date,
# delete - relativedelta(months=6) just for testing before datacontainer fix
"reference_end_date": datacontainer.end_date - relativedelta(months=6),
"reference_end_date": datacontainer.end_date,
"prediction_start_date": None,
"prediction_end_date": None,
}
historic_stock = {
"population": {},
"base_rates": [],
"adjusted_rates": [],
}
adjusted_costs = {"adjusted_costs": []}
adjusted_costs = None

adjusted_rates = None

# default_values should define the model default parameters, like reference_date and the stock data and so on. Decide what should be default with Michael
session_scenario, created = SessionScenario.objects.get_or_create(
id=session_scenario_id,
defaults={
"user_id": current_user.id,
"historic_filters": historic_filters,
"prediction_filters": prediction_filters,
"prediction_parameters": prediction_parameters,
"historic_stock": historic_stock,
"adjusted_costs": adjusted_costs,
"adjusted_rates": adjusted_rates,
},
)

Expand Down Expand Up @@ -105,8 +103,18 @@ def exit_rates(request):
)
if form.is_valid():
data = form.save()
session_scenario.adjusted_rates = data
session_scenario.save()

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

session_scenario.adjusted_rates = new_rates
session_scenario.save()

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

else:
Expand Down Expand Up @@ -157,8 +165,18 @@ def transition_rates(request):
)
if form.is_valid():
data = form.save()
session_scenario.adjusted_rates = data
session_scenario.save()

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

session_scenario.adjusted_rates = new_rates
session_scenario.save()

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

else:
Expand Down

0 comments on commit 6749731

Please sign in to comment.