From bf28d5e37f30f4979aa9ca6e306adc417a67b999 Mon Sep 17 00:00:00 2001 From: Robert Kern Date: Tue, 11 Apr 2017 12:58:32 -0700 Subject: [PATCH 1/5] BUG: Avoid internal .api import because of circularity in some instances. --- chaco/text_plot_1d.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chaco/text_plot_1d.py b/chaco/text_plot_1d.py index bbab675ad..045f9c493 100644 --- a/chaco/text_plot_1d.py +++ b/chaco/text_plot_1d.py @@ -11,12 +11,13 @@ from numpy import array, empty # Enthought library imports -from chaco.api import ArrayDataSource, Label from enable.api import black_color_trait from kiva.trait_defs.kiva_font_trait import KivaFont from traits.api import Bool, Enum, Float, Int, Instance, List, on_trait_change # local imports +from .array_data_source import ArrayDataSource +from .label import Label from .base_1d_plot import Base1DPlot From 89c3967c192f3ef64d6258b5b4fec58d4d2d62f2 Mon Sep 17 00:00:00 2001 From: Robert Kern Date: Tue, 11 Apr 2017 12:58:59 -0700 Subject: [PATCH 2/5] ENH: Avoid FutureWarnings because of comparisons between arrays and None. --- chaco/axis.py | 8 +-- chaco/base.py | 12 ++-- chaco/data_label.py | 4 +- chaco/label_axis.py | 4 +- chaco/legend.py | 8 +-- chaco/scatterplot.py | 8 +-- chaco/tests/test_array_or_none.py | 60 +++++++++++++++++++ chaco/tools/range_selection.py | 6 +- .../tools/tests/range_selection_test_case.py | 18 ++++++ 9 files changed, 103 insertions(+), 25 deletions(-) create mode 100644 chaco/tests/test_array_or_none.py diff --git a/chaco/axis.py b/chaco/axis.py index 77692fda4..17763ad08 100644 --- a/chaco/axis.py +++ b/chaco/axis.py @@ -11,7 +11,7 @@ from enable.api import ColorTrait, LineStyle from kiva.trait_defs.kiva_font_trait import KivaFont from traits.api import Any, Float, Int, Str, Trait, Unicode, \ - Bool, Event, List, Array, Instance, Enum, Callable + Bool, Event, List, Array, Instance, Enum, Callable, ArrayOrNone # Local relative imports from ticks import AbstractTickGenerator, DefaultTickGenerator, MinorTickGenerator @@ -148,9 +148,9 @@ class PlotAxis(AbstractOverlay): # Cached position calculations _tick_list = List # These are caches of their respective positions - _tick_positions = Any #List - _tick_label_list = Any - _tick_label_positions = Any + _tick_positions = ArrayOrNone() + _tick_label_list = ArrayOrNone() + _tick_label_positions = ArrayOrNone() _tick_label_bounding_boxes = List _major_axis_size = Float _minor_axis_size = Float diff --git a/chaco/base.py b/chaco/base.py index b05810235..3a4603360 100644 --- a/chaco/base.py +++ b/chaco/base.py @@ -10,23 +10,23 @@ nonzero, pi, searchsorted, seterr, sin, int8) # Enthought library imports -from traits.api import CArray, Enum, Trait +from traits.api import Enum, ArrayOrNone delta = {'ascending': 1, 'descending': -1, 'flat': 0} # Dimensions # A single array of numbers. -NumericalSequenceTrait = Trait(None, None, CArray(value=empty(0))) +NumericalSequenceTrait = ArrayOrNone() # A sequence of pairs of numbers, i.e., an Nx2 array. -PointTrait = Trait(None, None, CArray(value=empty(0))) +PointTrait = ArrayOrNone(shape=(None, 2)) -# An NxM array of numbers. -ImageTrait = Trait(None, None, CArray(value=empty(0))) +# An NxM array of numbers or NxMxRGB(A) array of colors. +ImageTrait = ArrayOrNone() # An 3D array of numbers of shape (Nx, Ny, Nz) -CubeTrait = Trait(None, None, CArray(value=empty(0))) +CubeTrait = ArrayOrNone(shape=(None, None, None)) # This enumeration lists the fundamental mathematical coordinate types that diff --git a/chaco/data_label.py b/chaco/data_label.py index 9a223ae71..5270fdf1c 100644 --- a/chaco/data_label.py +++ b/chaco/data_label.py @@ -6,7 +6,7 @@ from numpy.linalg import norm # Enthought library imports -from traits.api import Any, Array, Bool, Enum, Float, Int, List, \ +from traits.api import Any, ArrayOrNone, Bool, Enum, Float, Int, List, \ Str, Tuple, Trait, on_trait_change, Property from enable.api import ColorTrait, MarkerTrait @@ -163,7 +163,7 @@ class DataLabel(ToolTip): custom_symbol = Any # The point in data space where this label should anchor itself. - data_point = Trait(None, None, Tuple, List, Array) + data_point = ArrayOrNone() # The location of the data label relative to the data point. label_position = LabelPositionTrait diff --git a/chaco/label_axis.py b/chaco/label_axis.py index 9a710d185..b9b02921e 100644 --- a/chaco/label_axis.py +++ b/chaco/label_axis.py @@ -5,7 +5,7 @@ from numpy import array, float64, inf, searchsorted, take, unique # Enthought library imports -from traits.api import Any, Str, List, Float +from traits.api import ArrayOrNone, Str, List, Float # Local, relative imports from axis import PlotAxis @@ -23,7 +23,7 @@ class LabelAxis(PlotAxis): label_rotation = Float(0) # List of indices of ticks - positions = Any # List(Float), Array + positions = ArrayOrNone() def _compute_tick_positions(self, gc, component=None): """ Calculates the positions for the tick marks. diff --git a/chaco/legend.py b/chaco/legend.py index a012ff342..f6a7d53eb 100644 --- a/chaco/legend.py +++ b/chaco/legend.py @@ -9,8 +9,8 @@ from enable.api import black_color_trait, white_color_trait from enable.font_metrics_provider import font_metrics_provider from kiva.trait_defs.kiva_font_trait import KivaFont -from traits.api import Any, Dict, Enum, Bool, HasTraits, Int, \ - Instance, List, CList, Float, Str +from traits.api import ArrayOrNone, Bool, CList, Dict, Enum, Float, \ + HasTraits, Instance, Int, List, Str # Local relative imports from abstract_overlay import AbstractOverlay @@ -161,7 +161,7 @@ class Legend(AbstractOverlay): _cached_labels = List # A cached array of label sizes. - _cached_label_sizes = Any + _cached_label_sizes = ArrayOrNone() # A cached list of label names. _cached_label_names = CList @@ -173,7 +173,7 @@ class Legend(AbstractOverlay): _cached_visible_plots = CList # A cached array of label positions relative to the legend's origin - _cached_label_positions = Any + _cached_label_positions = ArrayOrNone() def is_in(self, x, y): """ overloads from parent class because legend alignment diff --git a/chaco/scatterplot.py b/chaco/scatterplot.py index c8c08282a..5394804b1 100644 --- a/chaco/scatterplot.py +++ b/chaco/scatterplot.py @@ -11,10 +11,10 @@ # Enthought library imports from enable.api import black_color_trait, ColorTrait, AbstractMarker, \ - CustomMarker, MarkerNameDict, MarkerTrait + CustomMarker, MarkerNameDict, MarkerTrait from kiva.constants import STROKE -from traits.api import Any, Array, Bool, Float, Trait, Callable, Property, \ - Tuple, Either, cached_property +from traits.api import Any, Array, ArrayOrNone, Bool, Float, Callable, \ + Property, Tuple, Either, cached_property from traitsui.api import View, VGroup, Item # Local relative imports @@ -232,7 +232,7 @@ class ScatterPlot(BaseXYPlot): # Private traits #------------------------------------------------------------------------ - _cached_selected_pts = Trait(None, None, Array) + _cached_selected_pts = ArrayOrNone _cached_selected_screen_pts = Array _cached_point_mask = Array _cached_selection_point_mask = Array diff --git a/chaco/tests/test_array_or_none.py b/chaco/tests/test_array_or_none.py new file mode 100644 index 000000000..17ff80123 --- /dev/null +++ b/chaco/tests/test_array_or_none.py @@ -0,0 +1,60 @@ +import unittest +import warnings + +import numpy as np + +from chaco.array_data_source import ArrayDataSource +from chaco.axis import PlotAxis +from chaco.data_label import DataLabel +from chaco.data_range_1d import DataRange1D +from chaco.label_axis import LabelAxis +from chaco.legend import Legend +from chaco.linear_mapper import LinearMapper +from chaco.scatterplot import ScatterPlot + + +class TestArrayOrNone(unittest.TestCase): + """ Test that the FutureWarning from numpy concerning comparison of arrays + against None are not issued. + """ + + def test_array_data_source(self): + with warnings.catch_warnings(record=True) as w: + src = ArrayDataSource(np.arange(5)) + self.assertEqual(w, []) + + def test_scatter_plot(self): + x = np.linspace(0.0, 1.0, 5) + sp = ScatterPlot() + with warnings.catch_warnings(record=True) as w: + sp._cached_selected_pts = None + sp._cached_selected_pts = np.column_stack([x, x]) + self.assertEqual(w, []) + + def test_plot_axis(self): + axis = PlotAxis() + with warnings.catch_warnings(record=True) as w: + axis._tick_positions = np.arange(10) + axis._tick_label_list = np.arange(10) + axis._tick_label_positions = np.arange(10) + self.assertEqual(w, []) + + def test_legend(self): + legend = Legend() + with warnings.catch_warnings(record=True) as w: + legend._cached_label_sizes = np.arange(10) + legend._cached_label_positions = np.arange(10) + self.assertEqual(w, []) + + def test_data_label(self): + label = DataLabel() + with warnings.catch_warnings(record=True) as w: + label.data_point = np.array([10.0, 20.0]) + self.assertEqual(w, []) + + def test_label_axis(self): + axis = LabelAxis() + with warnings.catch_warnings(record=True) as w: + axis.positions = np.arange(10) + self.assertEqual(w, []) + diff --git a/chaco/tools/range_selection.py b/chaco/tools/range_selection.py index 8d6146bc3..e03d7475f 100644 --- a/chaco/tools/range_selection.py +++ b/chaco/tools/range_selection.py @@ -4,8 +4,8 @@ from numpy import array # Enthought library imports -from traits.api import Any, Array, Bool, Enum, Event, Float, Int, Instance, \ - List, Property, Str, Trait, Tuple +from traits.api import Any, Array, ArrayOrNone, Bool, Enum, Event, Float, \ + Instance, Int, List, Property, Str, Trait from enable.api import KeySpec # Chaco imports @@ -133,7 +133,7 @@ class RangeSelection(AbstractController): # The data space start and end coordinates of the selected region, # expressed as a list. - _selection = Trait(None, None, Tuple, List, Array) + _selection = ArrayOrNone() # The selection in mask form. _selection_mask = Array diff --git a/chaco/tools/tests/range_selection_test_case.py b/chaco/tools/tests/range_selection_test_case.py index c94fb54af..b46ee7bf5 100644 --- a/chaco/tools/tests/range_selection_test_case.py +++ b/chaco/tools/tests/range_selection_test_case.py @@ -1,4 +1,5 @@ import unittest +import warnings import numpy as np @@ -51,6 +52,23 @@ def test_selecting_mouse_leave_clipping(self): self.assertTrue(selection[0] <= selection[1]) self.mouse_up(tool, x=x, y=y) + def test_selection_no_warning(self): + plot_data = ArrayPlotData() + arr = np.arange(4) + plot_data.set_data("x", arr) + plot_data.set_data("y", arr) + plot = Plot(plot_data) + renderer = plot.plot(('x', 'y'))[0] + tool = RangeSelection(renderer) + with warnings.catch_warnings(record=True) as w: + tool.selection = np.array([2.0, 3.0]) + self.assertEqual(w, []) + + # Accept tuples and lists and None + tool.selection = (1.5, 3.5) + tool.selection = [1.0, 2.0] + tool.selection = None + if __name__ == '__main__': import nose From 1d426d4e916b4a2615a14faa5d6bbd593500743a Mon Sep 17 00:00:00 2001 From: Robert Kern Date: Tue, 11 Apr 2017 13:56:56 -0700 Subject: [PATCH 3/5] ENH: Ignore known warnings, just to clean up the test suite. --- chaco/log_mapper.py | 4 +++- chaco/multi_array_data_source.py | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/chaco/log_mapper.py b/chaco/log_mapper.py index 803b43224..0ca76d6e6 100644 --- a/chaco/log_mapper.py +++ b/chaco/log_mapper.py @@ -3,6 +3,7 @@ # Major library imports from numpy import array, isnan, log, log10, exp, zeros, sometrue,\ floor, ceil, ndarray +import numpy as np # Enthought library imports from traits.api import Bool, Float @@ -54,7 +55,8 @@ def map_screen(self, data_array): intermediate = data_array*0.0 else: try: - mask = (data_array <= LOG_MINIMUM) | isnan(data_array) + with np.errstate(invalid='ignore'): + mask = (data_array <= LOG_MINIMUM) | isnan(data_array) if sometrue(mask): data_array = array(data_array, copy=True, ndmin=1) data_array[mask] = self.fill_value diff --git a/chaco/multi_array_data_source.py b/chaco/multi_array_data_source.py index 1b826c041..1f7bb952d 100644 --- a/chaco/multi_array_data_source.py +++ b/chaco/multi_array_data_source.py @@ -1,5 +1,7 @@ """ Defines the MultiArrayDataSource class. """ +import warnings + # Major package imports from numpy import nanmax, nanmin, array, shape, ones, bool, newaxis, nan_to_num @@ -179,8 +181,11 @@ def get_bounds(self, value = None, index = None): mini = nanmin(self._data[::, index]) else: # value is None and index is None: - maxi = nanmax(self._data) - mini = nanmin(self._data) + with warnings.catch_warnings(): + warnings.filterwarnings( + 'ignore', "All-NaN (slice|axis) encountered", RuntimeWarning) + maxi = nanmax(self._data) + mini = nanmin(self._data) return (mini, maxi) From e42deec8c374480f4b694132e6b87a68c3271dcd Mon Sep 17 00:00:00 2001 From: Robert Kern Date: Mon, 26 Jun 2017 09:45:02 -0700 Subject: [PATCH 4/5] DOC: Comments from review. --- chaco/tests/test_array_or_none.py | 2 ++ chaco/tools/range_selection.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/chaco/tests/test_array_or_none.py b/chaco/tests/test_array_or_none.py index 17ff80123..78aef8a4f 100644 --- a/chaco/tests/test_array_or_none.py +++ b/chaco/tests/test_array_or_none.py @@ -16,6 +16,8 @@ class TestArrayOrNone(unittest.TestCase): """ Test that the FutureWarning from numpy concerning comparison of arrays against None are not issued. + + These warnings began with numpy 1.9. """ def test_array_data_source(self): diff --git a/chaco/tools/range_selection.py b/chaco/tools/range_selection.py index e03d7475f..f057f8cf5 100644 --- a/chaco/tools/range_selection.py +++ b/chaco/tools/range_selection.py @@ -132,7 +132,7 @@ class RangeSelection(AbstractController): _axis_index = Trait(None, None, Int) # The data space start and end coordinates of the selected region, - # expressed as a list. + # expressed as an array. _selection = ArrayOrNone() # The selection in mask form. From 64c034a61e0a31c272331dadf1c2cba0a7f6e480 Mon Sep 17 00:00:00 2001 From: Robert Kern Date: Mon, 26 Jun 2017 10:11:01 -0700 Subject: [PATCH 5/5] BUG: follow Enable #250 in the location of points_in_polygon --- chaco/polygon_plot.py | 2 +- chaco/tools/lasso_selection.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/chaco/polygon_plot.py b/chaco/polygon_plot.py index 864d964c9..d8db99a3d 100644 --- a/chaco/polygon_plot.py +++ b/chaco/polygon_plot.py @@ -9,7 +9,7 @@ # Enthought library imports. from enable.api import LineStyle, black_color_trait, \ transparent_color_trait -from kiva.agg import points_in_polygon +from kiva.api import points_in_polygon from traits.api import Enum, Float, Tuple, Property, cached_property, \ on_trait_change diff --git a/chaco/tools/lasso_selection.py b/chaco/tools/lasso_selection.py index 721977756..f3c7bc57c 100644 --- a/chaco/tools/lasso_selection.py +++ b/chaco/tools/lasso_selection.py @@ -7,7 +7,7 @@ # Enthought library imports from traits.api import Any, Array, Enum, Event, Bool, Instance, \ Property, Str, Trait, List -from kiva.agg import points_in_polygon +from kiva.api import points_in_polygon # Chaco imports from chaco.api import AbstractController, AbstractDataSource, \