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
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+28 −1
Diff settings

Always

Just for now

Added docstring and unit test about Callable memoization

  • Loading branch information...
Philipp Rudiger Philipp Rudiger
Philipp Rudiger authored and Philipp Rudiger committed Jan 16, 2017
commit f0a66c7085da0c7de9a68f5db4b0d4fe92ed5884
@@ -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

This comment has been minimized.

Copy link
@jlstevens

jlstevens Jan 16, 2017

Contributor

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

unncessarily.
"""

This comment has been minimized.

Copy link
@jlstevens

jlstevens Jan 16, 2017

Contributor

Typo.


callable_function = param.Callable(default=lambda x: x, doc="""
@@ -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

@@ -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)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.