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

Fix representation for single constraints without coordinates #92

Merged
merged 1 commit into from
Jan 17, 2023
Merged
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
7 changes: 5 additions & 2 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
Release Notes
=============

.. Upcoming Release
.. ----------------
Upcoming Release
----------------

* Fix display for constraint with single entry and no coordinates.


Version 0.1.1
-------------
Expand Down
12 changes: 7 additions & 5 deletions linopy/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,16 @@ def print_single_expression(c, v, model):
"""
# catch case that to many terms would be printed
def print_line(expr):
res = ""
res = []
for i, (coeff, (name, coord)) in enumerate(expr):
coord_string = print_coord(coord)
if i:
res += f" {float(coeff):+.4} {name}{coord_string} "
# split sign and coefficient
coeff_string = f"{float(coeff):+.4}"
res.append(f"{coeff_string[0]} {coeff_string[1:]} {name}{coord_string}")
else:
res += f" {float(coeff):.4} {name}{coord_string} "
return res if res else " None"
res.append(f"{float(coeff):.4} {name}{coord_string}")
return " ".join(res) if len(res) else "None"

if isinstance(c, np.ndarray):
mask = v != -1
Expand All @@ -133,7 +135,7 @@ def print_line(expr):
truncate = max_terms // 2
expr = list(zip(c[:truncate], model.variables.get_label_position(v[:truncate])))
res = print_line(expr)
res += "... "
res += " ... "
expr = list(
zip(c[-truncate:], model.variables.get_label_position(v[-truncate:]))
)
Expand Down
14 changes: 7 additions & 7 deletions linopy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,25 @@ def __repr__(self):
Get the string representation of the Constraint.
"""
# return single if only one exist
if self.size == self.nterm:
if self.lhs.size == self.nterm:
expr_string = print_single_expression(
self.lhs.coeffs.values, self.lhs.vars.values, self.lhs.model
)
header = f"{self.type} `{self.name}`\n" + "-" * (
len(self.type) + len(self.name) + 3
)
name_string = f" `{self.name}`" if self.name else ""
header_string = str(self.type) + name_string
header = f"{header_string}\n" + "-" * (len(header_string))
return f"{header}\n{expr_string} {self.sign.item()} {self.rhs.item()}"

# create header string
name_string = f"`{self.name}`" if self.name else ""
shape_string = ", ".join(
[f"{self.dims[i]}: {self.shape[i]}" for i in range(self.ndim)]
)
shape_string = f"({shape_string})"
n_masked = (~self.mask).sum().item()
mask_string = f" - {n_masked} masked entries" if n_masked else ""
header = f"{self.type} `{self.name}` {shape_string}{mask_string}\n" + "-" * (
len(self.type) + len(self.name) + len(shape_string) + len(mask_string) + 4
)
header_string = f"{self.type} {name_string} {shape_string}" + mask_string
header = f"{header_string}\n" + "-" * (len(header_string))

# create data string, print only a few values
max_print = options["display_max_rows"]
Expand Down
6 changes: 4 additions & 2 deletions linopy/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ def __repr__(self):
lower = self.lower.item()
upper = self.upper.item()
coord = []
data_string = print_single_variable(self, self.name, coord, lower, upper)
return f"{header}\n{data_string}"
var_string, bound_string = print_single_variable(
self, self.name, coord, lower, upper
)
return f"{header}\n{var_string} {bound_string}"

# create header string
if self.shape:
Expand Down
17 changes: 16 additions & 1 deletion test/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ def test_linear_expression_long():


def test_scalar_linear_expression_repr():
repr(1 * u[0, 0])
for var in [u, v, x, y, z, a, b, c, d]:
coord = tuple([var.indexes[c][0] for c in var.dims])
repr(1 * var[coord])


def test_single_linear_repr():
for var in [u, v, x, y, z, a, b, c, d]:
coord = tuple([var.indexes[c][0] for c in var.dims])
repr(1 * var.loc[coord])


def test_anonymous_constraint_repr():
Expand All @@ -111,6 +119,13 @@ def test_scalar_constraint_repr():
repr(1 * u[0, 0] >= 0)


def test_single_constraint_repr():
for var in [u, v, x, y, z, a, b, c, d]:
coord = tuple([var.indexes[c][0] for c in var.dims])
repr(1 * var.loc[coord] == 0)
repr(1 * var.loc[coord] - var.loc[coord] == 0)


def test_constraint_repr():
for con in [cu, cv, cx, cy, cz, ca, cb, cc, cd, cav, cu_masked]:
repr(con)
Expand Down