-
Notifications
You must be signed in to change notification settings - Fork 51
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||
Comment on lines
+380
to
+383
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
def add_variables( | ||||||||||||
self, | ||||||||||||
lower: Any = -inf, | ||||||||||||
|
@@ -464,6 +495,7 @@ def add_variables( | |||||||||||
) | ||||||||||||
(data,) = xr.broadcast(data) | ||||||||||||
self.check_force_dim_names(data) | ||||||||||||
self.check_valid_dim_names(data) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
if mask is not None: | ||||||||||||
mask = as_dataarray(mask, coords=data.coords, dims=data.dims).astype(bool) | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.