Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it right that the Carbon Intensity of Solar, Wind, ... is ignored in your calculations? #673

Open
headscott opened this issue Sep 20, 2024 · 10 comments
Labels
question Further information is requested

Comments

@headscott
Copy link

headscott commented Sep 20, 2024

  • CodeCarbon version: 2.5.0
  • Python version: 3.12.4
  • Operating System: Linux

Description

I am about to write a paper about sustainability of AI for my university. Therefore I wanted to know what Electricity mix (Percentages of Coal, Petroleum, Natural Gas, Solar, ...) your code is guessing and how it gets the values/where are the guessed values from?

Where can I get more current information about the german energy mix/Where did you get it from?

What I Did

I saw in /codecarbon/core/emissions.py that there is a function called _region_energy_mix_to_emissions_rate which only uses coal, petroleum and natural gas. But on your website CodeCarbon there is also mentioned emissions for Solar, Wind, ...

@benoit-cty
Copy link
Contributor

Hello,

The function you mention is only used in the case of an error DataSourceException, which is not supposed to happen anymore.

Our data came from our_world_in_data and you can see them in the file https://github.com/mlco2/codecarbon/blob/master/codecarbon/data/private_infra/global_energy_mix.json

We could also use ElectricityMaps to get the energy mix in realtime.

Let us know when your paper is out !

@benoit-cty benoit-cty added the question Further information is requested label Sep 22, 2024
@headscott
Copy link
Author

headscott commented Sep 22, 2024

Is there a way for me to do something like get_regional_energy_mix(ISO_Code, Region) and get the same results that you used to calculate the CO2eq of the power consumption? If so, what should I do?

I tried this code, but it didn't work:

from codecarbon.external.geography import GeoMetadata
from codecarbon.input import DataSourceException
from codecarbon import EmissionsTracker

def get_my_energy_mix():
    # create EmissionTracker Instance
    tracker = EmissionsTracker()

    # define GeoMetadata for your region
    geo = GeoMetadata(
        country_iso_code,
        country_name,
        region,
        latitude,
        longitude
    )

    try:
        country_emissions_data = tracker._data_source.get_country_emissions_data(
            geo.country_iso_code.lower()
        )

        if geo.region not in country_emissions_data:
            # TODO: Deal with missing data, default to something
            raise ValueError(
                f"Region: {geo.region} not found for Country "
                + f" with ISO CODE: {geo.country_iso_code}"
            )

        print("Energy Mix for country:", country_emissions_data)

    except DataSourceException:
        # This country has regional data at the energy mix level,
        # not the emissions level

        try:
            country_energy_mix_data = tracker._data_source.get_global_energy_mix_data()
            if geo.region in country_energy_mix_data:
                region_energy_mix_data = country_energy_mix_data[geo.region]
                print("Energy Mix for region:", region_energy_mix_data)
            else:
                print(f"Energy mix-Data for {geo.region} is missing.")
        except KeyError as error:
            print("Error: Data not available.", error)

My paper will be in german btw

@benoit-cty
Copy link
Contributor

benoit-cty commented Sep 24, 2024

Hello,
You can do :

from codecarbon.input import DataSource
DataSource().get_global_energy_mix_data()["DEU"]
{
  "biofuel_TWh": 46.02,
  "carbon_intensity": 380.95,
  "coal_TWh": 135.35,
  "country_name": "Germany",
  "fossil_TWh": 231.48,
  "gas_TWh": 76,
  "hydroelectricity_TWh": 19.48,
  "iso_code": "DEU",
  "low_carbon_TWh": 273.31,
  "nuclear_TWh": 8.75,
  "oil_TWh": 20.13,
  "other_renewable_TWh": 46.23,
  "other_renewable_exc_biofuel_TWh": 0.21,
  "per_capita_Wh": 6060.295,
  "renewables_TWh": 264.56,
  "solar_TWh": 61.56,
  "total_TWh": 504.78999999999996,
  "wind_TWh": 137.29,
  "year": 2023
}

If you what to use realtime data, look at CO2 Signal in CodeCarbon as it was the previous name of ElectricityMaps API.

Even if it's in German it's always nice to see where and why CodeCarbon is used.

@headscott
Copy link
Author

headscott commented Sep 24, 2024

Okay thank you, this helped me. But I still have a problem:

this is the total energy consumption of one of my AI training runs: 5.4478812927977245kWh
this is the calculated emissions in kgCO2: 2.0995535235500222

if I use the 380.95 carbon intensity, I get: 5.4478812927977245 * 380.95 = 2.07537037849129315kg so its a bit less.
I've got version 2.5.0 btw.

And where do these 380.95 come from?

oil 20.13 -> 3.9878% of total Energy
coal 135.35 -> 26.8131%
gas 76 -> 15.0558%
-> total fossil TWh: 231.48 --> 45.8567%

solar 61.56 -> 12.1952%
wind 137.29 -> 27.1974%
hydro 19.48 -> 3.859%
biofuel 46.02 -> 9.1167%
-> total renewable 264.56 -> 52.4099%
nuclear 8.75 -> 1.7334%
total 504.79

CO2eq = 0.268131 * 995 + 0.039878 * 816 + 0.150558 * 743 + 0.03859 * 26 + 0.017334 * 29 + 0.121952 * 48 + 0.271974 * 26 = 425.626433
(I am using the formula from CodeCarbon Methodology). And there is even the value for Geothermal missing.

But even with this value, I get 5.4478812927977245 * 425.626433 = 2.318766kg CO2

If I calculate it backwards, the CO2eq should be: 5,4478812927977245 * CO2eq = 2,0995535235500222 ==> CO2eq = 385.3889

So is it an older energy mix used for that caluclation? Which would it be then, and how can I get the mix (like 10% Coal, 12% Solar, ...)?

UPDATE:

If I run your code, I get:

{
'biofuel_TWh': 41.51,
'carbon_intensity': 385.389,
'coal_TWh': 178.94,
'country_name': 'Germany',
'fossil_TWh': 280.96,
'gas_TWh': 79.8,
'hydroelectricity_TWh': 17.06,
'iso_code': 'DEU',
'low_carbon_TWh': 279.59,
'nuclear_TWh': 35.86,
'oil_TWh': 22.22,
'other_renewable_TWh': 41.75,
'other_renewable_exc_biofuel_TWh': 0.24,
'per_capita_Wh': 6723.654,
'renewables_TWh': 243.73,
'solar_TWh': 60.01,
'total_TWh': 560.55,
'wind_TWh': 124.91,
'year': 2022
}

and 385.389 should actually be correct for the calculation. But I still don't know how 385.389 is calculated with this energy mix

@benoit-cty
Copy link
Contributor

'carbon_intensity': 385.389, is not computed, it came directly from Our World in Data, here is what we do to build the dataset : https://github.com/mlco2/codecarbon/blob/master/codecarbon/data/private_infra/our_world_in_data.ipynb

@headscott
Copy link
Author

So, the computation formular on the mentioned website is nowhere used in your code?

@benoit-cty
Copy link
Contributor

Yes, it was computed before, but now we read directly the carbon_intensity as it is now provided by Our World in Data.

@headscott
Copy link
Author

okay thanks

@headscott headscott reopened this Oct 1, 2024
@headscott
Copy link
Author

headscott commented Oct 1, 2024

Wait, when I look at the Website OurWorldinData, it says:

Germany,DEU,2022,420.05743
Germany,DEU,2023,380.95047

so for the newest version, after mine (2.5.0), it should be correct using 380.95 for 2023. But my version said

'carbon_intensity': 385.389

for 2022, as you mentioned too. So it's different from what the website says currently. Or did the values of 2022 change this year on their site?

@benoit-cty
Copy link
Contributor

Thanks for reporting this, we will have a look in #674

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants