topolab-ot 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. topolab_ot-0.1.0/Cargo.toml +29 -0
  2. topolab_ot-0.1.0/LICENSE +21 -0
  3. topolab_ot-0.1.0/PKG-INFO +138 -0
  4. topolab_ot-0.1.0/README.md +107 -0
  5. topolab_ot-0.1.0/THIRD_PARTY_LICENSES.md +424 -0
  6. topolab_ot-0.1.0/crates/memory-phase/Cargo.toml +14 -0
  7. topolab_ot-0.1.0/crates/memory-phase/LICENSE +21 -0
  8. topolab_ot-0.1.0/crates/memory-phase/README.md +12 -0
  9. topolab_ot-0.1.0/crates/memory-phase/src/lib.rs +278 -0
  10. topolab_ot-0.1.0/crates/musl-math/Cargo.toml +19 -0
  11. topolab_ot-0.1.0/crates/musl-math/LICENSE +21 -0
  12. topolab_ot-0.1.0/crates/musl-math/README.md +20 -0
  13. topolab_ot-0.1.0/crates/musl-math/THIRD_PARTY_LICENSES.md +42 -0
  14. topolab_ot-0.1.0/crates/musl-math/src/data.rs +1236 -0
  15. topolab_ot-0.1.0/crates/musl-math/src/exp.rs +190 -0
  16. topolab_ot-0.1.0/crates/musl-math/src/hypot.rs +89 -0
  17. topolab_ot-0.1.0/crates/musl-math/src/lib.rs +107 -0
  18. topolab_ot-0.1.0/crates/musl-math/src/log.rs +125 -0
  19. topolab_ot-0.1.0/crates/musl-math/src/pow.rs +310 -0
  20. topolab_ot-0.1.0/crates/ot-core/Cargo.toml +38 -0
  21. topolab_ot-0.1.0/crates/ot-core/LICENSE +21 -0
  22. topolab_ot-0.1.0/crates/ot-core/README.md +40 -0
  23. topolab_ot-0.1.0/crates/ot-core/THIRD_PARTY_LICENSES.md +92 -0
  24. topolab_ot-0.1.0/crates/ot-core/src/barycenter.rs +915 -0
  25. topolab_ot-0.1.0/crates/ot-core/src/certificate.rs +782 -0
  26. topolab_ot-0.1.0/crates/ot-core/src/cost.rs +394 -0
  27. topolab_ot-0.1.0/crates/ot-core/src/directions.rs +339 -0
  28. topolab_ot-0.1.0/crates/ot-core/src/emd/assignment.rs +1253 -0
  29. topolab_ot-0.1.0/crates/ot-core/src/emd.rs +2064 -0
  30. topolab_ot-0.1.0/crates/ot-core/src/error.rs +264 -0
  31. topolab_ot-0.1.0/crates/ot-core/src/fastmath.rs +317 -0
  32. topolab_ot-0.1.0/crates/ot-core/src/fdlibm.rs +171 -0
  33. topolab_ot-0.1.0/crates/ot-core/src/gromov.rs +623 -0
  34. topolab_ot-0.1.0/crates/ot-core/src/kernel.rs +475 -0
  35. topolab_ot-0.1.0/crates/ot-core/src/lib.rs +65 -0
  36. topolab_ot-0.1.0/crates/ot-core/src/linalg.rs +611 -0
  37. topolab_ot-0.1.0/crates/ot-core/src/one_d.rs +256 -0
  38. topolab_ot-0.1.0/crates/ot-core/src/persistence.rs +906 -0
  39. topolab_ot-0.1.0/crates/ot-core/src/relax.rs +351 -0
  40. topolab_ot-0.1.0/crates/ot-core/src/sinkhorn.rs +2310 -0
  41. topolab_ot-0.1.0/crates/ot-core/src/spell.rs +196 -0
  42. topolab_ot-0.1.0/crates/ot-core/src/unbalanced.rs +651 -0
  43. topolab_ot-0.1.0/crates/ot-core/src/util.rs +202 -0
  44. topolab_ot-0.1.0/pyproject.toml +57 -0
  45. topolab_ot-0.1.0/python/Cargo.lock +245 -0
  46. topolab_ot-0.1.0/python/Cargo.toml +28 -0
  47. topolab_ot-0.1.0/python/LICENSE +21 -0
  48. topolab_ot-0.1.0/python/README.md +107 -0
  49. topolab_ot-0.1.0/python/THIRD_PARTY_LICENSES.md +424 -0
  50. topolab_ot-0.1.0/python/src/lib.rs +753 -0
  51. topolab_ot-0.1.0/python/src/names.rs +54 -0
  52. topolab_ot-0.1.0/python/topolab_ot/__init__.py +1115 -0
  53. topolab_ot-0.1.0/python/topolab_ot/__init__.pyi +149 -0
  54. topolab_ot-0.1.0/python/topolab_ot/_native.pyi +33 -0
  55. topolab_ot-0.1.0/python/topolab_ot/py.typed +0 -0
@@ -0,0 +1,29 @@
1
+ [workspace]
2
+ resolver = "2"
3
+ members = [
4
+ "crates/memory-phase", "crates/ot-core", "crates/ot-wasm", "crates/musl-math", "crates/musl-math-oracle"]
5
+
6
+ [workspace.package]
7
+ version = "0.2.0"
8
+ edition = "2021"
9
+ license = "MIT"
10
+ repository = "https://github.com/topo-lab/ot"
11
+ readme = "README.md"
12
+ authors = ["topolab <dev@topolab.ai>"]
13
+ rust-version = "1.86"
14
+
15
+ [profile.release]
16
+ opt-level = 3
17
+ lto = "fat"
18
+ codegen-units = 1
19
+ panic = "abort"
20
+ debug = false
21
+
22
+ [profile.bench]
23
+ inherits = "release"
24
+ debug = true
25
+
26
+ # Size-oriented profile for the shipped .wasm
27
+ [profile.wasm-size]
28
+ inherits = "release"
29
+ opt-level = "s"
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 topolab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,138 @@
1
+ Metadata-Version: 2.4
2
+ Name: topolab-ot
3
+ Version: 0.1.0
4
+ Classifier: Development Status :: 3 - Alpha
5
+ Classifier: Intended Audience :: Science/Research
6
+ Classifier: Operating System :: OS Independent
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3 :: Only
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Programming Language :: Python :: Implementation :: CPython
15
+ Classifier: Programming Language :: Rust
16
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
17
+ Classifier: Typing :: Typed
18
+ Requires-Dist: numpy>=1.23
19
+ License-File: LICENSE
20
+ License-File: THIRD_PARTY_LICENSES.md
21
+ Summary: Optimal transport: exact transport by network simplex, Sinkhorn and its divergence, 1-D and sliced Wasserstein, unbalanced transport, barycenters, Gromov-Wasserstein, and distances between persistence diagrams
22
+ Keywords: optimal-transport,wasserstein,sinkhorn,earth-movers-distance,persistence-diagrams
23
+ Home-Page: https://topolab.ai/ot/
24
+ Author-email: topolab <dev@topolab.ai>
25
+ License-Expression: MIT AND BSL-1.0 AND SunPro
26
+ Requires-Python: >=3.10
27
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
28
+ Project-URL: Documentation, https://topolab.ai/ot/docs/
29
+ Project-URL: Homepage, https://topolab.ai/ot/
30
+
31
+ # topolab-ot
32
+
33
+ Discrete optimal transport for Python: exact transport by network simplex,
34
+ Sinkhorn and its divergence, unbalanced transport, fixed-support barycenters,
35
+ entropic Gromov–Wasserstein, 1-D and sliced Wasserstein, and the Wasserstein
36
+ and bottleneck distances between persistence diagrams.
37
+
38
+ ```python
39
+ import numpy as np
40
+ import topolab_ot as ot
41
+
42
+ x = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]) # 3 source points
43
+ y = np.array([[1.0, 1.0], [2.0, 0.0], [0.0, 2.0]]) # 3 target points
44
+ a = np.full(3, 1 / 3) # weights of x
45
+ b = np.full(3, 1 / 3) # weights of y
46
+
47
+ M = ot.pairwise_cost(x, y) # (3, 3) squared Euclidean costs
48
+ exact = ot.emd(a, b, M) # exact optimal transport
49
+ print(f"{exact.cost:.6f}") # 1.333333
50
+ print(exact.plan.shape) # (3, 3)
51
+ print(ot.emd_verify(a, b, M, exact).ok) # True: the LP-duality certificate holds
52
+
53
+ soft = ot.sinkhorn(a, b, M, reg=0.1) # entropic transport
54
+ print(soft.converged, f"{soft.cost:.4f}") # True 1.3334
55
+ ```
56
+
57
+ ## Install
58
+
59
+ ```
60
+ pip install topolab-ot
61
+ ```
62
+
63
+ One wheel per platform serves CPython 3.10 and later. The only runtime
64
+ dependency is NumPy (1.23 or later).
65
+
66
+ ## The package
67
+
68
+ The functions are those of the npm package
69
+ [`@topolab/ot`](https://topolab.ai/ot/docs/) on the CPU, with the same
70
+ options and defaults, computed by the same Rust engine
71
+ ([`topolab-ot`](https://crates.io/crates/topolab-ot) on crates.io) in float64,
72
+ called natively. Names are in snake_case (`pairwiseCost` is `pairwise_cost`,
73
+ `maxIter` is `max_iter`), and options are keyword-only arguments.
74
+
75
+ | Function | Computes |
76
+ |---|---|
77
+ | `emd`, `emd_verify` | exact transport (the transportation LP) and a check of its LP-duality certificate |
78
+ | `sinkhorn`, `sinkhorn_plan`, `sinkhorn_divergence` | entropic transport, its plan from the potentials, the Sinkhorn divergence |
79
+ | `sinkhorn_unbalanced` | entropic transport with KL-penalised marginals |
80
+ | `barycenter` | fixed-support entropic barycenter, plain or debiased (experimental) |
81
+ | `gromov_wasserstein` | entropic Gromov–Wasserstein (experimental) |
82
+ | `wasserstein_1d`, `sliced_wasserstein`, `random_directions` | transport on the line, its average over directions, seeded unit directions |
83
+ | `diagram_wasserstein`, `diagram_bottleneck` | exact distances between persistence diagrams, with a matching |
84
+ | `pairwise_cost` | squared Euclidean, Euclidean and Minkowski cost matrices |
85
+
86
+ Inputs are NumPy arrays or anything `numpy.asarray` accepts, converted to
87
+ float64 once. Weights are 1-D arrays, a cost matrix between `n` sources and
88
+ `m` targets is an `(n, m)` array, a point cloud an `(n, d)` array, and a
89
+ persistence diagram a `(k, 2)` array of `(birth, death)` points, with
90
+ `death = inf` for a class that persists. Results are NumPy arrays and frozen
91
+ dataclasses with the fields of the npm results in snake_case.
92
+
93
+ A wrong type raises `TypeError`, an argument out of range `ValueError` with
94
+ the npm package's message, and the exact solver's pivot limit `RuntimeError`.
95
+ Every function releases the GIL while the engine runs, so threads can solve
96
+ problems in parallel.
97
+
98
+ Memory, in three cases:
99
+
100
+ - A buffer that cannot be held raises `MemoryError`: the float64 copy of an
101
+ input, the engine's copy of it, an output buffer, or a size that has no
102
+ memory layout (a capacity overflow).
103
+ - Input beyond a fixed limit raises `ValueError`, as any other invalid input
104
+ does: `diagram_wasserstein` refuses more than 16,777,216 dense cost entries
105
+ before allocating them.
106
+ - Only an allocation that fails in the middle of a computation, when the system
107
+ cannot provide memory the engine asks for, ends the Python process.
108
+
109
+ `random_directions`, `diagram_wasserstein` and `diagram_bottleneck` call no
110
+ host math library, so they give the same bits as the npm package on every
111
+ host. The other solvers call the platform's `exp`, `log` and `pow`, whose last
112
+ bits can differ from the ones the WebAssembly build links; the package's tests
113
+ compare their results with the npm package's within the tolerances of the npm
114
+ package's own tests.
115
+
116
+ The WebGPU solvers of the npm package (`GpuSinkhorn`, `GpuMatmul`) have no
117
+ counterpart here.
118
+
119
+ ## Documentation
120
+
121
+ - Getting started in Python (install, arrays, results, errors, threads,
122
+ memory): <https://topolab.ai/ot/docs/getting-started/?lang=python>
123
+ - API reference, in Python, from these docstrings:
124
+ <https://topolab.ai/ot/docs/api/?lang=python>
125
+ - The guides, with their examples in Python:
126
+ <https://topolab.ai/ot/docs/?lang=python>
127
+
128
+ The docstrings cite their sources as [n], listed at
129
+ <https://topolab.ai/ot/docs/references/>.
130
+
131
+ ## Licence
132
+
133
+ `MIT AND BSL-1.0 AND SunPro` (SPDX). `LICENSE` holds the MIT terms of
134
+ topolab's code. `THIRD_PARTY_LICENSES.md`, in the wheel and the sdist, holds
135
+ the Boost Software License 1.0 of the code derived from LEMON, the Sun
136
+ Microsystems notice of the code derived from fdlibm, the musl notice, and the
137
+ notices of the Rust crates compiled into the extension.
138
+
@@ -0,0 +1,107 @@
1
+ # topolab-ot
2
+
3
+ Discrete optimal transport for Python: exact transport by network simplex,
4
+ Sinkhorn and its divergence, unbalanced transport, fixed-support barycenters,
5
+ entropic Gromov–Wasserstein, 1-D and sliced Wasserstein, and the Wasserstein
6
+ and bottleneck distances between persistence diagrams.
7
+
8
+ ```python
9
+ import numpy as np
10
+ import topolab_ot as ot
11
+
12
+ x = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]) # 3 source points
13
+ y = np.array([[1.0, 1.0], [2.0, 0.0], [0.0, 2.0]]) # 3 target points
14
+ a = np.full(3, 1 / 3) # weights of x
15
+ b = np.full(3, 1 / 3) # weights of y
16
+
17
+ M = ot.pairwise_cost(x, y) # (3, 3) squared Euclidean costs
18
+ exact = ot.emd(a, b, M) # exact optimal transport
19
+ print(f"{exact.cost:.6f}") # 1.333333
20
+ print(exact.plan.shape) # (3, 3)
21
+ print(ot.emd_verify(a, b, M, exact).ok) # True: the LP-duality certificate holds
22
+
23
+ soft = ot.sinkhorn(a, b, M, reg=0.1) # entropic transport
24
+ print(soft.converged, f"{soft.cost:.4f}") # True 1.3334
25
+ ```
26
+
27
+ ## Install
28
+
29
+ ```
30
+ pip install topolab-ot
31
+ ```
32
+
33
+ One wheel per platform serves CPython 3.10 and later. The only runtime
34
+ dependency is NumPy (1.23 or later).
35
+
36
+ ## The package
37
+
38
+ The functions are those of the npm package
39
+ [`@topolab/ot`](https://topolab.ai/ot/docs/) on the CPU, with the same
40
+ options and defaults, computed by the same Rust engine
41
+ ([`topolab-ot`](https://crates.io/crates/topolab-ot) on crates.io) in float64,
42
+ called natively. Names are in snake_case (`pairwiseCost` is `pairwise_cost`,
43
+ `maxIter` is `max_iter`), and options are keyword-only arguments.
44
+
45
+ | Function | Computes |
46
+ |---|---|
47
+ | `emd`, `emd_verify` | exact transport (the transportation LP) and a check of its LP-duality certificate |
48
+ | `sinkhorn`, `sinkhorn_plan`, `sinkhorn_divergence` | entropic transport, its plan from the potentials, the Sinkhorn divergence |
49
+ | `sinkhorn_unbalanced` | entropic transport with KL-penalised marginals |
50
+ | `barycenter` | fixed-support entropic barycenter, plain or debiased (experimental) |
51
+ | `gromov_wasserstein` | entropic Gromov–Wasserstein (experimental) |
52
+ | `wasserstein_1d`, `sliced_wasserstein`, `random_directions` | transport on the line, its average over directions, seeded unit directions |
53
+ | `diagram_wasserstein`, `diagram_bottleneck` | exact distances between persistence diagrams, with a matching |
54
+ | `pairwise_cost` | squared Euclidean, Euclidean and Minkowski cost matrices |
55
+
56
+ Inputs are NumPy arrays or anything `numpy.asarray` accepts, converted to
57
+ float64 once. Weights are 1-D arrays, a cost matrix between `n` sources and
58
+ `m` targets is an `(n, m)` array, a point cloud an `(n, d)` array, and a
59
+ persistence diagram a `(k, 2)` array of `(birth, death)` points, with
60
+ `death = inf` for a class that persists. Results are NumPy arrays and frozen
61
+ dataclasses with the fields of the npm results in snake_case.
62
+
63
+ A wrong type raises `TypeError`, an argument out of range `ValueError` with
64
+ the npm package's message, and the exact solver's pivot limit `RuntimeError`.
65
+ Every function releases the GIL while the engine runs, so threads can solve
66
+ problems in parallel.
67
+
68
+ Memory, in three cases:
69
+
70
+ - A buffer that cannot be held raises `MemoryError`: the float64 copy of an
71
+ input, the engine's copy of it, an output buffer, or a size that has no
72
+ memory layout (a capacity overflow).
73
+ - Input beyond a fixed limit raises `ValueError`, as any other invalid input
74
+ does: `diagram_wasserstein` refuses more than 16,777,216 dense cost entries
75
+ before allocating them.
76
+ - Only an allocation that fails in the middle of a computation, when the system
77
+ cannot provide memory the engine asks for, ends the Python process.
78
+
79
+ `random_directions`, `diagram_wasserstein` and `diagram_bottleneck` call no
80
+ host math library, so they give the same bits as the npm package on every
81
+ host. The other solvers call the platform's `exp`, `log` and `pow`, whose last
82
+ bits can differ from the ones the WebAssembly build links; the package's tests
83
+ compare their results with the npm package's within the tolerances of the npm
84
+ package's own tests.
85
+
86
+ The WebGPU solvers of the npm package (`GpuSinkhorn`, `GpuMatmul`) have no
87
+ counterpart here.
88
+
89
+ ## Documentation
90
+
91
+ - Getting started in Python (install, arrays, results, errors, threads,
92
+ memory): <https://topolab.ai/ot/docs/getting-started/?lang=python>
93
+ - API reference, in Python, from these docstrings:
94
+ <https://topolab.ai/ot/docs/api/?lang=python>
95
+ - The guides, with their examples in Python:
96
+ <https://topolab.ai/ot/docs/?lang=python>
97
+
98
+ The docstrings cite their sources as [n], listed at
99
+ <https://topolab.ai/ot/docs/references/>.
100
+
101
+ ## Licence
102
+
103
+ `MIT AND BSL-1.0 AND SunPro` (SPDX). `LICENSE` holds the MIT terms of
104
+ topolab's code. `THIRD_PARTY_LICENSES.md`, in the wheel and the sdist, holds
105
+ the Boost Software License 1.0 of the code derived from LEMON, the Sun
106
+ Microsystems notice of the code derived from fdlibm, the musl notice, and the
107
+ notices of the Rust crates compiled into the extension.