Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add rectangular region selector #576

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 24 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
35 changes: 35 additions & 0 deletions docs/source/api/graphic_features/RectangleSelectionFeature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.. _api.RectangleSelectionFeature:

RectangleSelectionFeature
*************************

=========================
RectangleSelectionFeature
=========================
.. currentmodule:: fastplotlib.graphics._features

Constructor
~~~~~~~~~~~
.. autosummary::
:toctree: RectangleSelectionFeature_api

RectangleSelectionFeature

Properties
~~~~~~~~~~
.. autosummary::
:toctree: RectangleSelectionFeature_api

RectangleSelectionFeature.value

Methods
~~~~~~~
.. autosummary::
:toctree: RectangleSelectionFeature_api

RectangleSelectionFeature.add_event_handler
RectangleSelectionFeature.block_events
RectangleSelectionFeature.clear_event_handlers
RectangleSelectionFeature.remove_event_handler
RectangleSelectionFeature.set_value

1 change: 1 addition & 0 deletions docs/source/api/graphic_features/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Graphic Features
TextOutlineThickness
LinearSelectionFeature
LinearRegionSelectionFeature
RectangleSelectionFeature
Name
Offset
Rotation
Expand Down
1 change: 1 addition & 0 deletions docs/source/api/graphics/ImageGraphic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Methods
ImageGraphic.add_event_handler
ImageGraphic.add_linear_region_selector
ImageGraphic.add_linear_selector
ImageGraphic.add_rectangle_selector
ImageGraphic.clear_event_handlers
ImageGraphic.remove_event_handler
ImageGraphic.reset_vmin_vmax
Expand Down
1 change: 1 addition & 0 deletions docs/source/api/graphics/LineCollection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Methods
LineCollection.add_graphic
LineCollection.add_linear_region_selector
LineCollection.add_linear_selector
LineCollection.add_rectangle_selector
LineCollection.clear_event_handlers
LineCollection.remove_event_handler
LineCollection.remove_graphic
Expand Down
1 change: 1 addition & 0 deletions docs/source/api/graphics/LineGraphic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Methods
LineGraphic.add_event_handler
LineGraphic.add_linear_region_selector
LineGraphic.add_linear_selector
LineGraphic.add_rectangle_selector
LineGraphic.clear_event_handlers
LineGraphic.remove_event_handler
LineGraphic.rotate
Expand Down
1 change: 1 addition & 0 deletions docs/source/api/graphics/LineStack.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Methods
LineStack.add_graphic
LineStack.add_linear_region_selector
LineStack.add_linear_selector
LineStack.add_rectangle_selector
LineStack.clear_event_handlers
LineStack.remove_event_handler
LineStack.remove_graphic
Expand Down
53 changes: 53 additions & 0 deletions docs/source/api/selectors/RectangleSelector.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.. _api.RectangleSelector:

RectangleSelector
*****************

=================
RectangleSelector
=================
.. currentmodule:: fastplotlib

Constructor
~~~~~~~~~~~
.. autosummary::
:toctree: RectangleSelector_api

RectangleSelector

Properties
~~~~~~~~~~
.. autosummary::
:toctree: RectangleSelector_api

RectangleSelector.axes
RectangleSelector.axis
RectangleSelector.block_events
RectangleSelector.deleted
RectangleSelector.event_handlers
RectangleSelector.limits
RectangleSelector.name
RectangleSelector.offset
RectangleSelector.parent
RectangleSelector.rotation
RectangleSelector.selection
RectangleSelector.supported_events
RectangleSelector.visible
RectangleSelector.world_object

Methods
~~~~~~~
.. autosummary::
:toctree: RectangleSelector_api

RectangleSelector.add_axes
RectangleSelector.add_event_handler
RectangleSelector.clear_event_handlers
RectangleSelector.get_selected_data
RectangleSelector.get_selected_index
RectangleSelector.get_selected_indices
RectangleSelector.remove_event_handler
RectangleSelector.rotate
RectangleSelector.share_property
RectangleSelector.unshare_property

1 change: 1 addition & 0 deletions docs/source/api/selectors/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Selectors

LinearSelector
LinearRegionSelector
RectangleSelector
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
"numpy": ("https://numpy.org/doc/stable/", None),
"pygfx": ("https://pygfx.org/stable", None),
"pygfx": ("https://docs.pygfx.org/stable/", None),
"wgpu": ("https://wgpu-py.readthedocs.io/en/latest", None),
"fastplotlib": ("https://fastplotlib.readthedocs.io/en/latest/", None),
}
Expand Down
66 changes: 66 additions & 0 deletions examples/desktop/selectors/rectangle_selector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Rectangle Selectors
===================

Example showing how to use a `RectangleSelector` with line collections
"""

# test_example = false
# sphinx_gallery_pygfx_docs = 'screenshot'

import numpy as np
import fastplotlib as fpl
from itertools import product

# create a figure
figure = fpl.Figure(
size=(700, 560)
)


# generate some data
def make_circle(center, radius: float, n_points: int = 75) -> np.ndarray:
theta = np.linspace(0, 2 * np.pi, n_points)
xs = radius * np.sin(theta)
ys = radius * np.cos(theta)

return np.column_stack([xs, ys]) + center


spatial_dims = (50, 50)

circles = list()
for center in product(range(0, spatial_dims[0], 9), range(0, spatial_dims[1], 9)):
circles.append(make_circle(center, 3, n_points=75))

pos_xy = np.vstack(circles)

# add image
line_collection = figure[0, 0].add_line_collection(circles, cmap="jet", thickness=5)

# add rectangle selector to image graphic
rectangle_selector = line_collection.add_rectangle_selector()


# add event handler to highlight selected indices
@rectangle_selector.add_event_handler("selection")
def color_indices(ev):
line_collection.cmap = "jet"
ixs = ev.get_selected_indices()

# iterate through each of the selected indices, if the array size > 0 that mean it's under the selection
selected_line_ixs = [i for i in range(len(ixs)) if ixs[i].size > 0]
line_collection[selected_line_ixs].colors = "w"


# manually move selector to make a nice gallery image :D
rectangle_selector.selection = (15, 30, 15, 30)


figure.show()

# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively
# please see our docs for using fastplotlib interactively in ipython and jupyter
if __name__ == "__main__":
print(__doc__)
fpl.run()
52 changes: 52 additions & 0 deletions examples/desktop/selectors/rectangle_selector_zoom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Rectangle Selectors
===================
Example showing how to use a `RectangleSelector` with images
"""

# test_example = false
# sphinx_gallery_pygfx_docs = 'screenshot'

import imageio.v3 as iio
import fastplotlib as fpl

# create a figure
figure = fpl.Figure(
shape=(2, 1),
size=(700, 560)
)

# add image
image_graphic = figure[0, 0].add_image(data=iio.imread("imageio:camera.png"))

# add rectangle selector to image graphic
rectangle_selector = image_graphic.add_rectangle_selector()

# add a zoomed plot of the selected data
zoom_ig = figure[1, 0].add_image(rectangle_selector.get_selected_data())


# add event handler to update the data of the zoomed image as the selection changes
@rectangle_selector.add_event_handler("selection")
def update_data(ev):
# get the new data
new_data = ev.get_selected_data()

# remove the old zoomed image graphic
global zoom_ig

figure[1, 0].remove_graphic(zoom_ig)

# add new zoomed image of new data
zoom_ig = figure[1, 0].add_image(data=new_data)

# autoscale the plot
figure[1, 0].auto_scale()

figure.show()

# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively
# please see our docs for using fastplotlib interactively in ipython and jupyter
if __name__ == "__main__":
print(__doc__)
fpl.run()
7 changes: 6 additions & 1 deletion fastplotlib/graphics/_features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
TextOutlineThickness,
)

from ._selection_features import LinearSelectionFeature, LinearRegionSelectionFeature
from ._selection_features import (
LinearSelectionFeature,
LinearRegionSelectionFeature,
RectangleSelectionFeature,
)
from ._common import Name, Offset, Rotation, Visible, Deleted


Expand All @@ -56,6 +60,7 @@
"TextOutlineThickness",
"LinearSelectionFeature",
"LinearRegionSelectionFeature",
"RectangleSelectionFeature",
"Name",
"Offset",
"Rotation",
Expand Down
Loading
Loading