Skip to content

Commit

Permalink
Added docstring and unit test about Callable memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jan 16, 2017
1 parent 601d1f8 commit f0a66c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 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
unncessarily.
"""

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

0 comments on commit f0a66c7

Please sign in to comment.