Despace
Introduction
Hierarchical spatial decomposition has been shown to be super useful in many geometric problems like geological analysis and N-body simulations. The general goal is to index multi-dimensional data points while keeping spatially proximal points as close as possible. Traditionally, k-D trees and space filling curves (Hilbert/Peano and Morton curves) are constructed to divide the k dimensional space first. For k-D trees, every data point will be assigned to the tree leaves and leaves close to each other in the tree structure are also spatially close to each other. For space filling curves, each data point in the space is assigned to the nearest space filling curve vertex. The whole data set can therefore be sorted along the space filling curve direction.
Constructing and traversing the k-D trees and k-dimensional space filling curves can be annoying in some certain cases. Here I am instead trying to use a simple sorting scheme to index or sort k dimensional data so that close data points are stored closely in the final data structure.
Note: 'dimension' here means physical dimensions like in 2D and 3D cartesian coordinates. It's different from the definition of 'dimension' in numpy.ndarray which refers to number of axes to describe the data layout. Mostly we can think of the 'dimension' in this repo as the number of columns in a 2d numpy array (matrix).
Algorithm
- Sort the entire data points in the first dimension.
- Divide the data into two halves and sort each half in the second dimension.
- For each sorted half data, divide it into two halves and sort it in the third dimension.
- Continue the above procedure by circulating the dimension indices until dividing into single data points.
- Reconstruct the whole data set with the above pseudo-sorted data
Install
pip install despace
Current status
For a 8X8 grid, we get a Morton curve if we init the sort_type as Morton (the default):
We get a Hilbert curve if we init the sort_type as Hilbert:
k-D cases are primarily done for the Morton type. Visualization for 2D and 3D cases is supported. Below shows plots of N=10000 random points in a 2D square space and a 3D cube. The data points are blue-->red colored according to their indices from 0 to 9999.
For the Hilbert type, only 1D and 2D cases have been implemented.
Try it out
- You can play with the python script
generate_random.pyin theexamplesfolder like changing the number of data points
python generate_random.py 50 2 # use 50 data points for 2D.
And we get a figure like below:
- Use in your code
from despace import SortND
import numpy as np
s = SortND()
s(np.random.rand(50, 2))
s.plot(show_plot=False) # Set show_plot=True in jupyter-notebook
We shall get a similar figure as the above example.
- Try in Google Colab
Click the button and follow the examples in the Google Colab file.
Metadata
Release files for despace 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| despace-0.3.1.tar.gz | 15.2 kB | Without provenance | Details for despace-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| despace-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for despace-0.3.1-py3-none-any.whl |
Total release size: 29.2 kB
Release files / despace-0.3.1.tar.gz
| Download URL | despace-0.3.1.tar.gz |
|---|---|
| Size | 15.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
580c32cbc60c0d947bac0564c213bef633f783af67261be19ec236c9181dd624
|
|
BLAKE2b-256 checksum How to use checksums |
9209afda9e2c2ffee987de4e55d28df1d9ca37aa750f87854200f8e09a41bb22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Release files / despace-0.3.1-py3-none-any.whl
| Download URL | despace-0.3.1-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f08a697cdd9f3f8da21c9c621d4108d46b8538bec2097bb8ba7da86b223e83eb
|
|
BLAKE2b-256 checksum How to use checksums |
c829fe342df4c6fcae888fc9e27685e9883178dede546d8e26267a5849637d34
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Security for despace 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.