Skip to content

Conversation

@janheinrichmerker
Copy link
Contributor

This PR integrates a Python-specific CODECARBON_EMISSIONS measure, which, if CodeCarbon is installed, tracks the carbon emissions using CodeCarbon. This third-party measure might be used to compare results from different tracking backends.

@janheinrichmerker janheinrichmerker self-assigned this Oct 8, 2025
@janheinrichmerker janheinrichmerker added enhancement New feature or request lang:python Pull requests that update Python code labels Oct 8, 2025
@codecov
Copy link

codecov bot commented Oct 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.05%. Comparing base (0b2b1b2) to head (51e7e8c).

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #154      +/-   ##
============================================
+ Coverage     81.85%   83.05%   +1.20%     
  Complexity       22       22              
============================================
  Files             9       11       +2     
  Lines          1521     1629     +108     
  Branches         56       56              
============================================
+ Hits           1245     1353     +108     
  Misses          232      232              
  Partials         44       44              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@mam10eks mam10eks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, the only question that I have: should we use the offline emission tracker?

# Start the CodeCarbon tracking, if available.
if CODECARBON_INSTALLED:
if codecarbon_tracker is None:
codecarbon_tracker = EmissionsTracker()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need an internet connection? There was, if I recall correctly, also an offline tracker? We would need the offline tracker here, so that things are directly compatible with tira, which would be very helpful :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users can override the CodeCarbon tracker and use the offline variant, e.g., with:

with tirex_tracker.tracking(
    codecarbon_tracker=codecarbon.OfflineEmissionsTracker(country_iso_code="GER"),
) as results:
    # Do stuff

That code would fully work offline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is super complicated?

Can we maybe go into the direction that Tim mentioned below, to not report the CO2 emissions? And rather the raw energy? That would be the more clean way, as one could do the math to calculate the emissions later.

_PYTHON_MEASURES = {
**_PYTHON_MEASURES,
Measure.CODECARBON_EMISSIONS: MeasureInfo(
description="CO2 emissions in kg, as measured by CodeCarbon.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could instead (or additionally) report the energy itself since the estimated emissions are only the energy usage multiplied by the carbon intensity of the local energy mix. Alternatively, (and I don't know if the Codecarbon API makes this available), we could report Carbon intensity and energy usage and the user can multiply them themselves for the estimated emissions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be a good option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also resolve the country-code thing above, or?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request lang:python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants