Skip to content

Instantly share code, notes, and snippets.

@sutartmelson
Last active July 13, 2016 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sutartmelson/18451e19614cf92e14de7e671c54d164 to your computer and use it in GitHub Desktop.
Save sutartmelson/18451e19614cf92e14de7e671c54d164 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from bokeh.models.callbacks import Callback\n",
"from bokeh.core.properties import Instance \n",
"from bokeh.models import ColumnDataSource, Tool, CustomJS\n",
"import numpy as np\n",
"\n",
"\n",
"class DrawTool(Tool): \n",
" __implementation__ = \"\"\"\n",
"_ = require \"underscore\"\n",
"GestureTool = require \"models/tools/gestures/gesture_tool\"\n",
"Span = require \"models/annotations/span\"\n",
"p = require \"core/properties\"\n",
"count = 0\n",
"\n",
"\n",
"class DrawToolView extends GestureTool.View\n",
" \n",
" _pan_start: (e) -> \n",
" source = @mget('source') \n",
" if source.data.x.length == 0\n",
" count = 0\n",
" source.data = {x: [], y: []}\n",
" source.data.x.push([])\n",
" source.data.y.push([])\n",
" source.trigger('change')\n",
" count++\n",
" \n",
" _pan: (e) ->\n",
" frame = @plot_model.get('frame')\n",
" canvas = @plot_view.canvas\n",
" vx = canvas.sx_to_vx(e.bokeh.sx)\n",
" vy = canvas.sy_to_vy(e.bokeh.sy)\n",
" x = frame.get('x_mappers').default.map_from_target(vx)\n",
" y = frame.get('y_mappers').default.map_from_target(vy)\n",
" if not frame.contains(vx, vy)\n",
" return null\n",
" source = @mget('source')\n",
" source.data.x[count-1].push(x)\n",
" source.data.y[count-1].push(y)\n",
" source.trigger('change')\n",
" \n",
" _pan_end: (e) ->\n",
" source = @mget('source')\n",
" geometry = \n",
" {\n",
" type: 'line'\n",
" x: source.data.x[count-1]\n",
" y: source.data.y[count-1]\n",
" }\n",
" \n",
" callback = @mget(\"callback\")\n",
" if callback?\n",
" if _.isFunction(callback)\n",
" callback(@model, geometry)\n",
" else\n",
" callback.execute(@model, geometry)\n",
" \n",
" \n",
" return null\n",
"\n",
"DEFAULT_Y_OVERLAY = () -> new Span.Model({\n",
" render_mode: \"css\"\n",
" level: \"overlay\"\n",
" location_units: \"screen\"\n",
"})\n",
"class DrawTool extends GestureTool.Model\n",
" default_view: DrawToolView\n",
" type: \"DrawTool\"\n",
" tool_name: \"Draw Tool\"\n",
" icon: \"bk-tool-icon-lasso-select\"\n",
" event_type: \"pan\"\n",
" default_order: 12\n",
" @define {\n",
" source: [ p.Instance ]\n",
" callback: [p.Instance]\n",
" }\n",
"module.exports =\n",
" Model: DrawTool\n",
" View: DrawToolView\n",
" \"\"\" \n",
" source = Instance(ColumnDataSource) \n",
" callback = Instance(Callback, help = \"\"\"help\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from ipywidgets import interact, IntSlider\n",
"from bokeh.io import push_notebook\n",
"from bokeh.plotting import output_notebook, figure, show\n",
"from bokeh.resources import INLINE\n",
"output_notebook(resources=INLINE)\n",
"\n",
"images = []\n",
"class AnnotatingIterator:\n",
"\n",
" def __init__(self):\n",
" self._source = ColumnDataSource(data=dict(x=[], y=[]))\n",
" self._callback = CustomJS(args=dict(source = self._source), code = \"\"\"\n",
" tempx = cb_data[\"x\"]\n",
" tempy = cb_data[\"y\"]\n",
" command = \"x=\" + tempx\n",
" command2 = \"y=\" + tempy\n",
" command3 = \"xdata.append(x)\"\n",
" command4 = \"ydata.append(y)\"\n",
" IPython.notebook.kernel.execute(command)\n",
" IPython.notebook.kernel.execute(command2)\n",
" IPython.notebook.kernel.execute(command3)\n",
" IPython.notebook.kernel.execute(command4)\n",
" \"\"\")\n",
" self._target = None\n",
" self._plot_image = None\n",
" self._plot_line = None\n",
" self._plot = None\n",
" self._draw_color = 'red'\n",
" self._line_width = 5\n",
" self._plot_width = 512\n",
" self._plot_height = 512\n",
" self.__create_images()\n",
" \n",
" def __create_images(self):\n",
" N = 20\n",
" for k in range(N):\n",
" img = np.empty((N,N), dtype=np.uint32)\n",
" view = img.view(dtype=np.uint8).reshape((N, N, 4))\n",
" for i in range(N):\n",
" for j in range(N):\n",
" view[i, j, 0] = int(k/N*255)\n",
" view[i, j, 1] = int(i/N*255)\n",
" view[i, j, 2] = int(j/N*255)\n",
" view[i, j, 3] = 255\n",
" images.append(img)\n",
" \n",
" def create_plot_from_file(self):\n",
" \"\"\"\n",
" Processes image information from file and creates (but not display)\n",
" the bokeh plot. \n",
" \"\"\"\n",
" self.__create_plot(from_file=True)\n",
"\n",
" def create_plot_from_folder(self):\n",
" \"\"\"\n",
" Processes image information from file within the folder and creates\n",
" (but not display) the bokeh plot. \n",
" \"\"\"\n",
" self.__create_plot(from_file=False)\n",
"\n",
" def create_widgets(self):\n",
" \"\"\"\n",
" Creates and displays widgets for interacting with bokeh plot.\n",
" \"\"\"\n",
" length = 20\n",
" # create slider for selecting image slice,\n",
" # window width, and image level\n",
" \n",
" imageSlider = IntSlider(min=0, max=(length - 1), step=1,\n",
" continuous_update=False)\n",
" \n",
" interact(self.__callback,\n",
" image_index = imageSlider,\n",
" continuous_update=False)\n",
"\n",
" def draw(self):\n",
" \"\"\"\n",
" Displays the already created bokeh plot.\n",
" Plot and widgets must have already been created. \n",
" \"\"\"\n",
" self.__display_images()\n",
"\n",
" def __create_plot(self, from_file=False):\n",
" tool = DrawTool(source=self._source)\n",
" tool.callback = self._callback\n",
" self._plot = figure(plot_width=self._plot_width,\n",
" plot_height=self._plot_height,\n",
" x_range=(0,10), y_range=(0,10),\n",
" tools=[tool])\n",
"\n",
" def set_draw_color(self, color):\n",
" self._draw_color = color\n",
"\n",
" def set_line_width(self, width):\n",
" self._draw_width = width\n",
"\n",
" def set_plot_height(self, plot_height):\n",
" self._plot_height = plot_height\n",
"\n",
" def set_plot_width(self, plot_width):\n",
" self._plot_width = plot_width\n",
"\n",
" def __display_images(self):\n",
" img = images[0]\n",
" self._plot_image = self._plot.image_rgba(image=[img], x=0, y=0,\n",
" dw=10, dh=10, level='image')\n",
"\n",
" self._plot_line = self._plot.multi_line('x', 'y',\n",
" source=self._source,\n",
" line_color=self._draw_color,\n",
" line_width=self._line_width,\n",
" level='overlay')\n",
" self._target = show(self._plot)\n",
"\n",
" def __callback(self, image_index):\n",
" img = images[image_index]\n",
" if self._plot_image is not None and self._target is not None:\n",
" self._plot_image.data_source.data['image'] = [img]\n",
" self._plot_line.data_source.data = dict(x=[], y=[])\n",
" push_notebook(handle=self._target)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ai = AnnotatingIterator()\n",
"ai.create_plot_from_folder()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ai.create_widgets()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ai.draw()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment