Skip to content

Commit

Permalink
Raise warning when size_index set to non-numeric dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Dec 11, 2016
1 parent 46b2733 commit bf6c071
Show file tree
Hide file tree
Showing 3 changed files with 72 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))
if sizes.dtype.kind in ('i', 'f'):
sizes = compute_sizes(sizes, self.size_fn,
self.scaling_factor,
self.scaling_method, ms)
data[map_key] = np.sqrt(sizes)
mapping['size'] = map_key
else:
eltype = type(element).__name__
self.warning('%s dimension is not numeric, cannot '
'use to scale %s size.' % (sdim, eltype))

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)
if sizes.dtype.kind in ('i', 'f'):
style['s'] = compute_sizes(sizes, self.size_fn, self.scaling_factor,
self.scaling_method, ms)
ms = style.pop('s') if 's' in style else plt.rcParams['lines.markersize']
else:
eltype = type(element).__name__
self.warning('%s dimension is not numeric, cannot '
'use to scale %s size.' % (sdim, eltype))

style['edgecolors'] = style.pop('edgecolors', 'none')


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 = (range(10), range(10), 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 = (range(10), range(10), 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 bf6c071

Please sign in to comment.