Skip to content

fix: apply the documented coordinate rounding to all outputs, not just the API - #1385

Open
NoiceHax wants to merge 1 commit into
mlco2:masterfrom
NoiceHax:fix/issue-1376
Open

fix: apply the documented coordinate rounding to all outputs, not just the API#1385
NoiceHax wants to merge 1 commit into
mlco2:masterfrom
NoiceHax:fix/issue-1376

Conversation

@NoiceHax

Copy link
Copy Markdown

Description

The output reference says the latitude and longitude columns are written with reduced precision as a privacy measure. That was only true for the API. The rounding lived in ApiClient.add_run, and nothing rounded the values on the way to emissions.csv or any of the other output methods.

_ensure_geo_metadata copied the raw geolocation result into self._conf, and _prepare_emissions_data read those values straight back out. So the CSV got whatever precision the geolocation provider returned, which for a home connection can be a lot more precise than 11 km.

I moved the rounding to the one place the coordinates enter the config, so every output method now agrees. self._geo still holds the full precision values, because the Electricity Maps carbon intensity lookup reads geo.latitude and geo.longitude directly and would lose accuracy otherwise. I left the existing round() in api_client.py in place. It is now a second round that changes nothing on this path, but it still does real work for coordinates set by hand in a .codecarbon.config file, since those never go through _ensure_geo_metadata.

No documentation change is needed. The docs already describe the behaviour this makes true.

Related Issue

Fixes #1376

Motivation and Context

emissions.csv is the file people attach to papers and commit to repositories. The documentation told them the coordinates in it were already coarsened, and they were not. A documented privacy guarantee that does not hold is worse than no guarantee at all, so this takes option 1 from the issue and makes the code match what users were told.

The change is visible in the output. Coordinates in new CSV files will have one decimal place instead of four or more.

How Has This Been Tested?

I added test_coordinates_precision_is_reduced_in_emissions_data to tests/test_emissions_tracker.py. It mocks the geolocation lookup with 48.8566 / 2.3522, calls _prepare_emissions_data(), and checks the result carries 48.9 / 2.4 while tracker._geo still holds the full precision values. I checked the test fails on the unpatched source and passes with the change.

I also ran the package suite locally with CODECARBON_ALLOW_MULTIPLE_RUNS=True. 620 passed, 3 failed. The three failures are tests/cli/test_cli_main.py::test_monitor_run_and_monitor, tests/test_docs_examples.py::test_doc_python_blocks[scikit-learn.md] and test_task_energy_with_live_update_interference. All three fail the same way on master with my changes stashed, so they are not caused by this PR. black --check is clean on both changed files.

Screenshots (if appropriate):

Not applicable.

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

AI Usage Disclosure

Please refer to docs/how-to/ai-policy.md for detailed guidelines on how to disclose AI usage in your PR. Accurately completing this section is mandatory.

  • 🟥 AI-vibecoded: You cannot explain the logic. Car analogy : the car drive by itself, you are outside it and just tell it where to go.
  • 🟠 AI-generated: Car analogy : the car drive by itself, you are inside and give instructions.
  • â­� AI-assisted. Car analogy : you drive the car, AI help you find your way.
  • â™»ï¸� No AI used. Car analogy : you drive the car.

I used an AI coding tool to write the patch and this description, and I reviewed and ran everything myself before opening the PR.

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the docs/how-to/contributing.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

The output reference says latitude and longitude are written with reduced
precision for privacy, but the rounding only happened in the API client.
emissions.csv and the other output methods got the full precision values
straight from the geolocation lookup.

Round once in _ensure_geo_metadata, where the coordinates are stored in the
config, so all output methods are consistent. self._geo keeps the full
precision values, which the Electricity Maps carbon intensity lookup needs.
@NoiceHax
NoiceHax requested a review from a team as a code owner August 15, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documented coordinate privacy protection is not applied to emissions.csv

1 participant