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

Explicit properties inheritance #287

Merged
merged 2 commits into from
May 12, 2024
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
87 changes: 70 additions & 17 deletions linopy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
LocIndexer,
align_lines_by_delimiter,
format_string_as_variable_name,
forward_as_properties,
generate_indices_for_printout,
get_label_position,
has_optimized_model,
Expand Down Expand Up @@ -62,18 +61,6 @@ def _con_unwrap(con):
return con.data if isinstance(con, Constraint) else con


@forward_as_properties(
data=[
"attrs",
"coords",
"indexes",
"dims",
"sizes",
],
labels=["values"],
lhs=["nterm"],
rhs=["ndim", "shape", "size"],
)
class Constraint:
"""
Projection to a single constraint in a model.
Expand Down Expand Up @@ -129,6 +116,76 @@ def __getitem__(self, selector) -> "Constraints":
data = Dataset({k: self.data[k][selector] for k in self.data}, attrs=self.attrs)
return self.__class__(data, self.model, self.name)

@property
def attrs(self):
"""
Get the attributes of the constraint.
"""
return self.data.attrs

@property
def coords(self):
"""
Get the coordinates of the constraint.
"""
return self.data.coords

@property
def indexes(self):
"""
Get the indexes of the constraint.
"""
return self.data.indexes

@property
def dims(self):
"""
Get the dimensions of the constraint.
"""
return self.data.dims

@property
def sizes(self):
"""
Get the sizes of the constraint.
"""
return self.data.sizes

@property
def values(self):
"""
Get the label values of the constraint.
"""
return self.labels.values if self.is_assigned else None

@property
def nterm(self):
"""
Get the number of terms in the constraint.
"""
return self.lhs.nterm

@property
def ndim(self):
"""
Get the number of dimensions of the constraint.
"""
return self.rhs.ndim

@property
def shape(self):
"""
Get the shape of the constraint.
"""
return self.rhs.shape

@property
def size(self):
"""
Get the size of the constraint.
"""
return self.rhs.size

@property
def loc(self):
return LocIndexer(self)
Expand Down Expand Up @@ -392,10 +449,6 @@ def dual(self, value):
value = DataArray(value).broadcast_like(self.labels)
self.data["dual"] = value

@property
def shape(self):
return self.labels.shape

@classmethod
def from_rule(cls, model, rule, coords):
"""
Expand Down
37 changes: 35 additions & 2 deletions linopy/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ def sum(self, **kwargs):
return LinearExpression(ds, self.model)


@forward_as_properties(data=["attrs", "coords", "indexes", "sizes"], const=["ndim"])
class LinearExpression:
"""
A linear expression consisting of terms of coefficients and variables.
Expand Down Expand Up @@ -589,6 +588,41 @@ def __getitem__(self, selector) -> Union["LinearExpression", "QuadraticExpressio
data = Dataset({k: self.data[k][selector] for k in self.data}, attrs=self.attrs)
return self.__class__(data, self.model)

@property
def attrs(self):
"""
Get the attributes of the expression
"""
return self.data.attrs

@property
def coords(self):
"""
Get the coordinates of the expression
"""
return self.data.coords

@property
def indexes(self):
"""
Get the indexes of the expression
"""
return self.data.indexes

@property
def sizes(self):
"""
Get the sizes of the expression
"""
return self.data.sizes

@property
def ndim(self):
"""
Get the number of dimensions.
"""
return self.const.ndim

@property
def loc(self):
return LocIndexer(self)
Expand Down Expand Up @@ -1281,7 +1315,6 @@ def mask_func(data):
stack = exprwrap(Dataset.stack)


@forward_as_properties(data=["attrs", "coords", "indexes", "sizes"])
class QuadraticExpression(LinearExpression):
"""
A quadratic expression consisting of terms of coefficients and variables.
Expand Down
77 changes: 63 additions & 14 deletions linopy/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy as np

from linopy import expressions
from linopy.common import forward_as_properties


def objwrap(method, *default_args, **new_default_kwargs):
Expand All @@ -33,19 +32,6 @@ def _objwrap(obj, *args, **kwargs):
return _objwrap


@forward_as_properties(
expression=[
"attrs",
"coords",
"indexes",
"sizes",
"flat",
"coeffs",
"vars",
"data",
"nterm",
]
)
class Objective:
"""
An objective expression containing all relevant information.
Expand Down Expand Up @@ -83,6 +69,69 @@ def __repr__(self) -> str:

return f"Objective:\n----------\n{expr_string}\n{sense_string}\n{value_string}"

@property
def attrs(self):
"""
Returns the attributes of the objective.
"""
return self.expression.attrs

@property
def coords(self):
"""
Returns the coordinates of the objective.
"""
return self.expression.coords

@property
def indexes(self):
"""
Returns the indexes of the objective.
"""
return self.expression.indexes

@property
def sizes(self):
"""
Returns the sizes of the objective.
"""
return self.expression.sizes

@property
def flat(self):
"""
Returns the flattened objective.
"""
return self.expression.flat

@property
def coeffs(self):
"""
Returns the coefficients of the objective.
"""
return self.expression.coeffs

@property
def vars(self):
"""
Returns the variables of the objective.
"""
return self.expression.vars

@property
def data(self):
"""
Returns the data of the objective.
"""
return self.expression.data

@property
def nterm(self):
"""
Returns the number of terms in the objective.
"""
return self.expression.nterm

@property
def expression(self):
"""
Expand Down
Loading
Loading