Skip to content

Commit

Permalink
Implemented DynamicMap.map method
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Apr 10, 2017
1 parent 2e6e31b commit 762852f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 16 additions & 0 deletions holoviews/core/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,22 @@ def _cache(self, key, val):
self.data[key] = val


def map(self, map_fn, specs=None, clone=True):
"""
Recursively replaces elements using a map function when the
specification applies. Extends regular map with functionality
to dynamically apply functions.
"""
deep_mapped = super(DynamicMap, self).map(map_fn, specs, clone)
if isinstance(deep_mapped, type(self)):
from ..util import Dynamic
def apply_map(obj):
return obj.map(map_fn, specs, clone)
dmap = Dynamic(deep_mapped, shared_data=True, operation=apply_map)
return dmap
return deep_mapped


def relabel(self, label=None, group=None, depth=1):
"""
Assign a new label and/or group to an existing LabelledData
Expand Down
17 changes: 16 additions & 1 deletion tests/testdynamic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import deque

import numpy as np
from holoviews import Dimension, NdLayout, GridSpace
from holoviews import Dimension, NdLayout, GridSpace, Layout
from holoviews.core.spaces import DynamicMap, HoloMap, Callable
from holoviews.element import Image, Scatter, Curve, Text
from holoviews.streams import PositionXY, PositionX, PositionY
Expand Down Expand Up @@ -85,6 +85,21 @@ def test_deep_select_slice_kdim_no_match(self):
dmap = DynamicMap(fn, kdims=[Dimension('Test', range=(10, 20))])
self.assertEqual(dmap.select(DynamicMap, x=(5, 10))[10], fn(10))

def test_deep_map_apply_element_function(self):
fn = lambda i: Curve(np.arange(i))
dmap = DynamicMap(fn, kdims=[Dimension('Test', range=(10, 20))])
mapped = dmap.map(lambda x: x.clone(x.data*2), Curve)
curve = fn(10)
self.assertEqual(mapped[10], curve.clone(curve.data*2))

def test_deep_map_apply_dmap_function(self):
fn = lambda i: Curve(np.arange(i))
dmap1 = DynamicMap(fn, kdims=[Dimension('Test', range=(10, 20))])
dmap2 = DynamicMap(fn, kdims=[Dimension('Test', range=(10, 20))])
mapped = (dmap1 + dmap2).map(lambda x: x[10], DynamicMap)
self.assertEqual(mapped, Layout([('DynamicMap.I', fn(10)),
('DynamicMap.II', fn(10))]))


class DynamicTestCallableBounded(ComparisonTestCase):

Expand Down

0 comments on commit 762852f

Please sign in to comment.