Fixed tests

This commit is contained in:
Khalim Conn-Kowlessar 2023-06-23 16:03:40 +01:00
parent 4afac71fa0
commit 9f36efb2a1
5 changed files with 49 additions and 33 deletions

View file

@ -1,3 +1,5 @@
import pandas as pd
import re
from epc_api.client import EpcClient from epc_api.client import EpcClient
from model_data.config import EPC_AUTH_TOKEN from model_data.config import EPC_AUTH_TOKEN
from model_data.OpenUprnClient import OpenUprnClient from model_data.OpenUprnClient import OpenUprnClient
@ -135,3 +137,22 @@ class Property(BaseUtility):
self.in_conservation_area = ConservationAreaClient.IN_CONSERVATION_AREA self.in_conservation_area = ConservationAreaClient.IN_CONSERVATION_AREA
else: else:
self.in_conservation_area = ConservationAreaClient.UNKNOWN self.in_conservation_area = ConservationAreaClient.UNKNOWN
def set_year_built(self):
"""
Estimates when the property was built based on as much available data as possible.
"""
if self.full_sap_epc:
return pd.to_datetime(self.full_sap_epc["lodgement-date"]).year
if self.data["construction-age-band"] not in self.DATA_ANOMALY_MATCHES:
# Take the lower limit. If we're pessimistic about the age of the property, that at least means we have
# more options for recommendations if that age falls before the year that insulation in walls became
# common practice
band = [int(x) for x in re.findall(r'\b\d{4}\b', self.data["construction-age-band"])]
return band[0]
# We don't know when the property was built
return None

View file

@ -37,6 +37,7 @@ def handler():
for p in input_properties: for p in input_properties:
p.search_address_epc() p.search_address_epc()
p.set_year_built()
uprns = [p.data['uprn'] for p in input_properties] uprns = [p.data['uprn'] for p in input_properties]
@ -118,14 +119,14 @@ def handler():
# Now, given the components, we want to idenfity upgrade options # Now, given the components, we want to idenfity upgrade options
import pandas as pd import pandas as pd
walls_df = pd.DataFrame( floors_df = pd.DataFrame(
[{"address1": p.address1, **p.walls} for p in input_properties] [{"address1": p.address1, **p.floor} for p in input_properties]
) )
input_properties[7].data["address1"] input_properties[0].data["address1"]
input_properties[7].data["postcode"] input_properties[0].data["postcode"]
walls_df["address1"].values[7] floors_df["address1"].values[0]
walls_df["original_description"].values[7] floors_df["original_description"].values[0]
from model_data.recommendations.WallRecommendations import WallRecommendations from model_data.recommendations.WallRecommendations import WallRecommendations
self = WallRecommendations(property_instance=input_properties[7], uvalue_estimates=uvalue_estimates) self = WallRecommendations(property_instance=input_properties[7], uvalue_estimates=uvalue_estimates)

View file

@ -0,0 +1,15 @@
from model_data.BaseUtility import BaseUtility
from model_data.Property import Property
from model_data.analysis.UvalueEstimations import UvalueEstimations
class FloorRecommendations(BaseUtility):
def __init__(self, property_instance: Property, uvalue_estimates: UvalueEstimations):
self.property = property_instance
self.uvalue_estimates = uvalue_estimates
# For audit purposes, when estimating u values we'll store it
self.estimated_u_value = None
# Will contains a list of recommended measures
self.recommendations = []

View file

@ -215,7 +215,6 @@ class WallRecommendations(BaseUtility):
def __init__(self, property_instance: Property, uvalue_estimates: UvalueEstimations): def __init__(self, property_instance: Property, uvalue_estimates: UvalueEstimations):
self.property = property_instance self.property = property_instance
self.year_built = self._year_property_was_built()
self.uvalue_estimates = uvalue_estimates self.uvalue_estimates = uvalue_estimates
# For audit purposes, when estimating u values we'll store it # For audit purposes, when estimating u values we'll store it
self.estimated_u_value = None self.estimated_u_value = None
@ -236,25 +235,6 @@ class WallRecommendations(BaseUtility):
return True return True
def _year_property_was_built(self):
"""
Estimates when the property was built based on as much available data as possible.
"""
if self.property.full_sap_epc:
return pd.to_datetime(self.property.full_sap_epc["lodgement-date"]).year
if self.property.data["construction-age-band"] not in self.DATA_ANOMALY_MATCHES:
# Take the lower limit. If we're pessimistic about the age of the property, that at least means we have
# more options for recommendations if that age falls before the year that insulation in walls became
# common practice
band = [int(x) for x in re.findall(r'\b\d{4}\b', self.property.data["construction-age-band"])]
return band[0]
# We don't know when the property was built
return None
def recommend(self): def recommend(self):
# if building built after 1990 + we're able to identify U-value + # if building built after 1990 + we're able to identify U-value +
# U-value less than 0.18 and if in or close to a conversation area, # U-value less than 0.18 and if in or close to a conversation area,
@ -277,7 +257,7 @@ class WallRecommendations(BaseUtility):
# We can't detect it's a cavity wall, but it was built after 1990 so likely built with insulation already # We can't detect it's a cavity wall, but it was built after 1990 so likely built with insulation already
# + it already has a U-value WORSE than the building regulations, so we recommend either internal or # + it already has a U-value WORSE than the building regulations, so we recommend either internal or
# external wall insulation # external wall insulation
if (not is_cavity_wall) and (self.year_built >= self.YEAR_WALLS_BUILT_WITH_INSULATION) and ( if (not is_cavity_wall) and (self.property.year_built >= self.YEAR_WALLS_BUILT_WITH_INSULATION) and (
u_value >= self.BUILDING_REGULATIONS_PART_L_MAX_U_VALUE u_value >= self.BUILDING_REGULATIONS_PART_L_MAX_U_VALUE
): ):
# Recommend insulation # Recommend insulation
@ -286,7 +266,7 @@ class WallRecommendations(BaseUtility):
# We can't detect it's a cavity wall, but it was built after 1990 so likely built with insulation already # We can't detect it's a cavity wall, but it was built after 1990 so likely built with insulation already
# + it already has a U-value better than the building regulations, so we don't need to recommend anything # + it already has a U-value better than the building regulations, so we don't need to recommend anything
if (not is_cavity_wall) and (self.year_built >= self.YEAR_WALLS_BUILT_WITH_INSULATION) and ( if (not is_cavity_wall) and (self.property.year_built >= self.YEAR_WALLS_BUILT_WITH_INSULATION) and (
u_value <= self.BUILDING_REGULATIONS_PART_L_MAX_U_VALUE u_value <= self.BUILDING_REGULATIONS_PART_L_MAX_U_VALUE
): ):
# Recommend nothing # Recommend nothing

View file

@ -44,8 +44,6 @@ class TestWallRecommendations:
assert obj.property assert obj.property
assert obj.uvalue_estimates assert obj.uvalue_estimates
assert obj.year_built == 2014
def test_uvalue_0_16(self, input_properties, uvalue_estimates): def test_uvalue_0_16(self, input_properties, uvalue_estimates):
""" """
This tests the wall description Average thermal transmittance 0.16 W/m-¦K This tests the wall description Average thermal transmittance 0.16 W/m-¦K
@ -55,9 +53,9 @@ class TestWallRecommendations:
Since properties built after 1990 are typically built with insulation and this property Since properties built after 1990 are typically built with insulation and this property
already has really good insulation, we do NOT recommend any measures for this property already has really good insulation, we do NOT recommend any measures for this property
""" """
input_properties[0].year_built = 2014
recommender = WallRecommendations(property_instance=input_properties[0], uvalue_estimates=uvalue_estimates) recommender = WallRecommendations(property_instance=input_properties[0], uvalue_estimates=uvalue_estimates)
assert recommender.property.walls["original_description"] == "Average thermal transmittance 0.16 W/m-¦K" assert recommender.property.walls["original_description"] == "Average thermal transmittance 0.16 W/m-¦K"
assert recommender.year_built == 2014
recommender.recommend() recommender.recommend()
# This should be empty # This should be empty
assert recommender.recommendations == [] assert recommender.recommendations == []
@ -71,9 +69,9 @@ class TestWallRecommendations:
This property is not in a conservation area, however it's a flat so we don't recommend external wall insulation This property is not in a conservation area, however it's a flat so we don't recommend external wall insulation
""" """
input_properties[1].year_built = 1930
recommender = WallRecommendations(property_instance=input_properties[1], uvalue_estimates=uvalue_estimates) recommender = WallRecommendations(property_instance=input_properties[1], uvalue_estimates=uvalue_estimates)
assert recommender.property.walls["original_description"] == "Solid brick, as built, no insulation (assumed)" assert recommender.property.walls["original_description"] == "Solid brick, as built, no insulation (assumed)"
assert recommender.year_built == 1930
assert not recommender.ewi_valid assert not recommender.ewi_valid
assert recommender.property.in_conservation_area == "not_in_conservation_area" assert recommender.property.in_conservation_area == "not_in_conservation_area"
assert recommender.property.data["property-type"] == "Flat" assert recommender.property.data["property-type"] == "Flat"
@ -105,10 +103,11 @@ class TestWallRecommendations:
This property is not in a conservation area, however it's a flat so we don't recommend external wall insulation This property is not in a conservation area, however it's a flat so we don't recommend external wall insulation
""" """
input_properties[6].year_built = 1991
recommender = WallRecommendations(property_instance=input_properties[6], uvalue_estimates=uvalue_estimates) recommender = WallRecommendations(property_instance=input_properties[6], uvalue_estimates=uvalue_estimates)
assert recommender.property.walls["original_description"] == "Solid brick, as built, insulated (assumed)" assert recommender.property.walls["original_description"] == "Solid brick, as built, insulated (assumed)"
assert recommender.year_built == 1991
assert not recommender.ewi_valid assert not recommender.ewi_valid
assert recommender.property.in_conservation_area == "not_in_conservation_area" assert recommender.property.in_conservation_area == "not_in_conservation_area"
assert recommender.property.data["property-type"] == "Flat" assert recommender.property.data["property-type"] == "Flat"