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 3, 2017
1 parent 92ac5b5 commit 3c7a25d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
21 changes: 21 additions & 0 deletions holoviews/core/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,27 @@ 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.
"""
if specs and not isinstance(specs, list): specs = [specs]
applies = specs is None or any(self.matches(spec) for spec in specs)

if applies:
deep_mapped = map_fn(self)
else:
deep_mapped = self.clone() if clone else self
for k, v in deep_mapped.data.items():
deep_mapped[k] = v.map(map_fn, specs, clone)

from ..util import Dynamic
def dynamic_map(obj):
return obj.map(map_fn, specs, clone)
return Dynamic(deep_mapped, shared_data=True, operation=dynamic_map)


def relabel(self, label=None, group=None, depth=1):
"""
Assign a new label and/or group to an existing LabelledData
Expand Down
34 changes: 19 additions & 15 deletions holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,22 +1163,26 @@ def arglexsort(arrays):

def get_dynamic_item(map_obj, dimensions, key):
"""
Looks up an item in a DynamicMap given a list of dimensions
and a corresponding key. The dimensions must be a subset
of the map_obj key dimensions.
"""
dmaps = map_obj.traverse(lambda x: x, ['DynamicMap'])
dmap = dmaps[0] if dmaps else map_obj
if key == () and (not dimensions or not dmap.kdims):
map_obj.traverse(lambda x: x[()], ['DynamicMap'])
return key, map_obj.map(lambda x: x.last, ['DynamicMap'])
elif isinstance(key, tuple):
dims = {d.name: k for d, k in zip(dimensions, key)
if d in map_obj.kdims}
key = tuple(dims.get(d.name) for d in map_obj.kdims)
el = map_obj.select(['DynamicMap', 'HoloMap'], **dims)
Traverses the supplied object selecting the requested key on
all HoloMap and DynamicMap objects.
"""
map_specs = ('HoloMap', 'DynamicMap')
overlay_specs = ('Overlay', 'NdOverlay')
if any(map_obj.matches(spec) for spec in map_specs):
if key == ():
el = map_obj[key]
else:
dims = {d.name: k for d, k in zip(dimensions, key)
if d in map_obj.kdims}
key = tuple(dims.get(d.name) for d in map_obj.kdims)
el = map_obj.select(map_specs, **dims)
elif map_obj._deep_indexable and not any(map_obj.matches(spec) for spec in overlay_specs):
el = map_obj.clone(shared_data=False)
for k, v in map_obj.data.items():
_, deep_obj = get_dynamic_item(v, dimensions, key)
el[k] = deep_obj
else:
el = None
el = map_obj
return key, el


Expand Down

0 comments on commit 3c7a25d

Please sign in to comment.