Skip to content

Commit 082c7fb

Browse files
coroaFabianHofmannlkstrp
authored
fix(statistics): fix statistics regression with investment periods (PyPSA#1172)
* fix(statistics): fix statistics regression with investment periods Follow-up to PyPSA#1044. Closes PyPSA#1144. Iterative intersection in `filter_active_assets` only accounted for components already active in the first time period. * add release notes * add test * add pr ref to release note --------- Co-authored-by: Fabian <[email protected]> Co-authored-by: Lukas Trippe <[email protected]>
1 parent 7b02177 commit 082c7fb

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

doc/references/release-notes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ Features
3030
(https://github.com/PyPSA/PyPSA/pull/1155)
3131

3232

33+
Bug fixes
34+
---------
35+
36+
* **Regression hotfix**: Fixed a critical bug in statistics functions for multi-investment networks where built years and lifetimes were not being correctly considered. In version `v0.32.0`, only components active in the first time period were being included in statistics calculations. The fix ensures all components are properly represented according to their respective built years and lifetimes across all investment periods. (https://github.com/PyPSA/PyPSA/pull/1172)
37+
3338
* The expressions function `n.optimize.expressions.capacity` now uses the absolute efficiency
3439
to calculate the capacity at link ports, unless a `bus_carrier` is defined or `at_port` is set to True.
3540
This is in line with the behavior of the statistics functions (`statistics.installed_capacity`, `statistics.optimal_capacity`).

pypsa/statistics/abstract.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,12 @@ def _filter_active_assets(self, n: Network, c: str, obj: Any) -> Any:
245245
idx = self._get_component_index(obj, c)
246246
if not self.is_multi_indexed:
247247
mask = n.get_active_assets(c)
248-
idx = mask.index[mask].intersection(idx)
249-
return obj.loc[idx]
248+
return obj.loc[mask.index[mask].intersection(idx)]
250249

251250
per_period = {}
252251
for p in n.investment_periods:
253252
mask = n.get_active_assets(c, p)
254-
idx = mask.index[mask].intersection(idx)
255-
per_period[p] = obj.loc[idx]
253+
per_period[p] = obj.loc[mask.index[mask].intersection(idx)]
256254

257255
return self._concat_periods(per_period, c)
258256

test/test_bugs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
import pypsa
66

77

8+
def test_1144():
9+
"""
10+
See https://github.com/PyPSA/PyPSA/issues/1144.
11+
"""
12+
n = pypsa.examples.ac_dc_meshed()
13+
n.generators["build_year"] = [2020, 2020, 2030, 2030, 2040, 2040]
14+
n.investment_periods = [2020, 2030, 2040]
15+
capacity = n.statistics.installed_capacity(comps="Generator")
16+
assert capacity[2020].sum() < capacity[2030].sum() < capacity[2040].sum()
17+
18+
819
def test_890():
920
"""
1021
See https://github.com/PyPSA/PyPSA/issues/890.

0 commit comments

Comments
 (0)