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

sanitize infinity values: fix multiindex assignment #371

Merged
merged 5 commits into from
Oct 31, 2024
Merged
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
25 changes: 12 additions & 13 deletions linopy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,33 +864,32 @@ def sanitize_zeros(self) -> None:
"""
for name in self:
not_zero = abs(self[name].coeffs) > 1e-10
constraint = self[name]
constraint.vars = self[name].vars.where(not_zero, -1)
constraint.coeffs = self[name].coeffs.where(not_zero)
con = self[name]
con.vars = self[name].vars.where(not_zero, -1)
con.coeffs = self[name].coeffs.where(not_zero)

def sanitize_missings(self) -> None:
"""
Set constraints labels to -1 where all variables in the lhs are
missing.
"""
for name in self:
contains_non_missing = (self[name].vars != -1).any(self[name].term_dim)
self[name].data["labels"] = self[name].labels.where(
contains_non_missing, -1
)
con = self[name]
contains_non_missing = (con.vars != -1).any(con.term_dim)
labels = self[name].labels.where(contains_non_missing, -1)
con._data = assign_multiindex_safe(con.data, labels=labels)

def sanitize_infinities(self) -> None:
"""
Replace infinite values in the constraints with a large value.
"""
for name in self:
constraint = self[name]
valid_infinity_values = (
(constraint.sign == LESS_EQUAL) & (constraint.rhs == np.inf)
) | ((constraint.sign == GREATER_EQUAL) & (constraint.rhs == -np.inf))
self[name].data["labels"] = self[name].labels.where(
~valid_infinity_values, -1
con = self[name]
valid_infinity_values = ((con.sign == LESS_EQUAL) & (con.rhs == np.inf)) | (
(con.sign == GREATER_EQUAL) & (con.rhs == -np.inf)
)
labels = con.labels.where(~valid_infinity_values, -1)
con._data = assign_multiindex_safe(con.data, labels=labels)

def get_name_by_label(self, label: Union[int, float]) -> str:
"""
Expand Down