Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mlco2_codecarbon /tests /test_emissions.py
| import unittest | |
| from unittest.mock import patch | |
| from codecarbon.core.emissions import Emissions | |
| from codecarbon.core.units import Energy | |
| from codecarbon.external.geography import CloudMetadata, GeoMetadata | |
| from codecarbon.input import DataSource | |
| from tests.testutils import get_test_data_source | |
| class TestEmissions(unittest.TestCase): | |
| def setUp(self) -> None: | |
| # GIVEN | |
| self._data_source = get_test_data_source() | |
| self._emissions = Emissions(self._data_source) | |
| def test_get_emissions_CLOUD_AWS(self): | |
| # WHEN | |
| emissions = self._emissions.get_cloud_emissions( | |
| Energy.from_energy(kWh=0.6), | |
| CloudMetadata(provider="aws", region="us-east-1"), | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.285, places=2) | |
| def test_emissions_CLOUD_AZURE(self): | |
| # WHEN | |
| emissions = self._emissions.get_cloud_emissions( | |
| Energy.from_energy(kWh=1.5), | |
| CloudMetadata(provider="azure", region="eastus"), | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.7125, places=2) | |
| def test_emissions_CLOUD_GCP(self): | |
| emissions = self._emissions.get_cloud_emissions( | |
| Energy.from_energy(kWh=0.01), | |
| CloudMetadata(provider="gcp", region="us-central1"), | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.0010, places=2) | |
| def test_get_carbon_intensity_per_source_data(self): | |
| # pytest tests/test_emissions.py::TestEmissions::test_get_carbon_intensity_per_source_data | |
| carbon_intensity = DataSource().get_carbon_intensity_per_source_data() | |
| self.assertEqual(len(carbon_intensity.keys()), 21) | |
| self.assertGreater(carbon_intensity["coal"], 800) | |
| self.assertLess(carbon_intensity["wind"], 80) | |
| def test_get_emissions_PRIVATE_INFRA_FRA(self): | |
| """ | |
| European country is a specific case as we have there carbon intensity to | |
| without computation. | |
| """ | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=1), | |
| GeoMetadata(country_iso_code="FRA", country_name="France"), | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 56.04 / 1000, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_UNKNOWN(self): | |
| """ | |
| Test with a country that is not in the list of known countries. | |
| """ | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=1_000), | |
| GeoMetadata(country_iso_code="UNK", country_name="Unknown"), | |
| ) | |
| # THEN | |
| carbon_intensity_per_source = ( | |
| DataSource().get_carbon_intensity_per_source_data() | |
| ) | |
| default_emissions = carbon_intensity_per_source.get("world_average") | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, default_emissions, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_NOR(self): | |
| """ | |
| Norway utilises hydropower more than any other country around the globe | |
| """ | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=1), | |
| GeoMetadata(country_iso_code="NOR", country_name="Norway"), | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 26.4 / 1_000, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_USA_WITH_REGION(self): | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=0.3), | |
| GeoMetadata( | |
| country_iso_code="USA", country_name="United States", region="Illinois" | |
| ), | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.11, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_USA_WITHOUT_REGION(self): | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=0.3), | |
| GeoMetadata(country_iso_code="USA", country_name="United States"), | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.115, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_USA_WITHOUT_COUNTRYNAME(self): | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=0.3), GeoMetadata(country_iso_code="USA") | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.115, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_CANADA_WITHOUT_REGION(self): | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=3), | |
| GeoMetadata(country_iso_code="CAN", country_name="Canada"), | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.5101, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_CANADA_WITH_REGION(self): | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=3), | |
| GeoMetadata( | |
| country_iso_code="CAN", country_name="Canada", region="ontario" | |
| ), | |
| ) | |
| manually_computed_emissions = ( | |
| 3 * (0.995725971 * 0.0 + 0.8166885263 * 0.1 + 0.7438415916 * 8.6) / 100.1 | |
| ) | |
| # THEN | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, manually_computed_emissions, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_unknown_country(self): | |
| """ | |
| If we do not know the country we fallback to a default value. | |
| """ | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=1), | |
| GeoMetadata(country_iso_code="AAA", country_name="unknown"), | |
| ) | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.475, places=2) | |
| def test_get_emissions_PRIVATE_INFRA_without_country_metadata(self): | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=1), | |
| GeoMetadata(country_iso_code=None, country_name=None, region=None), | |
| ) | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.475, places=2) | |
| def test_private_infra_uses_forced_intensity_when_set(self, mocked_get_emissions): | |
| emissions_calculator = Emissions( | |
| self._data_source, force_carbon_intensity_g_co2e_kwh=50.0 | |
| ) | |
| emissions = emissions_calculator.get_private_infra_emissions( | |
| Energy.from_energy(kWh=2), | |
| GeoMetadata(country_iso_code="CAN", country_name="Canada"), | |
| ) | |
| self.assertAlmostEqual(emissions, 0.1, places=6) | |
| mocked_get_emissions.assert_not_called() | |
| def test_cloud_uses_forced_intensity_when_set(self): | |
| emissions_calculator = Emissions( | |
| self._data_source, force_carbon_intensity_g_co2e_kwh=100.0 | |
| ) | |
| emissions = emissions_calculator.get_cloud_emissions( | |
| Energy.from_energy(kWh=2), | |
| CloudMetadata(provider="aws", region="us-east-1"), | |
| ) | |
| self.assertAlmostEqual(emissions, 0.2, places=6) | |
| def test_get_emissions_PRIVATE_INFRA_NORDIC_REGION(self): | |
| # WHEN | |
| # Test Nordic region (Sweden SE2) | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=1.0), | |
| GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2"), | |
| ) | |
| # THEN | |
| # Nordic regions use static emission factors from the JSON file | |
| # SE2 has an emission factor specified in nordic_country_energy_mix.json | |
| assert isinstance(emissions, float) | |
| self.assertAlmostEqual(emissions, 0.018, places=6) | |
| def test_get_emissions_PRIVATE_INFRA_NORDIC_FINLAND(self): | |
| # WHEN | |
| # Test Nordic region (Finland) | |
| emissions = self._emissions.get_private_infra_emissions( | |
| Energy.from_energy(kWh=2.5), | |
| GeoMetadata(country_iso_code="FIN", country_name="Finland", region="FI"), | |
| ) | |
| # THEN | |
| # Finland (FI) should use Nordic static emission factors | |
| assert isinstance(emissions, float) | |
| expected_emissions = 0.072 * 2.5 | |
| self.assertAlmostEqual(emissions, expected_emissions, places=6) | |
| def test_get_emissions_PRIVATE_INFRA_NORDIC_REGION_uses_static_factor_without_token( | |
| self, | |
| ): | |
| # GIVEN | |
| energy = Energy.from_energy(kWh=1.0) | |
| geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2") | |
| # WHEN | |
| emissions = self._emissions.get_private_infra_emissions(energy, geo) | |
| # THEN | |
| expected_country = self._emissions.get_country_emissions(energy, geo) | |
| nordic_data = self._data_source.get_nordic_country_energy_mix_data() | |
| emission_factor_g = nordic_data["data"]["SE2"]["emission_factor"] | |
| expected_nordic = (emission_factor_g / 1000) * energy.kWh | |
| self.assertAlmostEqual(emissions, expected_nordic, places=6) | |
| self.assertNotAlmostEqual(emissions, expected_country, places=4) | |
| def test_try_get_nordic_region_emissions_returns_none_without_region(self): | |
| # GIVEN | |
| energy = Energy.from_energy(kWh=1.0) | |
| geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region=None) | |
| # WHEN | |
| emissions = self._emissions._try_get_nordic_region_emissions(energy, geo) | |
| # THEN | |
| self.assertIsNone(emissions) | |
| def test_try_get_nordic_region_emissions_returns_none_for_non_nordic_region(self): | |
| # GIVEN | |
| energy = Energy.from_energy(kWh=1.0) | |
| geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="XYZ") | |
| # WHEN | |
| emissions = self._emissions._try_get_nordic_region_emissions(energy, geo) | |
| # THEN | |
| self.assertIsNone(emissions) | |
| def test_nordic_admin_regions_fall_back_to_country_without_regional_lookup(self): | |
| energy = Energy.from_energy(kWh=1.0) | |
| admin_regions = [ | |
| ("SWE", "Sweden", "Stockholm County"), | |
| ("NOR", "Norway", "Oslo"), | |
| ("FIN", "Finland", "Uusimaa"), | |
| ] | |
| for country_iso_code, country_name, region in admin_regions: | |
| with self.subTest(country_iso_code=country_iso_code, region=region): | |
| geo = GeoMetadata( | |
| country_iso_code=country_iso_code, | |
| country_name=country_name, | |
| region=region, | |
| ) | |
| expected_emissions = self._emissions.get_country_emissions(energy, geo) | |
| with ( | |
| patch.object( | |
| self._data_source, "get_nordic_country_energy_mix_data" | |
| ) as get_nordic_data, | |
| patch.object( | |
| self._data_source, "get_country_emissions_data" | |
| ) as get_regional_emissions_data, | |
| patch.object( | |
| self._data_source, "get_country_energy_mix_data" | |
| ) as get_regional_energy_mix_data, | |
| patch("codecarbon.core.emissions.logger.error") as log_error, | |
| patch("codecarbon.core.emissions.logger.warning") as log_warning, | |
| ): | |
| emissions = self._emissions.get_private_infra_emissions(energy, geo) | |
| self.assertAlmostEqual(emissions, expected_emissions, places=6) | |
| get_nordic_data.assert_not_called() | |
| get_regional_emissions_data.assert_not_called() | |
| get_regional_energy_mix_data.assert_not_called() | |
| log_error.assert_not_called() | |
| log_warning.assert_not_called() | |
| def test_nordic_region_missing_static_data_falls_back_to_country(self): | |
| energy = Energy.from_energy(kWh=1.0) | |
| geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2") | |
| expected_emissions = self._emissions.get_country_emissions(energy, geo) | |
| with ( | |
| patch.object( | |
| self._data_source, | |
| "get_nordic_country_energy_mix_data", | |
| return_value={"data": {}}, | |
| ), | |
| patch.object( | |
| self._data_source, "get_country_emissions_data" | |
| ) as get_regional_emissions_data, | |
| patch.object( | |
| self._data_source, "get_country_energy_mix_data" | |
| ) as get_regional_energy_mix_data, | |
| ): | |
| emissions = self._emissions.get_private_infra_emissions(energy, geo) | |
| self.assertAlmostEqual(emissions, expected_emissions, places=6) | |
| get_regional_emissions_data.assert_not_called() | |
| get_regional_energy_mix_data.assert_not_called() | |
| def test_try_get_nordic_region_emissions_returns_none_if_region_data_missing(self): | |
| # GIVEN | |
| energy = Energy.from_energy(kWh=1.0) | |
| geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2") | |
| # WHEN | |
| with patch.object( | |
| self._data_source, | |
| "get_nordic_country_energy_mix_data", | |
| return_value={"data": {}}, | |
| ): | |
| emissions = self._emissions._try_get_nordic_region_emissions(energy, geo) | |
| # THEN | |
| self.assertIsNone(emissions) | |
| def test_try_get_nordic_region_emissions_returns_none_on_data_loading_error(self): | |
| # GIVEN | |
| energy = Energy.from_energy(kWh=1.0) | |
| geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2") | |
| # WHEN | |
| with patch.object( | |
| self._data_source, | |
| "get_nordic_country_energy_mix_data", | |
| side_effect=Exception("boom"), | |
| ): | |
| emissions = self._emissions._try_get_nordic_region_emissions(energy, geo) | |
| # THEN | |
| self.assertIsNone(emissions) | |
Xet Storage Details
- Size:
- 14.3 kB
- Xet hash:
- 1943d209021bb19a8a95fb06ccf385d6d4c4809db688bf74e04c6809fc458081
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.