Skip to content

Ban implicit broadcasting over region dimension#840

Merged
tsmbland merged 36 commits into
mainfrom
broadcast_regions
May 7, 2026
Merged

Ban implicit broadcasting over region dimension#840
tsmbland merged 36 commits into
mainfrom
broadcast_regions

Conversation

@tsmbland
Copy link
Copy Markdown
Collaborator

@tsmbland tsmbland commented May 6, 2026

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.

tsmbland and others added 30 commits May 1, 2026 15:29
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>
Base automatically changed from cost_minimising_supply to main May 7, 2026 11:07
@tsmbland tsmbland marked this pull request as ready for review May 7, 2026 13:27
@tsmbland tsmbland requested a review from Copilot May 7, 2026 13:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 in muse.utilities to explicitly expand non-regional arrays along region.
  • Updated timeslice distribution and carbon price propagation logic to explicitly broadcast along region before 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.

Comment thread src/muse/__main__.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@tsmbland tsmbland merged commit 2421b87 into main May 7, 2026
14 checks passed
@github-project-automation github-project-automation Bot moved this to ✅ Done in MUSE May 7, 2026
@tsmbland tsmbland deleted the broadcast_regions branch May 7, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

2 participants