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

Added support for dynamic operations and overlaying #588

Merged
merged 9 commits into from
Apr 11, 2016
48 changes: 40 additions & 8 deletions holoviews/core/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
from functools import reduce
import param

try:
from itertools import izip as zip
except:
pass

from .dimension import ViewableElement
from .element import Element, HoloMap, GridSpace, Collator
from .layout import Layout
from .overlay import NdOverlay, Overlay
from .spaces import DynamicMap
from .traversal import unique_dimkeys

from . import util


class Operation(param.ParameterizedFunction):
Expand Down Expand Up @@ -74,6 +80,10 @@ class ElementOperation(Operation):
ElementOperation may turn overlays in new elements or vice versa.
"""

dynamic = param.Boolean(default=False, doc="""
Whether the operation should be applied dynamically
when a specific frame is requested.""")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this approach.

input_ranges = param.ClassSelector(default={},
class_=(dict, tuple), doc="""
Ranges to be used for input normalization (if applicable) in a
Expand Down Expand Up @@ -115,19 +125,41 @@ def __call__(self, element, **params):
kdims=element.kdims)
# Populate the axis layout
for pos, cell in element.items():
processed[pos] = self(cell, **params)
if self.p.dynamic and isinstance(cell, HoloMap):
def dynamic_operation(key):
return self(element[pos][key], **params)
processed_cell = self._make_dynamic(cell, dynamic_operation)
else:
processed_cell = self(cell, **params)
processed[pos] = processed_cell
elif isinstance(element, HoloMap):
mapped_items = [(k, self._process(el, key=k))
for k, el in element.items()]
refval = mapped_items[0][1]
processed = element.clone(mapped_items,
group=refval.group,
label=refval.label)
if self.p.dynamic:
def dynamic_operation(key):
return self(element[key], **params)
processed = self._make_dynamic(element, dynamic_operation)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion: I might consider using the signature _make_dynamic(hmap, key, params) and moving the nested function into that method (or using a lambda). This works as the function used is always generated by calling self.

This is a very mild preference and if you prefer to leave it as it is, I don't mind either.

else:
mapped_items = [(k, self._process(el, key=k))
for k, el in element.items()]
refval = mapped_items[0][1]
processed = element.clone(mapped_items,
group=refval.group,
label=refval.label)
else:
raise ValueError("Cannot process type %r" % type(element).__name__)
return processed


def _make_dynamic(self, hmap, dynamic_fn):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, how about making this a general utility to turn HoloMaps into DynamicMaps? That seems like it could be useful and would justify keeping the existing signature (overriding my suggestion above).

"""
Accepts a HoloMap and a dynamic callback function creating
an equivalent DynamicMap from the HoloMap.
"""
dim_values = zip(*hmap.data.keys())
params = util.get_param_values(hmap)
kdims = [d(values=list(values)) for d, values in zip(hmap.kdims, dim_values)]
return DynamicMap(dynamic_fn, **dict(params, kdims=kdims))



class MapOperation(param.ParameterizedFunction):
"""
Expand Down