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

LinearSelector #168

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 18 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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_region_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": [
"# `LinearRegionSelector` 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
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.