Numba-Kdtree - a fast KDTree implementation for numba
numba-kdtree provides a fast KDTree implementation for numba CPU functions based on scipys ckdtree.
The provided KDTree class is usable from both python and numba nopython functions calling directly into C code for performance critical sections utilizing a ctypes-like interface.
Once the KDTree class is compiled by numba, it is just as fast as the original scipy version.
Note: Currently only a basic subset of the original ckdtree interface is implemented.
Installation
Using pip
pip install numba-kdtree
From source
git clone https://github.com/mortacious/numba-kdtree.git
cd numba-kdtree
python -m pip install
Usage
import numpy as np
from numba_kdtree import KDTree
data = np.random.random(3_000_000).reshape(-1, 3)
kdtree = KDTree(data, leafsize=10)
# compute the nearest neighbors of the first 100 points
distances, indices = kdtree.query(data[:100], k=30)
# compute all points in a 0.5 radius around the first 100 points and return it's sorted indices
indices = kdtree.query_radius(data[:100], r=0.5, return_sorted=True)
The KDTree can be freely passed into a numba nopython function or constructed directly within it:
import numpy as np
from numba import njit
from numba_kdtree import KDTree
def numba_function_with_kdtree(kdtree, data):
for i in range(data.shape[0]):
distances, indices = kdtree.query(data[i], k=30)
#<Use the computed neighbors
data = np.random.random(3_000_000).reshape(-1, 3)
kdtree = KDTree(data, leafsize=10)
numba_function_with_kdtree(kdtree)
import numpy as np
from numba import njit
from numba_kdtree import KDTree
def numba_function_constructing_kdtree(kdtree, data):
kdtree = KDTree(data, leafsize=10)
for i in range(data.shape[0]):
distances, indices = kdtree.query(data[i], k=30)
#<Use the computed neighbors
data = np.random.random(10_000_000).reshape(-1, 10)
numba_function_constructing_kdtree(data)
Additionally, the KDTree object can also be serialized via pickle:
import pickle
data = np.random.random(3_000_000).reshape(-1, 3)
kdtree = KDTree(data, leafsize=10)
# pass the tree through pickle
# Note: This also copies the data array preserving it's integrity
serialized_tree = pickle.dumps(kdtree)
# The copied data array is now owned by the restored tree
restored_kdtree = pickle.loads(serialized_tree)
k = 10
# query the old tree
dd, ii, nn = kdtree.query_parallel(data[:100], k=k)
# query the new tree
dd_r, ii_r, nn_r = restored_kdtree.query_parallel(data[:100], k=k)
TODOs
- Implement the full scipy
ckdtreeinterface
Metadata
Release files for numba-kdtree 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| numba_kdtree-0.6.0.tar.gz | 31.7 kB | Details |
Built distributions (wheels)
Total release size: 2.7 MB
Release files / numba_kdtree-0.6.0.tar.gz
| Download URL | numba_kdtree-0.6.0.tar.gz |
|---|---|
| Size | 31.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
53ba8bb82cc820b06b771018698468cad5a9356bc9d8bfd7df7332c1887bd251
|
|
BLAKE2b-256 checksum How to use checksums |
4862d99cb142b118144635fae85368e2e586c52e4ec9d49d38aaf2176cd0ddcf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / numba_kdtree-0.6.0-cp313-cp313-win_amd64.whl
| Download URL | numba_kdtree-0.6.0-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 46.2 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
ede5d08abaa069a4fe24059ef278118ae2afecaf23ab57fc6858cc334e81cf6c
|
|
BLAKE2b-256 checksum How to use checksums |
0248559aa77dbfbc738f3f2997f6452303a274294031f17577d19f7d1b134005
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.13
|
Release files / numba_kdtree-0.6.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | numba_kdtree-0.6.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 481.3 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
a2b31ffd14cfa5a50f3107ca6ecdac3bc1892cc3cc8208e053ff41aaf8687476
|
|
BLAKE2b-256 checksum How to use checksums |
3f052a24018fe50f4e593b6795eb8040728f013af37606326e1a7a42861dd823
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / numba_kdtree-0.6.0-cp312-cp312-win_amd64.whl
| Download URL | numba_kdtree-0.6.0-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 46.2 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
c3537d4bd35934ff9716e4161d715e2036ddaa03080a83ea22b8365a89860b3c
|
|
BLAKE2b-256 checksum How to use checksums |
a09c2f93702257f79fdb7dd3f3a55ac87bdd5a69d651df3829a835582b3ec4e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.13
|
Release files / numba_kdtree-0.6.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | numba_kdtree-0.6.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 481.3 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
93f8bb9cf5965b5b6a24e0b9f665df4837cfecf025d9e87d25b3d0b59e96a9fe
|
|
BLAKE2b-256 checksum How to use checksums |
3af357c096b81ba13cc6bdd4f2d2e827da7f1c41b25c3c53e76e97e402b611d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / numba_kdtree-0.6.0-cp311-cp311-win_amd64.whl
| Download URL | numba_kdtree-0.6.0-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 46.2 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
029f1cfa815fa5e58a8ee47f02c0926f3d54ef44ab50cb68884e5928b22a1e63
|
|
BLAKE2b-256 checksum How to use checksums |
da25902e307bff7faa716a59eda062a861173428df02989b640adbea6a838c25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.13
|
Release files / numba_kdtree-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | numba_kdtree-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 481.1 kB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
4fe41e287392a901b90c8ab341cd21ff6285571f1175a368860a966a20b425f3
|
|
BLAKE2b-256 checksum How to use checksums |
426dc2d892b8fc5f74603bb467330652983af5269c0d4d8f0b0ec57a1710a8d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / numba_kdtree-0.6.0-cp310-cp310-win_amd64.whl
| Download URL | numba_kdtree-0.6.0-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 46.2 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
49b7d13f92d859e022c06c91ab38c04c6bba1ad891aaaa1646199600ccb57913
|
|
BLAKE2b-256 checksum How to use checksums |
a20f92deda143a75f9ace20ab435dfdc22360cdc4aeb8fa892351f6370515336
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.13
|
Release files / numba_kdtree-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | numba_kdtree-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 481.1 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
b2b62bb23c0db17c908e5bf1968b71340411ef062d9e961bdc58f20fe57572c9
|
|
BLAKE2b-256 checksum How to use checksums |
7ce053604ec230c656e50971260c5ee7836082219b8abdfd99974c1d60dff9a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / numba_kdtree-0.6.0-cp39-cp39-win_amd64.whl
| Download URL | numba_kdtree-0.6.0-cp39-cp39-win_amd64.whl |
|---|---|
| Size | 46.2 kB |
| Tags | CPython 3.9 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
1f7c221f502fb06016e3a76c49a4243f6426e93f289eaba805f62e9151b331c0
|
|
BLAKE2b-256 checksum How to use checksums |
f3c8d2411728e4cc7373ff2e8fe9fd10009901f9b499b9181a4b17ccf1bffc91
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.13
|
Release files / numba_kdtree-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | numba_kdtree-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 481.1 kB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
a668a123a552bd8cbd4d31c57b149c5ba9721247736e65dde02c731164e9a455
|
|
BLAKE2b-256 checksum How to use checksums |
01849aeff3848f2ab9e65c07701ae1d48d2d377dd7f31508d770b714276c9284
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|