Anisotropic masks¶
Mask preprocessing before meshing: resampling a mask with anisotropic voxels to cubic voxels.
Why this module exists¶
dw3d meshes a label image on its own voxel grid. When the voxels are longer along one axis (typically z) than
along the other two, meshing on that grid and scaling the mesh afterwards samples the surfaces coarsely along the
long axis: the junction triangles come out about k times taller (k = long spacing / short spacing) and their
normals, hence every contact angle read from them, are off. Resampling the mask to cubic voxels by shape-based
interpolation before meshing removes most of that error. Measured on masks down-sampled along z by k (each
coarse voxel takes the label covering most of it), one meshing seed, as the median over cases of each mesh's median
line-mean contact-angle error minus that of the same mask meshed at full resolution:
- on 12 simulated tissues of 90 and 165 cells voxelised at a real cell size (dw3d 0.7.0, before 0.8.0 pads thin
margins of medium on the native grid; not measured again, because some of these runs need 8 to 40 GiB of memory):
meshing the native grid costs +1.38 deg at
k = 5/3, +1.97 at 2 and +4.10 at 3; resampling first, +0.83, +0.71 and +1.17; - on foambryo's benchmark, whose cells are larger (42 cases): native +1.59, +2.25 and +5.91 deg at
k = 5/3, 2, 3(dw3d 0.8.0, which pads the thin margins of 18 of the 42 masks atk = 3); resampled +0.43, +1.45 and +1.27 (dw3d 0.7.1; the resampled route is not padded by default).
Tension errors follow. Repeating slices (nearest neighbour) is worse than shape-based interpolation at every
anisotropy above 2, and coarsening the two short axes, or passing the anisotropy to the distance transform alone,
is worse than not resampling at all. Nothing was measured above k = 3 at real cell size. foambryo's quadric angle
reading, which helps on the benchmark's larger cells, made every route worse at real cell size (dw3d 0.7.0's
meshes): keep foambryo's default reading.
Conventions¶
spacingis the voxel size along the array's axes(axis 0, axis 1, axis 2), so(dz, dy, dx)for a(z, y, x)stack, in any one unit. Exactly one axis may be coarser than the other two, which must be equal (to a relativeSPACING_RELATIVE_TOLERANCE); this is the case of a microscope stack with its z step larger than its pixel size. Other spacings raise.- Frame: the centre of voxel
jalong an axis lies atj * spacingalong that axis, so the first voxel of the input and of the output share their centre, and the output covers exactly the input's span between its first and last voxel centres. The output spacing is the short spacinghon every axis. - The output has the input's dtype and labels; no label is created.
UnmeasuredAnisotropyWarning ¶
Bases: UserWarning
The voxels are more than MEASURED_ANISOTROPY_RATIO_MAX times longer along one axis: accuracy not measured.
Issued by MeshReconstructionAlgorithm.construct_mesh_in_physical_units. The mesh is still built; filter this
category to silence it.
isotropic_spacing ¶
isotropic_spacing(spacing: tuple[float, float, float]) -> float
The cubic voxel size resample_to_isotropic produces: the shortest of the three spacings.
coarse_axis_and_factor ¶
coarse_axis_and_factor(spacing: tuple[float, float, float]) -> tuple[int, float] | None
The one coarse axis and its factor k = coarse / fine, or None when the voxels are cubic.
Raises:
| Type | Description |
|---|---|
ValueError
|
if a spacing is not positive and finite, or if the two finer axes differ (only one coarse axis is supported, the case of a z stack). |
resample_to_isotropic ¶
resample_to_isotropic(mask: NDArray, spacing: tuple[float, float, float], method: str = 'shape') -> NDArray
Resample a label mask with one coarse axis to cubic voxels of the short spacing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
NDArray
|
3-D integer label image ( |
required |
spacing
|
tuple[float, float, float]
|
voxel size along axes 0, 1, 2, so |
required |
method
|
str
|
|
'shape'
|
Returns:
| Type | Description |
|---|---|
NDArray
|
The resampled mask, same dtype, on cubic voxels of size |
NDArray
|
it has |
NDArray
|
array itself is returned**, unchanged and uncopied, so an isotropic mask goes through bitwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
for an unknown method, a mask that is not 3-D, or an unsupported spacing. |
The shape-based method (Raya & Udupa, 1990): for every label, including 0, its 2-D signed distance is computed
on every input slice across the coarse axis (positive inside, zero half-way between voxel centres) and
interpolated linearly between slice centres; each output voxel takes the label with the largest value, ties to
the smaller label. Where a label is present in one slice and absent from the next, the absent slice's value is
min(d - k, -1/2) (d the present slice's): the surface keeps closing at one voxel per voxel, but never
reaches the centre of a slice where the mask says the label is absent. Known bias: linear interpolation of a
concave distance profile underestimates it, so small convex cells shrink slightly (measured on balls of radius
9-14 voxels: -1 to -5 % of their volume at k = 3, -5 to -11 % at k = 4). Each label is computed only in its
bounding box (SHAPE_INTERPOLATION_MARGIN), so the cost grows with the cells' volume, not with the number of
labels times the volume: about 40 s and 23 GiB for a 1 866-cell tissue of 4.98e8 voxels at k = 5/3.
The measurements behind the choice are summarised in the module docstring.