dynamic-characterization 1.4.1__tar.gz → 1.4.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. {dynamic_characterization-1.4.1/dynamic_characterization.egg-info → dynamic_characterization-1.4.2}/PKG-INFO +1 -1
  2. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/__init__.py +1 -1
  3. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/dynamic_characterization.py +37 -17
  4. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/config.py +20 -3
  5. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2/dynamic_characterization.egg-info}/PKG-INFO +1 -1
  6. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization.egg-info/SOURCES.txt +1 -0
  7. dynamic_characterization-1.4.2/tests/test_characterize_prospective_fallback.py +68 -0
  8. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/tests/test_prospective.py +18 -0
  9. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/LICENSE +0 -0
  10. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/MANIFEST.in +0 -0
  11. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/README.md +0 -0
  12. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/classes.py +0 -0
  13. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/ipcc_ar6/__init__.py +0 -0
  14. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/ipcc_ar6/data/decay_multipliers.json +0 -0
  15. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/ipcc_ar6/data/hodnebrog20.csv +0 -0
  16. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/ipcc_ar6/radiative_forcing.py +0 -0
  17. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/original_temporalis_functions/__init__.py +0 -0
  18. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/original_temporalis_functions/radiative_forcing.py +0 -0
  19. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/__init__.py +0 -0
  20. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/agtp.py +0 -0
  21. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/agwp.py +0 -0
  22. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_001.xlsx +0 -0
  23. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_002.xlsx +0 -0
  24. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_003.xlsx +0 -0
  25. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_004.xlsx +0 -0
  26. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_009.xlsx +0 -0
  27. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_010.xlsx +0 -0
  28. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_011.xlsx +0 -0
  29. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_012.xlsx +0 -0
  30. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_013.xlsx +0 -0
  31. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_014.xlsx +0 -0
  32. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_015.xlsx +0 -0
  33. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_016.xlsx +0 -0
  34. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_017.xlsx +0 -0
  35. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_018.xlsx +0 -0
  36. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_019.xlsx +0 -0
  37. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_020.xlsx +0 -0
  38. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_021.xlsx +0 -0
  39. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_022.xlsx +0 -0
  40. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_023.xlsx +0 -0
  41. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_024.xlsx +0 -0
  42. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_025.xlsx +0 -0
  43. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_026.xlsx +0 -0
  44. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data_loader.py +0 -0
  45. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/radiative_forcing.py +0 -0
  46. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization.egg-info/dependency_links.txt +0 -0
  47. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization.egg-info/requires.txt +0 -0
  48. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/dynamic_characterization.egg-info/top_level.txt +0 -0
  49. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/pyproject.toml +0 -0
  50. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/setup.cfg +0 -0
  51. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/tests/test_characterize_dynamic_inventory.py +0 -0
  52. {dynamic_characterization-1.4.1 → dynamic_characterization-1.4.2}/tests/test_default_characterization_functions.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dynamic_characterization
3
- Version: 1.4.1
3
+ Version: 1.4.2
4
4
  Summary: Collection of functions for characterizing life cycle inventories with temporal information
5
5
  Author-email: Timo Diepers <timo.diepers@ltt.rwth-aachen.de>
6
6
  Maintainer-email: Timo Diepers <timo.diepers@ltt.rwth-aachen.de>
@@ -20,7 +20,7 @@ __all__ = (
20
20
  "prospective",
21
21
  )
22
22
 
23
- __version__ = "1.4.1"
23
+ __version__ = "1.4.2"
24
24
 
25
25
  from . import ipcc_ar6, original_temporalis_functions, prospective
26
26
  from .dynamic_characterization import (
@@ -31,6 +31,15 @@ from dynamic_characterization.prospective.radiative_forcing import (
31
31
  )
32
32
 
33
33
 
34
+ # Characterization functions from the Watanabe module. These are the only ones that
35
+ # accept the `time_varying_re` argument.
36
+ _PROSPECTIVE_CHARACTERIZATION_FUNCTIONS = (
37
+ prospective_characterize_co2,
38
+ prospective_characterize_co2_uptake,
39
+ prospective_characterize_ch4,
40
+ prospective_characterize_n2o,
41
+ )
42
+
34
43
  # Building the default characterization functions reloads decay_multipliers.json and
35
44
  # issues one biosphere DB query per method flow. The result only depends on the BW
36
45
  # project, the method, and the boolean flags, so it is memoized across calls.
@@ -548,6 +557,24 @@ def _calculate_dynamic_time_horizon(
548
557
  return time_horizon
549
558
 
550
559
 
560
+ def _apply_characterization_function(
561
+ char_func,
562
+ row,
563
+ time_horizon,
564
+ time_varying_re: bool = False,
565
+ ) -> CharacterizedRow:
566
+ """
567
+ Call a characterization function, passing `time_varying_re` only to those that accept it.
568
+
569
+ Flows that are not covered by the Watanabe module (e.g. CO and the GHGs from
570
+ decay_multipliers.json) are characterized with IPCC AR6 functions, which have no
571
+ `time_varying_re` parameter.
572
+ """
573
+ if char_func in _PROSPECTIVE_CHARACTERIZATION_FUNCTIONS:
574
+ return char_func(row, time_horizon, time_varying_re=time_varying_re)
575
+ return char_func(row, time_horizon)
576
+
577
+
551
578
  def _characterize_radiative_forcing(
552
579
  characterization_functions, row, time_horizon
553
580
  ) -> CharacterizedRow:
@@ -601,7 +628,8 @@ def _characterize_pgwp(
601
628
  emission_year = int(str(emission_date.to_numpy())[:4])
602
629
 
603
630
  # Calculate AGWP for the gas using its characterization function
604
- radiative_forcing_ghg = characterization_functions[row.flow](
631
+ radiative_forcing_ghg = _apply_characterization_function(
632
+ characterization_functions[row.flow],
605
633
  row,
606
634
  dynamic_time_horizon,
607
635
  time_varying_re=time_varying_re,
@@ -673,9 +701,11 @@ def _characterize_pgtp(
673
701
  else:
674
702
  # For other GHGs, fall back to using integrated RF as proxy
675
703
  # This may not be as accurate but provides a fallback
676
- radiative_forcing_ghg = char_func(
704
+ radiative_forcing_ghg = _apply_characterization_function(
705
+ char_func,
677
706
  row,
678
707
  dynamic_time_horizon,
708
+ time_varying_re=time_varying_re,
679
709
  )
680
710
  agtp_gas = radiative_forcing_ghg.amount.sum()
681
711
 
@@ -708,19 +738,9 @@ def _characterize_prospective_radiative_forcing(
708
738
  For GHGs available in Watanabe (CO2, CH4, N2O), uses scenario-based radiative efficiencies.
709
739
  For other GHGs (when fallback_to_ipcc=True), uses standard IPCC AR6 functions.
710
740
  """
711
- char_func = characterization_functions[row.flow]
712
-
713
- # Check if this is a Watanabe function (they accept time_varying_re parameter)
714
- prospective_functions = (
715
- prospective_characterize_co2,
716
- prospective_characterize_co2_uptake,
717
- prospective_characterize_ch4,
718
- prospective_characterize_n2o,
741
+ return _apply_characterization_function(
742
+ characterization_functions[row.flow],
743
+ row,
744
+ time_horizon,
745
+ time_varying_re=time_varying_re,
719
746
  )
720
-
721
- if char_func in prospective_functions:
722
- # Watanabe functions support time_varying_re
723
- return char_func(row, time_horizon, time_varying_re=time_varying_re)
724
- else:
725
- # IPCC fallback functions don't support time_varying_re
726
- return char_func(row, time_horizon)
@@ -33,6 +33,25 @@ VALID_SCENARIOS: Set[Tuple[str, str, str]] = {
33
33
  # Module-level state
34
34
  _current_scenario: Optional[Dict[str, str]] = None
35
35
 
36
+ # Raised when a prospective characterization factor is requested before a scenario is
37
+ # chosen. This is often the first time users see this package: the prospective metrics
38
+ # are usually calculated through another package (e.g. bw_timex), so the message spells
39
+ # out the full import path and does not assume anything is imported already.
40
+ NO_SCENARIO_MESSAGE = """No scenario set.
41
+
42
+ The prospective characterization factors (Watanabe et al. 2026) used for the metrics
43
+ "pGWP", "pGTP" and "prospective_radiative_forcing" depend on a future scenario, so you
44
+ have to choose one before calculating them:
45
+
46
+ from dynamic_characterization.prospective import set_scenario
47
+ set_scenario(iam="IMAGE", ssp="SSP1", rcp="2.6")
48
+
49
+ This is done once per Python session and applies to all following calculations.
50
+
51
+ Each IAM comes with one SSP: IMAGE-SSP1, MESSAGE-SSP2, AIM-SSP3, GCAM4-SSP4,
52
+ REMIND-SSP5. The available RCPs are "2.6", "4.5", "6.0" and "8.5", but not for every
53
+ IAM - see dynamic_characterization.prospective.VALID_SCENARIOS for the full list."""
54
+
36
55
 
37
56
  def set_scenario(iam: str, ssp: str, rcp: str) -> None:
38
57
  """
@@ -79,9 +98,7 @@ def get_scenario() -> Dict[str, str]:
79
98
  If no scenario has been set
80
99
  """
81
100
  if _current_scenario is None:
82
- raise RuntimeError(
83
- "No scenario set. Call prospective.set_scenario(iam, ssp, rcp) first."
84
- )
101
+ raise RuntimeError(NO_SCENARIO_MESSAGE)
85
102
  return _current_scenario.copy()
86
103
 
87
104
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dynamic_characterization
3
- Version: 1.4.1
3
+ Version: 1.4.2
4
4
  Summary: Collection of functions for characterizing life cycle inventories with temporal information
5
5
  Author-email: Timo Diepers <timo.diepers@ltt.rwth-aachen.de>
6
6
  Maintainer-email: Timo Diepers <timo.diepers@ltt.rwth-aachen.de>
@@ -82,5 +82,6 @@ dynamic_characterization/prospective/data/es5c12391_si_024.xlsx
82
82
  dynamic_characterization/prospective/data/es5c12391_si_025.xlsx
83
83
  dynamic_characterization/prospective/data/es5c12391_si_026.xlsx
84
84
  tests/test_characterize_dynamic_inventory.py
85
+ tests/test_characterize_prospective_fallback.py
85
86
  tests/test_default_characterization_functions.py
86
87
  tests/test_prospective.py
@@ -0,0 +1,68 @@
1
+ """Prospective metrics with IPCC fallback functions for non-Watanabe GHGs.
2
+
3
+ Note: test_prospective.py replaces `dynamic_characterization` in sys.modules with a
4
+ stub at import time, which breaks importing the real package afterwards. This module
5
+ therefore has to sort before it alphabetically.
6
+ """
7
+
8
+ import numpy as np
9
+ import pandas as pd
10
+ import pytest
11
+
12
+ from dynamic_characterization import characterize
13
+ from dynamic_characterization.ipcc_ar6.radiative_forcing import (
14
+ characterize_co,
15
+ create_generic_characterization_function,
16
+ )
17
+ from dynamic_characterization.prospective import set_scenario
18
+ from dynamic_characterization.prospective.radiative_forcing import (
19
+ characterize_ch4 as prospective_characterize_ch4,
20
+ )
21
+
22
+ # Flow ids: 1 is covered by the Watanabe module, 2 and 3 only by IPCC AR6.
23
+ CH4_FLOW = 1
24
+ CO_FLOW = 2
25
+ GENERIC_FLOW = 3
26
+
27
+
28
+ def dynamic_inventory() -> pd.DataFrame:
29
+ return pd.DataFrame(
30
+ data={
31
+ "date": pd.Series(
32
+ data=["2030-01-01", "2030-01-01", "2030-01-01"],
33
+ dtype="datetime64[s]",
34
+ ),
35
+ "amount": pd.Series(data=[1.0, 1.0, 1.0], dtype="float64"),
36
+ "flow": pd.Series(data=[CH4_FLOW, CO_FLOW, GENERIC_FLOW], dtype="int"),
37
+ "activity": pd.Series(data=[10, 10, 10], dtype="int"),
38
+ }
39
+ )
40
+
41
+
42
+ def mixed_characterization_functions() -> dict:
43
+ # A generic function built from a decay series, as done for GHGs that are
44
+ # only in decay_multipliers.json (here: a stand-in decay series).
45
+ decay_series = np.linspace(0, 1e-13, 100)
46
+ return {
47
+ CH4_FLOW: prospective_characterize_ch4,
48
+ CO_FLOW: characterize_co,
49
+ GENERIC_FLOW: create_generic_characterization_function(decay_series),
50
+ }
51
+
52
+
53
+ @pytest.mark.parametrize("time_varying_re", [False, True])
54
+ @pytest.mark.parametrize("metric", ["pGWP", "pGTP", "prospective_radiative_forcing"])
55
+ def test_prospective_metrics_with_ipcc_fallback_functions(metric, time_varying_re):
56
+ """IPCC fallback functions don't take `time_varying_re` and must not get it."""
57
+ set_scenario(iam="IMAGE", ssp="SSP1", rcp="2.6")
58
+
59
+ characterized = characterize(
60
+ dynamic_inventory(),
61
+ metric=metric,
62
+ characterization_functions=mixed_characterization_functions(),
63
+ time_horizon=100,
64
+ time_varying_re=time_varying_re,
65
+ )
66
+
67
+ # All three flows are characterized, none silently dropped.
68
+ assert set(characterized.flow.unique()) == {CH4_FLOW, CO_FLOW, GENERIC_FLOW}
@@ -130,6 +130,24 @@ def test_get_scenario_without_set():
130
130
  config.get_scenario()
131
131
 
132
132
 
133
+ def test_get_scenario_without_set_message_is_actionable():
134
+ """The error has to be usable by someone who only knows the calling package."""
135
+ with pytest.raises(RuntimeError) as excinfo:
136
+ config.get_scenario()
137
+
138
+ message = str(excinfo.value)
139
+
140
+ # A copy-pasteable fix, with the full import path: users reaching this via
141
+ # bw_timex have never imported dynamic_characterization themselves.
142
+ assert "from dynamic_characterization.prospective import set_scenario" in message
143
+ assert 'set_scenario(iam="IMAGE", ssp="SSP1", rcp="2.6")' in message
144
+
145
+ # Which calculations need it, and which scenarios exist.
146
+ assert "pGWP" in message
147
+ assert "IMAGE-SSP1" in message
148
+ assert "REMIND-SSP5" in message
149
+
150
+
133
151
  def test_reset_scenario():
134
152
  """Reset should clear the current scenario."""
135
153
  config.set_scenario(iam="IMAGE", ssp="SSP1", rcp="2.6")