Skip to main content

Lp,q Tree

LpqTree is a C++ library that generalize k-d trees to Lp,q Minkowski mixed-norm (entry-wise matrix distance).
It can be used for, radius search and k-nearest neighbors search for a list of M×N matrices.

Distance / Norm computation

The Lp,q norm is defined like this :
Lpq
In the LpqTree code, matrix row and column are swapped, for faster operation along the last axis:

import numpy as np
def Lpq_norm(A, p, q):
    return np.sum( np.sum( np.abs(A)**p, axis=-1 )**(q/p), axis=-1)**(1.0/q)

Distance computation is optimized for on L2,1 applications, with M×2, M×3 or M×4 structures. Meanwhile, it also works for any M×N matrices using Lp,q, where 1 ≤ p ≤ 9 and 1 ≤ q ≤ 9.

K-d Tree Implementation

Modified version of Nanoflann k-d trees to support Lp,q norm.
It includes python binder, pybind11 , based of pynanoflann.

Installation

pip install lpqtree

Python Usage

import numpy as np
import lpqtree
import lpqtree.lpqpydist as lpqdist

# Create 3 matrices composed of four 2D points (4x2)
matrices_a = np.array([[[0.0, 0.0], [1.0, 1.0], [2.0, 1.0], [3.0, 2.0]],
                       [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [2.0, 2.0]],
                       [[0.0, 1.0], [1.0, 0.0], [2.0, 1.0], [3.0, 2.0]]])

# Create 2 matrices composed of four 2D points (4x2)
matrices_b = np.array([[[0.1, 0.1], [1.5, 0.8], [1.8, 1.0], [2.5, 2.0]],
                       [[3.0, 2.2], [2.0, 1.0], [1.0, 0.0], [0.0, 1.0]]])

# Search metric Lpq apply the "p-norm" along the last axis and the "q-norm" after
lpq_metric = "l21" # L21 is equivalent to the Sum of L2 norm

# Compute the Lpq-norm of each matrix in "matrices_a"
lpqdist.lpq_str_switch(matrices_a, lpq_metric)
# lpqdist.l21(matrices_a) == lpqdist.l1(lpqdist.l2(matrices_a))

# Compute all distances from each pair "matrices_a" to "matrices_b"
lpqdist.lpq_allpairs(matrices_a, matrices_b, p=2, q=1)

# Generate the k-d tree for the search (with "matrices_b")
mtree = lpqtree.KDTree(n_neighbors=1, radius=2.5, metric=lpq_metric)
mtree.fit(matrices_b)

# For each matrix in "matrices_a" search the nearest in "matrices_b"
nn_ids_b, nn_dist = mtree.query(matrices_a)

# For each matrix in "matrices_a" search the nearest in "matrices_b"
ids_a, ids_b, r_dists = mtree.radius_neighbors(matrices_a, radius=2.5)

# Get the adjacency matrix as scipycsr_matrix
adjacency_m = mtree.get_csr_matrix()

Reference

LpqTree paper is in progress ...

Cite as:

@article{st2022fast,
  title={Fast Streamline Search: An Exact Technique for Diffusion MRI Tractography},
  author={St-Onge, Etienne and Garyfallidis, Eleftherios and Collins, D Louis},
  journal={Neuroinformatics},
  pages={1--12},
  year={2022},
  publisher={Springer}
}

@misc{blanco2014nanoflann,
  title        = {nanoflann: a {C}++ header-only fork of {FLANN}, a library for Nearest Neighbor ({NN}) with KD-trees},
  author       = {Blanco, Jose Luis and Rai, Pranjal Kumar},
  howpublished = {\url{https://github.com/jlblancoc/nanoflann}},
  year         = {2014}
}

Metadata

Release files for lpqtree 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lpqtree 0.0.7
File Size Uploaded
lpqtree-0.0.7.tar.gz 45.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lpqtree 0.0.7
File Interpreter ABI Platform
lpqtree-0.0.7-cp310-cp310-manylinux_2_34_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.34+ x86-64 Details

Total release size: 19.2 MB

Release files / lpqtree-0.0.7.tar.gz

Download URL lpqtree-0.0.7.tar.gz
Size 45.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d1c3ba65277cb4c0a7a4aed4e44fbc275547d05d5cc52fd765538201fc8f6991
BLAKE2b-256 checksum
How to use checksums
05c39f9b26067ea69ffc64a86d3a38ad037e1aeaeaaa3b9251ec9c0ed4b11f0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / lpqtree-0.0.7-cp310-cp310-manylinux_2_34_x86_64.whl

Download URL lpqtree-0.0.7-cp310-cp310-manylinux_2_34_x86_64.whl
Size 19.1 MB
Tags CPython 3.10 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
2da59de629536f42f38f14e2907f11315ca60548e7160187c32ed1b87f78e904
BLAKE2b-256 checksum
How to use checksums
6e92c49d497fd09bfd1535d98173fdfc470f58e4ea80f2ff20efbbcf4e8fda62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

1 release file

0.0.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page