Skip to main content

Distance Matrices

Fast Numba-enabled CPU and GPU computations of 1D Earth Mover's (scipy.stats.wasserstein_distance) and Euclidean distances. 10000 x 10000 weighted Wasserstein distance matrix in ~15 s on an NVIDIA GeForce RTX 2060 GPU. GPU tends to be ~2x faster than parallelized CPU on Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz (6 physical cores).

Installation

conda pip
conda install -c sgbaird dist_matrix pip install dist_matrix

To best reflect the development workflow, you can clone the repository and install via flit:

git clone https://github.com/sparks-baird/dist-matrix.git
cd dist-matrix
conda install flit # alternatively, `pip install -e .`
flit install --pth-file # --pth-file flag is a Windows-compatible local installation; you can edit the source without reinstalling

Usage

You can compute distance matrices (more efficient per distance calculation) or access the lower-level single distance calculation.

Distance Matrices

There is a GPU version (dist_matrix.cuda_dist_matrix_full) as well as a CPU version (dist_matrix.njit_dist_matrix_full).

import numpy as np
from dist_matrix.cuda_dist_matrix_full import dist_matrix as gpu_dist_matrix
# from dist_matrix.njit_dist_matrix_full import dist_matrix as cpu_dist_matrix
n_features = 10
n_rows = 100
U, V, U_weights, V_weights = np.random.rand(4, n_rows, n_features)
distances = gpu_dist_matrix(
    U,
    V=V,
    U_weights=U_weights, # optional
    V_weights=V_weights, # optional
    metric="wasserstein", # "euclidean"
)

Single Distance Calculations

See metrics.py. Note that these lower-level functions are not GPU-accelerated.

import numpy as np
from dist_matrix.utils.metrics import wasserstein_distance, euclidean_distance
n_features = 10
u, v, u_weights, v_weights = np.random.rand(4, n_features)
presorted, cumweighted, prepended = [False, False, False]
em_dist = wasserstein_distance(u, v, u_weights, v_weights, presorted, cumweighted, prepended)
eucl_dist = euclidean_distance(u, v)

See Also

Element Mover's Distances via chem_wasserstein (based on ElM2D)

Metadata

Release files for dist_matrix 1.0.4

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

Source distribution (sdist)

Source distribution for dist_matrix 1.0.4
File Size Uploaded
dist_matrix-1.0.4.tar.gz 29.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dist_matrix 1.0.4
File Interpreter ABI Platform
dist_matrix-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 73.7 kB

Release files / dist_matrix-1.0.4.tar.gz

Download URL dist_matrix-1.0.4.tar.gz
Size 29.1 kB
Tags Source
SHA-256 checksum
How to use checksums
73a51836bc7e1709a5432103a006541c4d73af48732d4e71c75b4eefa5f45e9a
BLAKE2b-256 checksum
How to use checksums
92f971f1ddb26666c97ff2eacc4768134476a6ef6f5028b3eea3ab30ff592e30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.29.0

Release files / dist_matrix-1.0.4-py3-none-any.whl

Download URL dist_matrix-1.0.4-py3-none-any.whl
Size 44.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d1805a0d620a73b32d2f114be643df5c2be9b5447e768b73fdbcab6f38fadb8b
BLAKE2b-256 checksum
How to use checksums
390958f50b43ecec3eb2740e4a210cde62d3071e2025fd63f334acf71a0a4e34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.29.0

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

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