spacr.qt.widgets.segmentation_views

Four views of one segmentation, on one canvas, for any preview.

ONE WIDGET, TWO OWNERS. A segmentation preview has four things worth looking at: the image with the outlines on it, the label masks alone, the flow field the masks were pooled from, and the cell probability the flow was thresholded at. Mask generation’s live preview showed three of them through a dropdown; the plaque preview showed its own four through tabs, drawn by its own code. This module is the one place both can draw them from, so that a change to how a cell probability is coloured reaches both previews at once.

THE WIDGET KNOWS ARRAYS, NOT MODULES. It is handed an image, label masks, flow pictures and cell probability maps – each either one array or one per object – and draws whichever of VIEWS is chosen. Nothing in here names the Mask module, Cellpose settings or a plaque.

WHAT AN OWNER CAN CHANGE. The default renderers draw a plain overlay, categorical masks, the flows as Cellpose hands them and the probability on the magma scale. An owner with its own idea of an outline – the live preview’s colour choice and thickness – registers a renderer for that view with SegmentationViews.set_renderer() and keeps the other three. A renderer answers with a picture, with a sentence to show in the picture’s place, or with None for the widget’s own “nothing yet” sentence.

THE CANVAS IS PLUGGABLE. An owner that already has a canvas with a ruler and a hover line on it (the live preview’s _ZoomView) hands it in; otherwise a PictureCanvas is built, which fits, zooms, pans and saves on a right click like every other picture in the program.

Attributes

CELLPROB_COLORMAP

The perceptual scale the probability is drawn on.

DEFAULT_COLOURS

Colours dealt to object sets by position when an owner names none:

VIEWS

The four views, in the order every selector offers them.

VIEW_HINTS

One sentence per view, for a tooltip or a tab.

Classes

PictureCanvas

The canvas built when an owner brings none.

SegmentationViews

Overlay, masks, flows and cell probability, one at a time, on a canvas.

Functions

boundary_of(→ numpy.ndarray)

The 4-connected boundary of a label image, as a bool array.

cell_probability(→ Optional[numpy.ndarray])

Cellpose's logits as a probability, 0 to 1, over every object.

label_palette(→ numpy.ndarray)

count vivid colours, spread round the hue circle by the golden angle.

per_object(→ Arrays)

One array per object, whatever shape the owner handed over.

picture_name_of(→ str)

The file name a saved picture of view is offered under.

render_cellprob(→ Optional[numpy.ndarray])

The cell probability on the magma scale.

render_flows(→ Optional[numpy.ndarray])

The flow pictures, one per object, blended by maximum.

render_labels(→ Optional[numpy.ndarray])

Every object in its own colour on black.

render_overlay(→ Optional[numpy.ndarray])

The image with each object set's outline drawn over it.

to_qpixmap(→ PySide6.QtGui.QPixmap)

A QPixmap of rgb; a null one when there is nothing to draw.

to_rgb8(→ Optional[numpy.ndarray])

image as H x W x 3 uint8, or None for nothing drawable.

Module Contents

class spacr.qt.widgets.segmentation_views.PictureCanvas(parent=None)[source]

Bases: spacr.qt.widgets.zoom_view.ZoomableImageView

The canvas built when an owner brings none.

ZoomableImageView fits, zooms and pans; this adds what SegmentationViews asks of a canvas – the picture at its own resolution and a name for it – and the right-click save every other picture in the program has.

Parameters:

parent – parent widget; ownership only.

Build the canvas with the save menu on it.

picture() → PySide6.QtGui.QPixmap | None[source]

What is shown, at the resolution it was rendered at, or None.

picture_name() → str[source]

The name offered when the picture is saved.

set_picture_name(name: str) → None[source]

Name what the canvas shows, for the save dialog.

Parameters:

name – the suggested file name; empty means picture.

class spacr.qt.widgets.segmentation_views.SegmentationViews(parent=None, *, canvas=None)[source]

Bases: PySide6.QtWidgets.QWidget

Overlay, masks, flows and cell probability, one at a time, on a canvas.

Parameters:
  • parent – parent widget.

  • canvas – the widget the picture is drawn on. It needs set_pixmap(QPixmap) and picture(); set_picture_name is used when it is there. A PictureCanvas is built when none is given.

The widget is a stack of two pages: the canvas, and a sentence for a view that has nothing to show yet. canvas and message are both public so an owner can style or wire them.

Build the two pages and start on the waiting sentence.

arrays() → Dict[str, object][source]

What was last handed over, as the renderers see it.

is_showing_message() → bool[source]

Whether a sentence, rather than a picture, is on show.

make_selector(parent=None) → PySide6.QtWidgets.QComboBox[source]

A dropdown of the four views, kept in step with this widget.

The captions follow the language and the entries carry the English names as their data, as every value-carrying dropdown does. An owner may place it wherever its layout wants; there can be several.

Parameters:

parent – the dropdown’s parent; this widget by default.

message_text() → str[source]

The sentence on show, or "" while a picture is.

picture() → PySide6.QtGui.QPixmap | None[source]

The picture on the canvas, or None while a sentence shows.

picture_name() → str[source]

The file name a saved picture of the chosen view is offered under.

refresh() → None[source]

Redraw the chosen view from the arrays.

rendered() → numpy.ndarray | None[source]

The picture drawn last, as uint8 RGB, or None.

set_arrays(*, image=None, labels=None, flows=None, cellprob=None, refresh: bool = True) → None[source]

Hand over what there is to draw.

Parameters:
  • image – the field, any dtype, or None.

  • labels – one label image or {object: labels}.

  • flows – one flow picture or {object: picture}.

  • cellprob – one logit map or {object: map}.

  • refresh – redraw the chosen view now.

set_renderer(view: str, renderer: Renderer | None) → None[source]

Draw view with renderer instead of the default.

Parameters:
  • view – one of VIEWS.

  • renderer – called with the arrays (image, labels, flows, cellprob); answers a picture, a sentence to show instead, or None for the waiting sentence. None here restores the default renderer.

set_view(name: str, *, refresh: bool = True) → None[source]

Show name, and put every selector on it.

Parameters:
  • name – one of VIEWS.

  • refresh – redraw now; False when arrays are about to change too and one redraw at the end is wanted.

Raises:

ValueError – for a name that is not a view.

show_message(text: str) → None[source]

Show text where the picture would be.

Parameters:

text – what to say, already translated.

show_picture(picture) → None[source]

Put picture on the canvas and bring the canvas to the front.

Parameters:

picture – H x W x 3 uint8 (or anything to_rgb8() takes), or a QPixmap.

view() → str[source]

The view being shown, one of VIEWS.

views() → Tuple[str, ...][source]

The view names, in the order they are offered.

view_changed[source]

Emitted with the view’s name when the chosen view changes.

spacr.qt.widgets.segmentation_views.boundary_of(labels: numpy.ndarray) → numpy.ndarray[source]

The 4-connected boundary of a label image, as a bool array.

Parameters:

labels – H x W label image; a pixel is on the boundary when a 4-neighbour has a different label.

spacr.qt.widgets.segmentation_views.cell_probability(cellprob) → numpy.ndarray | None[source]

Cellpose’s logits as a probability, 0 to 1, over every object.

Several objects give one map: the highest probability at each pixel, so a nucleus pass and a cell pass do not hide one another.

Parameters:

cellprob – one H x W logit map or {object: map}.

Returns:

H x W float32, or None without any map.

spacr.qt.widgets.segmentation_views.label_palette(count: int, seed: int = 0) → numpy.ndarray[source]

count vivid colours, spread round the hue circle by the golden angle.

Deterministic for a given seed, so the same object keeps its colour from one redraw to the next.

Parameters:
  • count – how many colours; zero or less gives none.

  • seed – shifts the start of the sequence.

Returns:

count x 3 uint8.

spacr.qt.widgets.segmentation_views.per_object(value) → Arrays[source]

One array per object, whatever shape the owner handed over.

Parameters:

value – None, one array, or {object: array}.

Returns:

{object: array}; a bare array is keyed "" and None entries are dropped.

spacr.qt.widgets.segmentation_views.picture_name_of(view: str) → str[source]

The file name a saved picture of view is offered under.

Parameters:

view – one of VIEWS; lower-cased with spaces as underscores, and picture when empty.

spacr.qt.widgets.segmentation_views.render_cellprob(cellprob) → numpy.ndarray | None[source]

The cell probability on the magma scale.

Cellpose hands the probability back as logits; it is drawn as the probability itself on a FIXED scale, so two runs can be compared by eye and a cellprob_threshold of t sits at the colour of 1 / (1 + e^-t) in every picture.

Parameters:

cellprob – one H x W logit map or {object: map}.

Returns:

H x W x 3 uint8, or None without any map.

spacr.qt.widgets.segmentation_views.render_flows(flows) → numpy.ndarray | None[source]

The flow pictures, one per object, blended by maximum.

Parameters:

flows – one H x W x 3 picture or {object: picture}.

Returns:

H x W x 3 uint8, or None without any flows.

spacr.qt.widgets.segmentation_views.render_labels(labels) → numpy.ndarray | None[source]

Every object in its own colour on black.

Parameters:

labels – one label image or {object: labels}.

Returns:

H x W x 3 uint8, or None without any labels.

spacr.qt.widgets.segmentation_views.render_overlay(image, labels, *, colours: Mapping[str, Tuple[int, int, int]] | None = None, thickness: int = 1) → numpy.ndarray | None[source]

The image with each object set’s outline drawn over it.

Parameters:
  • image – the field, any dtype.

  • labels – one label image or {object: labels}.

  • colours – outline colour per object key; unnamed keys are dealt DEFAULT_COLOURS by position.

  • thickness – outline width in pixels, 1 to 5.

Returns:

H x W x 3 uint8, the plain image when there are no labels, or None when there is no image either.

spacr.qt.widgets.segmentation_views.to_qpixmap(rgb) → PySide6.QtGui.QPixmap[source]

A QPixmap of rgb; a null one when there is nothing to draw.

Parameters:

rgb – anything to_rgb8() accepts.

spacr.qt.widgets.segmentation_views.to_rgb8(image) → numpy.ndarray | None[source]

image as H x W x 3 uint8, or None for nothing drawable.

A uint8 picture passes through untouched. Anything else is stretched from its own minimum to its own maximum: the default for an owner that did not normalise, and no more than that, because normalisation is the owner’s business (the live preview has percentiles for it).

Parameters:

image – H x W, H x W x 1 or H x W x C in any dtype.

spacr.qt.widgets.segmentation_views.CELLPROB_COLORMAP = 'magma'[source]

The perceptual scale the probability is drawn on.

Magma runs dark to bright with no hue reversal, so “more probable” reads as “brighter” and a threshold is one colour on it; the plaque preview draws its probability on the same scale.

spacr.qt.widgets.segmentation_views.DEFAULT_COLOURS: Tuple[Tuple[int, int, int], ...] = ((32, 220, 32), (222, 82, 200), (32, 200, 220), (255, 220, 32), (240, 60, 60), (120, 120, 255),...[source]

Colours dealt to object sets by position when an owner names none: green, magenta, cyan, yellow, then round again.

spacr.qt.widgets.segmentation_views.VIEWS: Tuple[str, ...][source]

The four views, in the order every selector offers them.

spacr.qt.widgets.segmentation_views.VIEW_HINTS: Dict[str, str][source]

One sentence per view, for a tooltip or a tab.