Debian Codegraph @

README

raw
1 .. image:: https://github.com/storpipfugl/pykdtree/actions/workflows/deploy-wheels.yml/badge.svg?branch=master
2 :target: https://github.com/storpipfugl/pykdtree/actions/workflows/deploy-wheels.yml
3
4 ========
5 pykdtree
6 ========
7
8 Objective
9 ---------
10 pykdtree is a kd-tree implementation for fast nearest neighbour search in Python.
11 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.
12
13 The implementation is based on scipy.spatial.cKDTree and libANN by combining the best features from both and focus on implementation efficiency.
14
15 The interface is similar to that of scipy.spatial.cKDTree except only Euclidean distance measure is supported.
16
17 Queries are optionally multithreaded using OpenMP.
18
19 Installation
20 ------------
21
22 Pykdtree can be installed via pip:
23
24 .. code-block:: bash
25
26 pip install pykdtree
27
28 Or, if in a conda-based environment, with conda from the conda-forge channel:
29
30 .. code-block:: bash
31
32 conda install -c conda-forge pykdtree
33
34 Note that by default these packages (the binary wheels on PyPI and the binary
35 package on conda-forge) are only built with OpenMP for linux platforms.
36 To attempt to build from source with OpenMP support do:
37
38 .. code-block:: bash
39
40 export USE_OMP="probe"
41 pip install --no-binary pykdtree pykdtree
42
43 This may not work on some systems that don't have OpenMP installed. See the below development
44 instructions for more guidance. Disabling OpenMP can be accomplished by setting `USE_OMP` to ``"0"``
45 in the above commands.
46
47 Development Installation
48 ------------------------
49
50 If you wish to contribute to pykdtree then it is a good idea to install from source
51 so you can quickly see the effects of your changes.
52 By default pykdtree is built with OpenMP enabled queries on unix-like systems.
53 On linux this is done using libgomp. On OSX systems OpenMP is provided using the
54 clang compiler (conda environments use a separate compiler).
55
56 .. code-block:: bash
57
58 $ cd <pykdtree_dir>
59 $ pip install -e .
60
61 This installs pykdtree in an "editable" mode where changes to the Python files
62 are automatically reflected when running a new python interpreter instance
63 (ex. running a python script that uses pykdtree). It does not automatically rebuild
64 or recompile the `.mako` templates and `.pyx` Cython code in pykdtree. Editing
65 these files requires running the `pykdtree/render_template.py` script and then
66 rerunning the pip command above to recompile the Cython files.
67
68 If installation fails with undefined compiler flags or you want to use another OpenMP
69 implementation you may need to modify setup.py or specify additional pip command line
70 flags to match the library locations on your system.
71
72 Building without OpenMP support is controlled by the USE_OMP environment variable
73
74 .. code-block:: bash
75
76 $ cd <pykdtree_dir>
77 $ export USE_OMP=0
78 $ pip install -e .
79
80 Note evironment variables are by default not exported when using sudo so in this case do
81
82 .. code-block:: bash
83
84 $ USE_OMP=0 sudo -E pip install -e .
85
86
87 Control OpenMP usage
88 ^^^^^^^^^^^^^^^^^^^^
89
90 The ``USE_OMP`` variable can be set to one of a couple different options. If
91 set to ``"probe"``, the installation process (``setup.py``) will attempt to
92 determine what variant of OpenMP is available based on the compiler being used,
93 the platform being run on, and the Python environment being run with. It will
94 then use the flags specified by one of the other ``USE_OMP`` modes. Note that
95 in the case of MacOS, it will also try to identify if OpenMP is available from
96 macports or homebrew and include the necessary include and library paths.
97
98 If set to ``"gcc"`` or ``"gomp"`` then compiler and linking flags will be set
99 appropriately for "GNU OpenMP" (gomp) library. If set to ``"clang"`` or
100 ``"omp"`` then the flags will be set to support the "omp" library. If set to
101 ``"msvc"`` then flags will be set for the Microsoft Visual C++ compiler's
102 OpenMP variant. For backwards compatibility the previous ``"1"`` has the same
103 behavior as ``"probe"``. As mentioned above ``"0"`` can be used to disable
104 any detection of OpenMP or attempt to compile with it.
105
106 Usage
107 -----
108
109 The usage of pykdtree is similar to scipy.spatial.cKDTree so for now refer to its documentation
110
111 >>> from pykdtree.kdtree import KDTree
112 >>> kd_tree = KDTree(data_pts)
113 >>> dist, idx = kd_tree.query(query_pts, k=8)
114
115 The number of threads to be used in OpenMP enabled queries can be controlled with the standard OpenMP environment variable OMP_NUM_THREADS.
116
117 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**.
118 Increasing **leafsize** will reduce the memory overhead and construction time but increase query time.
119
120 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.
121
122 Free-threading (no GIL) support
123 -------------------------------
124
125 Pykdtree is compiled with the necessary flags to be run from a free-threaded
126 Python interpreter. That is, it can be called without the GIL. Once a
127 ``KDTree`` is constructed all state is stored internal to the object. Querying
128 the ``KDTree`` object can be done from multiple threads simultaneously.
129 ``pykdtree`` has never acquired the GIL for low-level operations so performance
130 improvements are expected to be minimal on a free-threaded interpreter.
131
132 Any issues using ``pykdtree`` with free-threading should be filed as a GitHub
133 issue.
134
135 Multi-threading Gotchas
136 -----------------------
137
138 If using pykdtree from a multi-worker configuration, for example with the
139 ``dask`` library, take care to control the number of dask and OpenMP workers.
140 On builds of pykdtree with OpenMP support (see "Control OpenMP usage" above),
141 OpenMP will default to one worker thread per logical core on your system. Dask
142 and libraries like it also tend to default to one worker thread per logical core.
143 These libraries can conflict resulting in cases like a dask worker thread
144 using pykdtree triggering OpenMP to create its workers. This has the potential of
145 creating N * N worker threads which can slow down your system as it tries to
146 manage and schedule that many threads.
147
148 In situations like this it is recommended to limit OpenMP to 1 or 2 workers by
149 defining the environment variable:
150
151 .. code-block:: bash
152
153 OMP_NUM_THREADS=1
154
155 This essentially shifts the parallelism responsibility to the high-level dask
156 library rather than the low-level OpenMP library.
157
158 Benchmarks
159 ----------
160 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.
161
162 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.
163
164 ================== ===================== ====== ======== ==================
165 Operation scipy.spatial.cKDTree libANN pykdtree pykdtree 4 threads
166 ------------------ --------------------- ------ -------- ------------------
167
168 Construction 100 304 96 96
169
170 query 1 neighbour 1267 294 223 70
171
172 Total 1 neighbour 1367 598 319 166
173
174 query 8 neighbours 2193 625 449 143
175
176 Total 8 neighbours 2293 929 545 293
177 ================== ===================== ====== ======== ==================
178
179 Looking at the combined construction and query this gives the following performance improvement relative to scipy.spatial.cKDTree
180
181 ========== ====== ======== ==================
182 Neighbours libANN pykdtree pykdtree 4 threads
183 ---------- ------ -------- ------------------
184 1 129% 329% 723%
185
186 8 147% 320% 682%
187 ========== ====== ======== ==================
188
189 Note: mileage will vary with the dataset at hand and computer architecture.
190
191 Test
192 ----
193 Run the unit tests using pytest
194
195 .. code-block:: bash
196
197 $ cd <pykdtree_dir>
198 $ pytest
199
200 Installing on AppVeyor
201 ----------------------
202
203 Pykdtree requires the "stdint.h" header file which is not available on certain
204 versions of Windows or certain Windows compilers including those on the
205 continuous integration platform AppVeyor. To get around this the header file(s)
206 can be downloaded and placed in the correct "include" directory. This can
207 be done by adding the `anaconda/missing-headers.ps1` script to your repository
208 and running it the install step of `appveyor.yml`:
209
210 # install missing headers that aren't included with MSVC 2008
211 # https://github.com/omnia-md/conda-recipes/pull/524
212 - "powershell ./appveyor/missing-headers.ps1"
213
214 In addition to this, AppVeyor does not support OpenMP so this feature must be
215 turned off by adding the following to `appveyor.yml` in the
216 `environment` section:
217
218 environment:
219 global:
220 # Don't build with openmp because it isn't supported in appveyor's compilers
221 USE_OMP: "0"