Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

LinearRegionSelector #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f045c7e
linear selector, basic functionality
kushalkolar Apr 14, 2023
6c5bc77
edges move with fill
kushalkolar Apr 14, 2023
39c6150
linear selector width can change by dragging edge lines
kushalkolar Apr 14, 2023
81c8563
docstring
kushalkolar Apr 14, 2023
885a082
comments
kushalkolar Apr 15, 2023
1ab25e6
highlight linear selector edges on mouse hover
kushalkolar Apr 15, 2023
ae11b62
add get_selected_data()
kushalkolar Apr 15, 2023
9e91966
can add selector from linegraphic, organization
kushalkolar Apr 15, 2023
a71f01e
allow using get_selected_data() from another graphic
kushalkolar Apr 15, 2023
971f5e8
update docstring
kushalkolar Apr 15, 2023
5628814
linearselector compensates for graphic worldobject position, rename h…
kushalkolar Apr 15, 2023
485dd15
fill and edges properly track mouse with movements
kushalkolar Apr 16, 2023
062a63a
del print
kushalkolar Apr 16, 2023
146a30e
line selector works on y axis too
kushalkolar Apr 16, 2023
495a3b6
remove prints
kushalkolar Apr 16, 2023
2ef80dd
fix position checks for y
kushalkolar Apr 16, 2023
9abe206
attempt at gc for LinearSelector, will do later
kushalkolar Apr 16, 2023
beaf442
add get_selected_indices()
kushalkolar Apr 16, 2023
5b7bcbb
properly compensate for graphics with position offset from 0, 0
kushalkolar Apr 17, 2023
6d35691
linear selector works for line collections and line stack
kushalkolar Apr 17, 2023
e078fc5
add linestack separation to size
kushalkolar Apr 17, 2023
033c4b1
add linear selector example nb
kushalkolar Apr 17, 2023
7f9b9b9
add selected_data and selected_indices to event pick info
kushalkolar Apr 18, 2023
a679493
rename to LinearRegionSelector
kushalkolar Apr 18, 2023
f9cb8d1
update example nb
kushalkolar Apr 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
301 changes: 301 additions & 0 deletions 301 examples/linear_selector.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "40bf515f-7ca3-4f16-8ec9-31076e8d4bde",
"metadata": {},
"source": [
"# `LinearSelector` with single lines"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41f4e1d0-9ae9-4e59-9883-d9339d985afe",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import fastplotlib as fpl\n",
"import numpy as np\n",
"\n",
"\n",
"gp = fpl.GridPlot((2, 2))\n",
"\n",
"# preallocated size for zoomed data\n",
"zoomed_prealloc = 1_000\n",
"\n",
"# data to plot\n",
"xs = np.linspace(0, 100, 1_000)\n",
"sine = np.sin(xs) * 20\n",
"\n",
"# make sine along x axis\n",
"sine_graphic_x = gp[0, 0].add_line(sine)\n",
"\n",
"# just something that looks different for line along y-axis\n",
"sine_y = sine\n",
"sine_y[sine_y > 0] = 0\n",
"\n",
"# sine along y axis\n",
"sine_graphic_y = gp[0, 1].add_line(np.column_stack([sine_y, xs]))\n",
"\n",
"# offset the position of the graphic to demonstrate `get_selected_data()` later\n",
"sine_graphic_y.position.set_x(50)\n",
"sine_graphic_y.position.set_y(50)\n",
"\n",
"# add linear selectors\n",
"ls_x = sine_graphic_x.add_linear_region_selector() # default axis is \"x\"\n",
"ls_y = sine_graphic_y.add_linear_region_selector(axis=\"y\")\n",
"\n",
"# preallocate array for storing zoomed in data\n",
"zoomed_init = np.column_stack([np.arange(zoomed_prealloc), np.random.rand(zoomed_prealloc)])\n",
"\n",
"# make line graphics for displaying zoomed data\n",
"zoomed_x = gp[1, 0].add_line(zoomed_init)\n",
"zoomed_y = gp[1, 1].add_line(zoomed_init)\n",
"\n",
"\n",
"def interpolate(subdata: np.ndarray, axis: int):\n",
" \"\"\"1D interpolation to display within the preallocated data array\"\"\"\n",
" x = np.arange(0, zoomed_prealloc)\n",
" xp = np.linspace(0, zoomed_prealloc, subdata.shape[0])\n",
" \n",
" # interpolate to preallocated size\n",
" return np.interp(x, xp, fp=subdata[:, axis]) # use the y-values\n",
"\n",
"\n",
"def set_zoom_x(ev):\n",
" \"\"\"sets zoomed x selector data\"\"\"\n",
" selected_data = ev.pick_info[\"selected_data\"]\n",
" zoomed_x.data = interpolate(selected_data, axis=1) # use the y-values\n",
" gp[1, 0].auto_scale()\n",
"\n",
"\n",
"def set_zoom_y(ev):\n",
" \"\"\"sets zoomed y selector data\"\"\"\n",
" selected_data = ev.pick_info[\"selected_data\"]\n",
" zoomed_y.data = -interpolate(selected_data, axis=0) # use the x-values\n",
" gp[1, 1].auto_scale()\n",
"\n",
"\n",
"# update zoomed plots when bounds change\n",
"ls_x.bounds.add_event_handler(set_zoom_x)\n",
"ls_y.bounds.add_event_handler(set_zoom_y)\n",
"\n",
"gp.show()"
]
},
{
"cell_type": "markdown",
"id": "66b1c599-42c0-4223-b33e-37c1ef077204",
"metadata": {},
"source": [
"### On the x-axis we have a 1-1 mapping from the data that we have passed and the line geometry positions. So the `bounds` min max corresponds directly to the data indices."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b26a37d-aa1d-478e-ad77-99f68a2b7d0c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ls_x.bounds()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2be060c-8f87-4b5c-8262-619768f6e6af",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ls_x.get_selected_indices()"
]
},
{
"cell_type": "markdown",
"id": "d1bef432-d764-4841-bd6d-9b9e4c86ff62",
"metadata": {},
"source": [
"### However, for the y-axis line we have passed a 2D array where we've used a linspace, so there is not a 1-1 mapping from the data to the line geometry positions. Use `get_selected_indices()` to get the indices of the data bounded by the current selection. In addition the position of the Graphic is not `(0, 0)`. You must use `get_selected_indices()` whenever you want the indices of the selected data."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c370d6d7-d92a-4680-8bf0-2f9d541028be",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ls_y.bounds()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdf351e1-63a2-4f5a-8199-8ac3f70909c1",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ls_y.get_selected_indices()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6fd608ad-9732-4f50-9d43-8630603c86d0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import fastplotlib as fpl\n",
"import numpy as np\n",
"\n",
"# data to plot\n",
"xs = np.linspace(0, 100, 1_000)\n",
"sine = np.sin(xs) * 20\n",
"cosine = np.cos(xs) * 20\n",
"\n",
"plot = fpl.GridPlot((5, 1))\n",
"\n",
"# sines and cosines\n",
"sines = [sine] * 2\n",
"cosines = [cosine] * 2\n",
"\n",
"# make line stack\n",
"line_stack = plot[0, 0].add_line_stack(sines + cosines, separation=50)\n",
"\n",
"# make selector\n",
"selector = line_stack.add_linear_region_selector()\n",
"\n",
"# populate subplots with preallocated graphics\n",
"for i, subplot in enumerate(plot):\n",
" if i == 0:\n",
" # skip the first one\n",
" continue\n",
" # make line graphics for displaying zoomed data\n",
" subplot.add_line(zoomed_init, name=\"zoomed\")\n",
"\n",
"\n",
"def update_zoomed_subplots(ev):\n",
" \"\"\"update the zoomed subplots\"\"\"\n",
" zoomed_data = selector.get_selected_data()\n",
" \n",
" for i in range(len(zoomed_data)):\n",
" data = interpolate(zoomed_data[i], axis=1)\n",
" plot[i + 1, 0][\"zoomed\"].data = data\n",
" plot[i + 1, 0].auto_scale()\n",
"\n",
"\n",
"selector.bounds.add_event_handler(update_zoomed_subplots)\n",
"plot.show()"
]
},
{
"cell_type": "markdown",
"id": "63acd2b6-958e-458d-bf01-903037644cfe",
"metadata": {},
"source": [
"# Large line stack with selector"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20e53223-6ccd-4145-bf67-32eb409d3b0a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import fastplotlib as fpl\n",
"import numpy as np\n",
"\n",
"# data to plot\n",
"xs = np.linspace(0, 250, 10_000)\n",
"sine = np.sin(xs) * 20\n",
"cosine = np.cos(xs) * 20\n",
"\n",
"plot = fpl.GridPlot((1, 2))\n",
"\n",
"# sines and cosines\n",
"sines = [sine] * 1_00\n",
"cosines = [cosine] * 1_00\n",
"\n",
"# make line stack\n",
"line_stack = plot[0, 0].add_line_stack(sines + cosines, separation=50)\n",
"\n",
"# make selector\n",
"stack_selector = line_stack.add_linear_region_selector(padding=200)\n",
"\n",
"zoomed_line_stack = plot[0, 1].add_line_stack([zoomed_init] * 2_000, separation=50, name=\"zoomed\")\n",
" \n",
"def update_zoomed_stack(ev):\n",
" \"\"\"update the zoomed subplots\"\"\"\n",
" zoomed_data = stack_selector.get_selected_data()\n",
" \n",
" for i in range(len(zoomed_data)):\n",
" data = interpolate(zoomed_data[i], axis=1)\n",
" zoomed_line_stack.graphics[i].data = data\n",
" \n",
" plot[0, 1].auto_scale()\n",
"\n",
"\n",
"stack_selector.bounds.add_event_handler(update_zoomed_stack)\n",
"plot.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3fa61ffd-43d5-42d0-b3e1-5541f58185cd",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"plot[0, 0].auto_scale()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80e276ba-23b3-43d0-9e0c-86acab79ac67",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 1 addition & 1 deletion 2 fastplotlib/graphics/features/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def _call_event_handlers(self, event_data: FeatureEvent):
func(event_data)
else:
func()
except:
except TypeError:
warn(f"Event handler {func} has an unresolvable argspec, calling it without arguments")
func()

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.