Rapidly skeletonize all non-zero labels in 2D and 3D numpy arrays using a TEASAR derived method. The returned list of skeletons is in the format used by cloud-volume.
On a 3.7 GHz Intel i7 processor, this package processed a 512x512x100 volume with 333 labels in under a minute. It processed a 512x512x512 volume with 2124 labels in eight to thirteen minutes (depending on whether fix_branching
is set).
Fig. 1: A Densely Labeled Volume Skeletonized with Kimimaro
Requires C++ compiler.
sudo apt-get install python3-dev g++
pip3 install numpy
pip3 install kimimaro
In the future, we may create a fully binary distribution.
Fig. 2: Memory Usage on a 512x512x512 Densely Labeled Volume
Figure 2 shows the memory usage and processessing time (a little over 15 minutes) required when Kimimaro was applied to a 512x512x512 cutout, labels, from a connectomics dataset containing 2124 connected components. The different sections of the algorithm are depicted. Grossly, the preamble runs for about a minute, skeletonization for about 14 minutes, and finalization within seconds. The peak memory usage was about 4.4 GB. The code below was used to process labels.
import kimimaro
labels = np.load(...)
skels = kimimaro.skeletonize(
labels,
teasar_params={
'scale': 4,
'const': 500,
'pdrf_exponent': 4,
'pdrf_scale': 100000,
'soma_detection_threshold': 1100,
'soma_acceptance_threshold': 3500,
'soma_invalidation_scale': 1.0,
'soma_invalidation_const': 300,
},
dust_threshold=1000,
anisotropy=(16,16,40),
fix_branching=True,
progress=True,
)
- If you only need a few labels skeletonized, pass in
object_ids
to bypass processing all the others. Ifobject_ids
contains only a single label, the masking operation will run faster. - You may save on peak memory usage by using a
cc_safety_factor
< 1, only if you are sure the connected components algorithm will generate many fewer labels than there are pixels in your image. - Larger TEASAR parameters scale and const require processing larger invalidation regions per path.
- Set
pdrf_exponent
to a small power of two (e.g. 1, 2, 4, 8, 16) for a small speedup. - If you are willing to sacrifice the improved branching behavior, you can set
fix_branching=False
for a moderate 1.1x to 1.5x speedup (assuming your TEASAR parameters and data allow branching).
The connectomics field commonly generates very large densely labeled volumes of neural tissue. Skeletons are one dimensional representations of two or three dimensional objects. They have many uses, a few of which are visualization of neurons, calculating global topological features, rapidly measuring electrical distances between objects, and imposing tree structures on neurons (useful for computation and user interfaces). There are several ways to compute skeletons and a few ways to define them [4]. After some experimentation, we found that the TEASAR [1] approach gave fairly good results. Other approaches include topological thinning ("onion peeling") and finding the centerline described by maximally inscribed spheres. Ignacio Arganda-Carreras, an alumnus of the Seung Lab, wrote a topological thinning plugin for Fiji called Skeletonize3d.
There are several implementations of TEASAR used in the connectomics field [3], however it is commonly understood that implementations of TEASAR are slow and can use tens of gigabytes of memory. Our goal to skeletonize all labels in a petavoxel scale image quickly showed clear that existing sparse implementations are impractical. While adapting a sparse approach to a cloud pipeline, we noticed that there are inefficiencies in repeated evaluation of the Euclidean Distance Transform (EDT), the repeated evaluation of the connected components algorithm, in the construction of the graph used by Dijkstra's algorithm where the edges are implied by the spatial relationships between voxels, in the memory cost, quadratic in the number of voxels, of representing a graph that is implicit in image, in the unnecessarily large data type used to represent relatively small cutouts, and in the repeated downloading of overlapping regions. We also found that the naive implmentation of TEASAR's "rolling invalidation ball" unnecessarily reevaluated large numbers of voxels in a way that could be loosely characterized as quadratic in the skeleton path length.
We further found that commodity implementations of the EDT supported only binary images and did not support anisotropic dimensions (though many papers defining those techniques included anisotropic operation). We were unable to find any available Python or C++ libraries for performing Dijkstra's shortest path on an image. Commodity implementations of connected components algorithms for images supported only binary images. Therefore, several libraries were devised to remedy these deficits (see Related Projects).
TEASAR: Tree-structure Extraction Algorithm for Accurate and Robust skeletons, a 2000 paper by M. Sato and others [1], is a member of a family of algorithms that transform two and three dimensional structures into a one dimensional "skeleton" embedded in that higher dimension. One might concieve of a skeleton as extracting a stick figure drawing of a binary image. This problem is more difficult than it might seem. There are different ways one might concieve of such a drawing. For example, a stick drawing of a banana might merely be a curved centerline and a drawing of a doughnut might be a closed loop. In our case of analyzing neurons, sometimes we want the skeleton to include spines, short protrusions from dendrites that usually have synapses attached, and sometimes we want only the characterize the run length of the main trunk of a neurite.
Additionally, data quality issues can be challenging as well. If one is skeletonizing a 2D image of a doughnut, but the angle were sufficiently declinated from the ring's orthogonal axis, would it even be possible to perform this task accurately? In a 3D case, if there are breaks or mergers in the labeling of a neuron, will the algorithm function sensibly? These issues are common in both manual and automatic image sementations.
In our problem domain of skeletonizing neurons from anisotropic voxel labels, our chosen algorithm should produce tree structures, handle fine or coarse detail extraction depending on the circumstances, handle voxel anisotropy, and be reasonably efficient in CPU and memory usage. TEASAR fufills these criteria. Notably, TEASAR doesn't guarantee the centeredness of the skeleton within the shape, but it makes an effort. The basic TEASAR algorithm is known to cut corners around turns and branch too early. A 2001 paper by members of the original TEASAR team describes a method for reducing the early branching issue on page 204, section 4.2.2. [2]
We implemented TEASAR but made several important deviations from the published algorithm in order to improve path centeredness, increase performance, and handle bulging cell somas. We opted not to implement the gradient vector field step from [2] as our implementation is already quite fast. The paper claims a reduction of 70-85% in input voxels, so it might be worth investigating.
In order to work with images that contain many labels, our general strategy is to perform as many actions as possible in such a way that all labels are treated in a single pass. Several of the component algorithms (e.g. connected components, euclidean distance transform) in our implementation can take several seconds to run per a pass, so it is important that they not be run hundreds or thousands of times. A large part of the engineering contribution of this package lies in the efficiency of these operations which reduce the runtime from the scale of hours to minutes.
Given a 3D labeled voxel array, I, with N >= 0 labels, and ordered triple describing voxel anisotropy A, our algorithm can be divided into three phases, the pramble, skeletonization, and finalization in that order.
The Preamble takes a 3D image containing N labels and efficiently generates the connected components, distance transform, and bounding boxes needed by the skeletonization phase.
- To enhance performance, if N is 0 return an empty set of skeletons.
- Label the M connected components, Icc, of I.
- To save memory, renumber the connected components in order from 1 to M. Adjust the data type of the new image to the smallest uint type that will contain M and overwrite Icc.
- Generate a mapping of the renumbered Icc to I to assign meaningful labels to skeletons later on and delete I to save memory.
- Compute E, the multi-label anisotropic Euclidean Distance Transform of Icc given A. E treats all interlabel edges as transform edges, but not the boundaries of the image. Black pixels are considered background.
- Gather a list, Lcc of unique labels from Icc and threshold which ones to process based on the number of voxels they represent to remove "dust".
- In one pass, compute the list of bounding boxes, B, corresponding to each label in Lcc.
In this phase, we extract the tree structured skeleton from each connected component label. Below, we reference variables defined in the Preamble. For clarity, we omit the soma specific processing and hold fix_branching=True
.
For each label l in Lcc and B...
- Extract Il, the cropped binary image tightly enclosing l from Icc using Bl
- Using Il and Bl, extract El from E. El is the cropped tightly enclosed EDT of l. This is much faster than recomputing the EDT for each binary image.
- Find an arbitrary foreground voxel and using that point as a source, compute the anisotropic euclidean distance field for Il. The coordinate of the maximum value is now "the root" r.
- From r, compute the euclidean distance field and save it as the distance from root field Dr.
- Compute the penalized distance from root field Pr =
pdrf_scale
* ((1 - El / max(El)) ^pdrf_exponent
) + Dr / max(Dr). - While Il contains foreground voxels:
- Identify a target coordinate, t, as the foreground voxel with maximum distance in Dr from r.
- Draw the shortest path p from r to t considering the voxel values in Pr as edge weights.
- For each vertex v in p, extend an invalidation cube of physical side length computed as
scale
* El(v) +const
and convert any foreground pixels in Il that overlap with these cubes to background pixels. - (Only if
fix_branching=True
) For each vertex coordinate v in p, set Pr(v) = 0. - Append p to a list of paths for this label.
- Using El, extract the distance to the nearest boundary each vertex in the skeleton represents.
- For each raw skeleton extracted from Il, translate the vertices by Bl to correct for the translation the cropping operation induced.
- Multiply the vertices by the anisotropy A to place them in physical space.
If soma processing is considered, we modify the root (r) search process as follows:
- If max(El) >
soma_detection_threshold
... - Fill toplogical holes in Il. Soma are large regions that often have dust from imperfect automatic labeling methods.
- Recompute El from this cleaned up image.
- If max(El) >
soma_acceptance_threshold
, divert to soma processing mode. - If in soma processing mode, continue, else go to step 3 in the algorithm above.
- Set r to the coordinate corresponding to max(El)
- Create an invalidation sphere of physical radius
soma_invalidation_scale
* max(El) +soma_invalidation_const
and erase foreground voxels from Il contained within it. This helps prevent errant paths from being drawn all over the soma. - Continue from step 4 in the above algorithm.
In the final phase, we agglomerate the disparate connected component skeletons into single skeletons and assign their labels corresponding to the input image. This step is artificially broken out compared to how intermingled its implementation is with skeletonization, but it's conceptually separate.
There were several places where we took a different approach than called for by the TEASAR authors.
The original TEASAR algorithm defines the Penalized Distance from Root voxel Field (PDRF, Pr above) as:
PDRF = 5000 * (1 - DBF / max(DBF))^16 + DAF
DBF is the Distance from Boundary Field (El above) and DAF is the Distance from Any voxel Field (Dr above).
We found the addition of the DAF tended to perturb the skeleton path from the centerline better described by the inverted DBF alone. We also found it helpful to modify the constant and exponent to tune cornering behavior. Initially, we completely stripped out the addition of the DAF from the PDRF, but this introduced a different kind of problem. The exponentiation of the PDRF caused floating point values to collapse in wide open spaces. This made the skeletons go crazy as they traced out a path described by floating point errors.
The DAF provides a very helpful gradient to follow between the root and the target voxel, we just don't want that gradient to knock the path off the centerline. Therefore, in light of the fact that the PDRF base field is very large, we add the normalized DAF which is just enough to overwhelm floating point errors and provide direction in wide tubes and bulges.
The original paper also called for selecting targets using the max(PDRF) foreground values. However, this is a bit strange since the PDRF values are dominated by boundary effects rather than a pure distance metric. Therefore, we select targets from the max(DAF) forground value.
The 2001 skeletonization paper [2] called for correcting early forking by computing a DAF using already computed path vertices as field sources. This allows Dijkstra's algorithm to trace the existing path cost free and diverge from it at a closer point to the target.
As we have strongly deemphasized the role of the DAF in dijkstra path finding, computing this field is unnecessary and we only need to set the PDRF to zero along the path of existing skeletons to achieve this effect. This saves us an expensive repeated DAF calculation per path.
However, we still incur a substantial cost for taking this approach because we had been computing a dijkstra "parental field" that recorded the shortest path to the root from every foreground voxel. We then used this saved result to rapidly compute all paths. However, as this zero weighting modification makes successive calculations dependent upon previous ones, we need to compute Dijkstra's algorithm anew for each path.
The original TEASAR paper calls for a "rolling invalidation ball" that erases foreground voxels in step 6(iii). A naive implementation of this ball is very expensive as each voxel in the path requires its own ball, and many of these voxels overlap. In some cases, it is possible that the whole volume will need to be pointlessly reevaluated for every voxel along the path from root to target. While it's possible to specical case the worst case, in the more common general case, a large amount of duplicate effort is being expended.
Therefore, we applied an algorithm using topological cues to perform the invalidation operation in linear time. For simplicity of implmentation, we substituted a cube shape instead of a sphere, but it would be possible to use one. The function name roll_invalidation_cube
is intended to evoke this awkwardness though it hasn't appeared to have been very important.
The two-pass algorithm is as follows. Given a binary image I, a skeleton S, and a set of vertices V:
- Let Bv be the set of bounding boxes that inscribe the spheres indicated by the TEASAR paper.
- Allocate a 3D signed integer array, T, the size and dimension of I representing the topology. T is initially set to all zeros.
- For each Bv:
- Set T(p) += 1 for all points p on Bv's left boundary along the x-axis.
- Set T(p) -= 1 for all points p on Bv's right boundary along the x-axis.
- Compute the bounding box Bglobal that inscribes the union of all Bv.
- A point p travels along the x-axis for each row of Bglobal starting on the YZ plane.
- Set integer coloring = 0
- At each index, coloring += T(p)
- If coloring > 0 or T(p) is non-zero (we're on the leaving edge), we are inside an invalidation cube and start converting foreground voxels into background voxels.
Several classic algorithms had to be specially tuned to make this module possible.
- edt: A single pass, multi-label anisotropy supporting euclidean distance transform implementation.
- dijkstra3d: Dijkstra's shortest-path algorithm defined on 26-connected 3D images. This avoids the time cost of edge generation and wasted memory of a graph representation.
- connected-components-3d: A connected components implementation defined on 26-connected 3D images with multiple labels.
- fastremap: Allows high speed renumbering of labels from 1 in a 3D array in order to reduce memory consumption caused by unnecessarily large 32 and 64-bit labels.
This module was originally designed to be used with CloudVolume and Igneous.
- CloudVolume: Serverless client for reading and writing petascale chunked images of neural tissue, meshes, and skeletons.
- Igneous: Distributed computation for visualizing connectomics datasets.
Some of the TEASAR modifications used in this package were first demonstrated by Alex Bae.
- skeletonization: Python implementation of modified TEASAR for sparse labels.
Alex Bae developed the precursor skeletonization package and several modifications to TEASAR that we use in this package. Alex also developed the postprocessing approach used for stitching skeletons using 50% overlap. Will Silversmith adapted these techniques for mass production, refined several basic algorithms for handling thousands of labels at once, and rewrote them into the Kimimaro package. Will added trickle DAF and zero weighted previously explored paths to the algorithm. Forrest Collman added parameter flexibility and helped tune DAF computation performance. Sven Dorkenwald and Forrest both provided helpful discussions and feedback.
- M. Sato, I. Bitter, M.A. Bender, A.E. Kaufman, and M. Nakajima. "TEASAR: Tree-structure Extraction Algorithm for Accurate and Robust Skeletons". Proc. 8th Pacific Conf. on Computer Graphics and Applications. Oct. 2000. doi: 10.1109/PCCGA.2000.883951 (link)
- I. Bitter, A.E. Kaufman, and M. Sato. "Penalized-distance volumetric skeleton algorithm". IEEE Transactions on Visualization and Computer Graphics Vol. 7, Iss. 3, Jul-Sep 2001. doi: 10.1109/2945.942688 (link)
- T. Zhao, S. Plaza. "Automatic Neuron Type Identification by Neurite Localization in the Drosophila Medulla". Sept. 2014. arXiv:1409.1892 [q-bio.NC] (link)
- A. Tagliasacchi, T. Delame, M. Spagnuolo, N. Amenta, A. Telea. "3D Skeletons: A State-of-the-Art Report". May 2016. Computer Graphics Forum. Vol. 35, Iss. 2. doi: 10.1111/cgf.12865 (link)