Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check for valid dimension name before creating new variable #360

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def check_valid_dim_names(self, ds: DataArray | Dataset) -> None:
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:
Comment on lines +380 to +383
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
contains_unsupported_dim_names = any(
dim in unsupported_dim_names for dim in list(ds.dims)
)
if contains_unsupported_dim_names:
if any(dim in unsupported_dim_names for dim in ds.dims):

Maybe a little more condensed?

raise ValueError(
"Added data contains unsupported dimension names. "
"Dimensions cannot be named 'labels', 'coeffs', 'vars', 'sign' or 'rhs'."
)
else:
return
Comment on lines +388 to +389
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.check_valid_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
18 changes: 18 additions & 0 deletions test/test_variable_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ def test_variable_assignment_without_coords_and_dims_names():
assert x.dims == ("i", "j")


def test_variable_assignment_without_coords_and_invalid_dims_names():
# setting bounds without explicit coords
m = Model()
lower = np.zeros((10, 10))
upper = np.ones((10, 10))
with pytest.raises(ValueError):
m.add_variables(lower, upper, name="x", dims=["sign", "j"])


def test_variable_assignment_without_coords_in_bounds():
# setting bounds without explicit coords
m = Model()
Expand All @@ -141,6 +150,15 @@ def test_variable_assignment_without_coords_in_bounds():
assert x.dims == ("i", "j")


def test_variable_assignment_without_coords_in_bounds_invalid_dims_names():
# setting bounds without explicit coords
m = Model()
lower = xr.DataArray(np.zeros((10, 10)), dims=["i", "sign"])
upper = xr.DataArray(np.ones((10, 10)), dims=["i", "sign"])
with pytest.raises(ValueError):
m.add_variables(lower, upper, name="x")


def test_variable_assignment_without_coords_pandas_types():
# setting bounds without explicit coords
m = Model()
Expand Down