Skip to main content

wnet

Wasserstein Network (wnet) is a Python/C++ library for working with Wasserstein distances. It uses the Min Cost Flow algorithm as implemented by the LEMON library, exposed to Python via the pylmcf module, enabling efficient computation and manipulation of Wasserstein distances between multidimensional distributions.

NetworkSimplex variants expose raw dual certificates and continuous-supply supporting cuts. The 1D SlopeDP backend also supplies certified cuts through optional direct DP recovery. ConvexSweep supplies certified cuts through templated traceback and eager native recovery with solve(..., with_duals=True). See Dual cuts. Both chain solvers offer optional storage="tree" backed by a swappable std::map policy and storage="augmented" backed by a persistent augmented treap. Flat storage remains the default. See Balanced-tree storage.

See the release notes for changes in each release.

Features

  • Wasserstein and Truncated Wasserstein distance between multidimensional distributions (dimensions 1–20)
  • Three distance metrics: L1, L2, L∞
  • Order-p (Lp) Wasserstein for any real p ≥ 1 (per-pair cost = ground_distance**p, result is the p-th root; fractional p via automatic cost scaling)
  • Derivatives with respect to peak intensities and spectrum mixture proportions
  • Position gradients (∂cost/∂position) with warm-restart re-solving after peak position updates
  • Support for distribution mixtures and efficient recalculation with changed mixture proportions
  • Picklable Distribution objects

Installation

You can install the Python package using pip:

pip install wnet

Usage

Basic distance

import numpy as np
from wnet import WassersteinDistance, Distribution
from wnet.distances import DistanceMetric

positions1 = np.array([[0, 1, 5, 10], [0, 0, 0, 3]])
intensities1 = np.array([10, 5, 5, 5])

positions2 = np.array([[1, 10], [0, 0]])
intensities2 = np.array([20, 5])

S1 = Distribution(positions1, intensities1)
S2 = Distribution(positions2, intensities2)

print(WassersteinDistance(S1, S2, DistanceMetric.L1))
# 45.0

Order-p (Lp) Wasserstein

By default the distance is the 1-Wasserstein distance. Pass p to use the order-p Wasserstein distance, where each unit of mass moved a ground distance d costs d**p and the returned value is the p-th root of the optimal transport cost:

from wnet import TruncatedWassersteinDistance

# Quadratic (W2) Wasserstein distance with a Euclidean ground metric
print(WassersteinDistance(S1, S2, DistanceMetric.L2, p=2))
print(TruncatedWassersteinDistance(S1, S2, DistanceMetric.L2, max_distance=3.0, p=2))

# Fractional orders work too (e.g. p = 1.5)
print(WassersteinDistance(S1, S2, DistanceMetric.L2, p=1.5))

p can be any real number ≥ 1. The ground metric (L1/L2/L∞) is chosen independently of p. For p != 1 the dense transport network is used by default: the chain solver's hop costs are additive along the chain, which exponentiated step costs are not. In 1D there is a chain-native alternative, ConvexSweep, which sweeps the sorted positions and prices pairs directly; select it with split_distance= and solver=ConvexSweep().

p == 1 is bit-exact with the classic 1-Wasserstein distance. For p != 1 the cost d**p is fractional, so the integer min-cost-flow solver works in automatically scaled units (round(scale_factor() * d**p)); the public results divide that scale back out, so no tuning is needed.

At the WassersteinNetwork level, total_cost() and all derivatives are in W_p**p units (the sum of d**p · flow); take the p-th root for the literal W_p distance, as WassersteinDistance() does.

Truncated Wasserstein

Mass that cannot be matched within max_distance is discarded at a fixed cost rather than transported arbitrarily far:

from wnet import TruncatedWassersteinDistance

print(TruncatedWassersteinDistance(S1, S2, DistanceMetric.L2, max_distance=3.0))

Derivatives w.r.t. peak intensities

signal_part_derivatives() returns the marginal cost of increasing each theoretical peak's intensity by 1 — useful for scoring how well each peak is explained. It needs an escape route, so add one of the trash edges before build():

from wnet import WassersteinNetwork

W = WassersteinNetwork(S1, [S2], DistanceMetric.L2, max_distance=10.0)
W.add_simple_trash(10.0)
W.build()
W.solve()

derivs = W.signal_part_derivatives()   # {spectrum_id: {peak_index: derivative}}
print(derivs[0])                       # marginals for the peaks of S2
# {0: 10.0, 1: 10.0}

The keys are the theoretical spectra in the order they were passed, so derivs[k][i] is the marginal for peak i of the k-th target. spectrum_proportion_derivatives() returns the gradient with respect to scaling each spectrum's proportion, as an np.ndarray indexed by spectrum.

Both are in W_p**p units, like total_cost().

Optimising peak positions

After an initial solve, positions can be updated and re-solved cheaply via a warm restart. update_positions_and_get_gradient(new_base, new_targets) takes replacement Distribution objects with the same peak counts, re-solves, and returns ∂cost/∂position for all peaks so you can feed them into a gradient-based optimiser:

W = WassersteinNetwork(S1, [S2], DistanceMetric.L2, max_distance=10.0)
W.add_simple_trash(10.0)
W.build()
W.solve()

positions = S1.positions.copy()          # [DIM, N]; the property is a read-only view
for _ in range(100):
    moved = Distribution(positions, S1.intensities)
    grad_empirical, grad_theoretical = W.update_positions_and_get_gradient(moved, [S2])
    # gradients are [N, DIM], positions are [DIM, N]
    positions -= 0.01 * grad_empirical.T

grad_theoretical is a list holding one [N_k, DIM] array per target spectrum. Gradients are of total_cost(), i.e. the W_p**p objective; for the literal W_p multiply by (1/p) * total_cost()**(1/p - 1).

If you only want the re-solve and not the gradient, update_positions_and_solve(new_base, new_targets) is the cheaper call. Both keep the graph topology fixed, so the peak counts may not change; on 1D chain networks the peaks may also not cross one another.

Licence

MIT Licence

Citation

If you use this software, please cite:

Król J, Bochenek M, Jopa S, Kazimierczuk K, Gambin A, Startek MP (2026). WNetAlign: fast and accurate spectra alignment using truncated Wasserstein distance and network simplex. Briefings in Bioinformatics, 27(3), bbag247. https://doi.org/10.1093/bib/bbag247

@article{krol2026wnetalign,
  title   = {WNetAlign: fast and accurate spectra alignment using truncated Wasserstein distance and network simplex},
  author  = {Kr{\'o}l, Justyna and Bochenek, Maria and Jopa, Sylwia and Kazimierczuk, Krzysztof and Gambin, Anna and Startek, Micha{\l} Piotr},
  journal = {Briefings in Bioinformatics},
  volume  = {27},
  number  = {3},
  pages   = {bbag247},
  year    = {2026},
  doi     = {10.1093/bib/bbag247}
}
  • pylmcf - Python bindings for Min Cost Flow algorithms from LEMON library.
  • wnetalign - Alignment of MS/NMR spectra using Truncated Wasserstein Distance

Metadata

Release files for wnet 1.5.0

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

Source distribution (sdist)

Source distribution for wnet 1.5.0
File Size Uploaded
wnet-1.5.0.tar.gz 167.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for wnet 1.5.0
File
wnet-1.5.0-cp315-abi3t-win_arm64.whl CPython 3.15 abi3t Windows ARM64 Details
wnet-1.5.0-cp315-abi3t-win_amd64.whl CPython 3.15 abi3t Windows x86-64 Details
wnet-1.5.0-cp315-abi3t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.15 abi3t Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details
wnet-1.5.0-cp315-abi3t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl CPython 3.15 abi3t Linux glibc 2.24+ ARM64, Linux glibc 2.28+ ARM64 Details
wnet-1.5.0-cp315-abi3t-macosx_11_0_arm64.whl CPython 3.15 abi3t macOS 11.0+ ARM64 Details
wnet-1.5.0-cp315-abi3t-macosx_10_15_x86_64.whl CPython 3.15 abi3t macOS 10.15+ x86-64 Details
wnet-1.5.0-cp310-abi3-win_arm64.whl CPython 3.10 abi3 Windows ARM64 Details
wnet-1.5.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
wnet-1.5.0-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details
wnet-1.5.0-cp310-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl CPython 3.10 abi3 Linux glibc 2.24+ ARM64, Linux glibc 2.28+ ARM64 Details
wnet-1.5.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
wnet-1.5.0-cp310-abi3-macosx_10_15_x86_64.whl CPython 3.10 abi3 macOS 10.15+ x86-64 Details

Total release size: 21.2 MB

Release files / wnet-1.5.0.tar.gz

Download URL wnet-1.5.0.tar.gz
Size 167.6 kB
Tags Source
SHA-256 checksum
How to use checksums
64d74b68b36b768fced52b01d888b2e2fa2c1eb5ccfbdca9ce3fe24cd479c23b
BLAKE2b-256 checksum
How to use checksums
6ce0be1e2580fbe15347c079d4c92c1ad7f530b59ec0e0381818f4544cfa002a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp315-abi3t-win_arm64.whl

Download URL wnet-1.5.0-cp315-abi3t-win_arm64.whl
Size 1.6 MB
Tags CPython 3.15 Windows ARM64 abi3t
SHA-256 checksum
How to use checksums
a37353e3b331fda0bb74e852ac462d7de9aa4c60537814444ed62e9de684b4dd
BLAKE2b-256 checksum
How to use checksums
e386fe93fbae3a16a8b6ba2b5069e1329c608d0073a20c560cc7eca4525b6956
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp315-abi3t-win_amd64.whl

Download URL wnet-1.5.0-cp315-abi3t-win_amd64.whl
Size 1.5 MB
Tags CPython 3.15 Windows x86-64 abi3t
SHA-256 checksum
How to use checksums
42208ba58cacf54df61849e4f80716995e07ca9b52e57dd8fc10174bdb87aa25
BLAKE2b-256 checksum
How to use checksums
ddcdbfb97b6256850eb0fa366da96b54436c0330bf4bd274cb3bac42019119ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp315-abi3t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL wnet-1.5.0-cp315-abi3t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 2.2 MB
Tags CPython 3.15 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 abi3t
SHA-256 checksum
How to use checksums
295180135069a4174baa41385a328a9a2b47f30843f45fcb9e8cbfc5df3e4683
BLAKE2b-256 checksum
How to use checksums
357a7cbc297444615dae76e8fcf8e388aff3d5053e25d7a789f1149d90716f3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp315-abi3t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl

Download URL wnet-1.5.0-cp315-abi3t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Size 2.0 MB
Tags CPython 3.15 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 abi3t
SHA-256 checksum
How to use checksums
b18847978a11948c99473c6cd3d35f9ce0924ca4a352d48e08e25fb5cb34aecb
BLAKE2b-256 checksum
How to use checksums
ff7aa52104460430824a2fbc32f5b4922dc6b7932837926ebe4e87e822be8e6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp315-abi3t-macosx_11_0_arm64.whl

Download URL wnet-1.5.0-cp315-abi3t-macosx_11_0_arm64.whl
Size 1.5 MB
Tags CPython 3.15 abi3t macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3aece8fc6bda3ee4dbf25c80773eee5b11b49362af5da4fea652f0a24fba101d
BLAKE2b-256 checksum
How to use checksums
f61c684878fdd7fc8a1ff170be85a1d65c64abe935457ddae5c124c0a5338bab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp315-abi3t-macosx_10_15_x86_64.whl

Download URL wnet-1.5.0-cp315-abi3t-macosx_10_15_x86_64.whl
Size 1.7 MB
Tags CPython 3.15 abi3t macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
7a889b01c13580c381ae3a92b48d6d3eb364e2bef3971613ee613e436f54d4a6
BLAKE2b-256 checksum
How to use checksums
4827ec8c834a9995c3cd4084bac325f4e0ab7c779229d5644370e83210a0d3aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp310-abi3-win_arm64.whl

Download URL wnet-1.5.0-cp310-abi3-win_arm64.whl
Size 1.5 MB
Tags CPython 3.10 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
36a0c78620033474f7dcc66840a3cdaeca17fecd642e43c4259696580a7b32d9
BLAKE2b-256 checksum
How to use checksums
adb6f2570e90586aa5474915629e6bdd9fb2e1d627eed916b7dbe7dfa02edd5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp310-abi3-win_amd64.whl

Download URL wnet-1.5.0-cp310-abi3-win_amd64.whl
Size 1.5 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
6ca491aec09fec701b78f0cb9122892ff67a116830f13f21d79acd0278599667
BLAKE2b-256 checksum
How to use checksums
cbfb4f762307eabb55a646158b51d5a449753800e2dcff7ad2aec1c4fcc9acc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL wnet-1.5.0-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 2.2 MB
Tags CPython 3.10 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
ebd7f3c281f7c806d6ef1b38ed62e36af7ac32ee8891aba488ecd918e0d39937
BLAKE2b-256 checksum
How to use checksums
e9874c1e33f109429d7830dec8863bce7694289e60d00d5b28bf95b729503159
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp310-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl

Download URL wnet-1.5.0-cp310-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Size 2.0 MB
Tags CPython 3.10 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
65e56418039f2ddb1ca1478f59cc9b2b05479c5dfae9eccde163cc3c09ce3f8f
BLAKE2b-256 checksum
How to use checksums
dcd13c2de538298d12bcf09ca315a023762d46bd67fa164b3bded17c6804d616
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL wnet-1.5.0-cp310-abi3-macosx_11_0_arm64.whl
Size 1.5 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
56b16057cf47552b85f8963e40474a9645a3e74760eb5a6d01b467f76ead0a34
BLAKE2b-256 checksum
How to use checksums
3b78748279d7d2758004bf9a7ada2aa1b9dfe0d41c58f5be8da8ed90aa351a17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / wnet-1.5.0-cp310-abi3-macosx_10_15_x86_64.whl

Download URL wnet-1.5.0-cp310-abi3-macosx_10_15_x86_64.whl
Size 1.7 MB
Tags CPython 3.10 abi3 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
e5b1b1aab153f322da62e38334b9fb76c75708a7fe427d91d4f5094561b2d494
BLAKE2b-256 checksum
How to use checksums
f17c6bae14ad0151b127da8f2ab32262ce2176e91c3562588215883f7786134b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log
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