Skip to content

Commit

Permalink
fix: unsupported dimension names (PyPSA#359)
Browse files Browse the repository at this point in the history
Add function to ensure that added variables do not use unvalid dimension names.
  • Loading branch information
tom-welfonder committed Oct 10, 2024
1 parent e11c575 commit 8140562
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions linopy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,37 @@ def check_force_dim_names(self, ds: DataArray | Dataset) -> None:
else:
return

def check_valid_dim_names(self, ds: DataArray | Dataset) -> None:
"""
Ensure that the added data does not lead to a naming conflict.
Parameters
----------
model : linopy.Model
ds : xr.DataArray/Variable/LinearExpression
Data that should be added to the model.
Raises
------
ValueError
If broadcasted data leads to unsupported dimension names.
Returns
-------
None.
"""
unsupported_dim_names = ["labels", "coeffs", "vars", "sign", "rhs"]
contains_unsupported_dim_names = any(
dim in unsupported_dim_names for dim in list(ds.dims)
)
if contains_unsupported_dim_names:
raise ValueError(
"Added data contains unsupported dimension names. "
"Dimensions cannot be named 'labels', 'coeffs', 'vars', 'sign' or 'rhs'."
)
else:
return

def add_variables(
self,
lower: Any = -inf,
Expand Down Expand Up @@ -464,6 +495,7 @@ def add_variables(
)
(data,) = xr.broadcast(data)
self.check_force_dim_names(data)
self.check_valid_dim_names(data)

if mask is not None:
mask = as_dataarray(mask, coords=data.coords, dims=data.dims).astype(bool)
Expand Down

0 comments on commit 8140562

Please sign in to comment.