Hilbert Bytes¶
Hilbert Bytes is a python library for converting to and from points in d-dimensions and their corresponding index on a hilbert curve. It’s similar to hilbertcurve and numpy-hilbert-curve but is faster and more space efficient than either by keeping manipulations at the byte level, and using numba to compile the results. It also uses arbitrary precision integers, allowing you to make the grid arbitrarily fine
Installation¶
pip install hilbert-bytes
Usage¶
import hilbert_bytes
import numpy as np
points = ... # arbitrary d-dimensional points
num, dim = points.shape
# convert to big-endian bytes
points_bytes = points[..., None].astype(">u8").view("u1")
index_bytes = hilbert_bytes.encode(points_bytes) # indies as big-endian ints
new_points_bytes = hilbert_bytes.decode(index_bytes)
If you want the indices as multi-byte ints, you can can do a similar trick in reverse:
index_bytes = ... # an array of big-endian ints
indices = index_bytes.view(">u8").astype("u8")[..., 0]
But note that this will only work if your index fits in 8 bytes
Methods¶
- hilbert_bytes.decode(indices: ndarray[Any, dtype[uint8]], ndim: int) ndarray[Any, dtype[uint8]] ¶
Decode dp-dimensional indices into d-dimensional points.
This function takes indices on the hilbert curve, and the output dimension and converts them to their coresponding points. All numbers are represented as arbitrary precision integers in big-endian form.
Example
If you want to use this native multi-byte integers, you can first cast them to a big-endian variant, then view it as bytes.
indices = ... index_bytes = indices[..., None].astype(">u8").view("u1") res = hilbert_bytes.decode(index_bytes, 2)
- Parameters:
indices ((n, dp)) – A collection of n indices stored as dp-byte big-endian unsigned integers.
ndim (d) – The dimension of points to decode into. It must divide dp, but you can always zero pad the left of indices.
- Returns:
points – A collection of n d-dimensional points that correspond to the indices along the hilbert-curve.
- Return type:
(n, d, p)
- hilbert_bytes.encode(points: ndarray[Any, dtype[uint8]]) ndarray[Any, dtype[uint8]] ¶
Encode d-dimensional points into their indices on a hilbert curve.
This function takes points in a d-dimensional space, and converts them to their index on the hilbert curve. All numbers are represented as arbitrary precision integers in big-endian form.
Example
If you want to use this native multi-byte integers, you can first cast them to a big-endian variant, then view it as bytes.
points = ... point_bytes = points[..., None].astype(">u8").view("u1") res = hilbert_bytes.encode(point_bytes)
- Parameters:
points ((n, d, p)) – A collection of n, d-dimensional points stored as p-byte big-endian unsigned integers.
- Returns:
indices – A collection of n big-endian unsigned integers that correspond to the index along the hilbert-curve for the input points.
- Return type:
(n, dp)