Ban implicit broadcasting over region dimension#840
Merged
Conversation
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
This reverts commit b83550a.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR disables implicit xarray broadcasting over the region dimension (during tests/debugging via the patched xarray broadcasting hook), and updates affected code paths to explicitly broadcast to region where needed.
Changes:
- Added
broadcast_regions()helper inmuse.utilitiesto explicitly expand non-regional arrays alongregion. - Updated timeslice distribution and carbon price propagation logic to explicitly broadcast along
regionbefore combining arrays. - Updated test fixtures (and one demand edge-case) to avoid region-implicit broadcasting failures.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
tests/conftest.py |
Updates test fixtures to explicitly broadcast inputs over region instead of relying on implicit broadcasting. |
src/muse/utilities.py |
Introduces broadcast_regions() helper to make region expansion explicit and validated. |
src/muse/timeslices.py |
Ensures distribute_timeslice() explicitly broadcasts timeslice fractions over region when needed. |
src/muse/mca.py |
Explicitly broadcasts the carbon price update over region before timeslice broadcasting. |
src/muse/demand_share.py |
Adds a guard for empty-asset capacity to return fully unmet demand. |
src/muse/__main__.py |
Extends the broadcast-compat patch to error on implicit region broadcasting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implicit broadcasting is a recipe for hidden bugs. It also makes the code way more readable to know the dimensions of the data structures involved in operations. May also help AI
In this PR I'm turning off implicit broadcasting over the "region" dimension, and have fixed up the code in a few places to explicitly broadcast when required. This mostly involved running the tests and example models and fixing up any issues flagged. Any instances of implicit broadcasting not covered by tests/example modes are likely still included. Hopefully there aren't any/many left, and if there are they're highly likely to be benign (this isn't as dangerous as broadcasting over the timeslice dimension where broadcasting is often not the correct approach). The good news is that the check is only raised during testing and debugging, so users will never see this message even if some parts of the code do still require implicit broadcasting.