Skip to content

Commit

Permalink
ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jezsadler committed Dec 9, 2024
1 parent 645013a commit 635ca36
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/omlt/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from omlt.base.language import DEFAULT_MODELING_LANGUAGE # noqa: I001
from omlt.base.language import DEFAULT_MODELING_LANGUAGE # noqa: I001

from omlt.base.constraint import (
OmltConstraint,
Expand Down
8 changes: 1 addition & 7 deletions src/omlt/base/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class OmltConstraint:

@property
def ctype(self):
return pyo.Constraint
Expand All @@ -19,9 +18,7 @@ def valid_model_component(self):
return True



class OmltConstraintScalar(OmltConstraint):

def __init__(self, lang: str = DEFAULT_MODELING_LANGUAGE, **kwargs: Any):
lhs = kwargs.pop("lhs", None)
if lhs is not None:
Expand Down Expand Up @@ -49,7 +46,6 @@ def __init__(self, lang: str = DEFAULT_MODELING_LANGUAGE, **kwargs: Any):


class OmltConstraintIndexed(OmltConstraint):

def __init__(
self, *indexes: Any, lang: str = DEFAULT_MODELING_LANGUAGE, **kwargs: Any
):
Expand All @@ -60,12 +56,10 @@ def __init__(
self.name = None
self.format = lang


def keys(self, *, sort=False): # noqa: ARG002
def keys(self, *, sort=False): # noqa: ARG002
yield from self._index_set



class OmltConstraintFactory:
def __init__(self):
self.scalars = {
Expand Down
10 changes: 4 additions & 6 deletions src/omlt/base/julia.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


class JuMPVarInfo:
def __init__( # noqa: PLR0913
def __init__( # noqa: PLR0913
self,
lower_bound=None,
upper_bound=None,
Expand Down Expand Up @@ -274,12 +274,10 @@ def ub(self, val):
def value(self):
return self._var.value


@value.setter
def value(self, val):
self._var.value = val


@property
def name(self):
return self._name
Expand Down Expand Up @@ -392,7 +390,7 @@ def __iter__(self):
"""Return an iterator of the component data keys."""
return self._vars.__iter__()

def construct(self, *, data=None): # noqa: ARG002
def construct(self, *, data=None): # noqa: ARG002
for idx in self._index_set:
if isinstance(idx, int):
name = str(self.name) + "[" + str(idx) + "]"
Expand Down Expand Up @@ -450,7 +448,7 @@ def __init__(self, *indexes: Any, **kwargs: Any):
self.format = "jump"
self._jumpcons = {idx: None for idx in self._index_set[0]}

def keys(self, *, sort=False): # noqa: ARG002
def keys(self, *, sort=False): # noqa: ARG002
yield from self._index_set

def __setitem__(self, label, item):
Expand Down Expand Up @@ -534,7 +532,7 @@ def add(self, a, b):
msg = ("Unrecognized types for addition, %s, %s", type(a), type(b))
raise TypeError(msg)

def subtract(self, a, b): # noqa: PLR0911
def subtract(self, a, b): # noqa: PLR0911
if isinstance(a, (int, float)) and isinstance(b, (JumpVar, OmltScalarJuMP)):
return jump.AffExpr(a, jump.OrderedDict([(b.varref, -1)]))
if isinstance(a, JumpVar) and isinstance(b, (int, float)):
Expand Down
4 changes: 1 addition & 3 deletions src/omlt/base/pyomo.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ def _parse_expression_tuple_term(self, term):
return term._expression
if isinstance(term, OmltScalarPyomo):
return term._pyovar
if isinstance(
term, (pyo.Expression, pyo.Var, VarData, int, float, float32)
):
if isinstance(term, (pyo.Expression, pyo.Var, VarData, int, float, float32)):
return term
msg = ("Term of expression %s is an unsupported type. %s", term, type(term))
raise TypeError(msg)
Expand Down
1 change: 1 addition & 0 deletions src/omlt/neuralnet/activations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
variable, and :math:`y` denotes post-activation variable.
"""

from typing import Any

from .linear import linear_activation_constraint, linear_activation_function
Expand Down
1 change: 1 addition & 0 deletions src/omlt/neuralnet/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

OUTPUT_DIMENSIONS = 3


class Layer:
"""Base layer class.
Expand Down
2 changes: 1 addition & 1 deletion src/omlt/neuralnet/layers/full_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from omlt.neuralnet.layer import ConvLayer2D, PoolingLayer2D


def full_space_dense_layer(net_block, net, layer_block, layer): # noqa: PLR0912
def full_space_dense_layer(net_block, net, layer_block, layer): # noqa: PLR0912
r"""Add full-space formulation of the dense layer to the block.
.. math::
Expand Down
2 changes: 1 addition & 1 deletion src/omlt/neuralnet/layers/partition_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def default_partition_split_func(w, n):
return np.array_split(sorted_indexes, n)


def partition_based_dense_relu_layer(net_block, net, layer_block, layer, split_func): # noqa: C901,PLR0912,PLR0915
def partition_based_dense_relu_layer(net_block, net, layer_block, layer, split_func): # noqa: C901,PLR0912,PLR0915
r"""Partition-based ReLU activation formulation.
Generates the constraints for the ReLU activation function:
Expand Down
10 changes: 6 additions & 4 deletions src/omlt/neuralnet/nn_formulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def _build_neural_network_formulation( # noqa: C901,PLR0912,PLR0915
var_factory = OmltVarFactory()

if block._format == "pyomo":

@block.Block(block.layers)
def layer(b, layer_id):
net_layer = net.layer(layer_id)
Expand All @@ -187,7 +188,7 @@ def layer(b, layer_id):
return b
else:
block.layers.construct()
block.layer = {lyr : OmltBlockCore() for lyr in block.layers}
block.layer = {lyr: OmltBlockCore() for lyr in block.layers}
for lyr in block.layer:
block.layer[lyr]._format = block._format
for layer_id in block.layers:
Expand Down Expand Up @@ -362,7 +363,7 @@ def __init__(self, network_structure, activation_functions=None):
def _supported_default_activation_functions(self):
return dict(_DEFAULT_ACTIVATION_FUNCTIONS)

def _build_formulation(self): # noqa: C901,PLR0912
def _build_formulation(self): # noqa: C901,PLR0912
_setup_scaled_inputs_outputs(
self.block, self.__scaling_object, self.__scaled_input_bounds
)
Expand All @@ -377,7 +378,7 @@ def _build_formulation(self): # noqa: C901,PLR0912
block.layer = pyo.Block(block.layers)
else:
block.layers.construct()
block.layer = {lyr : OmltBlockCore() for lyr in block.layers}
block.layer = {lyr: OmltBlockCore() for lyr in block.layers}
for lyr in block.layer:
block.layer[lyr]._format = block._format
# currently only support a single input layer
Expand Down Expand Up @@ -540,6 +541,7 @@ def _build_formulation(self): # noqa: C901,PLR0912,PLR0915
var_factory = OmltVarFactory()

if block._format == "pyomo":

@block.Block(block.layers)
def layer(b, layer_id):
b._format = block._format
Expand All @@ -562,7 +564,7 @@ def layer(b, layer_id):
return b
else:
block.layers.construct()
block.layer = {lyr : OmltBlockCore() for lyr in block.layers}
block.layer = {lyr: OmltBlockCore() for lyr in block.layers}
for layer_id in block.layers:
block.layer[layer_id]._format = block._format
net_layer = net.layer(layer_id)
Expand Down
1 change: 1 addition & 0 deletions src/omlt/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
expressions to the Pyomo model for the inputs and outputs of an ML model. An
implementation of a common scaling approach is included with `OffsetScaling`.
"""

import abc
from typing import Any

Expand Down
1 change: 1 addition & 0 deletions tests/base/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def _test_build_scalar_expressions(lang):
def test_build_scalar_exp_pyomo():
_test_build_scalar_expressions("pyomo")


def test_init_scalar_expression():
v1 = var_factory.new_var()
v1.domain = pyo.Integers
Expand Down
2 changes: 2 additions & 0 deletions tests/base/test_jump.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SIX = 6
ELEVEN = 11


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_variable_jump():
v = OmltScalarJuMP(bounds=(0, 5), initialize=(3,))
Expand Down Expand Up @@ -96,6 +97,7 @@ def test_expression_linear_jump():
constraint = var_minus_expr == expr_div_int
assert isinstance(constraint, OmltConstraintScalarJuMP)


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_expression_nonlinear_jump():
jump_block = OmltBlockJuMP()
Expand Down
4 changes: 1 addition & 3 deletions tests/base/test_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ def test_indexed_pyomo():


def test_indexed_invalid_lang():
expected_msg = (
"Variable format %s not recognized. Supported formats are %s"
)
expected_msg = "Variable format %s not recognized. Supported formats are %s"
with pytest.raises(KeyError, match=expected_msg):
var_factory.new_var(range(3), lang="test")
1 change: 1 addition & 0 deletions tests/neuralnet/test_relu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

NEAR_EQUAL = 1e-3


def test_two_node_bigm(two_node_network_relu):
m = pyo.ConcreteModel()
m.neural_net_block = OmltBlock()
Expand Down

0 comments on commit 635ca36

Please sign in to comment.