Skip to content

Fix TypeError for pGWP/pGTP with IPCC fallback functions - #29

Merged
TimoDiepers merged 3 commits into
mainfrom
fix/pgwp-ipcc-fallback-time-varying-re
Aug 14, 2026
Merged

TimoDiepers merged 3 commits into
mainfrom
fix/pgwp-ipcc-fallback-time-varying-re

Conversation

@TimoDiepers

Copy link
Copy Markdown
Member

Problem

Calculating pGWP (or pGTP) on an inventory that contains a GHG outside the Watanabe module fails:

TypeError: create_generic_characterization_function.<locals>.characterize_generic()
got an unexpected keyword argument 'time_varying_re'

_characterize_pgwp passed time_varying_re to every flow's characterization function. With fallback_to_ipcc=True (the default), flows that Watanabe doesn't cover - CO and the GHGs built from decay_multipliers.json - are mapped to IPCC AR6 functions with signature (series, period, cumulative), which have no such parameter. In practice this hits any ecoinvent-based pGWP calculation, since the usual climate change methods characterize more than CO2/CH4/N2O.

Fix

_characterize_prospective_radiative_forcing already had this check inline. It is now the shared helper _apply_characterization_function, which passes time_varying_re only to the Watanabe functions and is used by the pGWP, pGTP and prospective radiative forcing paths alike.

Tests

New tests/test_characterize_prospective_fallback.py: a mixed characterization function dict (prospective CH4 + IPCC characterize_co + a generic decay-series function) across the metrics pGWP, pGTP and prospective_radiative_forcing, with time_varying_re both False and True. It reproduces the TypeError without the fix and asserts that all three flows end up characterized. Full suite: 100 passed.

Note on the file name: test_prospective.py replaces dynamic_characterization in sys.modules with a stub at import time, so any test module importing the real package has to sort before it alphabetically. That is noted in the new module's docstring, but it is a landmine worth removing separately.

Not addressed here

For IPCC fallback gases, pGWP divides an AR6 AGWP by a Watanabe AGWP for CO2, i.e. a mixed reference. Whether these gases should instead use the IPCC CO2 reference is a modelling decision, left out of this fix.

The pGWP and pGTP metrics passed `time_varying_re` to every flow's
characterization function. With `fallback_to_ipcc=True` (the default), flows
that are not covered by the Watanabe module - CO and the GHGs built from
decay_multipliers.json - are characterized with IPCC AR6 functions, whose
signature is `(series, period, cumulative)`. Characterizing such a flow raised

    TypeError: create_generic_characterization_function.<locals>.characterize_generic()
    got an unexpected keyword argument 'time_varying_re'

`_characterize_prospective_radiative_forcing` already guarded against this
inline. That check is now a shared helper, `_apply_characterization_function`,
used by the pGWP, pGTP and prospective radiative forcing paths alike, so the
argument only reaches functions that accept it.
Prospective metrics are typically calculated through another package (bw_timex),
so users hitting this error have never imported dynamic_characterization
themselves. "Call prospective.set_scenario(iam, ssp, rcp) first" then leaves
them guessing what `prospective` is and which arguments are valid.

The message now states which metrics need a scenario, gives a copy-pasteable
call with the full import path, says that it is set once per session, and lists
the IAM-SSP pairs plus a pointer to VALID_SCENARIOS.
@TimoDiepers

Copy link
Copy Markdown
Member Author

Pushed a second, related usability fix onto this branch: the RuntimeError raised when a prospective metric is calculated without a scenario.

Old message:

No scenario set. Call prospective.set_scenario(iam, ssp, rcp) first.

That assumes the reader knows what prospective is - but users reaching this through bw_timex have never imported dynamic_characterization themselves, and it doesn't say which arguments are valid. New message:

No scenario set.

The prospective characterization factors (Watanabe et al. 2026) used for the metrics
"pGWP", "pGTP" and "prospective_radiative_forcing" depend on a future scenario, so you
have to choose one before calculating them - also if you calculate them through another
package, such as bw_timex:

    from dynamic_characterization.prospective import set_scenario
    set_scenario(iam="IMAGE", ssp="SSP1", rcp="2.6")

This is done once per Python session and applies to all following calculations.

Each IAM comes with one SSP: IMAGE-SSP1, MESSAGE-SSP2, AIM-SSP3, GCAM4-SSP4,
REMIND-SSP5. The available RCPs are "2.6", "4.5", "6.0" and "8.5", but not for every
IAM - see dynamic_characterization.prospective.VALID_SCENARIOS for the full list.

Covered by test_get_scenario_without_set_message_is_actionable; the existing match="No scenario set" tests still pass. Happy to split this into its own PR if you'd rather keep #29 to the TypeError.

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.

1 participant