napari.layers.Vectors

class napari.layers.Vectors(data, *, properties=None, edge_width=1, edge_color='red', edge_color_cycle=None, edge_colormap='viridis', edge_contrast_limits=None, length=1, name=None, metadata=None, scale=None, translate=None, rotate=None, shear=None, affine=None, opacity=0.7, blending='translucent', visible=True)[source]

Bases: napari.layers.base.base.Layer

Vectors layer renders lines onto the canvas.

Parameters
  • data ((N, 2, D) or (N1, N2, .., ND, D) array) – An (N, 2, D) array is interpreted as “coordinate-like” data and a list of N vectors with start point and projections of the vector in D dimensions. An (N1, N2, …, ND, D) array is interpreted as “image-like” data where there is a length D vector of the projections at each pixel.

  • properties (dict {str: array (N,)}, DataFrame) – Properties for each vector. Each property should be an array of length N, where N is the number of vectors.

  • edge_width (float) – Width for all vectors in pixels.

  • length (float) – Multiplicative factor on projections for length of all vectors.

  • edge_color (str) – Color of all of the vectors.

  • edge_color_cycle (np.ndarray, list) – Cycle of colors (provided as string name, RGB, or RGBA) to map to edge_color if a categorical attribute is used color the vectors.

  • edge_colormap (str, napari.utils.Colormap) – Colormap to set vector color if a continuous attribute is used to set edge_color.

  • edge_contrast_limits (None, (float, float)) – clims for mapping the property to a color map. These are the min and max value of the specified property that are mapped to 0 and 1, respectively. The default value is None. If set the none, the clims will be set to (property.min(), property.max())

  • name (str) – Name of the layer.

  • metadata (dict) – Layer metadata.

  • scale (tuple of float) – Scale factors for the layer.

  • translate (tuple of float) – Translation values for the layer.

  • rotate (float, 3-tuple of float, or n-D array.) – If a float convert into a 2D rotation matrix using that value as an angle. If 3-tuple convert into a 3D rotation matrix, using a yaw, pitch, roll convention. Otherwise assume an nD rotation. Angles are assumed to be in degrees. They can be converted from radians with np.degrees if needed.

  • shear (1-D array or n-D array) – Either a vector of upper triangular values, or an nD shear matrix with ones along the main diagonal.

  • affine (n-D array or napari.utils.transforms.Affine) – (N+1, N+1) affine transformation matrix in homogeneous coordinates. The first (N, N) entries correspond to a linear transform and the final column is a lenght N translation vector and a 1 or a napari AffineTransform object. If provided then translate, scale, rotate, and shear values are ignored.

  • opacity (float) – Opacity of the layer visual, between 0.0 and 1.0.

  • blending (str) – One of a list of preset blending modes that determines how RGB and alpha values of the layer visual get mixed. Allowed values are {‘opaque’, ‘translucent’, and ‘additive’}.

  • visible (bool) – Whether the layer visual is currently being displayed.

data

The start point and projections of N vectors in D dimensions.

Type

(N, 2, D) array

properties

Properties for each vector. Each property should be an array of length N, where N is the number of vectors.

Type

dict {str: array (N,)}, DataFrame

edge_width

Width for all vectors in pixels.

Type

float

length

Multiplicative factor on projections for length of all vectors.

Type

float

edge_color

Color of all of the vectors.

Type

str

edge_color_cycle

Cycle of colors (provided as string name, RGB, or RGBA) to map to edge_color if a categorical attribute is used color the vectors.

Type

np.ndarray, list

edge_colormap

Colormap to set vector color if a continuous attribute is used to set edge_color.

Type

str, napari.utils.Colormap

edge_contrast_limits

clims for mapping the property to a color map. These are the min and max value of the specified property that are mapped to 0 and 1, respectively. The default value is None. If set the none, the clims will be set to (property.min(), property.max())

Type

None, (float, float)

Notes

_view_data(M, 2, 2) array

The start point and projections of N vectors in 2D for vectors whose start point is in the currently viewed slice.

_view_face_color(M, 4) np.ndarray

colors for the M in view vectors

_view_indices(1, M) array

indices for the M in view vectors

_view_vertices(4M, 2) or (8M, 2) np.ndarray

the corner points for the M in view faces. Shape is (4M, 2) for 2D and (8M, 2) for 3D.

_view_faces(2M, 3) or (4M, 3) np.ndarray

indices of the _mesh_vertices that form the faces of the M in view vectors. Shape is (2M, 2) for 2D and (4M, 2) for 3D.

_property_choicesdict {str: array (N,)}

Possible values for the properties in Vectors.properties. If properties is not provided, it will be {} (empty dictionary).

_mesh_vertices(4N, 2) array

The four corner points for the mesh representation of each vector as as rectangle in the slice that it starts in.

_mesh_triangles(2N, 3) array

The integer indices of the _mesh_vertices that form the two triangles for the mesh representation of the vectors.

_max_vectors_thumbnailint

The maximum number of vectors that will ever be used to render the thumbnail. If more vectors are present then they are randomly subsampled.

Methods

Attributes

property affine

Affine transform.

Type

napari.utils.transforms.Affine

bind_key(key, func=<object object>, *, overwrite=False)

Bind a key combination to a keymap.

Parameters
  • keymap (dict of str: callable) – Keymap to modify.

  • key (str or ..) – Key combination. ... acts as a wildcard if no key combinations can be matched in the keymap (this will overwrite all key combinations further down the lookup chain).

  • func (callable, None, or ..) – Callable to bind to the key combination. If None is passed, unbind instead. ... acts as a blocker, effectively unbinding the key combination for all keymaps further down the lookup chain.

  • overwrite (bool, keyword-only, optional) – Whether to overwrite the key combination if it already exists.

Returns

unbound – Callable unbound by this operation, if any.

Return type

callable or None

Notes

Key combinations are represented in the form [modifier-]key, e.g. a, Control-c, or Control-Alt-Delete. Valid modifiers are Control, Alt, Shift, and Meta.

Letters will always be read as upper-case. Due to the native implementation of the key system, Shift pressed in certain key combinations may yield inconsistent or unexpected results. Therefore, it is not recommended to use Shift with non-letter keys. On OSX, Control is swapped with Meta such that pressing Command reads as Control.

Special keys include Shift, Control, Alt, Meta, Up, Down, Left, Right, PageUp, PageDown, Insert, Delete, Home, End, Escape, Backspace, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Space, Enter, and Tab

Functions take in only one argument: the parent that the function was bound to.

By default, all functions are assumed to work on key presses only, but can be denoted to work on release too by separating the function into two statements with the yield keyword:

@viewer.bind_key('h')
def hello_world(viewer):
    # on key press
    viewer.status = 'hello world!'

    yield

    # on key release
    viewer.status = 'goodbye world :('

To create a keymap that will block others, bind_key(..., ...)`.

property blending

Determines how RGB and alpha values get mixed.

Blending.OPAQUE

Allows for only the top layer to be visible and corresponds to depth_test=True, cull_face=False, blend=False.

Blending.TRANSLUCENT

Allows for multiple layers to be blended with different opacity and corresponds to depth_test=True, cull_face=False, blend=True, blend_func=(‘src_alpha’, ‘one_minus_src_alpha’).

Blending.ADDITIVE

Allows for multiple layers to be blended together with different colors and opacity. Useful for creating overlays. It corresponds to depth_test=False, cull_face=False, blend=True, blend_func=(‘src_alpha’, ‘one’).

Type

Blending mode

property coordinates

Cursor position in data coordinates.

property cursor

String identifying cursor displayed over canvas.

Type

str

property cursor_size

Size of cursor if custom. None yields default size.

Type

int | None

property data

start point and projections of vectors.

Type

(N, 2, D) array

Return type

ndarray

property displayed_coordinates

List of currently displayed coordinates.

displayed_coordinates is deprecated and will be removed in version 0.4.9. It should no longer be used as layers should will soon not know which dimensions are displayed. Instead you should use `[layer.coordinates[d] for d in viewer.dims.displayed]

Type

list

property edge_color

Array of RGBA edge colors (applied to all vectors)

Type

(1 x 4) np.ndarray

Return type

ndarray

property edge_color_cycle

Color cycle for edge_color. Can be a list of colors defined by name, RGB or RGBA

Type

list, np.ndarray

Return type

ndarray

property edge_color_mode

Edge color setting mode

DIRECT (default mode) allows each vector to be set arbitrarily

CYCLE allows the color to be set via a color cycle over an attribute

COLORMAP allows color to be set via a color map over an attribute

Type

str

Return type

ColorMode

property edge_colormap

Return the colormap to be applied to a property to get the edge color.

Returns

colormap – The Colormap object.

Return type

napari.utils.Colormap

property edge_contrast_limits

contrast limits for mapping the edge_color colormap property to 0 and 1

Type

None, (float, float)

Return type

Tuple[float, float]

property edge_width

Width for all vectors in pixels.

Type

float

Return type

Union[int, float]

property editable

Whether the current layer data is editable from the viewer.

Type

bool

property extent

Extent of layer in data and world coordinates.

Return type

Extent

get_status(position, *, world=False)

Status message of the data at a coordinate position.

Parameters
  • position (tuple) – Position in either data or world coordinates.

  • world (bool) – If True the position is taken to be in world coordinates and converted into data coordinates. False by default.

Returns

msg – String containing a message that can be used as a status update.

Return type

string

get_value(position, *, world=False)

Value of the data at a position.

If the layer is not visible, return None.

Parameters
  • position (tuple) – Position in either data or world coordinates.

  • world (bool) – If True the position is taken to be in world coordinates and converted into data coordinates. False by default.

Returns

value – Value of the data. If the layer is not visible return None.

Return type

tuple, None

property help

displayed in status bar bottom right.

Type

str

property interactive

Determine if canvas pan/zoom interactivity is enabled.

Type

bool

property length

Multiplicative factor for length of all vectors.

Type

float

Return type

Union[int, float]

property loaded

Return True if this layer is fully loaded in memory.

This base class says that layers are permanently in the loaded state. Derived classes that do asynchronous loading can override this.

Return type

bool

property name

Unique name of the layer.

Type

str

property ndim

Number of dimensions in the data.

Type

int

property opacity

Opacity value between 0.0 and 1.0.

Type

float

property position

Cursor position in world slice coordinates.

Type

tuple

property properties

Annotations for each point

Type

dict {str

Type

array (N,)}, DataFrame

Return type

Dict[str, ndarray]

refresh(event=None)

Refresh all layer data based on current view slice.

refresh_colors(update_color_mapping=False)[source]

Calculate and update edge colors if using a cycle or color map

Parameters

update_color_mapping (bool) – If set to True, the function will recalculate the color cycle map or colormap (whichever is being used). If set to False, the function will use the current color cycle map or color map. For example, if you are adding/modifying vectors and want them to be colored with the same mapping as the other vectors (i.e., the new vectors shouldn’t affect the color cycle map or colormap), set update_color_mapping=False. Default value is False.

property rotate

Rotation matrix in world coordinates.

Type

array

save(path, plugin=None)

Save this layer to path with default (or specified) plugin.

Parameters
  • path (str) – A filepath, directory, or URL to open. Extensions may be used to specify output format (provided a plugin is available for the requested format).

  • plugin (str, optional) – Name of the plugin to use for saving. If None then all plugins corresponding to appropriate hook specification will be looped through to find the first one that can save the data.

Returns

File paths of any files that were written.

Return type

list of str

property scale

Anisotropy factors to scale data into world coordinates.

Type

list

property selected

Whether this layer is selected or not.

Type

bool

property shear

Sheer matrix in world coordinates.

Type

array

property thumbnail

Integer array of thumbnail for the layer

Type

array

property translate

Factors to shift the layer by in units of world coordinates.

Type

list

property translate_grid

Factors to shift the layer by.

Type

list

property visible

Whether the visual is currently being displayed.

Type

bool

world_to_data(position)

Convert from world coordinates to data coordinates.

Parameters

position (tuple, list, 1D array) – Position in world coorindates. If longer then the number of dimensions of the layer, the later dimensions will be used.

Returns

Position in data coordinates.

Return type

tuple