Skip to content

Commit

Permalink
Merge pull request #1011 from ioam/point_size_warning
Browse files Browse the repository at this point in the history
Raise warning when size_index set to non-numeric dimension
  • Loading branch information
jlstevens authored Dec 11, 2016
2 parents 46b2733 + 0970453 commit 4d0d2a3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
15 changes: 11 additions & 4 deletions holoviews/plotting/bokeh/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,22 @@ def get_data(self, element, ranges=None, empty=False):
sdim = element.get_dimension(self.size_index)
if sdim:
map_key = 'size_' + sdim.name
mapping['size'] = map_key
if empty:
data[map_key] = []
mapping['size'] = map_key
else:
ms = style.get('size', np.sqrt(6))**2
sizes = element.dimension_values(self.size_index)
data[map_key] = np.sqrt(compute_sizes(sizes, self.size_fn,
self.scaling_factor,
self.scaling_method, ms))
sizes = compute_sizes(sizes, self.size_fn,
self.scaling_factor,
self.scaling_method, ms)
if sizes is None:
eltype = type(element).__name__
self.warning('%s dimension is not numeric, cannot '
'use to scale %s size.' % (sdim, eltype))
else:
data[map_key] = np.sqrt(sizes)
mapping['size'] = map_key

data[dims[xidx]] = [] if empty else element.dimension_values(xidx)
data[dims[yidx]] = [] if empty else element.dimension_values(yidx)
Expand Down
15 changes: 11 additions & 4 deletions holoviews/plotting/mpl/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,18 @@ def _compute_styles(self, element, ranges, style):
style['c'] = color
style['edgecolors'] = style.pop('edgecolors', style.pop('edgecolor', 'none'))

if element.get_dimension(self.size_index):
sdim = element.get_dimension(self.size_index)
if sdim:
sizes = element.dimension_values(self.size_index)
ms = style.pop('s') if 's' in style else plt.rcParams['lines.markersize']
style['s'] = compute_sizes(sizes, self.size_fn, self.scaling_factor,
self.scaling_method, ms)
ms = style['s'] if 's' in style else plt.rcParams['lines.markersize']
sizes = compute_sizes(sizes, self.size_fn, self.scaling_factor,
self.scaling_method, ms)
if sizes is None:
eltype = type(element).__name__
self.warning('%s dimension is not numeric, cannot '
'use to scale %s size.' % (sdim, eltype))
else:
style['s'] = sizes
style['edgecolors'] = style.pop('edgecolors', 'none')


Expand Down
2 changes: 2 additions & 0 deletions holoviews/plotting/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def compute_sizes(sizes, size_fn, scaling_factor, scaling_method, base_size):
base size and size_fn, which will be applied before
scaling.
"""
if sizes.dtype.kind not in ('i', 'f'):
return None
if scaling_method == 'area':
pass
elif scaling_method == 'width':
Expand Down
51 changes: 50 additions & 1 deletion tests/testplotinstantiation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""
Tests of plot instantiation (not display tests, just instantiation)
"""
from __future__ import unicode_literals

import logging
from collections import deque
from unittest import SkipTest
from io import BytesIO
from io import BytesIO, StringIO

import param
import numpy as np
from holoviews import (Dimension, Overlay, DynamicMap, Store,
NdOverlay, GridSpace)
Expand Down Expand Up @@ -41,6 +44,29 @@
plotly_renderer = None


class ParamLogStream(object):
"""
Context manager that replaces the param logger and captures
log messages in a StringIO stream.
"""

def __enter__(self):
self.stream = StringIO()
self._handler = logging.StreamHandler(self.stream)
self._logger = logging.getLogger('testlogger')
for handler in self._logger.handlers:
self._logger.removeHandler(handler)
self._logger.addHandler(self._handler)
self._param_logger = param.parameterized.logger
param.parameterized.logger = self._logger
return self

def __exit__(self, *args):
param.parameterized.logger = self._param_logger
self._handler.close()
self.stream.seek(0)


class TestMPLPlotInstantiation(ComparisonTestCase):

def setUp(self):
Expand Down Expand Up @@ -103,6 +129,17 @@ def history_callback(x, history=deque(maxlen=10)):
self.assertEqual(x, np.arange(10))
self.assertEqual(y, np.arange(10, 20))

def test_points_non_numeric_size_warning(self):
data = (np.arange(10), np.arange(10), list(map(chr, range(94,104))))
points = Points(data, vdims=['z'])(plot=dict(size_index=2))
with ParamLogStream() as log:
plot = mpl_renderer.get_plot(points)
log_msg = log.stream.read()
warning = ('%s: z dimension is not numeric, '
'cannot use to scale Points size.\n' % plot.name)
self.assertEqual(log_msg, warning)



class TestBokehPlotInstantiation(ComparisonTestCase):

Expand Down Expand Up @@ -245,6 +282,18 @@ def test_image_boolean_array(self):
self.assertEqual(source.data['image'][0],
np.array([[0, 1], [1, 0]]))

def test_points_non_numeric_size_warning(self):
data = (np.arange(10), np.arange(10), list(map(chr, range(94,104))))
points = Points(data, vdims=['z'])(plot=dict(size_index=2))
with ParamLogStream() as log:
plot = bokeh_renderer.get_plot(points)
log_msg = log.stream.read()
warning = ('%s: z dimension is not numeric, '
'cannot use to scale Points size.\n' % plot.name)
self.assertEqual(log_msg, warning)



class TestPlotlyPlotInstantiation(ComparisonTestCase):

def setUp(self):
Expand Down

0 comments on commit 4d0d2a3

Please sign in to comment.