Created
October 29, 2018 23:15
-
-
Save johncant/9b7ec7241d0178155b9c1a0441f02e6b to your computer and use it in GitHub Desktop.
Holoviews rename issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# This notebook reproduces a bug in Holoviews\n", | |
"\n", | |
"Renaming parameters in a stream using Stream.rename breaks the stream" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import holoviews as hv\n", | |
"from bokeh.io import output_notebook\n", | |
"import param\n", | |
"import parambokeh\n", | |
"from holoviews.streams import Stream, Pipe, ParamValues, Tap, PointerX\n", | |
"\n", | |
"hv.notebook_extension('bokeh')\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x = np.linspace(-2, 2, 100)\n", | |
"x2 = x**2\n", | |
"x3 = x**3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Show the data using a library that isn't the \"system under test\"\n", | |
"df = pd.DataFrame({\"x\": x, \"x2\": x2, \"x3\": x3})\n", | |
"df.plot()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class ParamClass(Stream):\n", | |
" whichcurve = param.ObjectSelector(default=\"x3\", objects=df.columns)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def simple_view(whichcurve):\n", | |
" \"\"\"\n", | |
" Generate a curve based on selected column\n", | |
" \"\"\"\n", | |
" plot_df = df.copy()\n", | |
" plot_df['y'] = plot_df[whichcurve]\n", | |
" plot_df = plot_df[[\"x\", \"y\"]]\n", | |
" \n", | |
" return hv.Curve(plot_df, \"x\", \"y\")\n", | |
"\n", | |
"def simple_view1(whichcurve1):\n", | |
" \"\"\"\n", | |
" Same as simple_view but with the argument renamed\n", | |
" \"\"\"\n", | |
" return simple_view(whichcurve=whichcurve1)\n", | |
"\n", | |
"\n", | |
"def working_example():\n", | |
" param_instance = ParamClass()\n", | |
"\n", | |
" parambokeh.Widgets(param_instance, continuous_update=True, callback=param_instance.event, on_init=True)\n", | |
" \n", | |
" return hv.DynamicMap(simple_view, streams=[param_instance])\n", | |
"\n", | |
"\n", | |
"def broken_example():\n", | |
" param_instance = ParamClass()\n", | |
"\n", | |
" parambokeh.Widgets(param_instance, continuous_update=True, callback=param_instance.event, on_init=True)\n", | |
" \n", | |
" # Renaming the stream parameter breaks the DynamicMap\n", | |
" return hv.DynamicMap(simple_view1, streams=[param_instance.rename(whichcurve=\"whichcurve1\")])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# This example lets you select which curve\n", | |
"working_example()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# In this example, selecting curve does not work\n", | |
"#\n", | |
"# Hard to see in a notebook, but in my bokeh app, when I use add_subscriber,\n", | |
"# I can see that the renamed stream events do indeed get fired. However, my\n", | |
"# graphs don't update.\n", | |
"broken_example()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment