Created
February 8, 2017 15:56
-
-
Save drs251/8218814fa3360b3301abc5df3bdb896f to your computer and use it in GitHub Desktop.
A better function to divide vdims by the maximum value - works for Holoviews Curves, Holomaps, NdLayouts, NdOverlays and DynamicMaps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import param | |
import holoviews.core.operation | |
import holoviews.core.spaces | |
class normalize_vdims(hv.ElementOperation): | |
relabel = param.Boolean(default=True) | |
def _process(self, element, key=None): | |
# Make copy of original data | |
data = {d.name: element[d.name] | |
for d in element.dimensions()} | |
# Normalize and relabel | |
vdims = [] | |
for vdim in element.vdims: | |
maxval = element.range(vdim)[1] | |
if (maxval != 0): | |
data[vdim.name] = data[vdim.name] / maxval | |
if self.p.relabel: | |
vdim = vdim(unit='norm.') | |
vdims.append(vdim) | |
return element.clone(data, vdims=vdims) | |
def __call__(self, element, **params): | |
self.p = param.ParamOverrides(self, params) | |
dynamic = ((self.p.dynamic == 'default' and | |
isinstance(element, hv.DynamicMap)) | |
or self.p.dynamic is True) | |
if isinstance(element, hv.CompositeOverlay) and not isinstance(element, hv.DynamicMap): | |
new_overlay = element.clone() | |
new_data = [] | |
for k, v in new_overlay.data.items(): | |
new_data.append((k, self._process(v, key=None))) | |
new_overlay.data = hv.OrderedDict(new_data) | |
processed = new_overlay | |
elif isinstance(element, hv.ViewableElement): | |
processed = self._process(element) | |
elif isinstance(element, hv.GridSpace): | |
# Initialize an empty axis layout | |
grid_data = ((pos, self(cell, **params)) | |
for pos, cell in element.items()) | |
processed = hv.GridSpace(grid_data, label=element.label, | |
kdims=element.kdims) | |
elif dynamic: | |
processed = hv.core.operation.DynamicFunction(element, function=self, kwargs=params) | |
elif isinstance(element, hv.DynamicMap): | |
if any((not d.values) for d in element.kdims): | |
raise ValueError('Applying a non-dynamic operation requires ' | |
'all DynamicMap key dimensions to define ' | |
'the sampling by specifying values.') | |
samples = tuple(d.values for d in element.kdims) | |
processed = self(element[samples], **params) | |
elif isinstance(element, hv.HoloMap) or isinstance(element, hv.NdLayout): | |
mapped_items = [(k, self._process(el, key=k)) | |
for k, el in element.items()] | |
processed = element.clone(mapped_items) | |
else: | |
raise ValueError("Cannot process type %r" % type(element).__name__) | |
return processed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment