Skip to content

Commit

Permalink
Added a more interesting example of a finite open DynamicMap
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens committed Feb 10, 2016
1 parent c7af1f6 commit 02d57b3
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions doc/Tutorials/Dynamic_Map.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,9 @@
},
"outputs": [],
"source": [
"(dmap.relabel('HMap') + hv.HoloMap({(N,r):shapes(N, r) for N in [3,4,5] \n",
" for r in [0.5,0.75]}, kdims=['N', 'radius']).relabel('DMAP'))"
"(dmap.relabel('HoloMap') \n",
" + hv.HoloMap({(N,r):shapes(N, r) for N in [3,4,5] for r in [0.5,0.75]}, \n",
" kdims=['N', 'radius']).relabel('DynamicMap'))"
]
},
{
Expand Down Expand Up @@ -686,7 +687,7 @@
},
"outputs": [],
"source": [
"dmap = hv.DynamicMap(sample_gaussian(), kdims=['step'])\n",
"dmap = hv.DynamicMap(gaussian_sampler(), kdims=['step'])\n",
"dmap"
]
},
Expand Down Expand Up @@ -783,7 +784,29 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Open mode ``DynamicMaps`` are finite and terminate if ``StopIteration`` is raised. This example uses a generator expression that is identical to the first open ``DynamicMap`` example above, except that it terminates after 20 phases:\n"
"Open mode ``DynamicMaps`` are finite and terminate if ``StopIteration`` is raised. This example terminates when two the mean of two sets of gaussian samples fall within a certain distance of each other:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def sample_distributions(samples=10, delta=50, tol=0.04):\n",
" np.random.seed(42)\n",
" while True:\n",
" gauss1 = np.random.normal(size=samples)\n",
" gauss2 = np.random.normal(size=samples)\n",
" data = (['A']*samples + ['B']*samples, np.hstack([gauss1, gauss2]))\n",
" diff = abs(gauss1.mean() - gauss2.mean())\n",
" if abs(gauss1.mean() - gauss2.mean()) > tol:\n",
" yield ((samples, diff), hv.BoxWhisker(data, kdims=['Group'], vdims=['Value']))\n",
" else:\n",
" raise StopIteration\n",
" samples+=delta"
]
},
{
Expand All @@ -794,16 +817,14 @@
},
"outputs": [],
"source": [
"dmap=hv.DynamicMap((hv.Image(np.sin(0.2*phase + (0.5*x**2+0.5*y**2))) for phase in range(21)), kdims=['phase'])\n",
"dmap = hv.DynamicMap(sample_distributions(), kdims=['samples', '$\\delta$'])\n",
"dmap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is trivial to adapt this generator expression to return (key, tuple) pairs, where the key may have one or more dimensions.\n",
"\n",
"Now if you are familiar with generators in Python, you might be wondering what happens when a finite generator is exhausted. First we should mention that casting a ``DynamicMap`` to a list is always finite, because ``__iter__`` returns the cache instead of a potentially infinite generator:"
]
},
Expand All @@ -822,7 +843,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As we know this ``DynamicMap`` is finite, we can exhaust it as follows:"
"As we know this ``DynamicMap`` is finite, we can make sure it is exhausted it as follows:"
]
},
{
Expand Down

0 comments on commit 02d57b3

Please sign in to comment.