Skip to content

The Polyscope viewer

Optional Polyscope viewer for a reconstructed mesh, for inspection and debugging.

Install it with the viewing extra (pip install "delaunay-watershed-3d[viewing]"), which brings Polyscope and matplotlib. Calling the viewer opens a Polyscope window.

The window draws the tissue, not its mirror image. The mesh's points are in the mask's index order, (z, y, x) for a microscope stack, and Polyscope reads the columns it is handed as its own (x, y, z). The viewer hands it the columns reversed, (x, y, z) = columns (2, 1, 0), the mask's column, row and slice, and each triangle (a, b, c) as (a, c, b), so that it still faces the same side (_drawn_points, _drawn_triangles). Exchanging two axes is a reflection, which reverses every triangle's orientation; reversing the triangles undoes that. Up to 0.7.0 the columns were handed as they are: every view was the mirror image of the tissue. Only what is drawn is reordered; the stored mesh and the files written (.rec, .vtk) keep the mask's order.

A 2-D mesh (dw2d's (points (n, 2), segments (m, 2), labels (m, 2)), or the algorithm after a 2-D mask) is drawn in a 2-D window by the same function. Each cell's boundary, the 2-D counterpart of the closed surface the 3-D view draws for it, is one curve network in the cell's colour, the colour the 3-D view gives the same label (the same generator, seeded as there). The cells are not filled: no polygon triangulation is needed, and none is done. Polyscope's (x, y) is the mesh's (column, -row) (_drawn_planar_points): a quarter-turn rotation, not a reflection, so the window shows the image as napari and matplotlib's imshow do, row 0 at the top and column 0 at the left. The camera looks straight down on the plane, with planar navigation (pan and zoom, no rotation), restored to the previous style when the function returns. An image or a mask given as image is drawn under the curves, one texel per pixel, pixel (row, column) centred on the mesh point (row, column) (times the pixel size of a mesh built in physical units).

Matthieu Perez, 2024.

plot_cells_polyscope

plot_cells_polyscope(reconstruct: MeshReconstructionAlgorithm | tuple[NDArray, NDArray, NDArray], anisotropy_factor: float = 1.0, clean_before: bool = True, clean_after: bool = True, transparency: bool = False, show: bool = True, view: str = 'Simple', scattering_coeff: float = 0.5, *, image: NDArray | None = None) -> None

Plot separated cells of the mesh in a polyscope viewer, in 3D or in 2D: the mesh decides.

Each cell (every label but the medium 0) is shown as its own surface, in a random colour (fixed seed). Polyscope's x, y, z are the mask's column, row and slice (the points' columns 2, 1, 0): the tissue is drawn as it is, not its mirror image (see the module's docstring).

A 2-D mesh (points (n, 2), (row, column)) opens a 2-D window instead: each cell's boundary as a curve network in the colour the 3-D view gives that label, (x, y) = (column, -row) (rows downward, as in napari and matplotlib), a camera straight down, image under the curves when given (the module's docstring).

Parameters:

Name Type Description Default
reconstruct MeshReconstructionAlgorithm | tuple

the algorithm whose last constructed mesh to show (dw3d's, after a 3-D or a 2-D mask, or dw2d's), or a mesh as three arrays: (points, triangles, labels) in 3D, dw2d's (points, segments, labels) in 2D.

required
anisotropy_factor float

Multiply column 0 of the points, the mask's axis 0 (z for a (z, y, x) stack), by this factor, for display only: a scaled copy is shown, and the reconstruction's own mesh is left unchanged. Defaults to 1.0. To build a mesh in physical units from a mask whose voxels are longer along z, use MeshReconstructionAlgorithm.construct_mesh_in_physical_units instead. 3D only: 2-D meshes have square pixels, and any other value raises ValueError for a 2-D mesh.

1.0
clean_before bool

Clean viewer before the function. Defaults to True.

True
clean_after bool

Clean viewer after the function. Defaults to True.

True
transparency bool

Allow transparency in viewer. Defaults to False.

False
show bool

Show viewer. Defaults to True.

True
view str

"Simple" or "Scattered", scatter different cells or not. Defaults to "Simple".

'Simple'
scattering_coeff float

If "Scattered" mode, Coefficient of scattering. Defaults to 0.5.

0.5
image NDArray | None

2D only: an image drawn under the mesh, on the mask's pixel grid: (rows, columns) grey (scaled between its 0.5 % and 99.5 % quantiles; a label mask is drawn this way too) or (rows, columns, 3 or 4) RGB(A) (floats in [0, 1] or integers in [0, 255]). Not drawn in the scattered view. Defaults to None. Keyword-only.

None

Raises:

Type Description
ImportError

if Polyscope is not installed (the viewing extra).

ValueError

for an option that has no meaning in the mesh's dimension (anisotropy_factor other than 1 with a 2-D mesh, image with a 3-D mesh), an image that is neither grey nor RGB(A), an unknown view, or a mesh whose points are neither (n, 3) nor (n, 2).