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¶
The perceptual scale the probability is drawn on. |
|
Colours dealt to object sets by position when an owner names none: |
|
The four views, in the order every selector offers them. |
|
One sentence per view, for a tooltip or a tab. |
Classes¶
The canvas built when an owner brings none. |
|
Overlay, masks, flows and cell probability, one at a time, on a canvas. |
Functions¶
|
The 4-connected boundary of a label image, as a bool array. |
|
Cellpose's logits as a probability, 0 to 1, over every object. |
|
|
|
One array per object, whatever shape the owner handed over. |
|
The file name a saved picture of |
|
The cell probability on the magma scale. |
|
The flow pictures, one per object, blended by maximum. |
|
Every object in its own colour on black. |
|
The image with each object set's outline drawn over it. |
|
A |
|
|
Module Contents¶
- class spacr.qt.widgets.segmentation_views.PictureCanvas(parent=None)[source]¶
Bases:
spacr.qt.widgets.zoom_view.ZoomableImageViewThe canvas built when an owner brings none.
ZoomableImageViewfits, zooms and pans; this adds whatSegmentationViewsasks 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.
- class spacr.qt.widgets.segmentation_views.SegmentationViews(parent=None, *, canvas=None)[source]¶
Bases:
PySide6.QtWidgets.QWidgetOverlay, 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)andpicture();set_picture_nameis used when it is there. APictureCanvasis 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.
canvasandmessageare both public so an owner can style or wire them.Build the two pages and start on the waiting sentence.
- 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.
- picture() PySide6.QtGui.QPixmap | None[source]¶
The picture on the canvas, or
Nonewhile a sentence shows.
- rendered() numpy.ndarray | None[source]¶
The picture drawn last, as
uint8RGB, orNone.
- 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
viewwithrendererinstead 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, orNonefor the waiting sentence.Nonehere 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;
Falsewhen 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
textwhere the picture would be.- Parameters:
text – what to say, already translated.
- 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 Wlabel 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 Wlogit map or{object: map}.- Returns:
H x Wfloat32, orNonewithout any map.
- spacr.qt.widgets.segmentation_views.label_palette(count: int, seed: int = 0) numpy.ndarray[source]¶
countvivid 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 3uint8.
- 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""andNoneentries are dropped.
- spacr.qt.widgets.segmentation_views.picture_name_of(view: str) str[source]¶
The file name a saved picture of
viewis offered under.- Parameters:
view – one of
VIEWS; lower-cased with spaces as underscores, andpicturewhen 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_thresholdoftsits at the colour of1 / (1 + e^-t)in every picture.- Parameters:
cellprob – one
H x Wlogit map or{object: map}.- Returns:
H x W x 3uint8, orNonewithout 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 3picture or{object: picture}.- Returns:
H x W x 3uint8, orNonewithout 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 3uint8, orNonewithout 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_COLOURSby position.thickness – outline width in pixels, 1 to 5.
- Returns:
H x W x 3uint8, the plain image when there are no labels, orNonewhen there is no image either.
- spacr.qt.widgets.segmentation_views.to_qpixmap(rgb) PySide6.QtGui.QPixmap[source]¶
A
QPixmapofrgb; 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]¶
imageasH x W x 3uint8, orNonefor nothing drawable.A
uint8picture 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 1orH x W x Cin 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.