Skip to content

Commit

Permalink
replace all true (#1015)
Browse files Browse the repository at this point in the history
* replace all true

* rewrite some alls

* 1 remove some error causing alls

---------

Co-authored-by: Chengyu Liu <cyliu@aus552cyliu.local>
  • Loading branch information
homosapien-lcy and Chengyu Liu authored Mar 9, 2023
1 parent 642e1db commit e18f955
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 55 deletions.
15 changes: 8 additions & 7 deletions kiva/agg/tests/test_affine_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from math import pi
import unittest

from numpy import array, allclose, ones, alltrue
import numpy as np
from numpy import array, allclose, ones

from kiva import agg

Expand All @@ -24,7 +25,7 @@ def test_init_from_array(self):
m = agg.AffineMatrix(a)
desired = ones(6, "d")
result = m.asarray()
assert alltrue(result == desired)
assert np.all(result == desired)

def test_init_from_array1(self):
a = ones(6, "D")
Expand Down Expand Up @@ -53,19 +54,19 @@ def test_imul(self):
a *= a
actual = a
desired = agg.AffineMatrix((4.0, 0, 0, 4.0, 0, 0))
assert alltrue(desired == actual)
assert np.all(desired == actual)

def test_asarray(self):
m = agg.AffineMatrix()
result = m.asarray()
desired = array((1.0, 0.0, 0.0, 1.0, 0.0, 0.0))
assert alltrue(result == desired)
assert np.all(result == desired)

def _test_zero_arg_transform(self, method, orig, desired):
m = agg.AffineMatrix(orig)
method(m)
result = m.asarray()
assert alltrue(result == desired)
assert np.all(result == desired)

def test_flip_x(self):
method = agg.AffineMatrix.flip_x
Expand All @@ -92,14 +93,14 @@ def test_multiply(self):
other = agg.AffineMatrix(orig)
m.multiply(other)
result = m.asarray()
assert alltrue(result == desired)
assert np.all(result == desired)

def test_determinant(self):
orig = array((1.0, 2.0, 3.0, 1.0, 4.0, 5.0))
desired = -5.0
m = agg.AffineMatrix(orig)
result = m.determinant()
assert alltrue(result == desired)
assert np.all(result == desired)

def test_invert(self):
orig = agg.AffineMatrix((1.0, 2.0, 3.0, 1.0, 4.0, 5.0))
Expand Down
5 changes: 3 additions & 2 deletions kiva/agg/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
from numpy import alltrue, ravel
import numpy as np
from numpy import all, ravel


class Utils(object):
def assertRavelEqual(self, x, y):
self.assertTrue(
alltrue(ravel(x) == ravel(y)), "\n%s\n !=\n%s" % (x, y)
(ravel(x) == ravel(y)).all(), "\n%s\n !=\n%s" % (x, y)
)
8 changes: 4 additions & 4 deletions kiva/basecore2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"""
import numpy as np
from numpy import alltrue, array, asarray, float64, pi
from numpy import array, asarray, float64, pi

from .constants import (
CAP_BUTT, CAP_ROUND, CAP_SQUARE, CLOSE, CONCAT_CTM, EOF_FILL_STROKE,
Expand Down Expand Up @@ -73,7 +73,7 @@ def is_fully_transparent(color):

def fill_equal(fill1, fill2):
""" Compares the two fill colors. """
return alltrue(fill1 == fill2)
return np.all(fill1 == fill2)


class GraphicsContextBase(AbstractGraphicsContext):
Expand Down Expand Up @@ -353,10 +353,10 @@ def set_line_dash(self, pattern, phase=0):
to start. phase defaults to 0.
"""
if not alltrue(pattern):
pattern = asarray(pattern)
if (pattern == 0).any():
self.state.line_state.line_dash = NO_DASH
return
pattern = asarray(pattern)
if len(pattern) < 2:
raise ValueError("dash pattern should have at least two entries.")
# not sure if this check is really needed.
Expand Down
4 changes: 2 additions & 2 deletions kiva/line_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
state (eg. Wx, SVG and PDF backends, but not Agg or QPainter).
"""

from numpy import alltrue, array, asarray, shape, sometrue
from numpy import array, asarray, shape, sometrue

from .constants import NO_DASH


def exactly_equal(arr1, arr2):
return shape(arr1) == shape(arr2) and alltrue(arr1 == arr2)
return shape(arr1) == shape(arr2) and (arr1 == arr2).all()


def is_dashed(dash):
Expand Down
6 changes: 3 additions & 3 deletions kiva/tests/agg/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import unittest

from numpy import (
alltrue, array, concatenate, dtype, frombuffer, newaxis, ones, pi, ravel,
array, concatenate, dtype, frombuffer, newaxis, ones, pi, ravel,
zeros,
)
from PIL import Image
Expand Down Expand Up @@ -76,7 +76,7 @@ def alpha_blend(src1, src2, alpha=1.0, ambient_alpha=1.0):
def assert_equal(desired, actual):
""" Only use for small arrays. """
try:
assert alltrue(ravel(actual) == ravel(desired))
assert (ravel(actual) == ravel(desired)).all()
except AssertionError:
size = sum(array(desired.shape))
if size < 10:
Expand All @@ -97,7 +97,7 @@ def assert_close(desired, actual, diff_allowed=2):
try:
# cast up so math doesn't underflow
diff = abs(ravel(actual.astype(Int32)) - ravel(desired.astype(Int32)))
assert alltrue(diff <= diff_allowed)
assert (diff <= diff_allowed).all()
except AssertionError:
size = sum(array(desired.shape))
if size < 10:
Expand Down
25 changes: 13 additions & 12 deletions kiva/tests/test_affine.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"""
import unittest

from numpy import allclose, alltrue, array, cos, dot, identity, pi, ravel
import numpy as np
from numpy import allclose, array, cos, dot, identity, pi, ravel

from kiva import affine

Expand All @@ -33,29 +34,29 @@ def test_from_values(self):
a, b, c, d, tx, ty = 1, 2, 3, 4, 5, 6
mat = affine.affine_from_values(a, b, c, d, tx, ty)
desired = array([[a, b, 0], [c, d, 0], [tx, ty, 1]])
assert alltrue(ravel(mat) == ravel(desired))
assert (ravel(mat) == ravel(desired)).all()

def test_from_scale(self):
transform = affine.affine_from_scale(5.0, 6.0)
pt1 = array([1.0, 1.0, 1.0])
actual = dot(pt1, transform)
desired = pt1 * array((5.0, 6.0, 1.0))
assert alltrue(actual == desired)
assert np.all(actual == desired)

def test_from_translation(self):
transform = affine.affine_from_translation(5.0, 6.0)
pt1 = array([1.0, 1.0, 1.0])
actual = dot(pt1, transform)
desired = pt1 + array((5.0, 6.0, 0.0))
assert alltrue(actual == desired)
assert np.all(actual == desired)

def test_from_rotation(self):
transform = affine.affine_from_rotation(pi / 4.0)
pt1 = array([1.0, 0.0, 1.0])
actual = dot(pt1, transform)
cos_pi_4 = cos(pi / 4.0)
desired = array((cos_pi_4, cos_pi_4, 1.0))
assert alltrue((actual - desired) < 1e-6)
assert ((actual - desired) < 1e-6).all()


class AffineOperationsTestCase(unittest.TestCase):
Expand All @@ -73,7 +74,7 @@ def test_scale(self):
actual = dot(pt1, transform2)
# this does the first transform and the scaling separately
desired = dot(pt1, transform1) * array((0.5, 1.5, 1.0))
assert alltrue((actual - desired) < 1e-6)
assert ((actual - desired) < 1e-6).all()

def test_translate(self):
a, b, c, d, tx, ty = 1, 2, 3, 4, 5, 6
Expand All @@ -84,7 +85,7 @@ def test_translate(self):
actual = dot(pt1, tot_transform)
# this does the first transform and the translate separately
desired = dot(dot(pt1, translate_transform), transform1)
assert alltrue((actual - desired) < 1e-6)
assert ((actual - desired) < 1e-6).all()

def test_rotate(self):
a, b, c, d, tx, ty = 1.0, 0, 0, 1.0, 0, 0
Expand All @@ -95,7 +96,7 @@ def test_rotate(self):
# this does the first transform and the translate separately
cos_pi_4 = 0.707_106_781_186_547_57
desired = array((cos_pi_4, cos_pi_4, 1.0))
assert alltrue((actual - desired) < 1e-6)
assert ((actual - desired) < 1e-6).all()

def test_invert(self):
""" An matrix times its inverse should produce the identity matrix
Expand All @@ -105,7 +106,7 @@ def test_invert(self):
transform2 = affine.invert(transform1)
desired = affine.affine_identity()
actual = dot(transform2, transform1)
assert alltrue((ravel(actual) - ravel(desired)) < 1e-6)
assert ((ravel(actual) - ravel(desired)) < 1e-6).all()

def test_concat(self):
a, b, c, d, tx, ty = 1, 2, 3, 4, 5, 6
Expand All @@ -117,7 +118,7 @@ def test_concat(self):
actual = dot(pt1, tot_transform)
# this does the first transform and the scaling separately
desired = dot(dot(pt1, transform2), transform1)
assert alltrue((actual - desired) < 1e-6)
assert ((actual - desired) < 1e-6).all()


class AffineInformationTestCase(unittest.TestCase):
Expand Down Expand Up @@ -169,7 +170,7 @@ def test_transform_point(self):
ctm = affine.affine_identity()
ctm = affine.translate(ctm, 5, 5)
new_pt = affine.transform_point(ctm, pt)
assert alltrue(new_pt == array((6, 6)))
assert (new_pt == array((6, 6))).all()

ctm = affine.rotate(ctm, pi)
new_pt = affine.transform_point(ctm, pt)
Expand All @@ -185,7 +186,7 @@ def test_transform_points(self):
ctm = affine.affine_identity()
ctm = affine.translate(ctm, 5, 5)
new_pt = affine.transform_points(ctm, pt)
assert alltrue(new_pt[0] == array((6, 6)))
assert (new_pt[0] == array((6, 6))).all()

ctm = affine.rotate(ctm, pi)
new_pt = affine.transform_points(ctm, pt)
Expand Down
Loading

0 comments on commit e18f955

Please sign in to comment.