dynamic-characterization 1.4.0__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.0/dynamic_characterization.egg-info → dynamic_characterization-1.4.2}/PKG-INFO +1 -1
  2. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/__init__.py +1 -1
  3. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/dynamic_characterization.py +57 -32
  4. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/config.py +20 -3
  5. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2/dynamic_characterization.egg-info}/PKG-INFO +1 -1
  6. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization.egg-info/SOURCES.txt +2 -0
  7. dynamic_characterization-1.4.2/tests/test_characterize_prospective_fallback.py +68 -0
  8. dynamic_characterization-1.4.2/tests/test_default_characterization_functions.py +210 -0
  9. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/tests/test_prospective.py +18 -0
  10. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/LICENSE +0 -0
  11. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/MANIFEST.in +0 -0
  12. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/README.md +0 -0
  13. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/classes.py +0 -0
  14. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/ipcc_ar6/__init__.py +0 -0
  15. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/ipcc_ar6/data/decay_multipliers.json +0 -0
  16. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/ipcc_ar6/data/hodnebrog20.csv +0 -0
  17. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/ipcc_ar6/radiative_forcing.py +0 -0
  18. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/original_temporalis_functions/__init__.py +0 -0
  19. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/original_temporalis_functions/radiative_forcing.py +0 -0
  20. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/__init__.py +0 -0
  21. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/agtp.py +0 -0
  22. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/agwp.py +0 -0
  23. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_001.xlsx +0 -0
  24. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_002.xlsx +0 -0
  25. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_003.xlsx +0 -0
  26. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_004.xlsx +0 -0
  27. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_009.xlsx +0 -0
  28. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_010.xlsx +0 -0
  29. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_011.xlsx +0 -0
  30. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_012.xlsx +0 -0
  31. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_013.xlsx +0 -0
  32. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_014.xlsx +0 -0
  33. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_015.xlsx +0 -0
  34. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_016.xlsx +0 -0
  35. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_017.xlsx +0 -0
  36. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_018.xlsx +0 -0
  37. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_019.xlsx +0 -0
  38. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_020.xlsx +0 -0
  39. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_021.xlsx +0 -0
  40. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_022.xlsx +0 -0
  41. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_023.xlsx +0 -0
  42. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_024.xlsx +0 -0
  43. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_025.xlsx +0 -0
  44. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data/es5c12391_si_026.xlsx +0 -0
  45. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/data_loader.py +0 -0
  46. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization/prospective/radiative_forcing.py +0 -0
  47. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization.egg-info/dependency_links.txt +0 -0
  48. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization.egg-info/requires.txt +0 -0
  49. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/dynamic_characterization.egg-info/top_level.txt +0 -0
  50. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/pyproject.toml +0 -0
  51. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/setup.cfg +0 -0
  52. {dynamic_characterization-1.4.0 → dynamic_characterization-1.4.2}/tests/test_characterize_dynamic_inventory.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dynamic_characterization
3
- Version: 1.4.0
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.0"
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.
@@ -457,21 +466,26 @@ def create_characterization_functions_from_method(
457
466
 
458
467
  for node in bioflow_nodes:
459
468
  if "carbon dioxide" in node["name"].lower():
460
- if "soil" in node.get("categories", []) and characterize_uptake:
461
- characterization_functions[node.id] = (
462
- co2_uptake_func # negative emission because uptake by soil
463
- )
464
- elif (
465
- "in air" in node.get("categories", [])
466
- and node.get("type", []) == "natural resource"
467
- and characterize_uptake
468
- ):
469
- # CO2 as a natural resource in air is assumed to be used for uptake in biomass or CDR processes
470
- characterization_functions[node.id] = (
471
- co2_uptake_func # negative emission because uptake by biomass or CDR processes
472
- )
473
- # explicitely exlude CO2 flow that are a natural resource, as these are uptake flows
474
- elif "in air" in node.get("categories", []) and not "natural resource" in node.get("type", []):
469
+ categories = node.get("categories") or ()
470
+ # uptake by soil, i.e. CO2 leaving the technosphere towards the soil compartment.
471
+ # Only the categories are checked, so that emissions to air from flows like
472
+ # "Carbon dioxide, from soil or biomass stock" are not misread as uptake.
473
+ is_soil_uptake = "soil" in categories
474
+ # CO2 as a natural resource in air is assumed to be used for uptake in biomass
475
+ # or CDR processes
476
+ is_uptake_resource = (
477
+ "in air" in categories and node.get("type") == "natural resource"
478
+ )
479
+
480
+ if is_soil_uptake or is_uptake_resource:
481
+ if characterize_uptake:
482
+ characterization_functions[node.id] = (
483
+ co2_uptake_func # negative emission because of uptake
484
+ )
485
+ # else: uptake is modelled outside of the dynamic characterization,
486
+ # so these flows are intentionally left uncharacterized
487
+ else:
488
+ # all remaining CO2 flows are emissions, regardless of the air subcategory
475
489
  characterization_functions[node.id] = co2_func
476
490
 
477
491
  elif (
@@ -543,6 +557,24 @@ def _calculate_dynamic_time_horizon(
543
557
  return time_horizon
544
558
 
545
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
+
546
578
  def _characterize_radiative_forcing(
547
579
  characterization_functions, row, time_horizon
548
580
  ) -> CharacterizedRow:
@@ -596,7 +628,8 @@ def _characterize_pgwp(
596
628
  emission_year = int(str(emission_date.to_numpy())[:4])
597
629
 
598
630
  # Calculate AGWP for the gas using its characterization function
599
- radiative_forcing_ghg = characterization_functions[row.flow](
631
+ radiative_forcing_ghg = _apply_characterization_function(
632
+ characterization_functions[row.flow],
600
633
  row,
601
634
  dynamic_time_horizon,
602
635
  time_varying_re=time_varying_re,
@@ -668,9 +701,11 @@ def _characterize_pgtp(
668
701
  else:
669
702
  # For other GHGs, fall back to using integrated RF as proxy
670
703
  # This may not be as accurate but provides a fallback
671
- radiative_forcing_ghg = char_func(
704
+ radiative_forcing_ghg = _apply_characterization_function(
705
+ char_func,
672
706
  row,
673
707
  dynamic_time_horizon,
708
+ time_varying_re=time_varying_re,
674
709
  )
675
710
  agtp_gas = radiative_forcing_ghg.amount.sum()
676
711
 
@@ -703,19 +738,9 @@ def _characterize_prospective_radiative_forcing(
703
738
  For GHGs available in Watanabe (CO2, CH4, N2O), uses scenario-based radiative efficiencies.
704
739
  For other GHGs (when fallback_to_ipcc=True), uses standard IPCC AR6 functions.
705
740
  """
706
- char_func = characterization_functions[row.flow]
707
-
708
- # Check if this is a Watanabe function (they accept time_varying_re parameter)
709
- prospective_functions = (
710
- prospective_characterize_co2,
711
- prospective_characterize_co2_uptake,
712
- prospective_characterize_ch4,
713
- 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,
714
746
  )
715
-
716
- if char_func in prospective_functions:
717
- # Watanabe functions support time_varying_re
718
- return char_func(row, time_horizon, time_varying_re=time_varying_re)
719
- else:
720
- # IPCC fallback functions don't support time_varying_re
721
- 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.0
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,4 +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
86
+ tests/test_default_characterization_functions.py
85
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}
@@ -0,0 +1,210 @@
1
+ """Tests for the default characterization functions built from a static LCIA method."""
2
+
3
+ import bw2data as bd
4
+ import numpy as np
5
+ import pandas as pd
6
+ import pytest
7
+ from bw2data.tests import bw2test
8
+
9
+ from dynamic_characterization import characterize
10
+ from dynamic_characterization.dynamic_characterization import (
11
+ clear_characterization_function_cache,
12
+ create_characterization_functions_from_method,
13
+ )
14
+ from dynamic_characterization.ipcc_ar6.radiative_forcing import (
15
+ characterize_ch4,
16
+ characterize_co2,
17
+ characterize_co2_uptake,
18
+ )
19
+
20
+ METHOD = ("test", "climate change", "GWP100")
21
+
22
+ BIOFLOWS = {
23
+ "co2_fossil_high_stacks": {
24
+ "name": "Carbon dioxide, fossil",
25
+ "categories": ("air", "non-urban air or from high stacks"),
26
+ "type": "emission",
27
+ "CAS number": "000124-38-9",
28
+ },
29
+ "co2_fossil_unspecified": {
30
+ "name": "Carbon dioxide, fossil",
31
+ "categories": ("air",),
32
+ "type": "emission",
33
+ "CAS number": "000124-38-9",
34
+ },
35
+ "co2_fossil_urban": {
36
+ "name": "Carbon dioxide, fossil",
37
+ "categories": ("air", "urban air close to ground"),
38
+ "type": "emission",
39
+ "CAS number": "000124-38-9",
40
+ },
41
+ "co2_fossil_stratosphere": {
42
+ "name": "Carbon dioxide, fossil",
43
+ "categories": ("air", "lower stratosphere + upper troposphere"),
44
+ "type": "emission",
45
+ "CAS number": "000124-38-9",
46
+ },
47
+ "co2_non_fossil": {
48
+ "name": "Carbon dioxide, non-fossil",
49
+ "categories": ("air", "non-urban air or from high stacks"),
50
+ "type": "emission",
51
+ "CAS number": "000124-38-9",
52
+ },
53
+ "co2_from_soil_or_biomass_stock": {
54
+ "name": "Carbon dioxide, from soil or biomass stock",
55
+ "categories": ("air",),
56
+ "type": "emission",
57
+ "CAS number": "000124-38-9",
58
+ },
59
+ "co2_to_soil": {
60
+ "name": "Carbon dioxide, to soil or biomass stock",
61
+ "categories": ("soil",),
62
+ "type": "emission",
63
+ "CAS number": "000124-38-9",
64
+ },
65
+ "co2_in_air": {
66
+ "name": "Carbon dioxide, in air",
67
+ "categories": ("natural resource", "in air"),
68
+ "type": "natural resource",
69
+ "CAS number": "000124-38-9",
70
+ },
71
+ "ch4_fossil": {
72
+ "name": "Methane, fossil",
73
+ "categories": ("air", "non-urban air or from high stacks"),
74
+ "type": "emission",
75
+ "CAS number": "000074-82-8",
76
+ },
77
+ }
78
+
79
+ # static CFs, roughly EF v3.1 GWP100; uptake flows are negative
80
+ CHARACTERIZATION_FACTORS = {
81
+ "co2_fossil_high_stacks": 1.0,
82
+ "co2_fossil_unspecified": 1.0,
83
+ "co2_fossil_urban": 1.0,
84
+ "co2_fossil_stratosphere": 1.0,
85
+ "co2_non_fossil": 1.0,
86
+ "co2_from_soil_or_biomass_stock": 1.0,
87
+ "co2_to_soil": -1.0,
88
+ "co2_in_air": -1.0,
89
+ "ch4_fossil": 29.8,
90
+ }
91
+
92
+
93
+ def setup_project():
94
+ """Write a minimal biosphere database and static method into the current project."""
95
+ clear_characterization_function_cache()
96
+ biosphere = bd.Database(bd.config.biosphere)
97
+ biosphere.write(
98
+ {
99
+ (bd.config.biosphere, code): data | {"code": code}
100
+ for code, data in BIOFLOWS.items()
101
+ }
102
+ )
103
+ bd.Method(METHOD).write(
104
+ [
105
+ ((bd.config.biosphere, code), factor)
106
+ for code, factor in CHARACTERIZATION_FACTORS.items()
107
+ ]
108
+ )
109
+ return {code: biosphere.get(code).id for code in BIOFLOWS}
110
+
111
+
112
+ @pytest.mark.parametrize(
113
+ "code",
114
+ [
115
+ "co2_fossil_high_stacks",
116
+ "co2_fossil_unspecified",
117
+ "co2_fossil_urban",
118
+ "co2_fossil_stratosphere",
119
+ "co2_non_fossil",
120
+ "co2_from_soil_or_biomass_stock",
121
+ ],
122
+ )
123
+ @bw2test
124
+ def test_co2_emission_flows_get_co2_function(code):
125
+ ids = setup_project()
126
+ functions = create_characterization_functions_from_method(METHOD)
127
+ assert functions[ids[code]] is characterize_co2
128
+
129
+
130
+ @bw2test
131
+ def test_co2_emission_flows_characterized_without_uptake():
132
+ """Disabling uptake must not drop the ordinary CO2 emission flows."""
133
+ ids = setup_project()
134
+ functions = create_characterization_functions_from_method(
135
+ METHOD, characterize_uptake=False
136
+ )
137
+ assert functions[ids["co2_fossil_high_stacks"]] is characterize_co2
138
+ assert functions[ids["co2_from_soil_or_biomass_stock"]] is characterize_co2
139
+
140
+
141
+ @bw2test
142
+ def test_uptake_flows_use_uptake_function():
143
+ ids = setup_project()
144
+ functions = create_characterization_functions_from_method(METHOD)
145
+ assert functions[ids["co2_in_air"]] is characterize_co2_uptake
146
+ assert functions[ids["co2_to_soil"]] is characterize_co2_uptake
147
+
148
+
149
+ @bw2test
150
+ def test_uptake_flows_absent_when_uptake_disabled():
151
+ ids = setup_project()
152
+ functions = create_characterization_functions_from_method(
153
+ METHOD, characterize_uptake=False
154
+ )
155
+ assert ids["co2_in_air"] not in functions
156
+ assert ids["co2_to_soil"] not in functions
157
+
158
+
159
+ @bw2test
160
+ def test_all_method_flows_are_characterized():
161
+ ids = setup_project()
162
+ functions = create_characterization_functions_from_method(METHOD)
163
+ assert set(functions) == set(ids.values())
164
+ assert functions[ids["ch4_fossil"]] is characterize_ch4
165
+
166
+
167
+ @bw2test
168
+ def test_dynamic_gwp100_matches_static_score():
169
+ """
170
+ End-to-end: with fixed_time_horizon=False, dynamic GWP100 of an inventory
171
+ must match the static GWP100 score of the same inventory within a few percent.
172
+ """
173
+ ids = setup_project()
174
+
175
+ amounts = {
176
+ "co2_fossil_high_stacks": 17864.7,
177
+ "co2_fossil_unspecified": 2322.3,
178
+ "co2_fossil_urban": 1287.9,
179
+ "co2_fossil_stratosphere": 1.9,
180
+ "co2_from_soil_or_biomass_stock": 24.7,
181
+ "co2_non_fossil": 100.0,
182
+ "co2_to_soil": 50.0,
183
+ "co2_in_air": 200.0,
184
+ "ch4_fossil": 30.0,
185
+ }
186
+
187
+ dynamic_inventory_df = pd.DataFrame(
188
+ {
189
+ "date": pd.Series(
190
+ ["2024-01-01"] * len(amounts), dtype="datetime64[s]"
191
+ ),
192
+ "amount": pd.Series(list(amounts.values()), dtype="float64"),
193
+ "flow": pd.Series([ids[code] for code in amounts], dtype="int"),
194
+ "activity": pd.Series([1] * len(amounts), dtype="int"),
195
+ }
196
+ )
197
+
198
+ static_score = sum(
199
+ amount * CHARACTERIZATION_FACTORS[code] for code, amount in amounts.items()
200
+ )
201
+
202
+ characterized = characterize(
203
+ dynamic_inventory_df,
204
+ metric="GWP",
205
+ base_lcia_method=METHOD,
206
+ time_horizon=100,
207
+ fixed_time_horizon=False,
208
+ )
209
+
210
+ assert np.isclose(characterized.amount.sum(), static_score, rtol=0.05)
@@ -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")