mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-30 13:10:47 +00:00
recommendation api working locally
This commit is contained in:
parent
28bdc119fd
commit
cda0ceb009
2 changed files with 38 additions and 45 deletions
|
|
@ -304,11 +304,6 @@ async def trigger_plan(body: PlanTriggerRequest):
|
||||||
if not property_recommendations:
|
if not property_recommendations:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# We'll unlist the recommendations so they're a bit easier to handle from here onwards
|
|
||||||
property_recommendations = [
|
|
||||||
rec for recommendations_by_type in property_recommendations for rec in recommendations_by_type
|
|
||||||
]
|
|
||||||
|
|
||||||
recommendations[p.id] = property_recommendations
|
recommendations[p.id] = property_recommendations
|
||||||
|
|
||||||
# Finally, we'll prepare data for predicting the impact on SAP
|
# Finally, we'll prepare data for predicting the impact on SAP
|
||||||
|
|
@ -330,25 +325,26 @@ async def trigger_plan(body: PlanTriggerRequest):
|
||||||
'Suspended, no insulation (assumed)': 'Suspended, insulated (assumed)',
|
'Suspended, no insulation (assumed)': 'Suspended, insulated (assumed)',
|
||||||
'Solid, no insulation (assumed)': 'Solid, insulated (assumed)',
|
'Solid, no insulation (assumed)': 'Solid, insulated (assumed)',
|
||||||
}
|
}
|
||||||
for rec in property_recommendations:
|
for recommendations_by_type in property_recommendations:
|
||||||
scoring_dict = {
|
for rec in recommendations_by_type:
|
||||||
"UPRN": p.data["uprn"],
|
scoring_dict = {
|
||||||
"id": "+".join([str(p.id), str(rec["recommendation_id"])]),
|
"UPRN": p.data["uprn"],
|
||||||
"LOCAL_AUTHORITY": p.data["local-authority"],
|
"id": "+".join([str(p.id), str(rec["recommendation_id"])]),
|
||||||
**starting_epc_data.to_dict("records")[0],
|
"LOCAL_AUTHORITY": p.data["local-authority"],
|
||||||
**ending_epc_data.to_dict("records")[0],
|
**starting_epc_data.to_dict("records")[0],
|
||||||
**fixed_data.to_dict("records")[0]
|
**ending_epc_data.to_dict("records")[0],
|
||||||
}
|
**fixed_data.to_dict("records")[0]
|
||||||
|
}
|
||||||
|
|
||||||
# We update the description to indicate it's insulated
|
# We update the description to indicate it's insulated
|
||||||
if rec["type"] == "wall_insulation":
|
if rec["type"] == "wall_insulation":
|
||||||
scoring_dict["WALLS_DESCRIPTION_ENDING"] = scoring_map[p.walls["clean_description"]]
|
scoring_dict["WALLS_DESCRIPTION_ENDING"] = scoring_map[p.walls["clean_description"]]
|
||||||
elif rec["type"] == "floor_insulation":
|
elif rec["type"] == "floor_insulation":
|
||||||
scoring_dict["FLOOR_DESCRIPTION_ENDING"] = scoring_map[p.floor["clean_description"]]
|
scoring_dict["FLOOR_DESCRIPTION_ENDING"] = scoring_map[p.floor["clean_description"]]
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("Implement me")
|
raise NotImplementedError("Implement me")
|
||||||
|
|
||||||
recommendations_scoring_data.append(scoring_dict)
|
recommendations_scoring_data.append(scoring_dict)
|
||||||
|
|
||||||
recommendations_scoring_data = pd.DataFrame(recommendations_scoring_data)
|
recommendations_scoring_data = pd.DataFrame(recommendations_scoring_data)
|
||||||
|
|
||||||
|
|
@ -416,10 +412,14 @@ async def trigger_plan(body: PlanTriggerRequest):
|
||||||
property = [p for p in input_properties if p.id == property_id][0]
|
property = [p for p in input_properties if p.id == property_id][0]
|
||||||
property_predictions = predictions[predictions["property_id"] == str(property_id)]
|
property_predictions = predictions[predictions["property_id"] == str(property_id)]
|
||||||
|
|
||||||
for rec in recommendations[property_id]:
|
for recommendations_by_type in recommendations[property_id]:
|
||||||
rec["sap_points"] = property_predictions[property_predictions["recommendation_id"] == str(
|
for rec in recommendations_by_type:
|
||||||
rec["recommendation_id"]
|
rec["sap_points"] = property_predictions[property_predictions["recommendation_id"] == str(
|
||||||
)]["RDSAP_CHANGE"].values[0]
|
rec["recommendation_id"]
|
||||||
|
)]["RDSAP_CHANGE"].values[0]
|
||||||
|
|
||||||
|
if not rec["sap_points"]:
|
||||||
|
raise ValueError("Sap points missing")
|
||||||
|
|
||||||
input_measures = prepare_input_measures(recommendations[property_id], body.goal)
|
input_measures = prepare_input_measures(recommendations[property_id], body.goal)
|
||||||
|
|
||||||
|
|
@ -441,20 +441,22 @@ async def trigger_plan(body: PlanTriggerRequest):
|
||||||
|
|
||||||
selected_recommendations = {r["id"] for r in solution}
|
selected_recommendations = {r["id"] for r in solution}
|
||||||
|
|
||||||
# For selected recommendations, mark them as default
|
# We'll use the set of selected recommendations to filter the recommendations to upload
|
||||||
for rec in recommendations[property_id]:
|
final_recommendations = [
|
||||||
rec["default"] = rec["recommendation_id"] in selected_recommendations
|
|
||||||
|
|
||||||
for p in input_properties:
|
|
||||||
property_recommendations = [
|
|
||||||
[
|
[
|
||||||
{**rec, "default": True if rec["recommendation_id"] in selected_recommendations else False}
|
{**rec, "default": True if rec["recommendation_id"] in selected_recommendations else False}
|
||||||
for rec in recommendations_by_type
|
for rec in recommendations_by_type
|
||||||
]
|
]
|
||||||
for recommendations_by_type in property_recommendations
|
for recommendations_by_type in recommendations[property_id]
|
||||||
]
|
]
|
||||||
|
|
||||||
input_measures = prepare_input_measures(property_recommendations, body.goal)
|
# We'll also unlist the recommendations so they're a bit easier to handle from here onwards
|
||||||
|
final_recommendations = [
|
||||||
|
rec for recommendations_by_type in final_recommendations for rec in recommendations_by_type
|
||||||
|
]
|
||||||
|
# We update recommendations[property_id]
|
||||||
|
|
||||||
|
recommendations[property_id] = final_recommendations
|
||||||
|
|
||||||
# 1) the property data
|
# 1) the property data
|
||||||
# 2) the property details (epc)
|
# 2) the property details (epc)
|
||||||
|
|
@ -476,16 +478,6 @@ async def trigger_plan(body: PlanTriggerRequest):
|
||||||
if not recommendations_to_upload:
|
if not recommendations_to_upload:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
property_predictions = predictions[predictions["property_id"] == str(p.id)]
|
|
||||||
for rec in recommendations_to_upload:
|
|
||||||
# Insert the prediction for sap points
|
|
||||||
rec["sap_points"] = property_predictions[property_predictions["recommendation_id"] == str(
|
|
||||||
rec["recommendation_id"]
|
|
||||||
)]["RDSAP_CHANGE"].values[0]
|
|
||||||
|
|
||||||
if not rec["sap_points"]:
|
|
||||||
raise ValueError("Sap points missing")
|
|
||||||
|
|
||||||
# Create a plan
|
# Create a plan
|
||||||
new_plan_id = create_plan(
|
new_plan_id = create_plan(
|
||||||
session,
|
session,
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ def prepare_input_measures(property_recommendations, goal):
|
||||||
raise NotImplementedError("Not implemented this gain type - investigate me")
|
raise NotImplementedError("Not implemented this gain type - investigate me")
|
||||||
|
|
||||||
input_measures = []
|
input_measures = []
|
||||||
for rec in property_recommendations:
|
for recs in property_recommendations:
|
||||||
input_measures.append(
|
input_measures.append(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
|
|
@ -26,6 +26,7 @@ def prepare_input_measures(property_recommendations, goal):
|
||||||
"gain": rec[goal_key],
|
"gain": rec[goal_key],
|
||||||
"type": rec["type"]
|
"type": rec["type"]
|
||||||
}
|
}
|
||||||
|
for rec in recs
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue