From ef5cc2e56e0c2df40949ae7e08c14df64147ba24 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Wed, 30 Oct 2019 16:40:11 +0000 Subject: [PATCH] Fix attempts to get the .shape attribute of a list or tuple. --- chaco/horizon_plot.py | 18 ++++++++---------- chaco/linear_mapper.py | 6 ++---- chaco/tests/test_linearmapper.py | 26 +++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/chaco/horizon_plot.py b/chaco/horizon_plot.py index a412a7ddd..015343f21 100644 --- a/chaco/horizon_plot.py +++ b/chaco/horizon_plot.py @@ -1,6 +1,6 @@ from __future__ import with_statement -from numpy import array, transpose, ndarray, empty +from numpy import array, float64, full_like, ndarray, transpose from traits.api import Instance, DelegatesTo, Bool, Int from enable.api import transparent_color_trait @@ -16,9 +16,7 @@ def map_screen(self, data_array): if self._null_data_range: if isinstance(data_array, (tuple, list, ndarray)): - x = empty(data_array.shape) - x.fill(self.low_pos) - return x + return full_like(data_array, self.low_pos, dtype=float64) else: return array([self.low_pos]) else: @@ -82,7 +80,7 @@ def _render(self, gc, points): # Get color bands bands = array(self.color_mapper._get_color_bands()) - with gc: + with gc: gc.clip_to_rect(self.x, self.y, self.width, self.height) # draw positive bands inc = -1 * array([0, y_plus_height]) @@ -93,10 +91,10 @@ def _render(self, gc, points): # draw negative bands if self.negative_bands: - if self.mirror: + if self.mirror: points[:,1] = oy - points[:,1] zeroy = oy - else: + else: points[:,1] += y_plus_height inc *= -1 zeroy = int(yhigh) + 2 @@ -114,14 +112,14 @@ def _render_fill(self, gc, face_col, points, ox, oy): gc.set_fill_color(tuple(face_col)) gc.begin_path() startx, starty = points[0] - gc.move_to(startx, oy) + gc.move_to(startx, oy) gc.line_to(startx, starty) gc.lines(points) endx, endy = points[-1] - gc.line_to(endx, oy) - gc.line_to(startx, oy) + gc.line_to(endx, oy) + gc.line_to(startx, oy) gc.close_path() gc.fill_path() diff --git a/chaco/linear_mapper.py b/chaco/linear_mapper.py index c2808c939..0a6313c7d 100644 --- a/chaco/linear_mapper.py +++ b/chaco/linear_mapper.py @@ -4,7 +4,7 @@ """ # Major library imports -from numpy import array, empty, ndarray +from numpy import array, float64, full_like, ndarray # Enthought library imports from traits.api import Bool, Float @@ -45,9 +45,7 @@ def map_screen(self, data_array): self._compute_scale() if self._null_data_range: if isinstance(data_array, (tuple, list, ndarray)): - x = empty(data_array.shape) - x.fill(self.low_pos) - return x + return full_like(data_array, self.low_pos, dtype=float64) else: return array([self.low_pos]) else: diff --git a/chaco/tests/test_linearmapper.py b/chaco/tests/test_linearmapper.py index ceada8caf..40554b6c3 100644 --- a/chaco/tests/test_linearmapper.py +++ b/chaco/tests/test_linearmapper.py @@ -1,6 +1,6 @@ import unittest -from numpy import array +from numpy import array, ndarray from numpy.testing import assert_array_almost_equal, assert_equal @@ -289,3 +289,27 @@ def test_update_high_pos_dont_stretch_data_with_zero(self): mapper.high_pos = 100.0 result = mapper.map_screen(ary) assert_array_almost_equal(result, array([50, 60, 70, 80, 90, 100])) + + def test_map_screen_with_null_data_range(self): + r = DataRange1D() + mapper = LinearMapper(range=r) + low_pos = mapper.low_pos + + data = array([5.6]) + result = mapper.map_screen(data) + self.assertIsInstance(result, ndarray) + self.assertEqual(result.shape, (1,)) + assert_array_almost_equal(result, array([low_pos])) + + # Test support for lists and tuples + data = [5.6] + result = mapper.map_screen(data) + self.assertIsInstance(result, ndarray) + self.assertEqual(result.shape, (1,)) + assert_array_almost_equal(result, array([low_pos])) + + data = (5.6,) + result = mapper.map_screen(data) + self.assertIsInstance(result, ndarray) + self.assertEqual(result.shape, (1,)) + assert_array_almost_equal(result, array([low_pos]))