Skip to content

Commit

Permalink
Implement collapse operation for NdMapping of Overlays (#5825)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Jul 24, 2023
1 parent ee642a1 commit acaa412
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
16 changes: 12 additions & 4 deletions holoviews/core/ndmapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ def collapse(self, dimensions=None, function=None, spreadfn=None, **kwargs):
elements
"""
from .data import concat
from .overlay import CompositeOverlay

if not dimensions:
dimensions = self.kdims
if not isinstance(dimensions, list): dimensions = [dimensions]
Expand All @@ -860,11 +862,17 @@ def collapse(self, dimensions=None, function=None, spreadfn=None, **kwargs):
if function:
agg = group_data.aggregate(group.last.kdims, function, spreadfn, **kwargs)
group_data = group.type(agg)
elif issubclass(group.type, CompositeOverlay) and hasattr(self, '_split_overlays'):
keys, maps = self._split_overlays()
group_data = group.type(OrderedDict([
(key, ndmap.collapse(function=function, spreadfn=spreadfn, **kwargs))
for key, ndmap in zip(keys, maps)
]))
else:
group_data = [el.data for el in group]
args = (group_data, function, group.last.kdims)
data = group.type.collapse_data(*args, **kwargs)
group_data = group.last.clone(data)
raise ValueError(
"Could not determine correct collapse operation "
"for items of type: {group.type!r}."
)
collapsed[key] = group_data
return collapsed if self.ndims-len(dimensions) else collapsed.last

Expand Down
41 changes: 40 additions & 1 deletion holoviews/tests/core/test_ndmapping.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from collections import OrderedDict

import numpy as np

from holoviews.core import Dimension
from holoviews.core.ndmapping import (
MultiDimensionalMapping, NdMapping, UniformNdMapping
)
from holoviews.core.overlay import Overlay
from holoviews.element import Curve
from holoviews.element.comparison import ComparisonTestCase
from holoviews import HoloMap, Dataset
import numpy as np

class DimensionTest(ComparisonTestCase):

Expand Down Expand Up @@ -269,3 +272,39 @@ def test_holomap_hist_two_dims(self):
hists = hmap.hist(dimension=['x', 'y'])
self.assertEqual(hists['right'].last.kdims, ['y'])
self.assertEqual(hists['top'].last.kdims, ['x'])

def test_holomap_collapse_overlay_no_function(self):
hmap = HoloMap({
(1,0): Curve(np.arange(8)) * Curve(-np.arange(8)),
(2,0): Curve(np.arange(8)**2) * Curve(-np.arange(8)**3)
}, kdims=["A","B"])
self.assertEqual(hmap.collapse(), Overlay([
(('Curve', 'I'), Dataset({
'A': np.concatenate([np.ones(8), np.ones(8)*2]),
'B': np.zeros(16),
'x': np.tile(np.arange(8), 2),
'y': np.concatenate([np.arange(8), np.arange(8)**2])
}, kdims=['A', 'B', 'x'], vdims=['y'])),
(('Curve', 'II'), Dataset({
'A': np.concatenate([np.ones(8), np.ones(8)*2]),
'B': np.zeros(16),
'x': np.tile(np.arange(8), 2),
'y': np.concatenate([-np.arange(8), -np.arange(8)**3])
}, kdims=['A', 'B', 'x'], vdims=['y']))
]))

def test_holomap_collapse_overlay_max(self):
hmap = HoloMap({
(1,0): Curve(np.arange(8)) * Curve(-np.arange(8)),
(2,0): Curve(np.arange(8)**2) * Curve(-np.arange(8)**3)
}, kdims=["A","B"])
self.assertEqual(hmap.collapse(function=np.max), Overlay([
(('Curve', 'I'), Curve({
'x': np.arange(8),
'y': np.arange(8)**2
}, kdims=['x'], vdims=['y'])),
(('Curve', 'II'), Curve({
'x': np.arange(8),
'y': -np.arange(8)
}, kdims=['x'], vdims=['y']))
]))

0 comments on commit acaa412

Please sign in to comment.