From 5fb8a70e7c4f0ca8c16a46d0719b48e8f2ebb099 Mon Sep 17 00:00:00 2001 From: Ioannis Filippidis Date: Fri, 28 May 2021 05:53:48 +0200 Subject: [PATCH] API: replace `assert` statements with `raise` statements - to ensure that when optimizations are requested from Python, the removal of `assert` statements will not affect these checks - to raise more specific exceptions than `AssertionError`, namely `ValueError`. --- polytope/polytope.py | 42 ++++++++++++++++++++++++++++-------------- polytope/solvers.py | 3 ++- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/polytope/polytope.py b/polytope/polytope.py index 3968ee8..820b3c8 100755 --- a/polytope/polytope.py +++ b/polytope/polytope.py @@ -519,25 +519,37 @@ def _rotate(polyreg, i=None, j=None, u=None, v=None, theta=None, R=None): # determine the rotation matrix based on inputs if R is not None: logger.debug("rotate: R=\n{}".format(R)) - assert i is None, i - assert j is None, j - assert theta is None, theta - assert u is None, u - assert v is None, v + if i is not None: + raise ValueError(i) + if j is not None: + raise ValueError(j) + if theta is not None: + raise ValueError(theta) + if u is not None: + raise ValueError(u) + if v is not None: + raise ValueError(v) elif i is not None and j is not None and theta is not None: logger.info("rotate via indices and angle.") - assert R is None, R - assert u is None, u - assert v is None, v + if R is not None: + raise ValueError(R) + if u is not None: + raise ValueError(u) + if v is not None: + raise ValueError(v) if i == j: raise ValueError("Must provide two unique basis vectors.") R = givens_rotation_matrix(i, j, theta, polyreg.dim) elif u is not None and v is not None: logger.info("rotate via 2 vectors.") - assert R is None, R - assert i is None, i - assert j is None, j - assert theta is None, theta + if R is not None: + raise ValueError(R) + if i is not None: + raise ValueError(i) + if j is not None: + raise ValueError(j) + if theta is not None: + raise ValueError(theta) R = solve_rotation_ap(u, v) else: raise ValueError("R or (i and j and theta) or (u and v) " @@ -724,7 +736,8 @@ def contains(self, points, abs_tol=ABS_TOL): """ if not isinstance(points, np.ndarray): points = np.array(points) - assert points.shape[0] == self.dim, 'points should be column vectors' + if points.shape[0] != self.dim: + raise ValueError('points should be column vectors') contained = np.full(points.shape[1], False, dtype=bool) for poly in self.list_poly: contained = np.logical_or( @@ -1626,7 +1639,8 @@ def extreme(poly1): for ix in xrange(nx): V[iv, ix] = H[iv, ix] / K[iv] + xmid[ix] a = V.size / nx - assert a.is_integer(), a + if not a.is_integer(): + raise AssertionError(a) a = int(a) poly1.vertices = V.reshape((a, nx)) return poly1.vertices diff --git a/polytope/solvers.py b/polytope/solvers.py index 33894ef..aa581b7 100644 --- a/polytope/solvers.py +++ b/polytope/solvers.py @@ -125,7 +125,8 @@ def _solve_lp_using_cvxopt(c, G, h, A=None, b=None, solver='glpk'): # thus match what `scipy.optimize.linprog` returns. x = sol['x'] if x is not None: - assert x.typecode == 'd', x.typecode + if x.typecode != 'd': + raise AssertionError(x.typecode) result['x'] = np.fromiter(x, dtype=np.double) else: result['x'] = None