From 24f4c3594b82b7fac9cd319cd57b4dfe36197802 Mon Sep 17 00:00:00 2001 From: phschiele Date: Sun, 12 May 2024 22:01:27 -0700 Subject: [PATCH] Ruff fixes + version bump --- dsp/cone_transforms.py | 6 +----- dsp/parser.py | 3 --- dsp/problem.py | 5 ++--- dsp/saddle_atoms.py | 11 +++++++---- dsp/saddle_extremum.py | 6 +----- pyproject.toml | 6 +++--- setup.cfg | 5 +---- 7 files changed, 15 insertions(+), 27 deletions(-) diff --git a/dsp/cone_transforms.py b/dsp/cone_transforms.py index e21f5ee..b4d88e8 100644 --- a/dsp/cone_transforms.py +++ b/dsp/cone_transforms.py @@ -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 @@ -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]] = {} @@ -296,7 +295,6 @@ def K_repr_FxGy( local_to_glob: LocalToGlob, switched: bool = False, ) -> KRepresentation: - z = cp.Variable(Fx.shape) constraints = [z >= Fx] @@ -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 @@ -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] diff --git a/dsp/parser.py b/dsp/parser.py index bbede98..4bb9e82 100644 --- a/dsp/parser.py +++ b/dsp/parser.py @@ -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: @@ -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 @@ -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 diff --git a/dsp/problem.py b/dsp/problem.py index 7c440c9..c2c4414 100644 --- a/dsp/problem.py +++ b/dsp/problem.py @@ -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) @@ -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 @@ -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: diff --git a/dsp/saddle_atoms.py b/dsp/saddle_atoms.py index cefc5a3..4a408da 100644 --- a/dsp/saddle_atoms.py +++ b/dsp/saddle_atoms.py @@ -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) @@ -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 @@ -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() @@ -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() diff --git a/dsp/saddle_extremum.py b/dsp/saddle_extremum.py index 6edf854..7f1aead 100644 --- a/dsp/saddle_extremum.py +++ b/dsp/saddle_extremum.py @@ -20,7 +20,6 @@ def __init__( f: cp.Expression, constraints: Iterable[cp.Constraint], ) -> None: - self.f = f self._parser = None @@ -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 @@ -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())) @@ -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, @@ -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())) diff --git a/pyproject.toml b/pyproject.toml index 383cda9..8822e2c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"] \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 0027079..93795e0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 = philipp.schiele@stat.uni-muenchen.de, ericlux@stanford.edu, boyd@stanford.edu url = https://github.com/cvxgrp/dsp @@ -23,6 +23,3 @@ dev = tox [options.package_data] * = README.md - -[flake8] -max-line-length = 100 \ No newline at end of file