Skip to content

Commit e1cb9a7

Browse files
author
benoit-cty
committed
lint
1 parent 1016d4a commit e1cb9a7

3 files changed

Lines changed: 43 additions & 23 deletions

File tree

codecarbon/core/emissions.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,45 @@ def get_private_infra_emissions(self, energy: Energy, geo: GeoMetadata) -> float
178178
# 3. Update codecarbon/data/private_infra/nordic_emissions.json
179179
# 4. Values should reflect the most recent annual average
180180
#
181-
182-
# Check for Nordic regions (SE1-4, NO1-5, FI) and use static emission factors
183-
nordic_regions = ["SE1", "SE2", "SE3", "SE4", "NO1", "NO2", "NO3", "NO4", "NO5", "FI"]
184-
if geo.region is not None and geo.region.upper() in nordic_regions:
185-
try:
186-
# Get Nordic energy mix data from cache
187-
nordic_data = self._data_source.get_nordic_country_energy_mix_data()
188-
region_data = nordic_data["data"].get(geo.region.upper())
189-
if region_data:
190-
emission_factor_g = region_data["emission_factor"] # gCO2eq/kWh
191-
emission_factor_kg = emission_factor_g / 1000 # Convert to kgCO2eq/kWh
192-
emissions = emission_factor_kg * energy.kWh # kgCO2eq
193-
logger.debug(f"Nordic region {geo.region}: Retrieved emissions using static factor "
194-
+ f"{emission_factor_g} gCO2eq/kWh: {emissions * 1000} g CO2eq"
195-
)
196-
return emissions
197-
except Exception as e:
198-
logger.warning(f"Error loading Nordic emissions data for {geo.region}: {e}. "
199-
+ "Falling back to default emission calculation.")
181+
182+
# Check for Nordic regions (SE1-4, NO1-5, FI) and use static emission factors
183+
nordic_regions = [
184+
"SE1",
185+
"SE2",
186+
"SE3",
187+
"SE4",
188+
"NO1",
189+
"NO2",
190+
"NO3",
191+
"NO4",
192+
"NO5",
193+
"FI",
194+
]
195+
if geo.region is not None and geo.region.upper() in nordic_regions:
196+
try:
197+
# Get Nordic energy mix data from cache
198+
nordic_data = (
199+
self._data_source.get_nordic_country_energy_mix_data()
200+
)
201+
region_data = nordic_data["data"].get(geo.region.upper())
202+
if region_data:
203+
emission_factor_g = region_data[
204+
"emission_factor"
205+
] # gCO2eq/kWh
206+
emission_factor_kg = (
207+
emission_factor_g / 1000
208+
) # Convert to kgCO2eq/kWh
209+
emissions = emission_factor_kg * energy.kWh # kgCO2eq
210+
logger.debug(
211+
f"Nordic region {geo.region}: Retrieved emissions using static factor "
212+
+ f"{emission_factor_g} gCO2eq/kWh: {emissions * 1000} g CO2eq"
213+
)
214+
return emissions
215+
except Exception as e:
216+
logger.warning(
217+
f"Error loading Nordic emissions data for {geo.region}: {e}. "
218+
+ "Falling back to default emission calculation."
219+
)
200220

201221
compute_with_regional_data: bool = (geo.region is not None) and (
202222
geo.country_iso_code.upper() in ["USA", "CAN"]

codecarbon/input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ def _load_static_data() -> None:
6060
path = _get_resource_path("data/hardware/cpu_power.csv")
6161
_CACHE["cpu_power"] = pd.read_csv(path)
6262

63-
6463
# Nordic country energy mix - used for emissions calculations
6564
path = _get_resource_path("data/private_infra/nordic_emissions.json")
6665
with open(path) as f:
6766
_CACHE["nordic_country_energy_mix"] = json.load(f)
6867

68+
6969
# Load static data at module import
7070
_load_static_data()
7171

tests/test_emissions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ def test_get_emissions_PRIVATE_INFRA_unknown_country(self):
173173
assert isinstance(emissions, float)
174174
self.assertAlmostEqual(emissions, 0.475, places=2)
175175

176-
def test_get_emissions_PRIVATE_INFRA_NORDIC_REGION(self):
176+
def test_get_emissions_PRIVATE_INFRA_NORDIC_REGION(self):
177177
# WHEN
178178
# Test Nordic region (Sweden SE2)
179-
179+
180180
emissions = self._emissions.get_private_infra_emissions(
181181
Energy.from_energy(kWh=1.0),
182182
GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2"),
@@ -191,7 +191,7 @@ def test_get_emissions_PRIVATE_INFRA_NORDIC_REGION(self):
191191
def test_get_emissions_PRIVATE_INFRA_NORDIC_FINLAND(self):
192192
# WHEN
193193
# Test Nordic region (Finland)
194-
194+
195195
emissions = self._emissions.get_private_infra_emissions(
196196
Energy.from_energy(kWh=2.5),
197197
GeoMetadata(country_iso_code="FIN", country_name="Finland", region="FI"),

0 commit comments

Comments
 (0)