Skip to content

Commit

Permalink
API: replace assert statements with raise statements
Browse files Browse the repository at this point in the history
- 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`.
  • Loading branch information
johnyf committed May 28, 2021
1 parent dc783e9 commit 5fb8a70
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
42 changes: 28 additions & 14 deletions polytope/polytope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) "
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion polytope/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fb8a70

Please sign in to comment.