napari.components package

Submodules

napari.components.dims module

class napari.components.dims.Dims(ndim=None, *, ndisplay=2, order=None, axis_labels=None)[source]

Bases: object

Dimensions object modeling slicing and displaying.

Parameters
  • ndim (int, optional) – Number of dimensions

  • ndisplay (int, optional) – Number of displayed dimensions.

  • order (list of int, optional) – Order in which dimensions are displayed where the last two or last three dimensions correspond to row x column or plane x row x column if ndisplay is 2 or 3.

  • axis_labels (list of str, optional) – Dimension names

events

Event emitter group

Type

EmitterGroup

range

List of tuples (min, max, step), one for each dimension

Type

list of 3-tuple

point

List of floats setting the current value of the range slider when in POINT mode, one for each dimension

Type

list of float

interval

List of tuples (min, max) setting the current selection of the range slider when in INTERVAL mode, one for each dimension

Type

list of 2-tuple

mode

List of DimsMode, one for each dimension

Type

list of DimsMode

clip

Flag if to clip indices based on range. Needed for image-like layers, but prevents shape-like layers from adding new shapes outside their range.

Type

bool

ndim

Number of dimensions.

Type

int

indices

Tuple of slice objects for slicing arrays on each dimension, one for each dimension

Type

tuple of slice object

displayed

List of dimensions that are displayed.

Type

tuple

not_displayed

List of dimensions that are not displayed.

Type

tuple

displayed_order

Order of only displayed dimensions.

Type

tuple

property axis_labels

List of labels for each axis.

property displayed

Dimensions that are displayed.

Type

Tuple

property displayed_order

Order of only displayed dimensions.

Type

Tuple

property indices

Tuple of slice objects for slicing arrays on each dimension.

property interval

(min, max) of each dimension if in INTERVAL mode.

Type

List of 2-tuple

property mode

List of DimsMode, one for each dimension.

Type

List of DimsMode

property ndim

Returns the number of dimensions.

Returns

ndim – Number of dimensions

Return type

int

property ndisplay

Number of displayed dimensions.

Type

Int

property not_displayed

Dimensions that are not displayed.

Type

Tuple

property order

Display order of dimensions.

Type

List of int

property point

value of each dimension if in POINT mode.

Type

List of int

property range

(min, max, step size) of each dimension.

Type

List of 3-tuple

reset()[source]

Reset dims values to initial states.

set_axis_label(axis: int, label: str)[source]

Sets a new axis label for the given axis.

Parameters
  • axis (int) – Dimension index

  • label (str) – Given label

set_interval(axis: int, interval: Sequence[Union[int, float]])[source]

Sets the interval used for cropping and projecting this dimension.

Parameters
  • axis (int) – Dimension index.

  • interval (tuple) – INTERVAL specified with (min, max).

set_mode(axis: int, mode: napari.components.dims_constants.DimsMode)[source]

Sets the mode: POINT or INTERVAL.

Parameters
  • axis (int) – Dimension index.

  • mode (POINT or INTERVAL) – Whether dimension is in the POINT or INTERVAL mode.

set_point(axis: int, value: Union[int, float])[source]

Sets the point at which to slice this dimension.

Parameters
  • axis (int) – Dimension index.

  • value (int or float) – Value of the point.

set_range(axis: int, _range: Sequence[Union[int, float]])[source]

Sets the range (min, max, step) for a given dimension.

Parameters
  • axis (int) – Dimension index.

  • range (tuple) – Range specified as (min, max, step).

napari.components.dims_constants module

class napari.components.dims_constants.DimsMode[source]

Bases: enum.Enum

An enumeration.

INTERVAL = 1
POINT = 0

napari.components.layerlist module

class napari.components.layerlist.LayerList[source]

Bases: napari.util.list._model.ListModel

List-like layer collection with built-in reordering and callback hooks.

events
Event hooks:
  • added(item, index): whenever an item is added

  • removed(item): whenever an item is removed

  • reordered(): whenever the list is reordered

Type

vispy.util.event.EmitterGroup

move_selected(index, insert)[source]

Reorder list by moving the item at index and inserting it at the insert index. If additional items are selected these will get inserted at the insert index too. This allows for rearranging the list based on dragging and dropping a selection of items, where index is the index of the primary item being dragged, and insert is the index of the drop location, and the selection indicates if multiple items are being dragged. If the moved layer is not selected select it.

Parameters
  • index (int) – Index of primary item to be moved

  • insert (int) – Index that item(s) will be inserted at

remove_selected()[source]

Removes selected items from list.

select_all()[source]

Selects all layers.

select_next(shift=False)[source]

Selects next item from list.

select_previous(shift=False)[source]

Selects previous item from list.

unselect_all(ignore=None)[source]

Unselects all layers expect any specified in ignore.

Parameters

ignore (Layer | None) – Layer that should not be unselected if specified.

napari.components.viewer_model module

class napari.components.viewer_model.ViewerModel(title='napari', ndisplay=2, order=None, axis_labels=None)[source]

Bases: napari.util.keybindings.KeymapMixin

Viewer containing the rendered scene, layers, and controlling elements including dimension sliders, and control bars for color limits.

Parameters
  • title (string) – The title of the viewer window.

  • ndisplay ({2, 3}) – Number of displayed dimensions.

  • order (tuple of int) – Order in which dimensions are displayed where the last two or last three dimensions correspond to row x column or plane x row x column if ndisplay is 2 or 3.

  • = list of str (axis_labels) – Dimension names.

window

Parent window.

Type

Window

layers

List of contained layers.

Type

LayerList

dims

Contains axes, indices, dimensions and sliders.

Type

Dimensions

themes

Preset color palettes.

Type

dict of str: dict of str: str

property active_layer

index of active_layer

Type

int

add_image(data=None, *, channel_axis=None, rgb=None, is_pyramid=None, colormap=None, contrast_limits=None, gamma=1, interpolation='nearest', rendering='mip', name=None, metadata=None, scale=None, translate=None, opacity=1, blending=None, visible=True, path=None)[source]

Add an image layer to the layers list.

Parameters
  • data (array or list of array) – Image data. Can be N dimensional. If the last dimension has length 3 or 4 can be interpreted as RGB or RGBA if rgb is True. If a list and arrays are decreasing in shape then the data is treated as an image pyramid.

  • channel_axis (int, optional) – Axis to expand image along.

  • rgb (bool) – Whether the image is rgb RGB or RGBA. If not specified by user and the last dimension of the data has length 3 or 4 it will be set as True. If False the image is interpreted as a luminance image.

  • is_pyramid (bool) – Whether the data is an image pyramid or not. Pyramid data is represented by a list of array like image data. If not specified by the user and if the data is a list of arrays that decrease in shape then it will be taken to be a pyramid. The first image in the list should be the largest.

  • colormap (str, vispy.Color.Colormap, tuple, dict, list) – Colormaps to use for luminance images. If a string must be the name of a supported colormap from vispy or matplotlib. If a tuple the first value must be a string to assign as a name to a colormap and the second item must be a Colormap. If a dict the key must be a string to assign as a name to a colormap and the value must be a Colormap. If a list then must be same length as the axis that is being expanded as channels, and each colormap is applied to each new image layer.

  • contrast_limits (list (2,)) – Color limits to be used for determining the colormap bounds for luminance images. If not passed is calculated as the min and max of the image. If list of lists then must be same length as the axis that is being expanded and then each colormap is applied to each image.

  • gamma (list, float) – Gamma correction for determining colormap linearity. Defaults to 1. If a list then must be same length as the axis that is being expanded and then each entry in the list is applied to each image.

  • interpolation (str) – Interpolation mode used by vispy. Must be one of our supported modes.

  • 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.

  • 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.

  • path (str or list of str) – Path or list of paths to image data. Paths can be passed as strings or pathlib.Path instances.

Returns

layer – The newly-created image layer or list of image layers.

Return type

napari.layers.Image or list

add_labels(data, *, is_pyramid=None, num_colors=50, seed=0.5, n_dimensional=False, name=None, metadata=None, scale=None, translate=None, opacity=0.7, blending='translucent', visible=True)[source]

Add a labels (or segmentation) layer to the layers list.

An image-like layer where every pixel contains an integer ID corresponding to the region it belongs to.

Parameters
  • data (array or list of array) – Labels data as an array or pyramid.

  • is_pyramid (bool) – Whether the data is an image pyramid or not. Pyramid data is represented by a list of array like image data. If not specified by the user and if the data is a list of arrays that decrease in shape then it will be taken to be a pyramid. The first image in the list should be the largest.

  • num_colors (int) – Number of unique colors to use in colormap.

  • seed (float) – Seed for colormap random generator.

  • n_dimensional (bool) – If True, paint and fill edit labels across all dimensions.

  • 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.

  • 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.

Returns

layer – The newly-created labels layer.

Return type

napari.layers.Labels

add_layer(layer)[source]

Add a layer to the viewer.

Parameters

layer (Layer) – Layer to add.

add_points(data=None, *, symbol='o', size=10, edge_width=1, edge_color='black', face_color='white', n_dimensional=False, name=None, metadata=None, scale=None, translate=None, opacity=1, blending='translucent', visible=True)[source]

Add a points layer to the layers list.

Parameters
  • data (array (N, D)) – Coordinates for N points in D dimensions.

  • symbol (str) – Symbol to be used for the point markers. Must be one of the following: arrow, clobber, cross, diamond, disc, hbar, ring, square, star, tailed_arrow, triangle_down, triangle_up, vbar, x.

  • size (float, array) – Size of the point marker. If given as a scalar, all points are made the same size. If given as an array, size must be the same broadcastable to the same shape as the data.

  • edge_width (float) – Width of the symbol edge in pixels.

  • edge_color (str) – Color of the point marker border.

  • face_color (str) – Color of the point marker body.

  • n_dimensional (bool) – If True, renders points not just in central plane but also in all n-dimensions according to specified point marker size.

  • 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.

  • 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.

Returns

layer – The newly-created points layer.

Return type

napari.layers.Points

Notes

See vispy’s marker visual docs for more details: http://api.vispy.org/en/latest/visuals.html#vispy.visuals.MarkersVisual

add_shapes(data=None, *, shape_type='rectangle', edge_width=1, edge_color='black', face_color='white', z_index=0, name=None, metadata=None, scale=None, translate=None, opacity=0.7, blending='translucent', visible=True)[source]

Add a shapes layer to the layers list.

Parameters
  • data (list or array) – List of shape data, where each element is an (N, D) array of the N vertices of a shape in D dimensions. Can be an 3-dimensional array if each shape has the same number of vertices.

  • shape_type (string or list) – String of shape shape_type, must be one of “{‘line’, ‘rectangle’, ‘ellipse’, ‘path’, ‘polygon’}”. If a list is supplied it must be the same length as the length of data and each element will be applied to each shape otherwise the same value will be used for all shapes.

  • edge_width (float or list) – Thickness of lines and edges. If a list is supplied it must be the same length as the length of data and each element will be applied to each shape otherwise the same value will be used for all shapes.

  • edge_color (str or list) – If string can be any color name recognized by vispy or hex value if starting with #. If array-like must be 1-dimensional array with 3 or 4 elements. If a list is supplied it must be the same length as the length of data and each element will be applied to each shape otherwise the same value will be used for all shapes.

  • face_color (str or list) – If string can be any color name recognized by vispy or hex value if starting with #. If array-like must be 1-dimensional array with 3 or 4 elements. If a list is supplied it must be the same length as the length of data and each element will be applied to each shape otherwise the same value will be used for all shapes.

  • z_index (int or list) – Specifier of z order priority. Shapes with higher z order are displayed ontop of others. If a list is supplied it must be the same length as the length of data and each element will be applied to each shape otherwise the same value will be used for all shapes.

  • 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.

  • opacity (float or list) – 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.

Returns

layer – The newly-created shapes layer.

Return type

napari.layers.Shapes

add_surface(data, *, colormap='gray', contrast_limits=None, gamma=1, name=None, metadata=None, scale=None, translate=None, opacity=1, blending='translucent', visible=True)[source]

Add a surface layer to the layers list.

Parameters
  • data (3-tuple of array) – The first element of the tuple is an (N, D) array of vertices of mesh triangles. The second is an (M, 3) array of int of indices of the mesh triangles. The third element is the (N, ) array of values used to color vertices.

  • colormap (str, vispy.Color.Colormap, tuple, dict) – Colormap to use for luminance images. If a string must be the name of a supported colormap from vispy or matplotlib. If a tuple the first value must be a string to assign as a name to a colormap and the second item must be a Colormap. If a dict the key must be a string to assign as a name to a colormap and the value must be a Colormap.

  • contrast_limits (list (2,)) – Color limits to be used for determining the colormap bounds for luminance images. If not passed is calculated as the min and max of the image.

  • gamma (float) – Gamma correction for determining colormap linearity. Defaults to 1.

  • 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.

  • 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.

Returns

layer – The newly-created surface layer.

Return type

napari.layers.Surface

add_vectors(data, *, edge_width=1, edge_color='red', length=1, name=None, metadata=None, scale=None, translate=None, opacity=0.7, blending='translucent', visible=True)[source]

Add a vectors layer to the layers list.

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.

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

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

  • edge_color (str) – Edge color of all the vectors.

  • 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.

  • 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.

Returns

layer – The newly-created vectors layer.

Return type

napari.layers.Vectors

class_keymap = {}
property cursor

String identifying cursor displayed over canvas.

Type

string

property cursor_size

Size of cursor if custom. None is yields default size

Type

int | None

property grid_size

Size of grid

Type

tuple

grid_view(n_row=None, n_column=None, stride=1)[source]

Arrange the current layers is a 2D grid.

Default behaviour is to make a square 2D grid.

Parameters
  • n_row (int, optional) – Number of rows in the grid.

  • n_column (int, optional) – Number of column in the grid.

  • stride (int, optional) – Number of layers to place in each grid square before moving on to the next square. The default ordering is to place the most visible layer in the top left corner of the grid. A negative stride will cause the order in which the layers are placed in the grid to be reversed.

property help

String that can be displayed to the user in the status bar with helpful usage tips.

Type

string

property interactive

Determines if canvas pan/zoom interactivity is enabled or not.

Type

bool

property palette

Color palette with which to style the viewer.

Type

dict of str

Type

str

reset_view()[source]

Resets the camera’s view using event.rect a 4-tuple of the x, y corner position followed by width and height of the camera

stack_view()[source]

Arrange the current layers is a stack.

property status

Status string

Type

string

property theme

Preset color palette.

Type

string or None

themes = {'dark': {'background': 'rgb(38, 41, 48)', 'canvas': 'black', 'console': 'rgb(0, 0, 0)', 'folder': 'dark', 'foreground': 'rgb(65, 72, 81)', 'highlight': 'rgb(106, 115, 128)', 'icon': 'rgb(209, 210, 212)', 'primary': 'rgb(90, 98, 108)', 'secondary': 'rgb(134, 142, 147)', 'syntax_style': 'native', 'text': 'rgb(240, 241, 242)'}, 'light': {'background': 'rgb(239, 235, 233)', 'canvas': 'white', 'console': 'rgb(255, 255, 255)', 'folder': 'light', 'foreground': 'rgb(214, 208, 206)', 'highlight': 'rgb(163, 158, 156)', 'icon': 'rgb(107, 105, 103)', 'primary': 'rgb(188, 184, 181)', 'secondary': 'rgb(150, 146, 144)', 'syntax_style': 'default', 'text': 'rgb(59, 58, 57)'}}
property title

String that is displayed in window title.

Type

string

to_svg(file=None, view_box=None)[source]

Convert the viewer state to an SVG. Non visible layers will be ignored.

Parameters
  • file (path-like object, optional) – An object representing a file system path. A path-like object is either a str or bytes object representing a path, or an object implementing the os.PathLike protocol. If passed the svg will be written to this file

  • view_box (4-tuple, optional) – View box of SVG canvas to be generated specified as min-x, min-y, width and height. If not specified, calculated from the last two dimensions of the view.

Returns

svg – SVG representation of the currently viewed layers.

Return type

string

Module contents

napari.components provides the public-facing models for widgets and other utilities that the user will be able to programmatically interact with.

Classes

Dims

Current indices along each data dimension, together with which dimensions are being displayed, projected, sliced…

LayerList

List of layers currently present in the viewer.

ViewerModel

Data viewer displaying the currently rendered scene and layer-related controls.