From 1e417d2ecb84394266c7d4fb24704daabd27a56e Mon Sep 17 00:00:00 2001 From: Chengyu Liu Date: Thu, 9 Mar 2023 11:23:40 +0900 Subject: [PATCH 1/3] replace all true --- kiva/agg/tests/test_affine_matrix.py | 14 ++++----- kiva/agg/tests/test_utils.py | 5 ++-- kiva/basecore2d.py | 6 ++-- kiva/line_state.py | 4 +-- kiva/tests/agg/test_image.py | 6 ++-- kiva/tests/test_affine.py | 24 +++++++-------- kiva/tests/test_basecore2d.py | 44 ++++++++++++++-------------- kiva/tests/test_graphics_context.py | 6 ++-- 8 files changed, 55 insertions(+), 54 deletions(-) diff --git a/kiva/agg/tests/test_affine_matrix.py b/kiva/agg/tests/test_affine_matrix.py index 21ee2fb4a..b531fa8cf 100644 --- a/kiva/agg/tests/test_affine_matrix.py +++ b/kiva/agg/tests/test_affine_matrix.py @@ -10,7 +10,7 @@ from math import pi import unittest -from numpy import array, allclose, ones, alltrue +from numpy import array, allclose, ones from kiva import agg @@ -24,7 +24,7 @@ def test_init_from_array(self): m = agg.AffineMatrix(a) desired = ones(6, "d") result = m.asarray() - assert alltrue(result == desired) + assert (result == desired).all() def test_init_from_array1(self): a = ones(6, "D") @@ -53,19 +53,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 (desired == actual).all() 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 (result == desired).all() def _test_zero_arg_transform(self, method, orig, desired): m = agg.AffineMatrix(orig) method(m) result = m.asarray() - assert alltrue(result == desired) + assert (result == desired).all() def test_flip_x(self): method = agg.AffineMatrix.flip_x @@ -92,14 +92,14 @@ def test_multiply(self): other = agg.AffineMatrix(orig) m.multiply(other) result = m.asarray() - assert alltrue(result == desired) + assert (result == desired).all() 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 (result == desired).all() def test_invert(self): orig = agg.AffineMatrix((1.0, 2.0, 3.0, 1.0, 4.0, 5.0)) diff --git a/kiva/agg/tests/test_utils.py b/kiva/agg/tests/test_utils.py index 201a6d276..05d54a572 100644 --- a/kiva/agg/tests/test_utils.py +++ b/kiva/agg/tests/test_utils.py @@ -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) + np.all(ravel(x) == ravel(y)), "\n%s\n !=\n%s" % (x, y) ) diff --git a/kiva/basecore2d.py b/kiva/basecore2d.py index 117abf11a..006c15a52 100644 --- a/kiva/basecore2d.py +++ b/kiva/basecore2d.py @@ -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, @@ -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): @@ -353,7 +353,7 @@ def set_line_dash(self, pattern, phase=0): to start. phase defaults to 0. """ - if not alltrue(pattern): + if not pattern.all(): self.state.line_state.line_dash = NO_DASH return pattern = asarray(pattern) diff --git a/kiva/line_state.py b/kiva/line_state.py index f865f0fc1..b82acea92 100644 --- a/kiva/line_state.py +++ b/kiva/line_state.py @@ -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): diff --git a/kiva/tests/agg/test_image.py b/kiva/tests/agg/test_image.py index 936cc9eb7..93cf7e57c 100644 --- a/kiva/tests/agg/test_image.py +++ b/kiva/tests/agg/test_image.py @@ -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 @@ -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: @@ -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: diff --git a/kiva/tests/test_affine.py b/kiva/tests/test_affine.py index 0d40e3bb2..c4c0646ca 100644 --- a/kiva/tests/test_affine.py +++ b/kiva/tests/test_affine.py @@ -19,7 +19,7 @@ """ import unittest -from numpy import allclose, alltrue, array, cos, dot, identity, pi, ravel +from numpy import allclose, array, cos, dot, identity, pi, ravel from kiva import affine @@ -33,21 +33,21 @@ 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 (actual == desired).all() 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 (actual == desired).all() def test_from_rotation(self): transform = affine.affine_from_rotation(pi / 4.0) @@ -55,7 +55,7 @@ def test_from_rotation(self): 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): @@ -73,7 +73,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 @@ -84,7 +84,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 @@ -95,7 +95,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 @@ -105,7 +105,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 @@ -117,7 +117,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): @@ -169,7 +169,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) @@ -185,7 +185,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) diff --git a/kiva/tests/test_basecore2d.py b/kiva/tests/test_basecore2d.py index 2792ce2ea..b415f47f8 100644 --- a/kiva/tests/test_basecore2d.py +++ b/kiva/tests/test_basecore2d.py @@ -19,7 +19,7 @@ import unittest -from numpy import alltrue, array, ravel +from numpy import array, ravel from kiva import affine from kiva import basecore2d @@ -111,7 +111,7 @@ def test_get_ctm(self): # default ctm should be identity matrix. desired = affine.affine_identity() actual = gc.get_ctm() - self.assertTrue(alltrue(ravel(actual == desired))) + self.assertTrue(ravel(actual == desired).all()) def test_scale_ctm(self): gc = basecore2d.GraphicsContextBase() @@ -120,7 +120,7 @@ def test_scale_ctm(self): desired = affine.scale(ident, sx, sy) gc.scale_ctm(sx, sy) actual = gc.get_ctm() - self.assertTrue(alltrue(ravel(actual == desired))) + self.assertTrue(ravel(actual == desired).all()) def test_rotate_ctm(self): gc = basecore2d.GraphicsContextBase() @@ -129,7 +129,7 @@ def test_rotate_ctm(self): desired = affine.rotate(ident, angle) gc.rotate_ctm(angle) actual = gc.get_ctm() - self.assertTrue(alltrue(ravel(actual == desired))) + self.assertTrue(ravel(actual == desired).all()) def test_translate_ctm(self): gc = basecore2d.GraphicsContextBase() @@ -138,7 +138,7 @@ def test_translate_ctm(self): desired = affine.translate(ident, x, y) gc.translate_ctm(x, y) actual = gc.get_ctm() - self.assertTrue(alltrue(ravel(actual == desired))) + self.assertTrue(ravel(actual == desired).all()) def test_concat_ctm(self): gc = basecore2d.GraphicsContextBase() @@ -147,7 +147,7 @@ def test_concat_ctm(self): desired = affine.concat(ident, trans) gc.concat_ctm(trans) actual = gc.get_ctm() - self.assertTrue(alltrue(ravel(actual == desired))) + self.assertTrue(ravel(actual == desired).all()) # ------------------------------------------------------------------------- # Setting drawing state variables @@ -236,17 +236,17 @@ def test_state_line_dash(self): self.assertTrue(gc.state.line_state.is_dashed()) self.assertEqual(gc.state.line_state.line_dash[0], 0) self.assertTrue( - alltrue( + ( ravel(gc.state.line_state.line_dash[1] == array([3.0, 4.0])) - ) + ).all() ) gc.restore_state() self.assertTrue(gc.state.line_state.is_dashed()) self.assertEqual(gc.state.line_state.line_dash[0], 2.0) self.assertTrue( - alltrue( + ( ravel(gc.state.line_state.line_dash[1] == array([1.0, 2.0])) - ) + ).all() ) # pattern must be a container with atleast two values @@ -280,29 +280,29 @@ def test_state_alpha(self): def test_state_fill_color(self): gc = basecore2d.GraphicsContextBase() # defaults to [0,0,0,1] - self.assertTrue(alltrue(gc.state.fill_color == array([0, 0, 0, 1]))) + self.assertTrue((gc.state.fill_color == array([0, 0, 0, 1])).all()) gc.set_fill_color((0, 1, 0, 1)) gc.save_state() gc.set_fill_color((1, 1, 1, 1)) - self.assertTrue(alltrue(gc.state.fill_color == array([1, 1, 1, 1]))) + self.assertTrue((gc.state.fill_color == array([1, 1, 1, 1])).all()) gc.restore_state() - self.assertTrue(alltrue(gc.state.fill_color == array([0, 1, 0, 1]))) + self.assertTrue((gc.state.fill_color == array([0, 1, 0, 1])).all()) def test_state_stroke_color(self): gc = basecore2d.GraphicsContextBase() # defaults to [0,0,0,1] self.assertTrue( - alltrue(gc.state.line_state.line_color == array([0, 0, 0, 1])) + (gc.state.line_state.line_color == array([0, 0, 0, 1])).all() ) gc.set_stroke_color((0, 1, 0, 1)) gc.save_state() gc.set_stroke_color((1, 1, 1, 1)) self.assertTrue( - alltrue(gc.state.line_state.line_color == array([1, 1, 1, 1])) + (gc.state.line_state.line_color == array([1, 1, 1, 1])).all() ) gc.restore_state() self.assertTrue( - alltrue(gc.state.line_state.line_color == array([0, 1, 0, 1])) + (gc.state.line_state.line_color == array([0, 1, 0, 1])).all() ) def test_state_character_spacing(self): @@ -349,13 +349,13 @@ def test_state_context_manager(self): self.assertEqual(gc.state.line_state.line_width, 10) gc.set_fill_color((1, 1, 1, 1)) self.assertTrue( - alltrue(gc.state.fill_color == array([1, 1, 1, 1])) + (gc.state.fill_color == array([1, 1, 1, 1])).all() ) # Verify that we're back to the earlier settings. self.assertEqual(gc.state.antialias, 0) self.assertEqual(gc.state.line_state.line_width, 5) - self.assertTrue(alltrue(gc.state.fill_color == array([0, 1, 0, 1]))) + self.assertTrue((gc.state.fill_color == array([0, 1, 0, 1])).all()) def test_state_context_manager_nested(self): gc = basecore2d.GraphicsContextBase() @@ -373,7 +373,7 @@ def test_state_context_manager_nested(self): self.assertEqual(gc.state.line_state.line_width, 10) gc.set_fill_color((1, 1, 1, 1)) self.assertTrue( - alltrue(gc.state.fill_color == array([1, 1, 1, 1])) + (gc.state.fill_color == array([1, 1, 1, 1])).all() ) with gc: @@ -384,20 +384,20 @@ def test_state_context_manager_nested(self): self.assertEqual(gc.state.line_state.line_width, 2) gc.set_fill_color((1, 1, 0, 1)) self.assertTrue( - alltrue(gc.state.fill_color == array([1, 1, 0, 1])) + (gc.state.fill_color == array([1, 1, 0, 1])).all() ) # Verify that we're back to the earlier settings. self.assertEqual(gc.state.antialias, 1) self.assertEqual(gc.state.line_state.line_width, 10) self.assertTrue( - alltrue(gc.state.fill_color == array([1, 1, 1, 1])) + (gc.state.fill_color == array([1, 1, 1, 1])).all() ) # Verify that we're back to the earlier settings. self.assertEqual(gc.state.antialias, 0) self.assertEqual(gc.state.line_state.line_width, 5) - self.assertTrue(alltrue(gc.state.fill_color == array([0, 1, 0, 1]))) + self.assertTrue((gc.state.fill_color == array([0, 1, 0, 1])).all()) # ------------------------------------------------------------------------- # Begin/End Page diff --git a/kiva/tests/test_graphics_context.py b/kiva/tests/test_graphics_context.py index 0a0e13a08..14e608227 100644 --- a/kiva/tests/test_graphics_context.py +++ b/kiva/tests/test_graphics_context.py @@ -10,7 +10,7 @@ import unittest from numpy import ( - alltrue, array, asarray, concatenate, dtype, newaxis, ones, ravel, zeros + array, asarray, concatenate, dtype, newaxis, ones, ravel, zeros ) from PIL import Image as PILImage @@ -161,7 +161,7 @@ def solid_bgra32(self, size, value=0.0, alpha=1.0): def assert_images_equal(self, 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: @@ -183,7 +183,7 @@ def assert_images_close(self, desired, actual, diff_allowed=2): 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: From 23f40fbd6bc18f0f71cfa0ad3b54d0786bc7fccf Mon Sep 17 00:00:00 2001 From: Chengyu Liu Date: Thu, 9 Mar 2023 11:43:46 +0900 Subject: [PATCH 2/3] rewrite some alls --- kiva/agg/tests/test_utils.py | 2 +- kiva/basecore2d.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kiva/agg/tests/test_utils.py b/kiva/agg/tests/test_utils.py index 05d54a572..349cd7ab8 100644 --- a/kiva/agg/tests/test_utils.py +++ b/kiva/agg/tests/test_utils.py @@ -14,5 +14,5 @@ class Utils(object): def assertRavelEqual(self, x, y): self.assertTrue( - np.all(ravel(x) == ravel(y)), "\n%s\n !=\n%s" % (x, y) + (ravel(x) == ravel(y)).all(), "\n%s\n !=\n%s" % (x, y) ) diff --git a/kiva/basecore2d.py b/kiva/basecore2d.py index 006c15a52..1de3ed4a6 100644 --- a/kiva/basecore2d.py +++ b/kiva/basecore2d.py @@ -353,10 +353,10 @@ def set_line_dash(self, pattern, phase=0): to start. phase defaults to 0. """ - if not pattern.all(): + 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. From 97b4c040b9a7d100f4f18c0c1c36e2f234ff9634 Mon Sep 17 00:00:00 2001 From: Chengyu Liu Date: Thu, 9 Mar 2023 12:23:14 +0900 Subject: [PATCH 3/3] 1 remove some error causing alls --- kiva/agg/tests/test_affine_matrix.py | 13 +++++++------ kiva/tests/test_affine.py | 5 +++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/kiva/agg/tests/test_affine_matrix.py b/kiva/agg/tests/test_affine_matrix.py index b531fa8cf..c77bd3b4f 100644 --- a/kiva/agg/tests/test_affine_matrix.py +++ b/kiva/agg/tests/test_affine_matrix.py @@ -10,6 +10,7 @@ from math import pi import unittest +import numpy as np from numpy import array, allclose, ones from kiva import agg @@ -24,7 +25,7 @@ def test_init_from_array(self): m = agg.AffineMatrix(a) desired = ones(6, "d") result = m.asarray() - assert (result == desired).all() + assert np.all(result == desired) def test_init_from_array1(self): a = ones(6, "D") @@ -53,19 +54,19 @@ def test_imul(self): a *= a actual = a desired = agg.AffineMatrix((4.0, 0, 0, 4.0, 0, 0)) - assert (desired == actual).all() + 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 (result == desired).all() + assert np.all(result == desired) def _test_zero_arg_transform(self, method, orig, desired): m = agg.AffineMatrix(orig) method(m) result = m.asarray() - assert (result == desired).all() + assert np.all(result == desired) def test_flip_x(self): method = agg.AffineMatrix.flip_x @@ -92,14 +93,14 @@ def test_multiply(self): other = agg.AffineMatrix(orig) m.multiply(other) result = m.asarray() - assert (result == desired).all() + 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 (result == desired).all() + assert np.all(result == desired) def test_invert(self): orig = agg.AffineMatrix((1.0, 2.0, 3.0, 1.0, 4.0, 5.0)) diff --git a/kiva/tests/test_affine.py b/kiva/tests/test_affine.py index c4c0646ca..2882f1dd8 100644 --- a/kiva/tests/test_affine.py +++ b/kiva/tests/test_affine.py @@ -19,6 +19,7 @@ """ import unittest +import numpy as np from numpy import allclose, array, cos, dot, identity, pi, ravel from kiva import affine @@ -40,14 +41,14 @@ def test_from_scale(self): pt1 = array([1.0, 1.0, 1.0]) actual = dot(pt1, transform) desired = pt1 * array((5.0, 6.0, 1.0)) - assert (actual == desired).all() + 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 (actual == desired).all() + assert np.all(actual == desired) def test_from_rotation(self): transform = affine.affine_from_rotation(pi / 4.0)