Skip to content

Commit

Permalink
Return carbon footprint as a float (gC02)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlestang committed Mar 18, 2024
1 parent d6bf0c8 commit f1c3e4f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
15 changes: 1 addition & 14 deletions cats/carbonFootprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,4 @@ def get_footprint_reduction_estimate(
best = energy * average_best_ci
now = energy * average_now_ci

return Estimates(*[format_footprint(e) for e in [now, best, now - best]])


def format_footprint(footprint_in_grams: float) -> str:
"""
Format the text to display the carbon footprint
:param footprint_g: [float] carbon footprint, in gCO2e
:return: [str] the text to display
"""
if footprint_in_grams < 1e3:
return f"{footprint_in_grams:,.0f} gCO2e"
if footprint_in_grams < 1e6:
return f"{footprint_in_grams / 1e3:,.0f} kgCO2e"
return f"{footprint_in_grams / 1e3:,.0f} TCO2e"
return Estimates(now, best, now - best)
6 changes: 4 additions & 2 deletions tests/test_footprint.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import datetime

from numpy.testing import assert_allclose

from cats.carbonFootprint import Estimates, get_footprint_reduction_estimate

JOBINFO = [(1, 2.0), (2, 3.0), (8, 1.0)]


def test_get_footprint_reduction_estimate():
expected = Estimates(now="3 gCO2e", best="2 gCO2e", savings="gCO2e")
expected = Estimates(now=3.2, best=2.4, savings=0.8)
est = get_footprint_reduction_estimate(
PUE=1.0,
jobinfo=JOBINFO,
runtime=datetime.timedelta(minutes=60),
average_best_ci=150, # gCO2/kWh
average_now_ci=200, # gCO2/kWh
)
assert est == expected
assert_allclose(expected, est)

0 comments on commit f1c3e4f

Please sign in to comment.