Skip to content

Fix NetworkCollection statistics for networks with different snapshots#1636

Merged
lkstrp merged 13 commits intomasterfrom
fix/network-collection-different-sns
Mar 27, 2026
Merged

Fix NetworkCollection statistics for networks with different snapshots#1636
lkstrp merged 13 commits intomasterfrom
fix/network-collection-different-sns

Conversation

@FabianHofmann
Copy link
Copy Markdown
Contributor

Closes #1605.

Changes proposed in this Pull Request

NetworkCollection statistics methods (supply(), opex(), energy_balance(), etc.) failed or returned wrong results when networks had different snapshots or snapshot weightings. Two root causes were identified and fixed:

  • snapshot_weightings used the return_from_first proxy pattern, returning only the first network's weights. This was changed to vertical_concat, producing a MultiIndex (network, snapshot) DataFrame with all networks' weightings.
  • _aggregate_with_weights performed weights @ df where the weights index and DataFrame column index were misaligned for collections. A new _weighted_sum_per_network method splits the computation by network key, computes weighted sums independently per network, then concatenates results.

The _aggregate_with_weights method was also restructured to use early returns for better readability.

A mock_single_bus_dispatch test helper was added to conftest.py to create networks with hard-set dispatch results without invoking a solver, keeping the new tests fast.

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in docs.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes docs/release-notes.md of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

#1605)

Move snapshot_weightings from return_from_first to vertical_concat pattern
and compute weighted sums per-network in _aggregate_with_weights.
…ize to handle both simple and collection cases
@FabianHofmann FabianHofmann requested a review from lkstrp March 25, 2026 11:21
Comment thread docs/release-notes.md Outdated
Comment thread pypsa/statistics/expressions.py Outdated
Comment thread pypsa/statistics/expressions.py Outdated
Comment thread pypsa/statistics/expressions.py
# Todo: here we leave out the weights, is that correct?
return df.agg(agg)
if agg != "sum":
return df.agg(agg)
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.

Why can the special collection handling be ignored for everything except sum? We never reach the collection aware state then

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

that is fine because only sum needs to know about the different weights. "mean" does not go here, so only "max" and "min" (never thought about "std" though), and these can be returned straight forwards

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.

True, you are right. I think std and median would need weights as well, but not sure if worth to add ourself, since pandas doesn't support it. Not sure if this should be noted somewhere. Maybe here https://docs.pypsa.org/latest/user-guide/statistics/#grouping

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed — this is not ideal. For now, I added a TODO comment but we should fix this mid-term

Comment thread pypsa/statistics/expressions.py Outdated
Comment thread pypsa/collection.py
Comment thread pypsa/collection.py
@@ -530,7 +529,6 @@ def _get_method_patterns() -> dict[str, str]:
"return_from_first": r"^("
r"\S+_components|"
r"snapshots|"
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.

Merging snapshots as return_from_first will still lead to issues when not just weightings but also snapshots differ, as you write in the PR?

As already discussed. The collection was never designed to handle different dimensions. But mid term we would need way to define strict same dimensions (snapshots here) and a way to convert all input networks there. For snapshot weightings it still makes sense to give them the multi index.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, added a note in the statistics user guide

Comment thread test/conftest.py Outdated
Comment thread test/test_collection_statistics.py
Copy link
Copy Markdown
Contributor Author

@FabianHofmann FabianHofmann left a comment

Choose a reason for hiding this comment

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

all comments adressed

Comment thread test/conftest.py Outdated
Comment thread pypsa/collection.py
Comment thread pypsa/collection.py
@@ -530,7 +529,6 @@ def _get_method_patterns() -> dict[str, str]:
"return_from_first": r"^("
r"\S+_components|"
r"snapshots|"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, added a note in the statistics user guide

# Todo: here we leave out the weights, is that correct?
return df.agg(agg)
if agg != "sum":
return df.agg(agg)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed — this is not ideal. For now, I added a TODO comment but we should fix this mid-term

@lkstrp lkstrp enabled auto-merge (squash) March 27, 2026 10:44
@lkstrp lkstrp merged commit fceb755 into master Mar 27, 2026
27 checks passed
@lkstrp lkstrp deleted the fix/network-collection-different-sns branch March 27, 2026 11:54
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.

statistics on NetworkCollection not working reliably with different snapshot_weightings

2 participants