Skip to content

Commit

Permalink
Ruff fixes + version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
phschiele committed May 13, 2024
1 parent 957a786 commit 24f4c35
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 27 deletions.
6 changes: 1 addition & 5 deletions dsp/cone_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def sum_of_K_reprs(cls, reprs: list[KRepresentation]) -> KRepresentation:
offset = np.sum([K.offset for K in reprs])

def f_concave(x: np.ndarray) -> cp.Expression:
nones = any([K.concave_expr(x) is None for K in reprs])
nones = any(K.concave_expr(x) is None for K in reprs)
return cp.sum([K.concave_expr(x) for K in reprs]) if not nones else None

concave_expr = f_concave
Expand Down Expand Up @@ -214,7 +214,6 @@ def K_repr_ax(a: cp.Expression) -> KRepresentation:

class LocalToGlob:
def __init__(self, x_variables: list[cp.Variable], y_variables: list[cp.Variable]) -> None:

self.outer_x_vars = x_variables
self.var_to_glob: dict[int, tuple[int, int]] = {}

Expand Down Expand Up @@ -296,7 +295,6 @@ def K_repr_FxGy(
local_to_glob: LocalToGlob,
switched: bool = False,
) -> KRepresentation:

z = cp.Variable(Fx.shape)
constraints = [z >= Fx]

Expand Down Expand Up @@ -335,7 +333,6 @@ def K_repr_bilin(
def get_cone_repr(
const: list[Constraint], variables: list[cp.Variable]
) -> tuple[dict[int, np.ndarray], np.ndarray, ConeDims]:

assert all(isinstance(v, cp.Variable) for v in variables)

# TODO: CVXPY does not have a stable API for getting the cone representation that is
Expand Down Expand Up @@ -504,7 +501,6 @@ def create_sparse_matrix_from_columns(
local_to_glob: LocalToGlob,
var_to_mat_mapping: dict[int, sp.csc_matrix],
) -> sp.csc_matrix:

cols = []
for v in variables:
start, end = local_to_glob.var_to_glob[v.id]
Expand Down
3 changes: 0 additions & 3 deletions dsp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def affine_error_message(affine_vars: list[cp.Variable]) -> str:

class Parser:
def __init__(self, convex_vars: set[cp.Variable], concave_vars: set[cp.Variable]) -> None:

self.convex_vars: set[cp.Variable] = convex_vars
self.concave_vars: set[cp.Variable] = concave_vars
if self.convex_vars & self.concave_vars:
Expand Down Expand Up @@ -170,7 +169,6 @@ def parse_expr_variables(self, expr: cp.Expression, switched: bool, **kwargs: di
def parse_expr_repr(
self, expr: cp.Expression, switched: bool, local_to_glob: LocalToGlob
) -> KRepresentation:

K_repr = self._parse_expr(expr, switched, repr_parse=True, local_to_glob=local_to_glob)
assert isinstance(K_repr, KRepresentation)
return K_repr
Expand All @@ -192,7 +190,6 @@ def contains_curvature_lumping(expr: cp.Expression) -> bool:
def _parse_expr(
self, expr: cp.Expression, switched: bool, repr_parse: bool, **kwargs: dict
) -> KRepresentation | None:

# constant
if not expr.variables():
assert expr.size == 1
Expand Down
5 changes: 2 additions & 3 deletions dsp/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def dualized_problem(
minimization_vars: Iterable[cp.Variable],
maximization_vars: Iterable[cp.Variable],
) -> tuple[list[Constraint], Objective]:

parser = initialize_parser(obj_expr, minimization_vars, maximization_vars, constraints)

local_to_glob_y = LocalToGlob(parser.convex_vars, parser.concave_vars)
Expand Down Expand Up @@ -147,7 +146,7 @@ def value(self) -> float | None:
def is_dsp(self) -> bool:
# try to form x_prob and catch the exception
try:
self.x_prob
self.x_prob # noqa
return True
except DSPError:
return False
Expand Down Expand Up @@ -269,7 +268,7 @@ def is_dsp(obj: cp.Problem | SaddlePointProblem | cp.Expression) -> bool:
return obj.is_dsp()
elif isinstance(obj, cp.Problem):
all_SE_atoms = get_problem_SE_atoms(obj)
return obj.is_dcp() and all([atom.is_dsp() for atom in all_SE_atoms])
return obj.is_dcp() and all(atom.is_dsp() for atom in all_SE_atoms)
elif isinstance(obj, cp.Expression):
return is_dsp_expr(obj)
else:
Expand Down
11 changes: 7 additions & 4 deletions dsp/saddle_atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def is_dsp(self) -> bool:
raise NotImplementedError

def numeric(self, values: list[np.ndarray | None]) -> np.ndarray | None:
if any([v is None for v in values]):
if any(v is None for v in values):
return None
return self._numeric(values)

Expand Down Expand Up @@ -101,7 +101,8 @@ def __init__(self, Fx: cp.Expression, Gy: cp.Expression) -> None:
if (not self.bilinear) and (not Gy.is_nonneg()):
warnings.warn(
"Gy is non-positive. The y domain of saddle_inner is Gy >="
" 0. The implicit constraint Gy >= 0 will be added to the problem."
" 0. The implicit constraint Gy >= 0 will be added to the problem.",
stacklevel=1,
)

self.Fx = Fx
Expand Down Expand Up @@ -226,7 +227,8 @@ def __init__(self, exponents: cp.Expression, weights: cp.Expression) -> None:
warnings.warn(
"Weights are non-positive. The domain of weighted log-sum-exp is y >="
" 0. The implicit constraint y >= 0 will be added to"
" the problem."
" the problem.",
stacklevel=1,
)

self.concave_composition = not weights.is_affine()
Expand Down Expand Up @@ -560,7 +562,8 @@ def __init__(self, x: cp.Expression, y: cp.Expression) -> None:
warnings.warn(
"Weights are non-positive. The domain of weighted_norm2 is y >="
" 0. The implicit constraint y >= 0 will be added to"
" the problem."
" the problem.",
stacklevel=1,
)

self.concave_composition = not y.is_affine()
Expand Down
6 changes: 1 addition & 5 deletions dsp/saddle_extremum.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(
f: cp.Expression,
constraints: Iterable[cp.Constraint],
) -> None:

self.f = f
self._parser = None

Expand All @@ -44,7 +43,7 @@ def _validate_arguments(self, constraints: list[cp.Constraint]) -> None:
def is_dsp(self) -> bool:
try:
self.parser # noqa
return all([c.is_dcp() for c in self.constraints])
return all(c.is_dcp() for c in self.constraints)
except DSPError:
return False

Expand Down Expand Up @@ -81,7 +80,6 @@ def __init__(
f: cp.Expression,
constraints: Iterable[cp.Constraint],
) -> None:

super().__init__(f, constraints)

self._concave_vars = set(filter(lambda v: isinstance(v, dsp.LocalVariable), f.variables()))
Expand All @@ -99,7 +97,6 @@ def concave_variables(self) -> list[cp.Variable]:
@property
def parser(self) -> Parser:
if self._parser is None:

parser = initialize_parser(
self.f,
minimization_vars=self.other_variables,
Expand Down Expand Up @@ -155,7 +152,6 @@ def __init__(
f: cp.Expression,
constraints: Iterable[cp.Constraint],
) -> None:

super().__init__(f, constraints)

self._convex_vars = set(filter(lambda v: isinstance(v, dsp.LocalVariable), f.variables()))
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.ruff]
select = ["E", "F", "I", "UP", "B", "ANN", "N", "C4"]
lint.select = ["E", "F", "I", "UP", "B", "ANN", "N", "C4"]
line-length = 100
target-version = "py37"
ignore = ["N806", "ANN101", "ANN102", "N801", "N802", "N803", "ANN401"]
target-version = "py38"
lint.ignore = ["N806", "ANN101", "ANN102", "N801", "N802", "N803", "ANN401"]
exclude = ["tests"]
5 changes: 1 addition & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = dsp-cvxpy
version = 0.1.0
version = 0.1.1
author = Philipp Schiele, Eric Luxenberg, Stephen Boyd
author_email = [email protected], [email protected], [email protected]
url = https://github.com/cvxgrp/dsp
Expand All @@ -23,6 +23,3 @@ dev = tox

[options.package_data]
* = README.md

[flake8]
max-line-length = 100

0 comments on commit 24f4c35

Please sign in to comment.