-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4eea0ad
commit 3227348
Showing
3 changed files
with
194 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "discrete-retention", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import json\n", | ||
"import math\n", | ||
"import numpy as np" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "ethical-posting", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"with open('./data.json') as f:\n", | ||
" data = json.load(f)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "emerging-marking", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"values = np.array(data['values'], dtype='float32')\n", | ||
"values = values.reshape((data['height'], data['width']))[:10,]\n", | ||
"values.shape" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "twelve-cabinet", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"from bqplot import Figure, LinearScale, Axis, ColorScale\n", | ||
"from bqplot_image_gl import ImageGL, Contour\n", | ||
"import ipywidgets as widgets\n", | ||
"scale_x = LinearScale(min=-1, max=4, allow_padding=False)\n", | ||
"scale_y = LinearScale(min=-1, max=4, allow_padding=False)\n", | ||
"scales = {'x': scale_x, 'y': scale_y}\n", | ||
"axis_x = Axis(scale=scale_x, label='x')\n", | ||
"axis_y = Axis(scale=scale_y, label='y', orientation='vertical')\n", | ||
"scales_image = {'x': scale_x, 'y': scale_y, 'image': ColorScale(min=np.min(values).item(), max=np.max(values).item())}\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "appreciated-transformation", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"figure = Figure(scales=scales, axes=[axis_x, axis_y])\n", | ||
"image = ImageGL(image=values, scales=scales_image, x=[0, 2], y=[0, 2])\n", | ||
"figure.marks = (image, )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "buried-cooperation", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"figure" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "sufficient-shirt", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from bqplot_image_gl.interacts import MouseInteraction" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "empirical-model", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"widget_label = widgets.Label(value=\"move cursor for information\")\n", | ||
"widget_label" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "known-possession", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"interaction = MouseInteraction(x_scale=scales_image['x'], y_scale=scales_image['y'], move_throttle=70)\n", | ||
"figure.interaction = interaction\n", | ||
"def on_mouse_msg(interaction, data, buffers):\n", | ||
" # it might be a good idea to throttle on the Python side as well, for instance when many computations\n", | ||
" # happen, we can effectively ignore the queue of messages\n", | ||
" if data['event'] == 'mousemove':\n", | ||
" domain_x = data['domain']['x']\n", | ||
" domain_y = data['domain']['y']\n", | ||
" normalized_x = (domain_x - image.x[0]) / (image.x[1] - image.x[0])\n", | ||
" normalized_y = (domain_y - image.y[0]) / (image.y[1] - image.y[0])\n", | ||
" # TODO: think about +/-1 and pixel edges\n", | ||
" pixel_x = int(math.floor(normalized_x * image.image.shape[1]))\n", | ||
" pixel_y = int(math.floor(normalized_y * image.image.shape[0]))\n", | ||
" if pixel_x >= 0 and pixel_x < image.image.shape[1] and pixel_y >= 0 and pixel_y < image.image.shape[0]:\n", | ||
" value = str(image.image[pixel_y, pixel_x])\n", | ||
" else:\n", | ||
" value = \"out of range\"\n", | ||
" msg = f\"x={pixel_x} y={pixel_y} value={value} (nx={normalized_x} ny={normalized_y})\"\n", | ||
" widget_label.value = msg\n", | ||
" elif data['event'] == 'mouseleave':\n", | ||
" widget_label.value = \"Bye!\"\n", | ||
" elif data['event'] == 'mouseenter':\n", | ||
" widget_label.value = \"Almost there...\" # this is is not visible because mousemove overwrites the msg\n", | ||
" \n", | ||
"interaction.on_msg(on_mouse_msg)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "federal-domestic", | ||
"metadata": {}, | ||
"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.8.6" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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