Skip to main content

Fast kd-tree implementation with OpenMP-enabled queries

Project description

https://github.com/storpipfugl/pykdtree/workflows/deploy-wheels/badge.svg?branch=master

pykdtree

Objective

pykdtree is a kd-tree implementation for fast nearest neighbour search in Python. The aim is to be the fastest implementation around for common use cases (low dimensions and low number of neighbours) for both tree construction and queries.

The implementation is based on scipy.spatial.cKDTree and libANN by combining the best features from both and focus on implementation efficiency.

The interface is similar to that of scipy.spatial.cKDTree except only Euclidean distance measure is supported.

Queries are optionally multithreaded using OpenMP.

Installation

By default pykdtree is built with OpenMP enabled queries using libgomp except on OSX systems using the clang compiler (conda environments use a separate compiler).

$ cd <pykdtree_dir>
$ python setup.py install

If it fails with undefined compiler flags or you want to use another OpenMP implementation please modify setup.py at the indicated point to match your system.

Building without OpenMP support is controlled by the USE_OMP environment variable

$ cd <pykdtree_dir>
$ export USE_OMP=0
$ python setup.py install

Note evironment variables are by default not exported when using sudo so in this case do

$ USE_OMP=0 sudo -E python setup.py install

Pykdtree can also be installed with conda via the conda-forge channel:

$ conda install -c conda-forge pykdtree

Usage

The usage of pykdtree is similar to scipy.spatial.cKDTree so for now refer to its documentation

>>> from pykdtree.kdtree import KDTree
>>> kd_tree = KDTree(data_pts)
>>> dist, idx = kd_tree.query(query_pts, k=8)

The number of threads to be used in OpenMP enabled queries can be controlled with the standard OpenMP environment variable OMP_NUM_THREADS.

The leafsize argument (number of data points per leaf) for the tree creation can be used to control the memory overhead of the kd-tree. pykdtree uses a default leafsize=16. Increasing leafsize will reduce the memory overhead and construction time but increase query time.

pykdtree accepts data in double precision (numpy.float64) or single precision (numpy.float32) floating point. If data of another type is used an internal copy in double precision is made resulting in a memory overhead. If the kd-tree is constructed on single precision data the query points must be single precision as well.

Benchmarks

Comparison with scipy.spatial.cKDTree and libANN. This benchmark is on geospatial 3D data with 10053632 data points and 4276224 query points. The results are indexed relative to the construction time of scipy.spatial.cKDTree. A leafsize of 10 (scipy.spatial.cKDTree default) is used.

Note: libANN is not thread safe. In this benchmark libANN is compiled with “-O3 -funroll-loops -ffast-math -fprefetch-loop-arrays” in order to achieve optimum performance.

Operation

scipy.spatial.cKDTree

libANN

pykdtree

pykdtree 4 threads

Construction

100

304

96

96

query 1 neighbour

1267

294

223

70

Total 1 neighbour

1367

598

319

166

query 8 neighbours

2193

625

449

143

Total 8 neighbours

2293

929

545

293

Looking at the combined construction and query this gives the following performance improvement relative to scipy.spatial.cKDTree

Neighbours

libANN

pykdtree

pykdtree 4 threads

1

129%

329%

723%

8

147%

320%

682%

Note: mileage will vary with the dataset at hand and computer architecture.

Test

Run the unit tests using pytest

$ cd <pykdtree_dir>
$ pytest

Installing on AppVeyor

Pykdtree requires the “stdint.h” header file which is not available on certain versions of Windows or certain Windows compilers including those on the continuous integration platform AppVeyor. To get around this the header file(s) can be downloaded and placed in the correct “include” directory. This can be done by adding the anaconda/missing-headers.ps1 script to your repository and running it the install step of appveyor.yml:

# install missing headers that aren’t included with MSVC 2008 # https://github.com/omnia-md/conda-recipes/pull/524 - “powershell ./appveyor/missing-headers.ps1”

In addition to this, AppVeyor does not support OpenMP so this feature must be turned off by adding the following to appveyor.yml in the environment section:

environment:
global:

# Don’t build with openmp because it isn’t supported in appveyor’s compilers USE_OMP: “0”

Changelog

v1.3.5 : Build Python 3.10 wheels and other CI updates

v1.3.4 : Fix Python 3.9 wheels not being built for linux

v1.3.3 : Add compatibility to python 3.9

v1.3.2 : Change OSX installation to not use OpenMP without conda interpreter

v1.3.1 : Fix masking in the “query” method introduced in 1.3.0

v1.3.0 : Keyword argument “mask” added to “query” method. OpenMP compilation now works for MS Visual Studio compiler

v1.2.2 : Build process fixes

v1.2.1 : Fixed OpenMP thread safety issue introduced in v1.2.0

v1.2.0 : 64 and 32 bit MSVC Windows support added

v1.1.1 : Same as v1.1 release due to incorrect pypi release

v1.1 : Build process improvements. Add data attribute to kdtree class for scipy interface compatibility

v1.0 : Switched license from GPLv3 to LGPLv3

v0.3 : Avoid zipping of installed egg

v0.2 : Reduced memory footprint. Can now handle single precision data internally avoiding copy conversion to double precision. Default leafsize changed from 10 to 16 as this reduces the memory footprint and makes it a cache line multiplum (negligible if any query performance observed in benchmarks). Reduced memory allocation for leaf nodes. Applied patch for building on OS X.

v0.1 : Initial version.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pykdtree-1.3.5.tar.gz (78.9 kB view details)

Uploaded Source

Built Distributions

pykdtree-1.3.5-cp310-cp310-win_amd64.whl (50.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

pykdtree-1.3.5-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (266.4 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.12+ x86-64

pykdtree-1.3.5-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl (254.2 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.12+ i686

pykdtree-1.3.5-cp310-cp310-macosx_10_15_x86_64.whl (59.9 kB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

pykdtree-1.3.5-cp39-cp39-win_amd64.whl (50.1 kB view details)

Uploaded CPython 3.9 Windows x86-64

pykdtree-1.3.5-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (264.8 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

pykdtree-1.3.5-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl (253.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

pykdtree-1.3.5-cp39-cp39-macosx_10_15_x86_64.whl (59.9 kB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

pykdtree-1.3.5-cp38-cp38-win_amd64.whl (49.9 kB view details)

Uploaded CPython 3.8 Windows x86-64

pykdtree-1.3.5-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (289.9 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

pykdtree-1.3.5-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl (273.4 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

pykdtree-1.3.5-cp38-cp38-macosx_10_15_x86_64.whl (58.9 kB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

pykdtree-1.3.5-cp37-cp37m-win_amd64.whl (49.0 kB view details)

Uploaded CPython 3.7m Windows x86-64

pykdtree-1.3.5-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (246.1 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

pykdtree-1.3.5-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl (232.3 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

pykdtree-1.3.5-cp37-cp37m-macosx_10_15_x86_64.whl (58.5 kB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

Details for the file pykdtree-1.3.5.tar.gz.

File metadata

  • Download URL: pykdtree-1.3.5.tar.gz
  • Upload date:
  • Size: 78.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for pykdtree-1.3.5.tar.gz
Algorithm Hash digest
SHA256 7342f75e731103e653fc1f6b9fdabc2410cf92b6e7b06160804a75786c9b5747
MD5 cdc64fc3e67cca01a9ebac4bc845245f
BLAKE2b-256 bc1dbb4115ee12fc8e418d67a4643ee418536f81521762808e5e51db608781fe

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5406c1d22479adc701bce7361650f1164581a7719564013ec15b1a35ad5d354b
MD5 ffb0f242ea94ad983146c5a6924c680c
BLAKE2b-256 7e82e57d0beb00dd70726fd51a08af4e1d4db0c8f00551d3153de5812e2bb6df

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d5f5acdb9c4211a45a0795b830129b9340732f1622968e0bb906aca2b089b596
MD5 5fb2c2bd8561de4ce6253b632f1aba08
BLAKE2b-256 43f05b4865991a1cb9461be9941b26714e554c5a88bec7341d1906c7bcd8e5d7

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 a6137e8ac5bd63d178606058da46f1ac3ce7d4f6cc6eb7516400670286c34adc
MD5 5e52a74bb044dbe021704fcbe81bd155
BLAKE2b-256 3682545807ff056088d0667ce882ff1f41d1b802bdfb2e7290fa0e8754e6d814

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 9fb9a5cbe91a98d38cf1ae7a6055c374763b89f7d8307255eb23a1c2f844054e
MD5 2299e74043058e310668231f2b3c6ba3
BLAKE2b-256 36e75bb84b0eb6d50303837ed259afb58f8992e86240cf05d8e730524f2ab5df

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pykdtree-1.3.5-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 50.1 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pykdtree-1.3.5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5b2020d189fa7647039a440a46bbe9c025a7598605ddd21be6a1428d63e2474d
MD5 2f72daf815578071fbdfbdf0b1626249
BLAKE2b-256 fcae4bd55aa89111f4402402854efcf799b468b79d3b8b482f97a76a15b34431

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 9cc1582fcb7541cd215eec469d6e3a567df02581672cd1c9fd989baffc13be3f
MD5 b832a37f0e59f43e30c40d5154643062
BLAKE2b-256 f4af4bc86a2d250a2eb81c5738d71dd252931a1644f8d122a0e24bd75603b788

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 61e7aa460b4cdd1a52f9005d2b2608dc3ffce6fac4e88236d858c6253918c9f6
MD5 99b9fa2d7c0b7aaa14653ed4faae5707
BLAKE2b-256 087611ba4457aa18fc94460fb3931add6276146a7813f6b168855697ac4c6913

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 39c94bbd345729598e076d2cec2204fcad70dc7891fa573ccf376814ac33b5b5
MD5 2d07cbf1508d9835ca45d90d042146d8
BLAKE2b-256 26554179d322a0b81f48902c5021a1eb48e3b7c67d86c9170728ecad24780fbd

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pykdtree-1.3.5-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 49.9 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for pykdtree-1.3.5-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a13b46ae33284840f5394389ba6a1b0972a7288e4c4e0750c1d3a33d26ea9649
MD5 5524e494020baa4d355041122dcf8c44
BLAKE2b-256 348deaf0179439a481df55248a8a340f9140e698a9f76e1f32bb8e25748095f0

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 26931233a8995cd0c889a6b1681671c0de1c17ec530dae4d7d5936a2fe3ffaa6
MD5 7a97c613ff353fd26f548426fe45b06c
BLAKE2b-256 db645208c5adea840820a50e4f23c2569ae209a9e819d496fd31896a86f4837c

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 7cace9febd9dd08c4bb19a3eb58e2dfa7614c7a8bcdaf3af7b0c9190ad665cdd
MD5 916a7c330bf281a8251153a3a533c39f
BLAKE2b-256 c29db19007aba2edff5d846d0fc3d81ca507c8c1eb737da4f16c0836cf9d1f59

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 870a2e563a65c61ff5dd6303327a45a10d5714d759517bc529d95e6017b21c5b
MD5 35f774e2f826527ef6ef4126880827d8
BLAKE2b-256 62b93a165307f50a6493116a6e7dccbf1d16bae1e22f1ac9e222abb476b0420a

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: pykdtree-1.3.5-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 49.0 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.9

File hashes

Hashes for pykdtree-1.3.5-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 680141f23658e13d5c55b4776408371069c3a397354186f668daf3f626254c97
MD5 422c0cf9586037132b68de530c66cd1b
BLAKE2b-256 3dbdb6610af8420f2b9634f93033ab789606ebf1a071915d94c87fbc041abe52

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 a5f2cabec6d2e27fe4016829b4485bda2187f8b26d91c42f76de01f5b4c1da5a
MD5 bc3526e620d167553d7952eb1e237e2f
BLAKE2b-256 c09d3418256981d36f43ed7c5f414e2328d2ebf183c0fe6424d7a1f48770f3be

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 c0dcb867e45eeaea489154ff76e1b213ed5bcca91e5a6fe61a9d3e1d2d394355
MD5 0953f129ce1125b6651f4bb3edde975b
BLAKE2b-256 7b5a3f6b4d2dedadf514b3fe88afbe513a6eaff494ce3b5b1b4f1dc1406cf83b

See more details on using hashes here.

File details

Details for the file pykdtree-1.3.5-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pykdtree-1.3.5-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 4389aac9a354e1cf635267396574a6c9bcc9e3379777fd302ae345419b30024e
MD5 ce84b82cbe8636fc4ec395e617afff7c
BLAKE2b-256 bd918927cecf6a2e3385df6bf35a15d48504e0a71a69e124d2e1215f841e003f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page