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

Raise warning when size_index set to non-numeric dimension #1011

Merged
merged 3 commits into from
Dec 11, 2016
Merged
Changes from 1 commit
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
Next Next commit
Raise warning when size_index set to non-numeric dimension
philippjfr committed Dec 11, 2016

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit bf6c07138e27c5f3a5ee06eb8c7cd6cf946abea4
15 changes: 11 additions & 4 deletions holoviews/plotting/bokeh/chart.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 11 additions & 4 deletions holoviews/plotting/mpl/chart.py
Original file line number Diff line number Diff line change
@@ -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')


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)
@@ -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):
@@ -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):

@@ -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):