Skip to content

Commit 20833e9

Browse files
committed
Fix mask broadcasting for numpy where in add_constraints
The switch from xarray's where() to numpy's where() broke dimension-aware broadcasting. A 1D mask with shape (10,) was being broadcast to (1, 10) instead of (10, 1), applying to the wrong dimension. Fix: Explicitly broadcast mask to match data.labels shape before using np.where.
1 parent 3316c0f commit 20833e9

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

linopy/model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,9 @@ def add_constraints(
750750
assert set(mask.dims).issubset(data.dims), (
751751
"Dimensions of mask not a subset of resulting labels dimensions."
752752
)
753+
# Broadcast mask to match data shape for correct numpy where behavior
754+
if mask.shape != data.labels.shape:
755+
mask, _ = xr.broadcast(mask, data.labels)
753756

754757
# Auto-mask based on null expressions or NaN RHS (use numpy for speed)
755758
if self.auto_mask:

0 commit comments

Comments
 (0)