From 4ebd516d2eae34b733d4deef0fa56331a81dbbe0 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Fri, 9 Aug 2024 14:57:29 +0100 Subject: [PATCH] refactoring router --- backend/app/plan/router.py | 58 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/backend/app/plan/router.py b/backend/app/plan/router.py index f92d5a10..fbdc2323 100644 --- a/backend/app/plan/router.py +++ b/backend/app/plan/router.py @@ -661,15 +661,7 @@ async def trigger_plan(body: PlanTriggerRequest): for key, scored in predictions_dict.items(): all_predictions[key] = pd.concat([all_predictions[key], scored]) - # We now produce predictions for the kwh models - - # Insert the predictions into the recommendations and run the optimiser - # TODO: If a recommendation has a negative impact on SAP, we should remove it - this seems to have become a - # possibility with heating system - # TODO: After optimising, if there are any cheap, quick win measures (e.g. insulate water tank with hot water - # cylinder jacket), we should add these to the recommendations as default - raise Exception("Add the cost impacts into the cost model") - logger.info("Optimising recommendations") + # Insert the predictions into the recommendations, and get the impact summary scoring_epcs = [] # For scoring the kwh models for property_id in recommendations.keys(): property_instance = [p for p in input_properties if p.id == property_id][0] @@ -686,10 +678,33 @@ async def trigger_plan(body: PlanTriggerRequest): # at each phase property_scoring_epcs = property_instance.update_simulation_epcs(impact_summary) scoring_epcs.extend(property_scoring_epcs) + recommendations[property_id] = recommendations_with_impact - input_measures = prepare_input_measures(recommendations_with_impact, body.goal) + # We call the API with the scoring epcs + scoring_epcs = pd.DataFrame(scoring_epcs) + scoring_epcs = kwh_client.transform(data=scoring_epcs, cleaned=cleaned) - current_sap_points = int(property_instance.data["current-energy-efficiency"]) + kwh_simulation_predictions = model_api.predict_all( + df=scoring_epcs, + bucket=get_settings().DATA_BUCKET, + prediction_buckets=get_prediction_buckets(), + model_prefixes=["heating_kwh_predictions", "hotwater_kwh_predictions"], + ) + + # TODO: Costing model, which should include today's costs! + + # Insert the predictions into the recommendations and run the optimiser + # TODO: If a recommendation has a negative impact on SAP, we should remove it - this seems to have become a + # possibility with heating system + # TODO: After optimising, if there are any cheap, quick win measures (e.g. insulate water tank with hot water + # cylinder jacket), we should add these to the recommendations as default + + for p in input_properties.keys(): + if not recommendations[p.id]: + continue + input_measures = prepare_input_measures(recommendations[p.id], body.goal) + + current_sap_points = int(p.data["current-energy-efficiency"]) target_sap_points = epc_to_sap_lower_bound(body.goal_value) sap_gain = CostOptimiser.calculate_sap_gain_with_slack(target_sap_points - current_sap_points) @@ -716,7 +731,7 @@ async def trigger_plan(body: PlanTriggerRequest): "internal_wall_insulation", "external_wall_insulation", "cavity_wall_insulation" ]): ventilation_rec = next( - (r[0] for r in recommendations_with_impact if r[0]["type"] == "mechanical_ventilation"), + (r[0] for r in recommendations[p.id] if r[0]["type"] == "mechanical_ventilation"), None ) @@ -730,29 +745,14 @@ async def trigger_plan(body: PlanTriggerRequest): {**rec, "default": True if rec["recommendation_id"] in selected_recommendations else False} for rec in recommendations_by_type ] - for recommendations_by_type in recommendations_with_impact + for recommendations_by_type in recommendations[p.id] ] # 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 ] - recommendations[property_id] = final_recommendations - - # We call the API with the scoring epcs - scoring_epcs = pd.DataFrame(scoring_epcs) - scoring_epcs = add_features_from_code(scoring_epcs) - scoring_epcs = add_estimate_annual_kwh(scoring_epcs) - # TODO: Drop all potential and env columns - kwh_simulation_predictions = model_api.predict_all( - df=scoring_epcs, - bucket=get_settings().DATA_BUCKET, - prediction_buckets=get_prediction_buckets(), - model_prefixes=["heating_kwh_predictions", "hotwater_kwh_predictions"], - extract_ids=True - ) - - # TODO: Costing model, which should include today's costs! + recommendations[p.id] = final_recommendations # We now insert into the recommendations for property_id in recommendations.keys():