hyperspy.drawing.widget module

class hyperspy.drawing.widget.DraggableWidgetBase(axes_manager, **kwargs)

Bases: hyperspy.drawing.widget.WidgetBase

Adds the position and indices properties, and adds a framework for letting the user drag the patch around. Also adds the moved event.

The default behavior is that position snaps to the values corresponding to the values of the axes grid (i.e. no subpixel values). This behavior can be controlled by the property snap_position.

Any inheritors must override these methods:

_onmousemove(self, event) _update_patch_position(self) _set_patch(self)

button_release(event)

whenever a mouse button is released

connect(ax)

Connect to the matplotlib Axes’ events.

property indices
onpick(event)
property position
property snap_position
class hyperspy.drawing.widget.ResizableDraggableWidgetBase(axes_manager, **kwargs)

Bases: hyperspy.drawing.widget.DraggableWidgetBase

Adds the size property and get_size_in_axes method, and adds a framework for letting the user resize the patch, including resizing by key strokes (‘+’, ‘-‘). Also adds the ‘resized’ event.

Utility functions for resizing are implemented by increase_size and decrease_size, which will in-/decrement the size by 1. Other utility functions include get_centre and get_centre_indices which returns the center position, and the internal _apply_changes which helps make sure that only one ‘changed’ event is fired for a combined move and resize.

Any inheritors must override these methods:

_update_patch_position(self) _update_patch_size(self) _update_patch_geometry(self) _set_patch(self)

button_release(event)

whenever a mouse button is released

connect(ax)

Connect to the matplotlib Axes’ events.

decrease_size()

Decrement all sizes by 1. Applied via ‘size’ property.

get_centre()

Get’s the center indices. The default implementation is simply the position + half the size in axes space, which should work for any symmetric widget, but more advanced widgets will need to decide whether to return the center of gravity or the geometrical center of the bounds.

get_centre_index()

Get’s the center position (in index space). The default implementation is simply the indices + half the size, which should work for any symmetric widget, but more advanced widgets will need to decide whether to return the center of gravity or the geometrical center of the bounds.

get_size_in_indices()

Gets the size property converted to the index space (via ‘axes’ attribute).

increase_size()

Increment all sizes by 1. Applied via ‘size’ property.

on_key_press(event)
onpick(event)
set_size_in_indices(value)

Sets the size property converted to the index space (via ‘axes’ attribute).

property size
property snap_all
property snap_size
class hyperspy.drawing.widget.ResizersMixin(resizers=True, **kwargs)

Bases: object

Widget mix-in for adding resizing manipulation handles.

The default handles are green boxes displayed on the outside corners of the boundaries. By default, the handles are only displayed when the widget is selected (picked in matplotlib terminology).

resizers{bool}

Property that determines whether the resizer handles should be used

resize_color{matplotlib color}

The color of the resize handles.

resize_pixel_size{tuple | None}

Size of the resize handles in screen pixels. If None, it is set equal to the size of one ‘data-pixel’ (image pixel size).

resizer_picked{False | int}

Inidcates which, if any, resizer was selected the last time the widget was picked. False if another patch was picked, or the index of the resizer handle that was picked.

onpick(event)

Picking of main patch is same as for widget base, but this also handles picking of the resize handles. If a resize handles is picked, picked is set to True, and resizer_picked is set to an integer indicating which handle was picked (0-3 for top left, top right, bottom left, bottom right). It is set to False if another widget was picked.

If the main patch is picked, the offset from the picked pixel to the position is stored in pick_offset. This can be used in e.g. _onmousemove to ease dragging code (prevent widget center/corner snapping to mouse).

property resizers
set_on(value)

Turns on/off resizers whet widget is turned on/off.

class hyperspy.drawing.widget.Widget1DBase(axes_manager, **kwargs)

Bases: hyperspy.drawing.widget.DraggableWidgetBase

A base class for 1D widgets.

It sets the right dimensions for size and position, adds the ‘border_thickness’ attribute and initalizes the ‘axes’ attribute to the first two navigation axes if possible, if not, the two first signal_axes are used. Other than that it mainly supplies common utility functions for inheritors, and implements required functions for ResizableDraggableWidgetBase.

The implementation for ResizableDraggableWidgetBase methods all assume that a Rectangle patch will be used, centered on position. If not, the inheriting class will have to override those as applicable.

class hyperspy.drawing.widget.Widget2DBase(axes_manager, **kwargs)

Bases: hyperspy.drawing.widget.ResizableDraggableWidgetBase

A base class for 2D widgets. It sets the right dimensions for size and position, adds the ‘border_thickness’ attribute and initalizes the ‘axes’ attribute to the first two navigation axes if possible, if not, the two first signal_axes are used. Other than that it mainly supplies common utility functions for inheritors, and implements required functions for ResizableDraggableWidgetBase.

The implementation for ResizableDraggableWidgetBase methods all assume that a Rectangle patch will be used, centered on position. If not, the inheriting class will have to override those as applicable.

class hyperspy.drawing.widget.WidgetBase(axes_manager=None, color='red', alpha=1.0, **kwargs)

Bases: object

Base class for interactive widgets/patches. A widget creates and maintains one or more matplotlib patches, and manages the interaction code so that the user can maniuplate it on the fly.

This base class implements functionality witch is common to all such widgets, mainly the code that manages the patch, axes management, and sets up common events (‘changed’ and ‘closed’).

Any inherting subclasses must implement the following methods:

_set_patch(self) _on_navigate(obj, name, old, new) # Only for widgets that can navigate

It should also make sure to fill the ‘axes’ attribute as early as possible (but after the base class init), so that it is available when needed.

property alpha
property axes
close(window=None)

Set the on state to off (removes patch and disconnects), and trigger events.closed.

property color
connect(ax)

Connect to the matplotlib Axes’ events.

connect_navigate()

Connect to the axes_manager such that changes in the widget or in the axes_manager are reflected in the other.

disconnect()

Disconnect from all events (both matplotlib and navigation).

disconnect_navigate()

Disconnect a previous naivgation connection.

draw_patch(*args)

Update the patch drawing.

is_on()

Determines if the widget is set to draw if valid (turned on).

select()

Cause this widget to be the selected widget in its MPL axes. This assumes that the widget has its patch added to the MPL axes.

set_mpl_ax(ax)

Set the matplotlib Axes that the widget will draw to. If the widget on state is True, it will also add the patch to the Axes, and connect to its default events.

set_on(value)

Change the on state of the widget. If turning off, all patches will be removed from the matplotlib axes and the widget will disconnect from all events. If turning on, the patch(es) will be added to the matplotlib axes, and the widget will connect to its default events.