From 63f7c4cb142fb36b7aeda3031b55e601abf0e8a1 Mon Sep 17 00:00:00 2001 From: Thibault Lestang Date: Mon, 18 Mar 2024 18:05:11 +0000 Subject: [PATCH] Return carbon footprint as a float (gC02) --- cats/carbonFootprint.py | 15 +-------------- tests/test_footprint.py | 6 ++++-- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/cats/carbonFootprint.py b/cats/carbonFootprint.py index 34e033e..968e22a 100644 --- a/cats/carbonFootprint.py +++ b/cats/carbonFootprint.py @@ -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) diff --git a/tests/test_footprint.py b/tests/test_footprint.py index 4847006..e590d7b 100644 --- a/tests/test_footprint.py +++ b/tests/test_footprint.py @@ -1,12 +1,14 @@ 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, @@ -14,4 +16,4 @@ def test_get_footprint_reduction_estimate(): average_best_ci=150, # gCO2/kWh average_now_ci=200, # gCO2/kWh ) - assert est == expected + assert_allclose(expected, est)