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 through the existing integer transport solves. See Dual cuts with integer network simplex.

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.4.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.4.0
File Size Uploaded
wnet-1.4.0.tar.gz 139.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for wnet 1.4.0
File
wnet-1.4.0-cp315-abi3t-win_arm64.whl CPython 3.15 abi3t Windows ARM64 Details
wnet-1.4.0-cp315-abi3t-win_amd64.whl CPython 3.15 abi3t Windows x86-64 Details
wnet-1.4.0-cp315-abi3t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.15 abi3t Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details
wnet-1.4.0-cp315-abi3t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl CPython 3.15 abi3t Linux glibc 2.28+ ARM64, Linux glibc 2.24+ ARM64 Details
wnet-1.4.0-cp315-abi3t-macosx_11_0_arm64.whl CPython 3.15 abi3t macOS 11.0+ ARM64 Details
wnet-1.4.0-cp315-abi3t-macosx_10_15_x86_64.whl CPython 3.15 abi3t macOS 10.15+ x86-64 Details
wnet-1.4.0-cp310-abi3-win_arm64.whl CPython 3.10 abi3 Windows ARM64 Details
wnet-1.4.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
wnet-1.4.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.4.0-cp310-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl CPython 3.10 abi3 Linux glibc 2.28+ ARM64, Linux glibc 2.24+ ARM64 Details
wnet-1.4.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
wnet-1.4.0-cp310-abi3-macosx_10_15_x86_64.whl CPython 3.10 abi3 macOS 10.15+ x86-64 Details

Total release size: 19.4 MB

Release files / wnet-1.4.0.tar.gz

Download URL wnet-1.4.0.tar.gz
Size 139.1 kB
Tags Source
SHA-256 checksum
How to use checksums
dece17279654ab3f8e14f1aa9e5facdb3b5837144aa4d2e8073abb9e81ad15f6
BLAKE2b-256 checksum
How to use checksums
1f818c855e559077f4379f4215df983c4175657847a8e8e6e79d1de3f493ed9b
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp315-abi3t-win_arm64.whl
Size 1.5 MB
Tags CPython 3.15 Windows ARM64 abi3t
SHA-256 checksum
How to use checksums
f8ca71136ca9f91080c6cdcf59d7f5a82658f2fd13b9f599fea53438d37a7083
BLAKE2b-256 checksum
How to use checksums
320dacb769ec4987de07054ee47a6d935d6514dbf1f0b1e58c0d7865f33804f7
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp315-abi3t-win_amd64.whl
Size 1.4 MB
Tags CPython 3.15 Windows x86-64 abi3t
SHA-256 checksum
How to use checksums
532ede7fb703f6a93e21da91a1bf99bcb0035c42bbe1552ded83fe335e03d52d
BLAKE2b-256 checksum
How to use checksums
495a9214fd5060937c2e04a98d46980f9380ec88a022ebf7b41494c55f3c46d0
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp315-abi3t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 2.1 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
4d1d7ba3d89d6c019f0cf3f304be8074e41b2498f9f3089ede64ae5766e69d9c
BLAKE2b-256 checksum
How to use checksums
407197ffa4f4eb76e55ebec69a5ed77584db3a4ef82c19b152d48ac7c3e433b1
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp315-abi3t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Size 1.8 MB
Tags CPython 3.15 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 abi3t
SHA-256 checksum
How to use checksums
07b227376c83b050a86edc73e4572e445d29894a461e1ec3c11f38dbb8ba657a
BLAKE2b-256 checksum
How to use checksums
0790d6f8844c908c9dfb30a74a381cfa9d2ac49e3a7ba15b9a8a37e11cadd6cb
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp315-abi3t-macosx_11_0_arm64.whl
Size 1.4 MB
Tags CPython 3.15 abi3t macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
cb425ab1188f9242dabbae338efba47bacdb1aea6b18661d331ab3f898b1c7ce
BLAKE2b-256 checksum
How to use checksums
f03afa3a12b87c4f054834980000136e8714fbf745bf34f84dcbcd479185bc80
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp315-abi3t-macosx_10_15_x86_64.whl
Size 1.5 MB
Tags CPython 3.15 abi3t macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
df389be0470647180168661dbffd93dc5208b11e4f6414d50fd816863062175c
BLAKE2b-256 checksum
How to use checksums
9053990521bbfbb45f844f348b2c395decb9fb8f56fd8358d9b137259ed7e1c2
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp310-abi3-win_arm64.whl
Size 1.4 MB
Tags CPython 3.10 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
ec74dbe592eae48f2b883bfca20cad8de8df1e49d087c0c13af77818f3d28224
BLAKE2b-256 checksum
How to use checksums
757e6d3757fc6389eade66a845747891b1c4d77a7faa1ad123ac4824840e604b
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp310-abi3-win_amd64.whl
Size 1.3 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
2f458daf2b442606707a4b3f05c8d2b086d58efd01f1f4fb72f9b2ecaa50ec60
BLAKE2b-256 checksum
How to use checksums
48255eec474259ddbf937e4044514aa49691548f8b3605086fafa5a006d52ad1
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 2.1 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
3eb4ce605f1fcf8a37b7edaefc54b9da28b04eaaa77186e8e78a6ac8b49afb7d
BLAKE2b-256 checksum
How to use checksums
8565a2d801a9133691037ebebae9945c1cc7e029f01114816064481fe6feff35
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp310-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Size 1.8 MB
Tags CPython 3.10 Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
20ebdc5a2f48dc396f74fed85408b6a492cbc7ed98af3195a1e30842ec57eafe
BLAKE2b-256 checksum
How to use checksums
72c157503b4944875af1ba01458d428d162f4413acc157f7bab8b7b66d054ae8
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp310-abi3-macosx_11_0_arm64.whl
Size 1.4 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
20a630d24414af16c90305cc860083ba9b986fd7b9a6e12012f22fde74ab5352
BLAKE2b-256 checksum
How to use checksums
8d77538a574a27741181a99f66363f827ed21323c654948edc77ac1d55cd3dff
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 9, 2026.

Transparency log

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

Download URL wnet-1.4.0-cp310-abi3-macosx_10_15_x86_64.whl
Size 1.5 MB
Tags CPython 3.10 abi3 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
ba3c6d914ccedbea7f384542a2ce3996a7aba16d5d0cbd1dc7b60fe96067be97
BLAKE2b-256 checksum
How to use checksums
a037616dd8420f59fdfe8394776e5e7acc5ad7cd336356bfca611de4e709108b
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 9, 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