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

Add memoization to dynamic Callable class #1063

Merged
merged 4 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions holoviews/core/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,10 @@ class Callable(param.Parameterized):
allowing their inputs (and in future outputs) to be defined.
This makes it possible to wrap DynamicMaps with streams and
makes it possible to traverse the graph of operations applied
to a DynamicMap.
to a DynamicMap. Additionally a Callable will memoize the last
returned value based on the arguments to the function and the
state of all streams on its inputs, avoiding calling the function
Copy link
Contributor

Choose a reason for hiding this comment

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

I think either 'avoiding calls to the function' or 'to avoid calling the function ...' would be read better.

unncessarily.
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo.


callable_function = param.Callable(default=lambda x: x, doc="""
Expand All @@ -413,8 +416,22 @@ class Callable(param.Parameterized):
inputs = param.List(default=[], doc="""
The list of inputs the callable function is wrapping.""")

def __init__(self, **params):
super(Callable, self).__init__(**params)
self._memoized = {}

def __call__(self, *args, **kwargs):
return self.callable_function(*args, **kwargs)
inputs = [inp for inp in self.inputs if isinstance(inp, DynamicMap)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Very minor point - I would call the loop variable i given that inp is a bit weird and you probably don't want to clash with the input built-in. Alternatively, this is one instance of a pure filter so you could consider using that (it would be shorter).

streams = [s for inp in inputs for s in get_nested_streams(inp)]
values = tuple(tuple(sorted(s.contents.items())) for s in streams)
Copy link
Contributor

@jlstevens jlstevens Jan 16, 2017

Choose a reason for hiding this comment

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

This means it is memoizing on the stream parameters in ascending alphanumeric order. I think that is fine (and makes sense!) but it is worth noting that this is one particular policy for how streams parameters are ordered into a tuple key. This is important to stay consistent if we ever need it somewhere else and I am now wondering if it might be worth having a utility to codify the idea....up to you.

Copy link
Member Author

Choose a reason for hiding this comment

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

It really doesn't matter much, it just needs to be consistently ordered. If we're using it somewhere else it can use the same scheme or another scheme without having any effect here.

Copy link
Contributor

Choose a reason for hiding this comment

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

True - my point is that we should try to be consistent anyway!

key = args + tuple(sorted(kwargs.items())) + values

if key in self._memoized:
return self._memoized[key]
else:
ret = self.callable_function(*args, **kwargs)
self._memoized = {key : ret}
return ret


def get_nested_streams(dmap):
Expand Down Expand Up @@ -500,6 +517,8 @@ class DynamicMap(HoloMap):
""")

def __init__(self, callback, initial_items=None, **params):
if not isinstance(callback, Callable) and not isinstance(callback, types.GeneratorType):
callback = Callable(callable_function=callback)
Copy link
Contributor

Choose a reason for hiding this comment

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

This means all callbacks will now be wrapped in Callable. This is a change from the previous behavior, and probably a good one (more consistent, at least with __mull__) though we should now document this. It would also be good to have an example of where a user might want to supply Callable themselves....

Copy link
Contributor

Choose a reason for hiding this comment

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

For instance, we might want an example of the user declaring a Callable with their own set of inputs.

super(DynamicMap, self).__init__(initial_items, callback=callback, **params)

# Set source to self if not already specified
Expand All @@ -514,7 +533,6 @@ def __init__(self, callback, initial_items=None, **params):

self.call_mode = self._validate_mode()
self.mode = 'bounded' if self.call_mode == 'key' else 'open'
self._dimensionless_cache = False


def _initial_key(self):
Expand Down Expand Up @@ -762,7 +780,7 @@ def __getitem__(self, key):
try:
dimensionless = util.dimensionless_contents(get_nested_streams(self),
self.kdims, no_duplicates=False)
if (dimensionless and not self._dimensionless_cache):
if dimensionless:
raise KeyError('Using dimensionless streams disables DynamicMap cache')
cache = super(DynamicMap,self).__getitem__(key)
# Return selected cache items in a new DynamicMap
Expand Down
26 changes: 0 additions & 26 deletions holoviews/core/traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,3 @@ def hierarchical(keys):
store1[v2].append(v1)
hierarchies.append(store2 if hierarchy else {})
return hierarchies


class dimensionless_cache(object):
"""
Context manager which temporarily enables lookup of frame in the
cache on a DynamicMap with dimensionless streams. Allows passing
any Dimensioned object which might contain a DynamicMap and
whether to enable the cache. This allows looking up an item
without triggering the callback. Useful when the object is looked
up multiple times as part of some processing pipeline.
"""

def __init__(self, obj, allow_cache_lookup=True):
self.obj = obj
self._allow_cache_lookup = allow_cache_lookup

def __enter__(self):
self.set_cache_flag(self._allow_cache_lookup)

def __exit__(self, exc_type, exc_val, exc_tb):
self.set_cache_flag(False)

def set_cache_flag(self, value):
self.obj.traverse(lambda x: setattr(x, '_dimensionless_cache', value),
['DynamicMap'])

7 changes: 2 additions & 5 deletions holoviews/plotting/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from ..core.options import Store, Compositor, SkipRendering
from ..core.overlay import NdOverlay
from ..core.spaces import HoloMap, DynamicMap
from ..core.traversal import dimensionless_cache
from ..core.util import stream_parameters
from ..element import Table
from .util import (get_dynamic_mode, initialize_sampled, dim_axis_label,
Expand Down Expand Up @@ -625,8 +624,7 @@ def _get_frame(self, key):
self.current_key = key
return self.current_frame
elif self.dynamic:
with dimensionless_cache(self.hmap, not self._force or not self.drawn):
key, frame = util.get_dynamic_item(self.hmap, self.dimensions, key)
key, frame = util.get_dynamic_item(self.hmap, self.dimensions, key)
traverse_setter(self, '_force', False)
if not isinstance(key, tuple): key = (key,)
key_map = dict(zip([d.name for d in self.hmap.kdims], key))
Expand Down Expand Up @@ -977,8 +975,7 @@ def _get_frame(self, key):
if d in item.dimensions('key')], key)
self.current_key = tuple(k[1] for k in dim_keys)
elif item.traverse(lambda x: x, [DynamicMap]):
with dimensionless_cache(item, not self._force or not self.drawn):
key, frame = util.get_dynamic_item(item, self.dimensions, key)
key, frame = util.get_dynamic_item(item, self.dimensions, key)
layout_frame[path] = frame
continue
elif self.uniform:
Expand Down
24 changes: 24 additions & 0 deletions tests/testdynamic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from holoviews import Dimension, DynamicMap, Image, HoloMap, Scatter, Curve
from holoviews.streams import PositionXY
from holoviews.util import Dynamic
from holoviews.element.comparison import ComparisonTestCase

Expand Down Expand Up @@ -202,3 +203,26 @@ def test_dynamic_holomap_overlay(self):
dynamic_overlay = dmap * hmap
overlaid = Image(sine_array(0,5)) * Image(sine_array(0,10))
self.assertEqual(dynamic_overlay[5], overlaid)

def test_dynamic_overlay_memoization(self):
"""Tests that Callable memoizes unchanged callbacks"""
def fn(x, y):
return Scatter([(x, y)])
dmap = DynamicMap(fn, kdims=[], streams=[PositionXY()])

counter = [0]
def fn2(x, y):
counter[0] += 1
return Image(np.random.rand(10, 10))
dmap2 = DynamicMap(fn2, kdims=[], streams=[PositionXY()])

overlaid = dmap * dmap2
overlay = overlaid[()]
self.assertEqual(overlay.Scatter.I, fn(0, 0))

dmap.event(x=1, y=2)
overlay = overlaid[()]
# Ensure dmap return value was updated
self.assertEqual(overlay.Scatter.I, fn(1, 2))
# Ensure dmap2 callback was called only once
self.assertEqual(counter[0], 1)