Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ArrayOrNone #350

Merged
merged 5 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions chaco/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions chaco/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions chaco/data_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions chaco/label_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions chaco/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion chaco/log_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions chaco/multi_array_data_source.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion chaco/polygon_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions chaco/scatterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions chaco/tests/test_array_or_none.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful to expand upon this to include the numpy version where this is a warning and the version where it will become an exception.

against None are not issued.

These warnings began with numpy 1.9.
"""

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, [])

3 changes: 2 additions & 1 deletion chaco/text_plot_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion chaco/tools/lasso_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
8 changes: 4 additions & 4 deletions chaco/tools/range_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -132,8 +132,8 @@ class RangeSelection(AbstractController):
_axis_index = Trait(None, None, Int)

# The data space start and end coordinates of the selected region,
# expressed as a list.
_selection = Trait(None, None, Tuple, List, Array)
# expressed as an array.
_selection = ArrayOrNone()

# The selection in mask form.
_selection_mask = Array
Expand Down
18 changes: 18 additions & 0 deletions chaco/tools/tests/range_selection_test_case.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import warnings

import numpy as np

Expand Down Expand Up @@ -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
Expand Down