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

Overlay collate #952

Merged
merged 3 commits into from
Oct 28, 2016
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
27 changes: 14 additions & 13 deletions holoviews/core/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,6 @@ def relabel_item_paths(cls, items):
return list(zip(paths, path_items))


@classmethod
def _from_values(cls, val):
return reduce(lambda x,y: x+y, val).display('auto')

@classmethod
def from_values(cls, val):
"""
Expand All @@ -379,15 +375,20 @@ def from_values(cls, val):
collection = isinstance(val, (list, tuple))
if type(val) is cls:
return val
elif collection and len(val)>1:
return cls._from_values(val)
elif collection:
val = val[0]
group = group_sanitizer(val.group)
group = ''.join([group[0].upper(), group[1:]])
label = label_sanitizer(val.label if val.label else 'I')
label = ''.join([label[0].upper(), label[1:]])
return cls(items=[((group, label), val)])
elif not collection:
val = [val]
paths, items = [], []
count = 2
for v in val:
group = group_sanitizer(v.group)
group = ''.join([group[0].upper(), group[1:]])
label = label_sanitizer(v.label if v.label else 'I')
label = ''.join([label[0].upper(), label[1:]])
new_path, count = cls.new_path((group, label), v, paths, count)
new_path = tuple(''.join((p[0].upper(), p[1:])) for p in new_path)
paths.append(new_path)
items.append((new_path, v))
return cls(items=items)


def __init__(self, items=None, identifier=None, parent=None, **kwargs):
Expand Down
8 changes: 8 additions & 0 deletions holoviews/core/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ def __mul__(self, other):
return Overlay(items=self.relabel_item_paths(items)).display('all')


def collate(self):
"""
Collates any objects in the Overlay resolving any issues
the recommended nesting structure.
"""
return reduce(lambda x,y: x*y, self.values())


def collapse(self, function):
"""
Collapses all the Elements in the Overlay using the
Expand Down
15 changes: 14 additions & 1 deletion holoviews/plotting/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import param

from ..core import (HoloMap, DynamicMap, CompositeOverlay, Layout,
GridSpace, NdLayout, Store)
GridSpace, NdLayout, Store, Overlay)
from ..core.util import (match_spec, is_number, wrap_tuple, basestring,
get_overlay_spec, unique_iterator, safe_unicode)

Expand All @@ -14,6 +14,9 @@ def displayable(obj):
Predicate that returns whether the object is displayable or not
(i.e whether the object obeys the nesting hierarchy
"""
if isinstance(obj, Overlay) and any(isinstance(o, (HoloMap, GridSpace))
for o in obj):
return False
if isinstance(obj, HoloMap):
return not (obj.type in [Layout, GridSpace, NdLayout])
if isinstance(obj, (GridSpace, Layout, NdLayout)):
Expand All @@ -28,6 +31,16 @@ class Warning(param.Parameterized): pass
display_warning = Warning(name='Warning')

def collate(obj):
if isinstance(obj, Overlay):
nested_type = [type(o).__name__ for o in obj
if isinstance(o, (HoloMap, GridSpace))][0]
display_warning.warning("Nesting %ss within an Overlay makes it difficult "
"to access your data or control how it appears; "
"we recommend calling .collate() on the Overlay "
"in order to follow the recommended nesting "
"structure shown in the Composing Data tutorial"
"(http://git.io/vtIQh)" % nested_type)
return obj.collate()
if isinstance(obj, HoloMap):
display_warning.warning("Nesting %ss within a HoloMap makes it difficult "
"to access your data or control how it appears; "
Expand Down
17 changes: 15 additions & 2 deletions tests/testcollation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import numpy as np

from holoviews.core import Collator, HoloMap, NdOverlay
from holoviews.core import Collator, HoloMap, NdOverlay, Overlay, GridSpace
from holoviews.element import Curve
from holoviews.element.comparison import ComparisonTestCase

class TestCollation(unittest.TestCase):

class TestCollation(ComparisonTestCase):
def setUp(self):
alphas, betas, deltas = 2, 2, 2
Bs = list(range(100))
Expand Down Expand Up @@ -67,3 +69,14 @@ def test_collate_layout_hmap(self):
collated = collated()
self.assertEqual(repr(collated), repr(layout))
self.assertEqual(collated.dimensions(), layout.dimensions())

def test_overlay_hmap_collate(self):
hmap = HoloMap({i: Curve(np.arange(10)*i) for i in range(3)})
overlaid = Overlay([hmap, hmap, hmap]).collate()
self.assertEqual(overlaid, hmap*hmap*hmap)

def test_overlay_gridspace_collate(self):
grid = GridSpace({(i,j): Curve(np.arange(10)*i) for i in range(3)
for j in range(3)})
overlaid = Overlay([grid, grid, grid]).collate()
self.assertEqual(overlaid, grid*grid*grid)