Implementation of Dijkstra's Shortest Path algorithm on 3D images.
Project description
dijkstra3d
Dijkstra's Shortest Path variants for 26-connected 3D Image Volumes or 8-connected 2D images.
Perform dijkstra's shortest path algorithm on a 3D image grid. Vertices are voxels and edges are the 26 nearest neighbors (except for the edges of the image where the number of edges is reduced). For given input voxels A and B, the edge weight from A to B is B and from B to A is A. All weights must be non-negative (incl. negative zero).
What Problem does this Package Solve?
This package was developed in the course of exploring TEASAR skeletonization of 3D image volumes (now available in Kimimaro). Other commonly available packages implementing Dijkstra used matricies or object graphs as their underlying implementation. In either case, these generic graph packages necessitate explicitly creating the graph's edges and vertices, which turned out to be a significant computational cost compared with the search time. Additionally, some implementations required memory quadratic in the number of vertices (e.g. an NxN matrix for N nodes) which becomes prohibitive for large arrays. In some cases, a compressed sparse matrix representation was used to remain within memory limits.
Neither of graph construction nor quadratic memory pressure are necessary for an image analysis application. The edges between voxels (3D pixels) are regular and implicit in the rectangular structure of the image. Additionally, the cost of each edge can be stored a single time instead of 26 times in contiguous uncompressed memory regions for faster performance.
Available Dijkstra Variants
The following variants are available in 2D and 3D:
- dijkstra - Shortest path between source and target. Early termination on finding the target. Bidirectional version available.
- parental_field / query_shortest_path - Compute shortest path between source and all targets. Use query_shortest_path to make repeated queries against the result set.
- euclidean_distance_field - Given a boolean label field and a source vertex, compute the anisotropic euclidean distance from the source to all labeled vertices.
- distance_field - Given a numerical field, for each directed edge from adjacent voxels A and B, use B as the edge weight. In this fashion, compute the distance from a source point for all finite voxels.
Python Use
import dijkstra3d
import numpy as np
field = np.ones((512, 512, 512), dtype=np.int32)
source = (0,0,0)
target = (511, 511, 511)
path = dijkstra3d.dijkstra(field, source, target) # terminates early
path = dijkstra3d.dijkstra(field, source, target, bidirectional=True) # 2x memory usage, faster algorithm
print(path.shape)
parents = dijkstra3d.parental_field(field, source=(0,0,0))
path = dijkstra3d.path_from_parents(parents, target=(511, 511, 511))
print(path.shape)
dist_field = dijkstra3d.euclidean_distance_field(field, source=(0,0,0), anisotropy=(4,4,40))
dist_field = dijkstra3d.distance_field(field, source=(0,0,0))
C++ Use
#include <vector>
#include "dijkstra3d.hpp"
// 3d array represented as 1d array
float* labels = new float[512*512*512]();
// x + sx * y + sx * sy * z
int source = 0 + 512 * 5 + 512 * 512 * 3; // coordinate <0, 5, 3>
int target = 128 + 512 * 128 + 512 * 512 * 128; // coordinate <128, 128, 128>
vector<unsigned int> path = dijkstra::dijkstra3d<float>(
labels, /*sx=*/512, /*sy=*/512, /*sz=*/512,
source, target
);
vector<unsigned int> path = dijkstra::bidirectional_dijkstra3d<float>(
labels, /*sx=*/512, /*sy=*/512, /*sz=*/512,
source, target
);
uint32_t* parents = dijkstra::parental_field3d<float>(labels, /*sx=*/512, /*sy=*/512, /*sz=*/512, source);
vector<unsigned int> path = dijkstra::query_shortest_path(parents, target);
float* field = dijkstra::euclidean_distance_field3d<float>(
labels,
/*sx=*/512, /*sy=*/512, /*sz=*/512,
/*wx=*/4, /*wy=*/4, /*wz=*/40,
source);
float* field = dijkstra::distance_field3d<float>(labels, /*sx=*/512, /*sy=*/512, /*sz=*/512, source);
Python pip
Binary Installation
pip install dijkstra3d
Python pip
Source Installation
Requires a C++ compiler.
pip install numpy
pip install dijkstra3d
Python Direct Installation
Requires a C++ compiler.
git clone https://github.com/seung-lab/dijkstra3d.git
cd dijkstra3d
virtualenv -p python3 venv
source venv/bin/activate
pip install -r requirements.txt
python setup.py develop
Performance
I ran the algorithm on a field of ones from the bottom left corner to the top right corner of a 512x512x512 int8 image using a 3.7 GHz Intel i7-4920K CPU. Unidirectional search takes about 39.5 seconds (3.4 MVx/sec) with a maximum memory usage of about 1300 MB. In the unidirectional case, this test forces the algorithm to process nearly all of the volume (dijkstra aborts early when the target is found). In the bidirectional case, the volume is processed in about 11.5 seconds (11.7 MVx/sec) with a peak memory usage of about 2300 MB.
Theoretical unidirectional memory allocation breakdown: 128 MB source image, 512 MB distance field, 512 MB parents field (1152 MB). Theoretical bidirectional memory allocation breakdown: 128 MB source image, 2x 512 distance field, 2x 512 MB parental field (2176 MB).
Fig. 1: A benchmark of dijkstra.dijkstra run on a 5123 voxel field of ones from bottom left source to top right target. (black) bidirectional search (blue) unidirectional search.
import numpy as np
import time
import dijkstra3d
field = np.ones((512,512,512), order='F', dtype=np.int8)
s = time.time()
path = dijkstra3d.dijkstra(x, source=(0,0,0), target=(511, 511, 511), bidirectional=True) # or False
print(time.time() - s)
What is that pairing_heap.hpp?
Early on, I anticipated using decrease key in my heap and implemented a pairing heap, which is supposed to be an improvement on the Fibbonacci heap. However, I ended up not using decrease key, and the STL priority queue ended up being faster. If you need a pairing heap outside of boost, check it out.
References
- E. W. Dijkstra. "A Note on Two Problems in Connexion with Graphs" Numerische Mathematik 1. pp. 269-271. (1959)
- E. W. Dijkstra. "Go To Statement Considered Harmful". Communications of the ACM. Vol. 11, No. 3, pp. 147-148. (1968)
- Pohl, Ira. "Bi-directional Search", in Meltzer, Bernard; Michie, Donald (eds.), Machine Intelligence, 6, Edinburgh University Press, pp. 127–140. (1971)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for dijkstra3d-1.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a127f40e27fbefabd463aa9b7485f05b4f637eb774f530d1fe451382dd7e659 |
|
MD5 | ea0bb5d2b033c5ea2d9ef90c72ce1b9b |
|
BLAKE2b-256 | 1080c2430799dd0fc4571a84d372bf17deb6b8722e92baf9b12323de6d80a64f |
Hashes for dijkstra3d-1.2.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c24efa8ae5e4ee66acf5c87b71ac62785d7630a6862278fa3589f54acc028e4a |
|
MD5 | a18e14f6a4f2ac09be049ee6bfc779d3 |
|
BLAKE2b-256 | 9e0258cd0f9e074afa84c66b6d9c89e97cdcc1de2278f3efdf5c1e8ae6822a6f |
Hashes for dijkstra3d-1.2.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3688c1e5c3ae07b29762ab129b1f35fbee3d45006f48b06643f62f77ce04c8b2 |
|
MD5 | cdc669e02ff8ca610789b2bd16fb8a5b |
|
BLAKE2b-256 | de0e52ef11f0d8e5e5cd1cb40bd28ffa77c4d19888e54e19f933a90be55967d2 |
Hashes for dijkstra3d-1.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9403310ae83270d5361e8d7a00009a409a78f169b75f75f9a9f88ef402601dd5 |
|
MD5 | a62aa9a691e02da8574cb321fbbca05a |
|
BLAKE2b-256 | a1ec04da8a299609075c6a004ddfa3e082362d37081378592e74123e42b8fc0d |
Hashes for dijkstra3d-1.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a8a8594f90ed8eef0cf4231e260e00e2149c991f5dababa5d613f53977216e0 |
|
MD5 | 1a653bf9410d42e2334645030df39174 |
|
BLAKE2b-256 | c4e535e03933b1592bf8085cfb3165b370785183fe86b8395029cad095181743 |
Hashes for dijkstra3d-1.2.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1852f47a98007c13060a1fac0be685f0bcb9170379b260b47ff77f248fd1201 |
|
MD5 | 497fbc445c03a4eae1dd57ba73bc4696 |
|
BLAKE2b-256 | 11324159280dcc895e826c3d496ff64451cf26d826038e9889d73fe5977be769 |
Hashes for dijkstra3d-1.2.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bd4d89355d5b1f4cdb4758be76dfffecf5ff6dc48bca9a9c151b4b5f37b7ba4 |
|
MD5 | f0e84dd2dfd52b81e8968f633def69c1 |
|
BLAKE2b-256 | a4568808ceef76d4e3b657f9619e5dbd9fa8a2c523d4d36e3251ad4896864996 |
Hashes for dijkstra3d-1.2.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f910b23e771b6f56452cd9efcd6833185b5e7e00a4f9a6529e155926978bc63 |
|
MD5 | e287fa68dae104b45965fdb33a8c1aa4 |
|
BLAKE2b-256 | c58f4e5d7ce91ba9e4e1c93bbd6d4863722b5ba1ff76a28948728cd927224c30 |
Hashes for dijkstra3d-1.2.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fc4de03e2c9ba6b5e82a6e2293bcc4442660e902cba2ebcc536241743cf2882 |
|
MD5 | 69bbd5d56f8f6e6f2aec9eded406a337 |
|
BLAKE2b-256 | 9979315da37116135a9d423bfa7076233cb4649d0a48769b11ad26e681e3d987 |
Hashes for dijkstra3d-1.2.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a1430d0e25d6da797bec88c43245685827dc3dd3e06dc7e9e5b30c28566049f |
|
MD5 | e9afec17438905922406c2be8ba3ff66 |
|
BLAKE2b-256 | 9e53e1e48bc9e4b129950cb7d0d4916dec3a1c6871997c03e6630a27d548b32a |
Hashes for dijkstra3d-1.2.0-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40af72167b32e67f9766aacd85e339f20ce520e29096290084b2af0cf673bc09 |
|
MD5 | aec2dafee75df29f79335fd67b25dd22 |
|
BLAKE2b-256 | c6f7e9890d0ee43406ce1edd0345cd0b9e4281f20da50880e8ff7986250b848e |
Hashes for dijkstra3d-1.2.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 247375af3e5a14e2054480f1e02ff6bd957eb9224da2054470386f4600ea7715 |
|
MD5 | 73fdb8b0a8c758936d963ad2ab1510fd |
|
BLAKE2b-256 | db15ec1c740f7715054cdcacf4e0d048ae8d7010e3c8eab8b565621b5908a8ff |
Hashes for dijkstra3d-1.2.0-cp27-cp27m-macosx_10_14_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2ffe427bc4d8edbd80d8e8e7aff4d5a341b600ec09d3077b898cc557b87a253 |
|
MD5 | 7a9cdd09d680720b6c359a99f95b0f97 |
|
BLAKE2b-256 | 97d4cdd6a56c8307b45a147a4ede0dcf80107ba83a81eff74c14fd6ea7b83bb2 |