Skip to content

Update emissions.py with CO2 equivalent notation - #36

Open
byersiiasa wants to merge 7 commits into
IAMconsortium:mainfrom
byersiiasa:patch-1
Open

Update emissions.py with CO2 equivalent notation#36
byersiiasa wants to merge 7 commits into
IAMconsortium:mainfrom
byersiiasa:patch-1

Conversation

@byersiiasa

Copy link
Copy Markdown

Added CO2-eq notation as this is currently used in our AR6 and other variable templates

Added CO2-eq  notation as this is currently used in our AR6 and other variable templates
@khaeru

khaeru commented Mar 8, 2022

Copy link
Copy Markdown
Contributor

I suspect this may not work, because the underlying package, pint, interprets "-" as a minus sign. But let's see.

@codecov-commenter

codecov-commenter commented Mar 8, 2022

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.48%. Comparing base (3b78494) to head (371452c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #36   +/-   ##
=======================================
  Coverage   91.48%   91.48%           
=======================================
  Files           6        6           
  Lines         282      282           
=======================================
  Hits          258      258           
  Misses         24       24           

☔ View full report in Codecov by Harness.
📢 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.

@danielhuppmann

Copy link
Copy Markdown
Member

Note that you edited a file that has as its first line "# DO NOT ALTER THIS FILE MANUALLY!"...

@khaeru khaeru left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please expand the tests so that the new symbol you've added is tested:

@pytest.mark.parametrize(
"metric, species_in, species_out, expected_value",
[
("AR6GWP100", "CH4", "CO2", 27.9),
("AR5CCFGWP100", "CH4", "CO2", 34),
("AR5GWP100", "CH4", "CO2", 28),
("AR5GWP100", "CH4", "CO2e", 28),
("AR4GWP100", "CH4", "CO2", 25),
("SARGWP100", "CH4", "CO2", 21),
# Same-species conversion with metric=None and compatible names
(None, "CO2", "CO2_eq", 1.0),
(None, "CO2eq", "CO2e", 1.0),
# Species names which are substrings of one another match correctly
("AR5GWP100", "HFC143", "CO2", 328.0),
("AR5GWP100", "HFC143a", "CO2", 4800.0),
],
)
def test_convert_gwp(units, metric, species_in, species_out, expected_value):

@khaeru

khaeru commented Mar 8, 2022

Copy link
Copy Markdown
Contributor

@danielhuppmann good eye! 😅

@byersiiasa you should instead modify the variable _EMI_EQUIV in the file update.py.

@byersiiasa

Copy link
Copy Markdown
Author

Please expand the tests so that the new symbol you've added is tested:

@pytest.mark.parametrize(
"metric, species_in, species_out, expected_value",
[
("AR6GWP100", "CH4", "CO2", 27.9),
("AR5CCFGWP100", "CH4", "CO2", 34),
("AR5GWP100", "CH4", "CO2", 28),
("AR5GWP100", "CH4", "CO2e", 28),
("AR4GWP100", "CH4", "CO2", 25),
("SARGWP100", "CH4", "CO2", 21),
# Same-species conversion with metric=None and compatible names
(None, "CO2", "CO2_eq", 1.0),
(None, "CO2eq", "CO2e", 1.0),
# Species names which are substrings of one another match correctly
("AR5GWP100", "HFC143", "CO2", 328.0),
("AR5GWP100", "HFC143a", "CO2", 4800.0),
],
)
def test_convert_gwp(units, metric, species_in, species_out, expected_value):

@khaeru sorry not quite intuitive to me here, should I add:
(None, "CO2-eq", "CO2e", 1.0)
or other like (None, "CO2", "CO2-eq", 1.0)

Also - #L92
("AR5GWP100", "CH4", "CO2e", 28), should not have the e? or should all L89-94 be CO2e?

@khaeru

khaeru commented Mar 8, 2022

Copy link
Copy Markdown
Contributor

Here are the relevant pytest docs: https://docs.pytest.org/en/stable/how-to/parametrize.html

To paraphrase, the 4 elements of that tuple get mapped to the function arguments named on L87, i.e. "species_in", "species_out", etc. Then you can see in the function body how those are used to call the tested function. Each line is an individual test case, so e.g.

  • ("AR5GWP100", "CH4", "CO2", 28), tests that this particular conversion works,
  • ("AR5GWP100", "CH4", "CO2e", 28), tests that "CO2e" is recognized as equivalent to "CO2" for a target ("species_out"), etc.

You could test "CO2-eq" as species_in and/or species_out, depending on how you expect that it should work!

Added a helper function to normalize CO2 variant names for consistency in comparisons.
Aims to pass the tests.
Comment thread iam_units/__init__.py
# Replace underscores and hyphens, convert to lowercase for comparison
normalized = species.lower().replace("_", "").replace("-", "")
# Treat CO2, CO2e, CO2eq, CO2_eq, CO2-eq all as equivalent
if normalized in ("co2", "co2e", "co2eq"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that common-definitions uses CO2-equiv, see here.

Also, the method pyam.convert_unit() already supports handling of CO2-equivalent, see the tests here.

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.

4 participants