Reading meshes and masks¶
The meshes every inference reads: a DcelData from a segmentation mask, raw mesh arrays or a mesh file.
Inputs can be: - a segmentation mask (3D uint array) ; - a multimaterial mesh: points, triangles, labels ; - a file (.rec, or opened by meshio) containing a multimaterial mesh.
Output is always a DcelData mesh. Conventions: label 0 is the exterior medium; a mask's axes are
(z, y, x) and the mesh's coordinates follow them, column 0 along axis 0; the mesh is in voxel units
unless a spacing gives a physical one, and the inferred pressures are in the tension unit divided
by that length unit.
Matthieu Perez 2024.
MaskReconstruction
dataclass
¶
MaskReconstruction(points: NDArray[float64], triangles: NDArray[int64], labels: NDArray[int64], crop_faces: NDArray[bool_] | None, spacing: tuple[float, float, float], spacing_unit: str, min_distance: int, rec_bytes: bytes, n_cells: int, n_cut_cells: int)
A mask's mesh as foambryo mesh reconstructs it, held as plain arrays and the bytes of its .rec file.
Returned by reconstruct_mask. It holds no reconstruction algorithm, so it pickles to the size of the mesh
(a front end can reconstruct in a child process and send it back), and it writes the .rec file foambryo
mesh writes, byte for byte, because it holds the bytes delaunay-watershed-3d's own export wrote.
Attributes:
| Name | Type | Description |
|---|---|---|
points |
NDArray[float64]
|
the mesh's points, in the unit of |
triangles |
NDArray[int64]
|
the triangles, as the reconstruction returned them. |
labels |
NDArray[int64]
|
the two materials of each triangle. |
crop_faces |
NDArray[bool_] | None
|
|
spacing |
tuple[float, float, float]
|
the voxel size the mask was reconstructed with, |
spacing_unit |
str
|
its unit. |
min_distance |
int
|
the reconstruction's |
rec_bytes |
bytes
|
the |
n_cells |
int
|
the labels other than the medium that the mesh holds. |
n_cut_cells |
int
|
the cells the image boundary cuts. |
mesh ¶
mesh() -> DcelData
The mesh for inference, as dcel_mesh_from_segmentation_mask(mask, spacing=...) builds it.
Built from copies of the arrays, so the reconstruction is unchanged by any use of the mesh.
write_rec ¶
write_rec(filename: str | Path) -> None
Write the .rec file, byte for byte what foambryo mesh writes for the same mask and options.
summary ¶
summary() -> str
The counts foambryo mesh prints: points, triangles, cells, and those cut by the image boundary.
load_rec_appendix ¶
load_rec_appendix(*_args: object, **_kwargs: object) -> None
Raise because the installed dw3d does not provide the .rec metadata appendix reader.
This stands in for dw3d.io.load_rec_appendix, which is only available on
dw3d >= 0.4.0.
Raises:
| Type | Description |
|---|---|
ImportError
|
always; the installed dw3d lacks |
reconstruct_mask ¶
reconstruct_mask(segmentation_mask: NDArray[uint], spacing: tuple[float, float, float], *, spacing_unit: str = 'um', min_distance: int = 3) -> MaskReconstruction
Reconstruct a mask's mesh in physical units, exactly as foambryo mesh does.
The call is delaunay-watershed-3d's entry point for masks with a voxel size,
get_default_mesh_reconstruction_algorithm(min_distance, False).construct_mesh_in_physical_units(mask, spacing,
spacing_unit=spacing_unit, axis_order="zyx"): a mask whose voxels are longer along one axis is resampled to
cubic voxels of the shortest spacing, meshed, and the mesh is scaled to the unit of spacing; cubic voxels are
meshed as they are. Every face of the image box is a crop. The mesh's numbers are those of
dcel_mesh_from_segmentation_mask(mask, min_distance, spacing=spacing); only the unit recorded in the .rec
file depends on spacing_unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation_mask
|
NDArray[uint]
|
3-D labels, axis 0 = z (see |
required |
spacing
|
tuple[float, float, float]
|
|
required |
spacing_unit
|
str
|
the unit of |
'um'
|
min_distance
|
int
|
minimum distance between the reconstruction's points, in cubic voxels of the shortest spacing. Keyword-only. Defaults to 3, delaunay-watershed-3d's. |
3
|
Returns:
| Name | Type | Description |
|---|---|---|
MaskReconstruction |
MaskReconstruction
|
the mesh's arrays, its |
Raises:
| Type | Description |
|---|---|
ValueError
|
on a spacing the resampling cannot handle, or a mask the reconstruction refuses; or if the
installed delaunay-watershed-3d has no |
dcel_mesh_from_segmentation_mask ¶
dcel_mesh_from_segmentation_mask(segmentation_mask: NDArray[uint], min_distance: int = 3, print_info: bool = False, *, physical_image_faces: Iterable[str] = (), spacing: tuple[float, float, float] | None = None) -> DcelData
Obtain a DcelData mesh from a segmentation mask using Delaunay-Watershed 3D.
The default mesh reconstruction algorithm is used (its dither seed is 42; see
foambryo.infer_tensions_over_seeds for other seeds), with its junction relocation (on by
default since dw3d 0.5.0). The mesh carries the reconstruction's crop faces: the triangles a cell
has on a face of the image box that the cell reaches in the mask, where the image cut the tissue
(see foambryo.crop_faces).
Every inference method keeps them out of its equations and reports the pressures of the cells
they cut as nan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation_mask
|
NDArray[uint]
|
Instance segmentation image: 3-D integer labels, axes (z, y, x), label 0 the medium, one label per cell. The mesh's coordinates follow the mask's axes, column 0 along axis 0. |
required |
min_distance
|
int
|
minimum distance between points of the output mesh, in voxels.
Defaults to 3, dw3d's own default (it was 5 up to foambryo 0.4.2). Pass 5 to reproduce the
0.4 spacing; at 5 or more dw3d emits |
3
|
print_info
|
bool
|
print mesh construction details. Defaults to False. |
False
|
physical_image_faces
|
Iterable[str]
|
faces of the image box where the tissue
really ends, for example on a coverslip: |
()
|
spacing
|
tuple[float, float, float] | None
|
the voxel size along the mask's
axes 0, 1, 2, in any one unit; one axis (a z step) may be longer than the other two,
which must be equal. Keyword-only. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DcelData |
DcelData
|
The mesh reconstructed from the segmentation and ready for force inference, in
voxel units without |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Warns:
| Type | Description |
|---|---|
UserWarning
|
the reconstruction's own warnings, from delaunay-watershed-3d (for example
|
dcel_mesh_from_multimaterial_mesh ¶
dcel_mesh_from_multimaterial_mesh(points: NDArray[float64], triangles: NDArray[int64], labels: NDArray[int64], *, crop_faces: NDArray[bool_] | NDArray[integer] | None = None, source_spacing: tuple[float, float, float] | None = None) -> DcelData
Obtain a DcelData mesh for force inference from raw multimaterial mesh data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
NDArray[float64]
|
Points of the multimaterial mesh, shape |
required |
triangles
|
NDArray[int64]
|
Triangles of the multimaterial mesh, as vertex indices,
shape |
required |
labels
|
NDArray[int64]
|
the two materials on the sides of each triangle, shape
|
required |
crop_faces
|
NDArray | None
|
which triangles are crop faces, as a boolean mask
or row indices (see |
None
|
source_spacing
|
tuple | None
|
the voxel size of the source mask, recorded,
not used (see |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DcelData |
DcelData
|
The mesh ready for force inference. |
Raises:
| Type | Description |
|---|---|
ValueError
|
as |
dcel_mesh_from_file ¶
dcel_mesh_from_file(filename: str | Path) -> DcelData
Obtain a DcelData mesh for force inference from a multimaterial mesh file.
It can be either a .rec (or .arec) file created from Delaunay-Watershed 3D, or a mesh
that can be opened by meshio and contains a "label1" and "label2" data.
The .vtk meshes saved by Delaunay-Watershed 3D are an example of such meshes. The mesh is
read in the length unit of the file.
Crop faces are read from the file when it records them: in a .rec file, the metadata
appendix's crop_faces = <first row> <count> (the last block of triangle rows); in a file
meshio opens, a crop_face cell array. A file that records neither is read without crop
faces; if its mesh has triangles that look like crop faces (between the medium and
a cell, lying flat on a face of the mesh's bounding box over a large patch), an
UnrecordedCropFacesWarning says so, because they are then read as contact with the medium.
The source voxel size is read from a .rec file whose appendix says the mesh is in physical
units (coordinate_frame = physical, spacing = ...), as DcelData.source_spacing; it changes
no number. Any other file gives None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
Path to the file on disk. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DcelData |
DcelData
|
The mesh ready for force inference. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the file's crop record is inconsistent with its triangles, or as
|
Warns:
| Type | Description |
|---|---|
UnrecordedCropFacesWarning
|
when the file records no crop faces but its mesh has triangles that look like them. |