From 9a62184ab5adb8705a0382d0e2280a1851f2c3b0 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Wed, 7 Aug 2024 18:52:46 +0100 Subject: [PATCH] updating the simulation epcs --- backend/Property.py | 8 +-- backend/app/plan/router.py | 3 + recommendations/Recommendations.py | 100 +++++++++++++---------------- 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/backend/Property.py b/backend/Property.py index a5346643..d1858abe 100644 --- a/backend/Property.py +++ b/backend/Property.py @@ -460,8 +460,8 @@ class Property: sim_epc.update( { - "heating-cost-current": phase_impact["unadjusted_heating_cost"], - "hot-water-cost-current": phase_impact["unadjusted_hot_water_cost"], + "heating-cost-current": phase_impact["epc_heating_cost"], + "hot-water-cost-current": phase_impact["epc_hot_water_cost"], # CO₂ emissions per square metre floor area per year in kg/m². Since CO₂ emissions are in tonnes # per year, we multiply by 1000 to get kg/m² "co2-emiss-curr-per-floor-area": round( @@ -470,9 +470,9 @@ class Property: "co2-emissions-current": phase_impact["carbon"], "current-energy-rating": sap_to_epc(phase_impact["sap"]), "current-energy-efficiency": int(np.floor(phase_impact["sap"])), - "current-energy-cost": phase_impact["unadjusted_energy_cost"], "energy-consumption-current": phase_impact["heat_demand"], - "lighting-cost-current": phase_impact["unadjusted_lighting_cost"], + "lighting-cost-current": phase_impact["epc_lighting_cost"], + "phase": phase } ) updated_simulation_epcs.append(sim_epc) diff --git a/backend/app/plan/router.py b/backend/app/plan/router.py index e4759b7d..95ea7d92 100644 --- a/backend/app/plan/router.py +++ b/backend/app/plan/router.py @@ -872,6 +872,9 @@ async def trigger_plan(body: PlanTriggerRequest): ] recommendations[property_id] = final_recommendations + # We call the API with the scoring epcs + scoring_epcs = pd.DataFrame(scoring_epcs) + # 1) the property data # 2) the property details (epc) # 3) the recommendations diff --git a/recommendations/Recommendations.py b/recommendations/Recommendations.py index 67d38528..9d709639 100644 --- a/recommendations/Recommendations.py +++ b/recommendations/Recommendations.py @@ -1,4 +1,5 @@ import pandas as pd +import numpy as np from backend.Property import Property from typing import List from itertools import groupby @@ -395,14 +396,6 @@ class Recommendations: property_recommendations = recommendations[property_instance.id].copy() - # We calculate the impact by phase - phase_impact = { - prefix: property_predictions[prefix + "_predictions"].groupby("phase")["predictions"].median().reset_index() - for prefix in [ - "sap_change", "heat_demand", "carbon_change", "lighting_cost", "heating_cost", "hot_water_cost" - ] - } - # TODO: should fabric upgrades have an impact on hot water costs/kwh? # TODO: Generally, the costing models are just increasing. Maybe they're including something in the model # that they shouldn't e.g. SAP, carbon, heat demand etc? @@ -446,48 +439,24 @@ class Recommendations: } else: - previous_phase_values = { - "sap": ( - phase_impact["sap_change"][phase_impact["sap_change"]["phase"] == (rec["phase"] - 1)] - ["predictions"].values[0] - ), - "carbon": ( - phase_impact["carbon_change"][phase_impact["carbon_change"]["phase"] == (rec["phase"] - 1)] - ["predictions"].values[0] - ), - "heat_demand": ( - phase_impact["heat_demand"][phase_impact["heat_demand"]["phase"] == (rec["phase"] - 1)] - ["predictions"].values[0] - ), - } - if rec["type"] == "low_energy_lighting": - # Heating and hot water costs shouldn't change - # {'unadjusted_heating_cost': 501.8528134938132, 'unadjusted_hot_water_cost': - # 171.22534405283452, 'unadjusted_lighting_cost': 127.2} - previous_phase_unadjusted_costs = { - "unadjusted_heating_cost": phase_cost["heating_cost"]["predictions"].values[0], - "unadjusted_hot_water_cost": phase_cost["hot_water_cost"]["predictions"].values[0], - "unadjusted_lighting_cost": phase_impact["lighting_cost"][ - phase_impact["lighting_cost"]["phase"] == (rec["phase"] - 1) - ]["predictions"].values[0] - } + previous_phase_values_multiple = [x for x in impact_summary if x["phase"] == (rec["phase"] - 1)] + if len(previous_phase_values_multiple) != 1: + # Take an average of each of the previous phases + keys_to_median = [ + "sap", "carbon", "heat_demand", "epc_heating_cost", "epc_hot_water_cost", + "epc_lighting_cost" + ] + + previous_phase_values = {} + for key in keys_to_median: + values = [item[key] for item in previous_phase_values_multiple] + previous_phase_values[key] = np.median(values) + else: - # update heating and hot water costs - previous_phase_unadjusted_costs = { - "unadjusted_heating_cost": phase_impact["heating_cost"][ - phase_impact["heating_cost"]["phase"] == (rec["phase"] - 1) - ]["predictions"].values[0], - "unadjusted_hot_water_cost": phase_impact["hot_water_cost"][ - phase_impact["hot_water_cost"]["phase"] == (rec["phase"] - 1) - ]["predictions"].values[0], - "unadjusted_lighting_cost": phase_cost["lighting_cost"]["predictions"].values[0] - } + previous_phase_values = previous_phase_values_multiple[0] # We extract the values for the current phase - # TODO: For things like lighting costs for heating and hot water recommendations, we should actually - # update phase_cost since the phase cost should be the same as the previous phase - current_phase_values = { "sap": phase_energy_efficiency_metrics["sap_change"], "carbon": phase_energy_efficiency_metrics["carbon_change"], @@ -510,6 +479,27 @@ class Recommendations: current_phase_values.update(current_phase_costs) + increasing_variables = ["sap"] + decreasing_variables = [ + "carbon", "heat_demand", "epc_heating_cost", "epc_hot_water_cost", "epc_lighting_cost" + ] + # For increasing variables, the new value needs to be higher than the previous, otherwise we set it to + # the previous + # For decreasing variables, the new value should be lower than the previous, otherwise we set it to + # the previous + # In either case, we adjudge the recommendation to have had no/negligible impact + for v in increasing_variables: + current_phase_values[v] = ( + current_phase_values[v] if current_phase_values[v] > previous_phase_values[v] else + previous_phase_values[v] + ) + for v in previous_phase_values: + if v in decreasing_variables: + current_phase_values[v] = ( + current_phase_values[v] if current_phase_values[v] < previous_phase_values[v] else + previous_phase_values[v] + ) + property_phase_impact = { # Increasing "sap": current_phase_values["sap"] - previous_phase_values["sap"], @@ -518,19 +508,19 @@ class Recommendations: # Decreasing "heat_demand": previous_phase_values["heat_demand"] - current_phase_values["heat_demand"], # Decreasing - "unadjusted_heating_cost": ( - previous_phase_values["unadjusted_heating_cost"] - - current_phase_values["unadjusted_heating_cost"] + "epc_heating_cost": ( + previous_phase_values["epc_heating_cost"] - + current_phase_values["epc_heating_cost"] ), # Decreasing - "unadjusted_hot_water_cost": ( - previous_phase_values["unadjusted_hot_water_cost"] - - current_phase_values["unadjusted_hot_water_cost"] + "epc_hot_water_cost": ( + previous_phase_values["epc_hot_water_cost"] - + current_phase_values["epc_hot_water_cost"] ), # Decreasing - "unadjusted_lighting_cost": ( - previous_phase_values["unadjusted_lighting_cost"] - - current_phase_values["unadjusted_lighting_cost"] + "epc_lighting_cost": ( + previous_phase_values["epc_lighting_cost"] - + current_phase_values["epc_lighting_cost"] ) }