From b6ccc874d168e7a59eff0d60795a72622499b0b2 Mon Sep 17 00:00:00 2001 From: I542771 Date: Wed, 5 Jun 2024 20:06:48 +0200 Subject: [PATCH] refactor and adjust tests --- co2calculator/constants.py | 2 -- co2calculator/data_handlers.py | 3 ++- co2calculator/energy/calculate_energy.py | 19 +++++++++++-------- co2calculator/parameters.py | 8 +++++--- data/conversion_factors_heating.csv | 2 +- data/emission_factors_heating.csv | 2 +- data/test_data_users/heating.csv | 2 +- tests/functional/test_data_code_compliance.py | 1 + tests/unit/test_calculate_energy.py | 12 +++++------- 9 files changed, 27 insertions(+), 24 deletions(-) diff --git a/co2calculator/constants.py b/co2calculator/constants.py index 43d9b58..11f0904 100644 --- a/co2calculator/constants.py +++ b/co2calculator/constants.py @@ -7,8 +7,6 @@ import iso3166 from co2calculator.data_handlers import Airports -KWH_TO_TJ = 277777.77777778 - class HeatingFuel(enum.Enum): """Enum for heating fuel types""" diff --git a/co2calculator/data_handlers.py b/co2calculator/data_handlers.py index ec2df7d..c0d6fe6 100644 --- a/co2calculator/data_handlers.py +++ b/co2calculator/data_handlers.py @@ -122,8 +122,9 @@ def get(self, fuel_type, unit): :return: :rtype: """ + print(f'fuel_type == "{fuel_type.value}" & unit == "{unit}"') selected_factors = self.conversion_factors.query( - f'fuel_type=="{fuel_type}" & unit=="{unit}"' + f'fuel_type == "{fuel_type.value}" & unit == "{unit}"' ) if selected_factors.empty: raise ConversionFactorNotFound( diff --git a/co2calculator/energy/calculate_energy.py b/co2calculator/energy/calculate_energy.py index f695a5c..ce0090d 100644 --- a/co2calculator/energy/calculate_energy.py +++ b/co2calculator/energy/calculate_energy.py @@ -1,7 +1,7 @@ """Function colleciton to calculate energy type co2 emissions""" from typing import Union -from co2calculator.constants import KWH_TO_TJ, Unit +from co2calculator.constants import Unit from co2calculator.data_handlers import ConversionFactors, EmissionFactors from co2calculator.parameters import ( ElectricityEmissionParameters, @@ -31,14 +31,17 @@ def calc_co2_heating( if options is None: options = {} params = HeatingParameters(**options) - emission_params = HeatingEmissionParameters(**params.heating_emission_parameters) + emission_params = HeatingEmissionParameters( + **params.heating_emission_parameters.dict() + ) # Get the co2 factor - co2e = emission_factors.get(params.dict()) + co2e = emission_factors.get(emission_params.dict()) if params.unit is not Unit.KWH: + print(emission_params.fuel_type, params.unit) # Get the conversion factor - conversion_factor = ConversionFactors.get( + conversion_factor = conversion_factors.get( fuel_type=emission_params.fuel_type, unit=params.unit ) @@ -48,7 +51,7 @@ def calc_co2_heating( # co2 equivalents for heating and electricity refer to a consumption of 1 TJ # so consumption needs to be converted to TJ - return consumption_kwh * params.area_share / KWH_TO_TJ * co2e + return consumption_kwh * params.area_share * co2e def calc_co2_electricity( @@ -71,12 +74,12 @@ def calc_co2_electricity( options = {} params = ElectricityParameters(**options) emission_params = ElectricityEmissionParameters( - **params.electricity_emission_parameters + **params.electricity_emission_parameters.dict() ) # Get the co2 factor - co2e = emission_factors.get(params.dict()) + co2e = emission_factors.get(emission_params.dict()) # co2 equivalents for heating and electricity refer to a consumption of 1 TJ # so consumption needs to be converted to TJ - return consumption * emission_params.energy_share / KWH_TO_TJ * co2e + return consumption * params.energy_share / co2e diff --git a/co2calculator/parameters.py b/co2calculator/parameters.py index 553165c..85a9942 100644 --- a/co2calculator/parameters.py +++ b/co2calculator/parameters.py @@ -158,7 +158,9 @@ class ElectricityEmissionParameters(BaseModel): category: EmissionCategory = EmissionCategory.ELECTRICITY fuel_type: Union[ElectricityFuel, str] = ElectricityFuel.PRODUCTION_FUEL_MIX - country_code: CountryCode2 # TODO: Shall we set a default? Or add a watning if not provided? + country_code: Union[ + CountryCode2, str + ] = "DE" # TODO: Shall we set a default? Or add a warning if not provided? @validator("fuel_type", allow_reuse=True) def check_fueltype(cls, v): @@ -170,7 +172,7 @@ def check_fueltype(cls, v): class ElectricityParameters(BaseModel): electricity_emission_parameters: ElectricityEmissionParameters - energy_share: float + energy_share: float = 1.0 @validator("energy_share", allow_reuse=True) def check_energy_share(cls, v): @@ -182,7 +184,7 @@ class HeatingEmissionParameters(BaseModel): category: EmissionCategory = EmissionCategory.HEATING fuel_type: Union[HeatingFuel, str] = HeatingFuel.GAS - country_code: str = "global" + country_code: Union[CountryCode2, str] = "global" @validator("fuel_type", allow_reuse=True) def check_fueltype(cls, v): diff --git a/data/conversion_factors_heating.csv b/data/conversion_factors_heating.csv index 9bd38f5..8c0d03c 100644 --- a/data/conversion_factors_heating.csv +++ b/data/conversion_factors_heating.csv @@ -3,5 +3,5 @@ 1,liquid_gas,kg,14.1 2,coal,kg,6.0 3,pellet,kg,5.4 -4,woodchips,kg,5.2 +4,wood chips,kg,5.2 5,gas,m^3,10.8 diff --git a/data/emission_factors_heating.csv b/data/emission_factors_heating.csv index 9067ebe..25012b0 100644 --- a/data/emission_factors_heating.csv +++ b/data/emission_factors_heating.csv @@ -4,4 +4,4 @@ 2,global,heating,"UK, Department for Business, Energy & Industrial Strategy",LPG,kWh,liquid_gas,kg/kWh,0.21 3,global,heating,"UK, Department for Business, Energy & Industrial Strategy",fuel oil,kWh,oil,kg/kWh,0.27 4,global,heating,"UK, Department for Business, Energy & Industrial Strategy",wood pellets,kWh,pellet,kg/kWh,0.01074 -5,global,heating,"UK, Department for Business, Energy & Industrial Strategy",wood chips,kWh,woodchips,kg/kWh,0.01074 +5,global,heating,"UK, Department for Business, Energy & Industrial Strategy",wood chips,kWh,wood chips,kg/kWh,0.01074 diff --git a/data/test_data_users/heating.csv b/data/test_data_users/heating.csv index 1a4edda..925ecfd 100644 --- a/data/test_data_users/heating.csv +++ b/data/test_data_users/heating.csv @@ -1,5 +1,5 @@ group_id;building;timestamp;consumption;fuel_type;unit;group_share -1;;2018-01-01;250;woodchips;kg;1.0 +1;;2018-01-01;250;wood chips;kg;1.0 2;;2018-01-01;29653;gas;kWh;0.5 3;;2018-01-01;47569;oil;kWh;1.0 4;;2018-01-01;35124;district_heating;kWh;0.2 diff --git a/tests/functional/test_data_code_compliance.py b/tests/functional/test_data_code_compliance.py index e45cb79..ad2ac20 100644 --- a/tests/functional/test_data_code_compliance.py +++ b/tests/functional/test_data_code_compliance.py @@ -97,6 +97,7 @@ def test_defaults(default_parameters): """Test if default parameters are available in the csv files""" # Get the emission factor for the default parameter combination + print(default_parameters().dict()) co2e = emission_factors.get(default_parameters().dict()) assert isinstance( co2e, float diff --git a/tests/unit/test_calculate_energy.py b/tests/unit/test_calculate_energy.py index cb7f3bb..032cb74 100644 --- a/tests/unit/test_calculate_energy.py +++ b/tests/unit/test_calculate_energy.py @@ -4,11 +4,10 @@ def test_heating_woodchips(): - """Test co2e calculation for heating: woodchips""" + """Test co2e calculation for heating: wood chips""" # Given parameters consumption = 250 - co2e_kg_expected = 43.63 - + co2e_kg_expected = 13.962 func_options = { # Given parameters "heating_emission_parameters": { @@ -16,7 +15,6 @@ def test_heating_woodchips(): }, "unit": "kg", # conversion factor to kWh = 5.4 } - # Calculate co2e co2e = energy.calc_co2_heating(consumption=consumption, options=func_options) @@ -27,13 +25,13 @@ def test_heating_woodchips(): def test_electricity(): """Test co2e calculation for electricity""" # Given parameters - fuel_type = "german_energy_mix" consumption_kwh = 10000 - co2e_kg_expected = 3942.65 # emission factor: 109518 kg/TJ + co2e_kg_expected = 22265 func_options = { "electricity_emission_parameters": { - "fuel_type": "german_energy_mix" # emission factor: 109518 kg/TJ + "fuel_type": "production fuel mix", + "country_code": "DE", } }