Skip to content

Commit

Permalink
Merge pull request #667 from ioam/dynamic_groupby_tutorial
Browse files Browse the repository at this point in the history
Added Tutorial section to explain DynamicMap groupby methods
  • Loading branch information
jlstevens committed May 11, 2016
2 parents ebbd813 + 900a901 commit 79924b8
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions doc/Tutorials/Dynamic_Map.ipynb
Expand Up @@ -597,6 +597,106 @@
"* There cannot be more dimensions declared in the sampled ``DynamicMap`` than across the rest of the layout. We hope to relax this restriction in future."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using groupby to discretize a DynamicMap\n",
"\n",
"A DynamicMap also makes it easy to partially or completely discretize a function to evaluate in a complex plot. By grouping over specific dimensions dimensions which define a fixed sampling via the Dimension values parameter can be viewed as a ``GridSpace``, ``NdLayout`` or ``NdOverlay``. If a dimension specifies only a continuous range it can't be grouped over but it may still be explored using the widgets. This means we can plot partial or completely discretized views of a parameter space easily.\n",
"\n",
"#### Partially discretize\n",
"\n",
"Fundamentally all the groupby operations use the ``groupby`` method, however there are three convenience functions to group dimensions into an ``NdOverlay`` (``.overlay``), ``GridSpace`` (``.grid``) and ``NdLayout`` (``.layout``).\n",
"\n",
"Here we will evaluate a simple sine function with three dimensions, the phase, frequency and amplitude. We assign the frequency and amplitude discrete samples and define a continuous range for the phase."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xs = np.linspace(0, 2*np.pi)\n",
"\n",
"def sin(ph, f, amp):\n",
" return hv.Curve((xs, np.sin(xs*f+ph)*amp))\n",
"\n",
"kdims=[hv.Dimension('phase', range=(0, np.pi)),\n",
" hv.Dimension('frequency', values=[0.1, 1, 2, 5, 10]),\n",
" hv.Dimension('amplitude', values=[0.5, 5, 10])]\n",
"\n",
"sine_dmap = hv.DynamicMap(sin, kdims=kdims)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we define the amplitude dimension to be overlaid and the amplitude dimension to be gridded."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%opts GridSpace [show_legend=True]\n",
"sine_dmap.overlay('amplitude').grid('frequency')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Fully discretize\n",
"\n",
"Now we define a function to evaluate over a space, instead of sampling it manually we can evaluate it using the groupby method. The function is a spiral with a frequency and a first and second order phase term. Then we define the dimension values for all the parameters and declare the DynamicMap."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%opts Path (linewidth=1 color=Palette('Blues'))\n",
"\n",
"def spiral_equation(f, ph, ph2):\n",
" r = np.arange(0, 1, 0.005)\n",
" xs, ys = (r * fn(f*np.pi*np.sin(r+ph)+ph2) for fn in (np.cos, np.sin))\n",
" return hv.Path((xs, ys))\n",
"\n",
"kdims=[hv.Dimension('f', values=list(np.linspace(1, 10, 10))),\n",
" hv.Dimension('ph', values=list(np.linspace(0, np.pi, 10))),\n",
" hv.Dimension('ph2', values=list(np.linspace(0, np.pi, 4)))]\n",
"\n",
"spiral_dmap = hv.DynamicMap(spiral_equation, kdims=kdims)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can make use of the groupby method directly to group over the frequency and phase dimensions, which we will display as part of a GridSpace by setting the ``container_type``. This leaves second phase variable, which we assign to an NdOverlay by setting the ``group_type``:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%opts GridSpace [xaxis=None yaxis=None] Path [bgcolor='w' xaxis=None yaxis=None]\n",
"spiral_dmap.groupby(['f', 'ph'], group_type=hv.NdOverlay, container_type=hv.GridSpace)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 79924b8

Please sign in to comment.