From d62877b7c8d03274022f7c6c795d2d789de7b572 Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Tue, 21 Jan 2025 16:08:13 +0100 Subject: [PATCH 01/16] Add entropic_partial_wasserstein function I also fix tests in `sinkhorn_unbalanced` and `sinkhorn_unbalanced2` --- Project.toml | 2 +- docs/src/api.md | 6 ++++ src/PythonOT.jl | 3 +- src/lib.jl | 95 +++++++++++++++++++++++++++++++------------------ 4 files changed, 70 insertions(+), 36 deletions(-) diff --git a/Project.toml b/Project.toml index d8894ac..edfce28 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PythonOT" uuid = "3c485715-4278-42b2-9b5f-8f00e43c12ef" authors = ["David Widmann"] -version = "0.1.6" +version = "0.1.7" [deps] PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" diff --git a/docs/src/api.md b/docs/src/api.md index 00409b1..f9d7320 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -37,3 +37,9 @@ sinkhorn_unbalanced2 barycenter_unbalanced mm_unbalanced ``` + +## Partial optimal transport + +```@docs +entropic_partial_wasserstein +``` diff --git a/src/PythonOT.jl b/src/PythonOT.jl index d801f3d..4a1c71b 100644 --- a/src/PythonOT.jl +++ b/src/PythonOT.jl @@ -13,7 +13,8 @@ export emd, sinkhorn_unbalanced, sinkhorn_unbalanced2, empirical_sinkhorn_divergence, - mm_unbalanced + mm_unbalanced, + entropic_partial_wasserstein const pot = PyCall.PyNULL() diff --git a/src/lib.jl b/src/lib.jl index 07bb9af..c81ba30 100644 --- a/src/lib.jl +++ b/src/lib.jl @@ -306,29 +306,18 @@ Python function. # Examples ```jldoctest sinkhorn_unbalanced -julia> μ = [0.5, 0.2, 0.3]; +julia> μ = [0.5, 0.5]; -julia> ν = [0.0, 1.0]; +julia> ν = [0.5, 0.5]; -julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; +julia> C = [0.0 1.0; 1.0 0.0]; -julia> round.(sinkhorn_unbalanced(μ, ν, C, 0.01, 1_000); sigdigits=4) -3×2 Matrix{Float64}: - 0.0 0.5 - 0.0 0.2002 - 0.0 0.2998 +julia> round.(sinkhorn_unbalanced(μ, ν, C, 1, 1); sigdigits=7) +2×2 Matrix{Float64}: + 0.322054 0.118477 + 0.118477 0.322054 ``` -It is possible to provide multiple target marginals as columns of a matrix. In this case the -optimal transport costs are returned: - -```jldoctest sinkhorn_unbalanced -julia> ν = [0.0 0.5; 1.0 0.5]; - -julia> round.(sinkhorn_unbalanced(μ, ν, C, 0.01, 1_000); sigdigits=4) -2-element Vector{Float64}: - 0.9497 - 0.4494 ``` See also: [`sinkhorn_unbalanced2`](@ref) @@ -365,25 +354,14 @@ Python function. # Examples ```jldoctest sinkhorn_unbalanced2 -julia> μ = [0.5, 0.2, 0.3]; - -julia> ν = [0.0, 1.0]; - -julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; +julia> μ = [0.5, 0.1]; -julia> round.(sinkhorn_unbalanced2(μ, ν, C, 0.01, 1_000); sigdigits=4) -0.9497 -``` - -It is possible to provide multiple target marginals as columns of a matrix: +julia> ν = [0.5, 0.5]; -```jldoctest sinkhorn_unbalanced2 -julia> ν = [0.0 0.5; 1.0 0.5]; +julia> C = [0.0 1.0; 1.0 0.0]; -julia> round.(sinkhorn_unbalanced2(μ, ν, C, 0.01, 1_000); sigdigits=4) -2-element Vector{Float64}: - 0.9497 - 0.4494 +julia> round.(sinkhorn_unbalanced2(μ, ν, C, 1., 1.); sigdigits=8) +0.19600125 ``` See also: [`sinkhorn_unbalanced`](@ref) @@ -566,3 +544,52 @@ julia> round.(mm_unbalanced(a, b, M, 5, div="l2"), digits=2) function mm_unbalanced(a, b, M, reg_m; kwargs...) return pot.unbalanced.mm_unbalanced(a, b, M, reg_m; kwargs...) end + + +""" + entropic_partial_wasserstein(a, b, M, reg; kwargs...) + +Solves the partial optimal transport problem and returns the OT plan +The function considers the following problem: + +```math +\\gamma = \\mathop{\\arg \\min}_\\gamma \\quad \\langle \\gamma, +\\mathbf{M} \\rangle_F + \\mathrm{reg} \\cdot\\Omega(\\gamma) + +s.t. \\gamma \\mathbf{1} &\\leq \\mathbf{a} \\\\ + \\gamma^T \\mathbf{1} &\\leq \\mathbf{b} \\\\ + \\gamma &\\geq 0 \\\\ + \\mathbf{1}^T \\gamma^T \\mathbf{1} = m + &\\leq \\min\\{\\|\\mathbf{a}\\|_1, \\|\\mathbf{b}\\|_1\\} \\\\ +``` + +where : + +- `M` is the metric cost matrix +- ``\\Omega`` is the entropic regularization term, ``\\Omega=\\sum_{i,j} \\gamma_{i,j}\\log(\\gamma_{i,j})`` +- `a` and `b` are the sample weights +- `m` is the amount of mass to be transported + +This function is a wrapper of the function +[`entropic_partial_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.entropic_partial_wasserstein) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. + + +# Examples + +```jldoctest +julia> a = [.1, .2]; + +julia> b = [.1, .1]; + +julia> M = [0. 1.; 2. 3.]; + +julia> round.(entropic_partial_wasserstein(a, b, M, 1, m=0.1), digits=2) +2×2 Matrix{Float64}: + 0.06 0.02 + 0.01 0.0 +""" +function entropic_partial_wasserstein(a, b, M, reg; kwargs...) + return pot.partial.entropic_partial_wasserstein(a, b, M, reg; kwargs...) +end From 90877fa627b62cfd66d8fa150a6231f52a14af94 Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Tue, 21 Jan 2025 16:21:51 +0100 Subject: [PATCH 02/16] Update lib.jl Fix problems in doc generation --- src/lib.jl | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/lib.jl b/src/lib.jl index c81ba30..65460cb 100644 --- a/src/lib.jl +++ b/src/lib.jl @@ -555,20 +555,11 @@ The function considers the following problem: ```math \\gamma = \\mathop{\\arg \\min}_\\gamma \\quad \\langle \\gamma, \\mathbf{M} \\rangle_F + \\mathrm{reg} \\cdot\\Omega(\\gamma) - -s.t. \\gamma \\mathbf{1} &\\leq \\mathbf{a} \\\\ - \\gamma^T \\mathbf{1} &\\leq \\mathbf{b} \\\\ - \\gamma &\\geq 0 \\\\ - \\mathbf{1}^T \\gamma^T \\mathbf{1} = m - &\\leq \\min\\{\\|\\mathbf{a}\\|_1, \\|\\mathbf{b}\\|_1\\} \\\\ ``` -where : - -- `M` is the metric cost matrix -- ``\\Omega`` is the entropic regularization term, ``\\Omega=\\sum_{i,j} \\gamma_{i,j}\\log(\\gamma_{i,j})`` - `a` and `b` are the sample weights -- `m` is the amount of mass to be transported +- `M` is the metric cost matrix +- `reg` is a regularization term > 0 This function is a wrapper of the function [`entropic_partial_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.entropic_partial_wasserstein) in the @@ -589,6 +580,8 @@ julia> round.(entropic_partial_wasserstein(a, b, M, 1, m=0.1), digits=2) 2×2 Matrix{Float64}: 0.06 0.02 0.01 0.0 +``` + """ function entropic_partial_wasserstein(a, b, M, reg; kwargs...) return pot.partial.entropic_partial_wasserstein(a, b, M, reg; kwargs...) From f080ec7256a66f20459fe62226047ca0ad9c627e Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Fri, 21 Nov 2025 15:06:00 +0100 Subject: [PATCH 03/16] Add partial_wasserstein2 function and export Introduces the partial_wasserstein2 function in lib.jl, wrapping the corresponding PythonOT method, and exports it in PythonOT.jl for external use. --- src/PythonOT.jl | 3 ++- src/lib.jl | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/PythonOT.jl b/src/PythonOT.jl index 4a1c71b..8053369 100644 --- a/src/PythonOT.jl +++ b/src/PythonOT.jl @@ -14,7 +14,8 @@ export emd, sinkhorn_unbalanced2, empirical_sinkhorn_divergence, mm_unbalanced, - entropic_partial_wasserstein + entropic_partial_wasserstein, + partial_wasserstein2 const pot = PyCall.PyNULL() diff --git a/src/lib.jl b/src/lib.jl index 65460cb..462f3c5 100644 --- a/src/lib.jl +++ b/src/lib.jl @@ -586,3 +586,7 @@ julia> round.(entropic_partial_wasserstein(a, b, M, 1, m=0.1), digits=2) function entropic_partial_wasserstein(a, b, M, reg; kwargs...) return pot.partial.entropic_partial_wasserstein(a, b, M, reg; kwargs...) end + +function partial_wasserstein2(a, b, M; kwargs...) + return pot.partial.partial_wasserstein2(a, b, M; kwargs...) +end From 984f0ce1f189887ea15752c07c7e679060e52c8c Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Fri, 21 Nov 2025 15:07:21 +0100 Subject: [PATCH 04/16] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index edfce28..4a8f3f9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PythonOT" uuid = "3c485715-4278-42b2-9b5f-8f00e43c12ef" authors = ["David Widmann"] -version = "0.1.7" +version = "0.1.8" [deps] PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" From e569c662b32df544752ada6b4fc7aff35735c6af Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Fri, 21 Nov 2025 15:27:17 +0100 Subject: [PATCH 05/16] Add partial_wasserstein function to API Introduces the partial_wasserstein function to the Julia API, wrapping pot.partial.partial_wasserstein2. Also updates the package version to 0.1.9 and exports the new function. --- Project.toml | 2 +- src/PythonOT.jl | 1 + src/lib.jl | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 4a8f3f9..1d38470 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PythonOT" uuid = "3c485715-4278-42b2-9b5f-8f00e43c12ef" authors = ["David Widmann"] -version = "0.1.8" +version = "0.1.9" [deps] PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" diff --git a/src/PythonOT.jl b/src/PythonOT.jl index 8053369..f6f24df 100644 --- a/src/PythonOT.jl +++ b/src/PythonOT.jl @@ -15,6 +15,7 @@ export emd, empirical_sinkhorn_divergence, mm_unbalanced, entropic_partial_wasserstein, + partial_wasserstein, partial_wasserstein2 const pot = PyCall.PyNULL() diff --git a/src/lib.jl b/src/lib.jl index 462f3c5..c3e6442 100644 --- a/src/lib.jl +++ b/src/lib.jl @@ -587,6 +587,10 @@ function entropic_partial_wasserstein(a, b, M, reg; kwargs...) return pot.partial.entropic_partial_wasserstein(a, b, M, reg; kwargs...) end +function partial_wasserstein(a, b, M; kwargs...) + return pot.partial.partial_wasserstein2(a, b, M; kwargs...) +end + function partial_wasserstein2(a, b, M; kwargs...) return pot.partial.partial_wasserstein2(a, b, M; kwargs...) end From e285ca3ff6d00d1ddca6c531c73ceaa63b873c4f Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 26 Nov 2025 15:30:01 +0100 Subject: [PATCH 06/16] Add entropic_partial_gromov_wasserstein2 function Exports and implements the entropic_partial_gromov_wasserstein2 function, providing a Julia wrapper for the corresponding POT library method. --- src/PythonOT.jl | 1 + src/lib.jl | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/PythonOT.jl b/src/PythonOT.jl index f6f24df..bc50251 100644 --- a/src/PythonOT.jl +++ b/src/PythonOT.jl @@ -15,6 +15,7 @@ export emd, empirical_sinkhorn_divergence, mm_unbalanced, entropic_partial_wasserstein, + entropic_partial_gromov_wasserstein2, partial_wasserstein, partial_wasserstein2 diff --git a/src/lib.jl b/src/lib.jl index c3e6442..341bc96 100644 --- a/src/lib.jl +++ b/src/lib.jl @@ -594,3 +594,7 @@ end function partial_wasserstein2(a, b, M; kwargs...) return pot.partial.partial_wasserstein2(a, b, M; kwargs...) end + +function entropic_partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) + return pot.partial.entropic_partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) +end From 8d7b1c6980d234275f594030b3b706507d8a2a40 Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 26 Nov 2025 15:43:17 +0100 Subject: [PATCH 07/16] Add entropic_partial_gromov_wasserstein function Introduces the entropic_partial_gromov_wasserstein function to the API and exports it. This provides access to the corresponding functionality from the underlying pot.partial module. --- src/PythonOT.jl | 1 + src/lib.jl | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/PythonOT.jl b/src/PythonOT.jl index bc50251..dc51d6d 100644 --- a/src/PythonOT.jl +++ b/src/PythonOT.jl @@ -15,6 +15,7 @@ export emd, empirical_sinkhorn_divergence, mm_unbalanced, entropic_partial_wasserstein, + entropic_partial_gromov_wasserstein, entropic_partial_gromov_wasserstein2, partial_wasserstein, partial_wasserstein2 diff --git a/src/lib.jl b/src/lib.jl index 341bc96..f78db7c 100644 --- a/src/lib.jl +++ b/src/lib.jl @@ -595,6 +595,10 @@ function partial_wasserstein2(a, b, M; kwargs...) return pot.partial.partial_wasserstein2(a, b, M; kwargs...) end +function entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) + return pot.partial.entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) +end + function entropic_partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) return pot.partial.entropic_partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) end From 90aa8c71a5372269e9a3d5b5f5cbd1a15ff20ea8 Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 10:24:06 +0100 Subject: [PATCH 08/16] Expand and reorganize POT wrappers Group wrappers by solver family and expose additional transport algorithms, utilities, and domain adaptation functions. Fix partial Wasserstein plan and entropic partial Gromov cost wrappers, and update API documentation and tests. --- docs/src/api.md | 70 ++++++ src/PythonOT.jl | 44 +++- src/bregman.jl | 291 ++++++++++++++++++++++ src/da.jl | 38 +++ src/gromov.jl | 144 +++++++++++ src/lib.jl | 604 ---------------------------------------------- src/lp.jl | 207 ++++++++++++++++ src/partial.jl | 131 ++++++++++ src/sliced.jl | 51 ++++ src/smooth.jl | 17 +- src/unbalanced.jl | 252 +++++++++++++++++++ src/utils.jl | 51 ++++ test/runtests.jl | 46 ++++ 13 files changed, 1339 insertions(+), 607 deletions(-) create mode 100644 src/bregman.jl create mode 100644 src/da.jl create mode 100644 src/gromov.jl delete mode 100644 src/lib.jl create mode 100644 src/lp.jl create mode 100644 src/partial.jl create mode 100644 src/sliced.jl create mode 100644 src/unbalanced.jl create mode 100644 src/utils.jl diff --git a/docs/src/api.md b/docs/src/api.md index f9d7320..7b4db7c 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -7,6 +7,20 @@ emd emd2 emd_1d emd2_1d +wasserstein_1d +wasserstein_circle +binary_search_circle +linear_circular_ot +free_support_barycenter +barycenter_debiased +convolutional_barycenter2d +sinkhorn_log +sinkhorn_stabilized +sinkhorn_epsilon_scaling +greenkhorn +screenkhorn +empirical_sinkhorn +empirical_sinkhorn2 ``` ## Regularized optimal transport @@ -27,6 +41,7 @@ using PythonOT.Smooth ```@docs PythonOT.Smooth.smooth_ot_dual +PythonOT.Smooth.smooth_ot_semi_dual ``` ## Unbalanced optimal transport @@ -36,10 +51,65 @@ sinkhorn_unbalanced sinkhorn_unbalanced2 barycenter_unbalanced mm_unbalanced +mm_unbalanced2 +sinkhorn_unbalanced_translation_invariant +lbfgsb_unbalanced +lbfgsb_unbalanced2 +uot_1d ``` ## Partial optimal transport ```@docs entropic_partial_wasserstein +partial_wasserstein +partial_wasserstein2 +partial_wasserstein_lagrange +``` + +## Gromov-Wasserstein + +```@docs +gromov_wasserstein +gromov_wasserstein2 +entropic_gromov_wasserstein +entropic_gromov_wasserstein2 +fused_gromov_wasserstein +fused_gromov_wasserstein2 +entropic_fused_gromov_wasserstein +semirelaxed_gromov_wasserstein +partial_gromov_wasserstein +partial_gromov_wasserstein2 +entropic_partial_gromov_wasserstein +entropic_partial_gromov_wasserstein2 +gromov_barycenters +fgw_barycenters +``` + +## Sliced optimal transport + +```@docs +sliced_wasserstein_distance +max_sliced_wasserstein_distance +sliced_wasserstein_sphere +PythonOT.get_random_projections +``` + +## Domain adaptation + +```@docs +emd_laplace +sinkhorn_l1l2_gl +sinkhorn_lpl1_mm +``` + +## Utilities and generic solvers + +These helpers are not exported; call them as `PythonOT.dist`, etc. + +```@docs +PythonOT.dist +PythonOT.unif +PythonOT.solve +PythonOT.solve_sample ``` diff --git a/src/PythonOT.jl b/src/PythonOT.jl index dc51d6d..44826fc 100644 --- a/src/PythonOT.jl +++ b/src/PythonOT.jl @@ -18,11 +18,51 @@ export emd, entropic_partial_gromov_wasserstein, entropic_partial_gromov_wasserstein2, partial_wasserstein, - partial_wasserstein2 + partial_wasserstein2, + gromov_wasserstein, + entropic_gromov_wasserstein, + partial_wasserstein_lagrange, + partial_gromov_wasserstein, + partial_gromov_wasserstein2, + wasserstein_1d, + wasserstein_circle, + binary_search_circle, + linear_circular_ot, + free_support_barycenter, + sinkhorn_log, + sinkhorn_stabilized, + sinkhorn_epsilon_scaling, + greenkhorn, + screenkhorn, + empirical_sinkhorn, + empirical_sinkhorn2, + barycenter_debiased, + convolutional_barycenter2d, + mm_unbalanced2, + sinkhorn_unbalanced_translation_invariant, + lbfgsb_unbalanced, + lbfgsb_unbalanced2, + uot_1d, + gromov_wasserstein2, + entropic_gromov_wasserstein2, + fused_gromov_wasserstein, + fused_gromov_wasserstein2, + entropic_fused_gromov_wasserstein, + semirelaxed_gromov_wasserstein, + gromov_barycenters, + fgw_barycenters, + sliced_wasserstein_distance, + max_sliced_wasserstein_distance, + sliced_wasserstein_sphere, + emd_laplace, + sinkhorn_l1l2_gl, + sinkhorn_lpl1_mm const pot = PyCall.PyNULL() -include("lib.jl") +for f in ("lp", "bregman", "unbalanced", "partial", "gromov", "sliced", "da", "utils") + include(f * ".jl") +end include("smooth.jl") function __init__() diff --git a/src/bregman.jl b/src/bregman.jl new file mode 100644 index 0000000..2430d36 --- /dev/null +++ b/src/bregman.jl @@ -0,0 +1,291 @@ +""" + sinkhorn(μ, ν, C, ε; kwargs...) + +Compute the optimal transport plan for the entropic regularization optimal transport problem +with source and target marginals `μ` and `ν`, cost matrix `C` of size +`(length(μ), length(ν))`, and entropic regularization parameter `ε`. + +The optimal transport plan `γ` is of the same size as `C` and solves +```math +\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle ++ \\varepsilon \\Omega(\\gamma), +``` +where ``\\Omega(\\gamma) = \\sum_{i,j} \\gamma_{i,j} \\log \\gamma_{i,j}`` is the entropic +regularization term. + +This function is a wrapper of the function +[`sinkhorn`](https://pythonot.github.io/all.html#ot.sinkhorn) in the Python Optimal +Transport package. Keyword arguments are listed in the documentation of the Python function. + +# Examples + +```jldoctest sinkhorn +julia> μ = [0.5, 0.2, 0.3]; + +julia> ν = [0.0, 1.0]; + +julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; + +julia> sinkhorn(μ, ν, C, 0.01) +3×2 Matrix{Float64}: + 0.0 0.5 + 0.0 0.2 + 0.0 0.3 +``` + +It is possible to provide multiple target marginals as columns of a matrix. In this case the +optimal transport costs are returned: + +```jldoctest sinkhorn +julia> ν = [0.0 0.5; 1.0 0.5]; + +julia> round.(sinkhorn(μ, ν, C, 0.01); sigdigits=6) +2-element Vector{Float64}: + 0.95 + 0.45 +``` + +See also: [`sinkhorn2`](@ref) +""" +function sinkhorn(μ, ν, C, ε; kwargs...) + return pot.sinkhorn(μ, ν, PyCall.PyReverseDims(permutedims(C)), ε; kwargs...) +end + +""" + sinkhorn2(μ, ν, C, ε; kwargs...) + +Compute the optimal transport cost for the entropic regularization optimal transport problem +with source and target marginals `μ` and `ν`, cost matrix `C` of size +`(length(μ), length(ν))`, and entropic regularization parameter `ε`. + +The optimal transport cost is the scalar value +```math +\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle ++ \\varepsilon \\Omega(\\gamma), +``` +where ``\\Omega(\\gamma) = \\sum_{i,j} \\gamma_{i,j} \\log \\gamma_{i,j}`` is the entropic +regularization term. + +This function is a wrapper of the function +[`sinkhorn2`](https://pythonot.github.io/all.html#ot.sinkhorn2) in the Python Optimal +Transport package. Keyword arguments are listed in the documentation of the Python function. + +# Examples + +```jldoctest sinkhorn2 +julia> μ = [0.5, 0.2, 0.3]; + +julia> ν = [0.0, 1.0]; + +julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; + +julia> round(sinkhorn2(μ, ν, C, 0.01); sigdigits=6) +0.95 +``` + +It is possible to provide multiple target marginals as columns of a matrix. + +```jldoctest sinkhorn2 +julia> ν = [0.0 0.5; 1.0 0.5]; + +julia> round.(sinkhorn2(μ, ν, C, 0.01); sigdigits=6) +2-element Vector{Float64}: + 0.95 + 0.45 +``` + +See also: [`sinkhorn`](@ref) +""" +function sinkhorn2(μ, ν, C, ε; kwargs...) + return pot.sinkhorn2(μ, ν, PyCall.PyReverseDims(permutedims(C)), ε; kwargs...) +end + +""" + empirical_sinkhorn_divergence(xsource, xtarget, ε; kwargs...) + +Compute the Sinkhorn divergence from empirical data, where `xsource` and `xtarget` are +arrays representing samples in the source domain and target domain, respectively, and `ε` +is the regularization term. + +This function is a wrapper of the function +[`ot.bregman.empirical_sinkhorn_divergence`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.empirical_sinkhorn_divergence) +in the Python Optimal Transport package. Keyword arguments are listed in the documentation of the Python function. + +# Examples + +```jldoctest +julia> xsource = [1.0]; + +julia> xtarget = [2.0, 3.0]; + +julia> ε = 0.01; + +julia> empirical_sinkhorn_divergence(xsource, xtarget, ε) ≈ + sinkhorn2([1], [0.5, 0.5], [1.0 4.0], ε) - + ( + sinkhorn2([1], [1], zeros(1, 1), ε) + + sinkhorn2([0.5, 0.5], [0.5, 0.5], Float64[0 1; 1 0], ε) + ) / 2 +true +``` + +See also: [`sinkhorn2`](@ref) +""" +function empirical_sinkhorn_divergence(xsource, xtarget, ε; kwargs...) + return pot.bregman.empirical_sinkhorn_divergence( + reshape(xsource, Val(2)), reshape(xtarget, Val(2)), ε; kwargs... + ) +end + +""" + barycenter(A, C, ε; kwargs...) + +Compute the entropically regularized Wasserstein barycenter with histograms `A`, cost matrix +`C`, and entropic regularization parameter `ε`. + +The Wasserstein barycenter is a histogram and solves +```math +\\inf_{a} \\sum_{i} W_{\\varepsilon,C}(a, a_i), +``` +where the histograms ``a_i`` are columns of matrix `A` and ``W_{\\varepsilon,C}(a, a_i)`` +is the optimal transport cost for the entropically regularized optimal transport problem +with marginals ``a`` and ``a_i``, cost matrix ``C``, and entropic regularization parameter +``\\varepsilon``. Optionally, weights of the histograms ``a_i`` can be provided with the +keyword argument `weights`. + +This function is a wrapper of the function +[`barycenter`](https://pythonot.github.io/all.html#ot.barycenter) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. + +# Examples + +```jldoctest +julia> A = rand(10, 3); + +julia> A ./= sum(A; dims=1); + +julia> C = rand(10, 10); + +julia> isapprox(sum(barycenter(A, C, 0.01; method="sinkhorn_stabilized")), 1; atol=1e-4) +true +``` +""" +barycenter(A, C, ε; kwargs...) = pot.barycenter(A, C, ε; kwargs...) + +""" + sinkhorn_log(μ, ν, C, ε; kwargs...) + +Solve entropic regularized OT with the log-domain Sinkhorn algorithm and return the plan. + +This function is a wrapper of the function +[`ot.bregman.sinkhorn_log`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.sinkhorn_log) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function sinkhorn_log(μ, ν, C, ε; kwargs...) + return pot.bregman.sinkhorn_log(μ, ν, C, ε; kwargs...) +end + +""" + sinkhorn_stabilized(μ, ν, C, ε; kwargs...) + +Solve entropic regularized OT with the stabilized Sinkhorn algorithm and return the plan. + +This function is a wrapper of the function +[`ot.bregman.sinkhorn_stabilized`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.sinkhorn_stabilized) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function sinkhorn_stabilized(μ, ν, C, ε; kwargs...) + return pot.bregman.sinkhorn_stabilized(μ, ν, C, ε; kwargs...) +end + +""" + sinkhorn_epsilon_scaling(μ, ν, C, ε; kwargs...) + +Solve entropic regularized OT with Sinkhorn and epsilon scaling and return the plan. + +This function is a wrapper of the function +[`ot.bregman.sinkhorn_epsilon_scaling`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.sinkhorn_epsilon_scaling) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function sinkhorn_epsilon_scaling(μ, ν, C, ε; kwargs...) + return pot.bregman.sinkhorn_epsilon_scaling(μ, ν, C, ε; kwargs...) +end + +""" + greenkhorn(μ, ν, C, ε; kwargs...) + +Solve entropic regularized OT with the Greenkhorn algorithm and return the plan. + +This function is a wrapper of the function +[`ot.bregman.greenkhorn`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.greenkhorn) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function greenkhorn(μ, ν, C, ε; kwargs...) + return pot.bregman.greenkhorn(μ, ν, C, ε; kwargs...) +end + +""" + screenkhorn(μ, ν, C, ε; kwargs...) + +Solve entropic regularized OT with the Screenkhorn screening algorithm and return the plan. + +This function is a wrapper of the function +[`ot.bregman.screenkhorn`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.screenkhorn) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function screenkhorn(μ, ν, C, ε; kwargs...) + return pot.bregman.screenkhorn(μ, ν, C, ε; kwargs...) +end + +""" + empirical_sinkhorn(xsource, xtarget, ε; kwargs...) + +Solve entropic regularized OT between empirical samples (rows are samples) and return the plan. + +This function is a wrapper of the function +[`ot.bregman.empirical_sinkhorn`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.empirical_sinkhorn) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function empirical_sinkhorn(xsource, xtarget, ε; kwargs...) + return pot.bregman.empirical_sinkhorn(xsource, xtarget, ε; kwargs...) +end + +""" + empirical_sinkhorn2(xsource, xtarget, ε; kwargs...) + +Solve entropic regularized OT between empirical samples (rows are samples) and return the cost. + +This function is a wrapper of the function +[`ot.bregman.empirical_sinkhorn2`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.empirical_sinkhorn2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function empirical_sinkhorn2(xsource, xtarget, ε; kwargs...) + return pot.bregman.empirical_sinkhorn2(xsource, xtarget, ε; kwargs...) +end + +""" + barycenter_debiased(A, C, ε; kwargs...) + +Compute the debiased entropically regularized Wasserstein barycenter of histograms `A` (columns). + +This function is a wrapper of the function +[`ot.bregman.barycenter_debiased`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.barycenter_debiased) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function barycenter_debiased(A, C, ε; kwargs...) + return pot.bregman.barycenter_debiased(A, C, ε; kwargs...) +end + +""" + convolutional_barycenter2d(A, ε; kwargs...) + +Compute the entropic Wasserstein barycenter of 2D images stacked in the 3D array `A` with a convolutional Sinkhorn scheme. + +This function is a wrapper of the function +[`ot.bregman.convolutional_barycenter2d`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.convolutional_barycenter2d) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function convolutional_barycenter2d(A, ε; kwargs...) + return pot.bregman.convolutional_barycenter2d(A, ε; kwargs...) +end diff --git a/src/da.jl b/src/da.jl new file mode 100644 index 0000000..c4a1049 --- /dev/null +++ b/src/da.jl @@ -0,0 +1,38 @@ +""" + emd_laplace(a, b, xs, xt, M; kwargs...) + +Solve the optimal transport problem with Laplacian regularization (domain adaptation). + +This function is a wrapper of the function +[`ot.da.emd_laplace`](https://pythonot.github.io/gen_modules/ot.da.html#ot.da.emd_laplace) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function emd_laplace(a, b, xs, xt, M; kwargs...) + return pot.da.emd_laplace(a, b, xs, xt, M; kwargs...) +end + +""" + sinkhorn_l1l2_gl(a, labels_a, b, M, reg; kwargs...) + +Solve entropic OT with a group-lasso (l1-l2) class regularizer (domain adaptation). + +This function is a wrapper of the function +[`ot.da.sinkhorn_l1l2_gl`](https://pythonot.github.io/gen_modules/ot.da.html#ot.da.sinkhorn_l1l2_gl) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function sinkhorn_l1l2_gl(a, labels_a, b, M, reg; kwargs...) + return pot.da.sinkhorn_l1l2_gl(a, labels_a, b, M, reg; kwargs...) +end + +""" + sinkhorn_lpl1_mm(a, labels_a, b, M, reg; kwargs...) + +Solve entropic OT with an lp-l1 class regularizer (domain adaptation). + +This function is a wrapper of the function +[`ot.da.sinkhorn_lpl1_mm`](https://pythonot.github.io/gen_modules/ot.da.html#ot.da.sinkhorn_lpl1_mm) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function sinkhorn_lpl1_mm(a, labels_a, b, M, reg; kwargs...) + return pot.da.sinkhorn_lpl1_mm(a, labels_a, b, M, reg; kwargs...) +end diff --git a/src/gromov.jl b/src/gromov.jl new file mode 100644 index 0000000..2a0703d --- /dev/null +++ b/src/gromov.jl @@ -0,0 +1,144 @@ +""" + gromov_wasserstein(μ, ν, Cμ, Cν, loss = "square_loss"; kwargs...) + +Compute the exact Gromov-Wasserstein transport plan between `(μ, Cμ)` and `(ν, Cν)`. + +The Gromov-Wasserstein transport problem seeks to find a minimizer of +```math +\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\sum_{i, j, k, l} L((C_μ)_{ik}, (C_ν)_{jl}) \\gamma_{ij} \\gamma_{kl}, +``` +where ``L`` is quadratic (`loss = "square_loss"`) or the Kullback-Leibler divergence (`loss = "kl_loss"`). + +This function is a wrapper of the function +[`gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.gromov_wasserstein) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. +""" +function gromov_wasserstein(μ, ν, Cμ, Cν, loss="square_loss"; kwargs...) + return pot.gromov.gromov_wasserstein(Cμ, Cν, μ, ν, loss; kwargs...) +end + +""" + entropic_gromov_wasserstein(μ, ν, Cμ, Cν, ε, loss = "square_loss"; kwargs...) + +Compute the entropy-regularized Gromov-Wasserstein transport plan between `(μ, Cμ)` and `(ν, Cν)` with parameter `ε`. + +The entropy-regularized Gromov-Wasserstein transport problem seeks to find a minimizer of +```math +\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\sum_{i, j, k, l} L((C_μ)_{ik}, (C_ν)_{jl}) \\gamma_{ij} \\gamma_{kl} + ε \\Omega(\\gamma), +``` +where ``L`` is quadratic (`loss = "square_loss"`) or the Kullback-Leibler divergence (`loss = "kl_loss"`) +and ``\\Omega(\\gamma) = \\sum_{ij} \\gamma_{ij} \\log(\\gamma_{ij})`` is the entropic regularization term. + +This function is a wrapper of the function +[`entropic_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.entropic_gromov_wasserstein) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. +""" +function entropic_gromov_wasserstein(μ, ν, Cμ, Cν, ε, loss="square_loss"; kwargs...) + return pot.gromov.entropic_gromov_wasserstein(Cμ, Cν, μ, ν, loss, ε; kwargs...) +end + +""" + gromov_wasserstein2(μ, ν, Cμ, Cν, loss="square_loss"; kwargs...) + +Compute the exact Gromov-Wasserstein cost between `(μ, Cμ)` and `(ν, Cν)`. + +This function is a wrapper of the function +[`ot.gromov.gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.gromov_wasserstein2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function gromov_wasserstein2(μ, ν, Cμ, Cν, loss="square_loss"; kwargs...) + return pot.gromov.gromov_wasserstein2(Cμ, Cν, μ, ν, loss; kwargs...) +end + +""" + entropic_gromov_wasserstein2(μ, ν, Cμ, Cν, ε, loss="square_loss"; kwargs...) + +Compute the entropy-regularized Gromov-Wasserstein cost between `(μ, Cμ)` and `(ν, Cν)`. + +This function is a wrapper of the function +[`ot.gromov.entropic_gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.entropic_gromov_wasserstein2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function entropic_gromov_wasserstein2(μ, ν, Cμ, Cν, ε, loss="square_loss"; kwargs...) + return pot.gromov.entropic_gromov_wasserstein2(Cμ, Cν, μ, ν, loss, ε; kwargs...) +end + +""" + fused_gromov_wasserstein(μ, ν, M, Cμ, Cν, loss="square_loss"; kwargs...) + +Compute the fused Gromov-Wasserstein transport plan with feature cost `M` (weight `alpha`). + +This function is a wrapper of the function +[`ot.gromov.fused_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.fused_gromov_wasserstein) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function fused_gromov_wasserstein(μ, ν, M, Cμ, Cν, loss="square_loss"; kwargs...) + return pot.gromov.fused_gromov_wasserstein(M, Cμ, Cν, μ, ν, loss; kwargs...) +end + +""" + fused_gromov_wasserstein2(μ, ν, M, Cμ, Cν, loss="square_loss"; kwargs...) + +Compute the fused Gromov-Wasserstein cost with feature cost `M` (weight `alpha`). + +This function is a wrapper of the function +[`ot.gromov.fused_gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.fused_gromov_wasserstein2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function fused_gromov_wasserstein2(μ, ν, M, Cμ, Cν, loss="square_loss"; kwargs...) + return pot.gromov.fused_gromov_wasserstein2(M, Cμ, Cν, μ, ν, loss; kwargs...) +end + +""" + entropic_fused_gromov_wasserstein(μ, ν, M, Cμ, Cν, ε, loss="square_loss"; kwargs...) + +Compute the entropy-regularized fused Gromov-Wasserstein transport plan. + +This function is a wrapper of the function +[`ot.gromov.entropic_fused_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.entropic_fused_gromov_wasserstein) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function entropic_fused_gromov_wasserstein(μ, ν, M, Cμ, Cν, ε, loss="square_loss"; kwargs...) + return pot.gromov.entropic_fused_gromov_wasserstein(M, Cμ, Cν, μ, ν, loss, ε; kwargs...) +end + +""" + semirelaxed_gromov_wasserstein(Cμ, Cν, μ, loss="square_loss"; kwargs...) + +Compute the semi-relaxed Gromov-Wasserstein transport plan (target marginal is free). + +This function is a wrapper of the function +[`ot.gromov.semirelaxed_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.semirelaxed_gromov_wasserstein) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function semirelaxed_gromov_wasserstein(Cμ, Cν, μ, loss="square_loss"; kwargs...) + return pot.gromov.semirelaxed_gromov_wasserstein(Cμ, Cν, μ, loss; kwargs...) +end + +""" + gromov_barycenters(N, Cs; kwargs...) + +Compute the Gromov-Wasserstein barycenter structure matrix of size `N` from the structure matrices `Cs`. + +This function is a wrapper of the function +[`ot.gromov.gromov_barycenters`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.gromov_barycenters) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function gromov_barycenters(N, Cs; kwargs...) + return pot.gromov.gromov_barycenters(N, Cs; kwargs...) +end + +""" + fgw_barycenters(N, Ys, Cs; kwargs...) + +Compute the fused Gromov-Wasserstein barycenter (features, structure) of size `N`. + +This function is a wrapper of the function +[`ot.gromov.fgw_barycenters`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.fgw_barycenters) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function fgw_barycenters(N, Ys, Cs; kwargs...) + return pot.gromov.fgw_barycenters(N, Ys, Cs; kwargs...) +end diff --git a/src/lib.jl b/src/lib.jl deleted file mode 100644 index f78db7c..0000000 --- a/src/lib.jl +++ /dev/null @@ -1,604 +0,0 @@ -""" - emd(μ, ν, C; kwargs...) - -Compute the optimal transport plan for the Monge-Kantorovich problem with source and target -marginals `μ` and `ν` and cost matrix `C` of size `(length(μ), length(ν))`. - -The optimal transport plan `γ` is of the same size as `C` and solves -```math -\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle. -``` - -This function is a wrapper of the function -[`emd`](https://pythonot.github.io/all.html#ot.emd) in the Python Optimal Transport -package. Keyword arguments are listed in the documentation of the Python function. - -# Examples - -```jldoctest -julia> μ = [0.5, 0.2, 0.3]; - -julia> ν = [0.0, 1.0]; - -julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; - -julia> emd(μ, ν, C) -3×2 Matrix{Float64}: - 0.0 0.5 - 0.0 0.2 - 0.0 0.3 -``` - -See also: [`emd2`](@ref) -""" -function emd(μ, ν, C; kwargs...) - return pot.lp.emd(μ, ν, PyCall.PyReverseDims(permutedims(C)); kwargs...) -end - -""" - emd2(μ, ν, C; kwargs...) - -Compute the optimal transport cost for the Monge-Kantorovich problem with source and target -marginals `μ` and `ν` and cost matrix `C` of size `(length(μ), length(ν))`. - -The optimal transport cost is the scalar value -```math -\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle. -``` - -This function is a wrapper of the function -[`emd2`](https://pythonot.github.io/all.html#ot.emd2) in the Python Optimal Transport -package. Keyword arguments are listed in the documentation of the Python function. - -# Examples - -```jldoctest -julia> μ = [0.5, 0.2, 0.3]; - -julia> ν = [0.0, 1.0]; - -julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; - -julia> emd2(μ, ν, C) -0.95 -``` - -See also: [`emd`](@ref) -""" -function emd2(μ, ν, C; kwargs...) - return pot.lp.emd2(μ, ν, PyCall.PyReverseDims(permutedims(C)); kwargs...) -end - -""" - emd_1d(xsource, xtarget; kwargs...) - -Compute the optimal transport plan for the Monge-Kantorovich problem with univariate -discrete measures with support `xsource` and `xtarget` as source and target marginals. - -This function is a wrapper of the function -[`emd_1d`](https://pythonot.github.io/all.html#ot.emd_1d) in the Python Optimal Transport -package. Keyword arguments are listed in the documentation of the Python function. - -# Examples - -```jldoctest -julia> xsource = [0.2, 0.5]; - -julia> xtarget = [0.8, 0.3]; - -julia> emd_1d(xsource, xtarget) -2×2 Matrix{Float64}: - 0.0 0.5 - 0.5 0.0 - -julia> histogram_source = [0.8, 0.2]; - -julia> histogram_target = [0.7, 0.3]; - -julia> emd_1d(xsource, xtarget; a=histogram_source, b=histogram_target) -2×2 Matrix{Float64}: - 0.5 0.3 - 0.2 0.0 -``` - -See also: [`emd`](@ref), [`emd2_1d`](@ref) -""" -function emd_1d(xsource, xtarget; kwargs...) - return pot.lp.emd_1d(xsource, xtarget; kwargs...) -end - -""" - emd2_1d(xsource, xtarget; kwargs...) - -Compute the optimal transport cost for the Monge-Kantorovich problem with univariate -discrete measures with support `xsource` and `xtarget` as source and target marginals. - -This function is a wrapper of the function -[`emd2_1d`](https://pythonot.github.io/all.html#ot.emd2_1d) in the Python Optimal Transport -package. Keyword arguments are listed in the documentation of the Python function. - -# Examples - -```jldoctest -julia> xsource = [0.2, 0.5]; - -julia> xtarget = [0.8, 0.3]; - -julia> round(emd2_1d(xsource, xtarget); sigdigits=6) -0.05 - -julia> histogram_source = [0.8, 0.2]; - -julia> histogram_target = [0.7, 0.3]; - -julia> round(emd2_1d(xsource, xtarget; a=histogram_source, b=histogram_target); sigdigits=6) -0.201 -``` - -See also: [`emd2`](@ref), [`emd2_1d`](@ref) -""" -function emd2_1d(xsource, xtarget; kwargs...) - return pot.lp.emd2_1d(xsource, xtarget; kwargs...) -end - -""" - sinkhorn(μ, ν, C, ε; kwargs...) - -Compute the optimal transport plan for the entropic regularization optimal transport problem -with source and target marginals `μ` and `ν`, cost matrix `C` of size -`(length(μ), length(ν))`, and entropic regularization parameter `ε`. - -The optimal transport plan `γ` is of the same size as `C` and solves -```math -\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle -+ \\varepsilon \\Omega(\\gamma), -``` -where ``\\Omega(\\gamma) = \\sum_{i,j} \\gamma_{i,j} \\log \\gamma_{i,j}`` is the entropic -regularization term. - -This function is a wrapper of the function -[`sinkhorn`](https://pythonot.github.io/all.html#ot.sinkhorn) in the Python Optimal -Transport package. Keyword arguments are listed in the documentation of the Python function. - -# Examples - -```jldoctest sinkhorn -julia> μ = [0.5, 0.2, 0.3]; - -julia> ν = [0.0, 1.0]; - -julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; - -julia> sinkhorn(μ, ν, C, 0.01) -3×2 Matrix{Float64}: - 0.0 0.5 - 0.0 0.2 - 0.0 0.3 -``` - -It is possible to provide multiple target marginals as columns of a matrix. In this case the -optimal transport costs are returned: - -```jldoctest sinkhorn -julia> ν = [0.0 0.5; 1.0 0.5]; - -julia> round.(sinkhorn(μ, ν, C, 0.01); sigdigits=6) -2-element Vector{Float64}: - 0.95 - 0.45 -``` - -See also: [`sinkhorn2`](@ref) -""" -function sinkhorn(μ, ν, C, ε; kwargs...) - return pot.sinkhorn(μ, ν, PyCall.PyReverseDims(permutedims(C)), ε; kwargs...) -end - -""" - sinkhorn2(μ, ν, C, ε; kwargs...) - -Compute the optimal transport cost for the entropic regularization optimal transport problem -with source and target marginals `μ` and `ν`, cost matrix `C` of size -`(length(μ), length(ν))`, and entropic regularization parameter `ε`. - -The optimal transport cost is the scalar value -```math -\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle -+ \\varepsilon \\Omega(\\gamma), -``` -where ``\\Omega(\\gamma) = \\sum_{i,j} \\gamma_{i,j} \\log \\gamma_{i,j}`` is the entropic -regularization term. - -This function is a wrapper of the function -[`sinkhorn2`](https://pythonot.github.io/all.html#ot.sinkhorn2) in the Python Optimal -Transport package. Keyword arguments are listed in the documentation of the Python function. - -# Examples - -```jldoctest sinkhorn2 -julia> μ = [0.5, 0.2, 0.3]; - -julia> ν = [0.0, 1.0]; - -julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; - -julia> round(sinkhorn2(μ, ν, C, 0.01); sigdigits=6) -0.95 -``` - -It is possible to provide multiple target marginals as columns of a matrix. - -```jldoctest sinkhorn2 -julia> ν = [0.0 0.5; 1.0 0.5]; - -julia> round.(sinkhorn2(μ, ν, C, 0.01); sigdigits=6) -2-element Vector{Float64}: - 0.95 - 0.45 -``` - -See also: [`sinkhorn`](@ref) -""" -function sinkhorn2(μ, ν, C, ε; kwargs...) - return pot.sinkhorn2(μ, ν, PyCall.PyReverseDims(permutedims(C)), ε; kwargs...) -end - -""" - empirical_sinkhorn_divergence(xsource, xtarget, ε; kwargs...) - -Compute the Sinkhorn divergence from empirical data, where `xsource` and `xtarget` are -arrays representing samples in the source domain and target domain, respectively, and `ε` -is the regularization term. - -This function is a wrapper of the function -[`ot.bregman.empirical_sinkhorn_divergence`](https://pythonot.github.io/gen_modules/ot.bregman.html#ot.bregman.empirical_sinkhorn_divergence) -in the Python Optimal Transport package. Keyword arguments are listed in the documentation of the Python function. - -# Examples - -```jldoctest -julia> xsource = [1.0]; - -julia> xtarget = [2.0, 3.0]; - -julia> ε = 0.01; - -julia> empirical_sinkhorn_divergence(xsource, xtarget, ε) ≈ - sinkhorn2([1], [0.5, 0.5], [1.0 4.0], ε) - - ( - sinkhorn2([1], [1], zeros(1, 1), ε) + - sinkhorn2([0.5, 0.5], [0.5, 0.5], Float64[0 1; 1 0], ε) - ) / 2 -true -``` - -See also: [`sinkhorn2`](@ref) -""" -function empirical_sinkhorn_divergence(xsource, xtarget, ε; kwargs...) - return pot.bregman.empirical_sinkhorn_divergence( - reshape(xsource, Val(2)), reshape(xtarget, Val(2)), ε; kwargs... - ) -end - -""" - sinkhorn_unbalanced(μ, ν, C, ε, λ; kwargs...) - -Compute the optimal transport plan for the unbalanced entropic regularization optimal -transport problem with source and target marginals `μ` and `ν`, cost matrix `C` of size -`(length(μ), length(ν))`, entropic regularization parameter `ε`, and marginal relaxation -term `λ`. - -The optimal transport plan `γ` is of the same size as `C` and solves -```math -\\inf_{\\gamma} \\langle \\gamma, C \\rangle -+ \\varepsilon \\Omega(\\gamma) -+ \\lambda \\mathrm{KL}(\\gamma 1, \\mu) -+ \\lambda \\mathrm{KL}(\\gamma^{\\mathsf{T}} 1, \\nu), -``` -where ``\\Omega(\\gamma) = \\sum_{i,j} \\gamma_{i,j} \\log \\gamma_{i,j}`` is the entropic -regularization term and ``\\mathrm{KL}`` is the Kullback-Leibler divergence. - -This function is a wrapper of the function -[`sinkhorn_unbalanced`](https://pythonot.github.io/all.html#ot.sinkhorn_unbalanced) in the -Python Optimal Transport package. Keyword arguments are listed in the documentation of the -Python function. - -# Examples - -```jldoctest sinkhorn_unbalanced -julia> μ = [0.5, 0.5]; - -julia> ν = [0.5, 0.5]; - -julia> C = [0.0 1.0; 1.0 0.0]; - -julia> round.(sinkhorn_unbalanced(μ, ν, C, 1, 1); sigdigits=7) -2×2 Matrix{Float64}: - 0.322054 0.118477 - 0.118477 0.322054 -``` - -``` - -See also: [`sinkhorn_unbalanced2`](@ref) -""" -function sinkhorn_unbalanced(μ, ν, C, ε, λ; kwargs...) - return pot.sinkhorn_unbalanced( - μ, ν, PyCall.PyReverseDims(permutedims(C)), ε, λ; kwargs... - ) -end - -""" - sinkhorn_unbalanced2(μ, ν, C, ε, λ; kwargs...) - -Compute the optimal transport cost for the unbalanced entropic regularization optimal -transport problem with source and target marginals `μ` and `ν`, cost matrix `C` of size -`(length(μ), length(ν))`, entropic regularization parameter `ε`, and marginal relaxation -term `λ`. - -The optimal transport cost is the scalar value -```math -\\inf_{\\gamma} \\langle \\gamma, C \\rangle -+ \\varepsilon \\Omega(\\gamma) -+ \\lambda \\mathrm{KL}(\\gamma 1, \\mu) -+ \\lambda \\mathrm{KL}(\\gamma^{\\mathsf{T}} 1, \\nu), -``` -where ``\\Omega(\\gamma) = \\sum_{i,j} \\gamma_{i,j} \\log \\gamma_{i,j}`` is the entropic -regularization term and ``\\mathrm{KL}`` is the Kullback-Leibler divergence. - -This function is a wrapper of the function -[`sinkhorn_unbalanced2`](https://pythonot.github.io/all.html#ot.sinkhorn_unbalanced2) in the -Python Optimal Transport package. Keyword arguments are listed in the documentation of the -Python function. - -# Examples - -```jldoctest sinkhorn_unbalanced2 -julia> μ = [0.5, 0.1]; - -julia> ν = [0.5, 0.5]; - -julia> C = [0.0 1.0; 1.0 0.0]; - -julia> round.(sinkhorn_unbalanced2(μ, ν, C, 1., 1.); sigdigits=8) -0.19600125 -``` - -See also: [`sinkhorn_unbalanced`](@ref) -""" -function sinkhorn_unbalanced2(μ, ν, C, ε, λ; kwargs...) - return pot.sinkhorn_unbalanced2( - μ, ν, PyCall.PyReverseDims(permutedims(C)), ε, λ; kwargs... - ) -end - -""" - barycenter(A, C, ε; kwargs...) - -Compute the entropically regularized Wasserstein barycenter with histograms `A`, cost matrix -`C`, and entropic regularization parameter `ε`. - -The Wasserstein barycenter is a histogram and solves -```math -\\inf_{a} \\sum_{i} W_{\\varepsilon,C}(a, a_i), -``` -where the histograms ``a_i`` are columns of matrix `A` and ``W_{\\varepsilon,C}(a, a_i)`` -is the optimal transport cost for the entropically regularized optimal transport problem -with marginals ``a`` and ``a_i``, cost matrix ``C``, and entropic regularization parameter -``\\varepsilon``. Optionally, weights of the histograms ``a_i`` can be provided with the -keyword argument `weights`. - -This function is a wrapper of the function -[`barycenter`](https://pythonot.github.io/all.html#ot.barycenter) in the -Python Optimal Transport package. Keyword arguments are listed in the documentation of the -Python function. - -# Examples - -```jldoctest -julia> A = rand(10, 3); - -julia> A ./= sum(A; dims=1); - -julia> C = rand(10, 10); - -julia> isapprox(sum(barycenter(A, C, 0.01; method="sinkhorn_stabilized")), 1; atol=1e-4) -true -``` -""" -barycenter(A, C, ε; kwargs...) = pot.barycenter(A, C, ε; kwargs...) - -""" - barycenter_unbalanced(A, C, ε, λ; kwargs...) - -Compute the entropically regularized unbalanced Wasserstein barycenter with histograms `A`, cost matrix -`C`, entropic regularization parameter `ε` and marginal relaxation parameter `λ`. - -The Wasserstein barycenter is a histogram and solves -```math -\\inf_{a} \\sum_{i} W_{\\varepsilon,C,\\lambda}(a, a_i), -``` -where the histograms ``a_i`` are columns of matrix `A` and ``W_{\\varepsilon,C,\\lambda}(a, a_i)}`` -is the optimal transport cost for the entropically regularized optimal transport problem -with marginals ``a`` and ``a_i``, cost matrix ``C``, entropic regularization parameter -``\\varepsilon`` and marginal relaxation parameter ``\\lambda``. Optionally, weights of the histograms ``a_i`` can be provided with the -keyword argument `weights`. - -This function is a wrapper of the function -[`barycenter_unbalanced`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.barycenter_unbalanced) in the -Python Optimal Transport package. Keyword arguments are listed in the documentation of the -Python function. - -# Examples - -```jldoctest -julia> A = rand(10, 3); - -julia> A ./= sum(A; dims=1); - -julia> C = rand(10, 10); - -julia> isapprox(sum(barycenter_unbalanced(A, C, 0.01, 1; method="sinkhorn_stabilized")), 1; atol=1e-4) -false - -julia> isapprox(sum(barycenter_unbalanced( - A, C, 0.01, 10_000; method="sinkhorn_stabilized", numItermax=5_000 - )), 1; atol=1e-4) -true -``` - -See also: [`barycenter`](@ref) -""" -function barycenter_unbalanced(A, C, ε, λ; kwargs...) - return pot.barycenter_unbalanced(A, C, ε, λ; kwargs...) -end - -""" - gromov_wasserstein(μ, ν, Cμ, Cν, loss = "square_loss"; kwargs...) - -Compute the exact Gromov-Wasserstein transport plan between `(μ, Cμ)` and `(ν, Cν)`. - -The Gromov-Wasserstein transport problem seeks to find a minimizer of -```math -\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\sum_{i, j, k, l} L((C_μ)_{ik}, (C_ν)_{jl}) \\gamma_{ij} \\gamma_{kl}, -``` -where ``L`` is quadratic (`loss = "square_loss"`) or the Kullback-Leibler divergence (`loss = "kl_loss"`). - -This function is a wrapper of the function -[`gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.gromov_wasserstein) in the -Python Optimal Transport package. Keyword arguments are listed in the documentation of the -Python function. -""" -function gromov_wasserstein(μ, ν, Cμ, Cν, loss="square_loss"; kwargs...) - return pot.gromov.gromov_wasserstein(Cμ, Cν, μ, ν, loss; kwargs...) -end - -""" - entropic_gromov_wasserstein(μ, ν, Cμ, Cν, ε, loss = "square_loss"; kwargs...) - -Compute the entropy-regularized Gromov-Wasserstein transport plan between `(μ, Cμ)` and `(ν, Cν)` with parameter `ε`. - -The entropy-regularized Gromov-Wasserstein transport problem seeks to find a minimizer of -```math -\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\sum_{i, j, k, l} L((C_μ)_{ik}, (C_ν)_{jl}) \\gamma_{ij} \\gamma_{kl} + ε \\Omega(\\gamma), -``` -where ``L`` is quadratic (`loss = "square_loss"`) or the Kullback-Leibler divergence (`loss = "kl_loss"`) -and ``\\Omega(\\gamma) = \\sum_{ij} \\gamma_{ij} \\log(\\gamma_{ij})`` is the entropic regularization term. - -This function is a wrapper of the function -[`entropic_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.entropic_gromov_wasserstein) in the -Python Optimal Transport package. Keyword arguments are listed in the documentation of the -Python function. -""" -function entropic_gromov_wasserstein(μ, ν, Cμ, Cν, ε, loss="square_loss"; kwargs...) - return pot.gromov.entropic_gromov_wasserstein(Cμ, Cν, μ, ν, loss, ε; kwargs...) -end - -""" - mm_unbalanced(a, b, M, reg_m; reg=0, c=a*b', kwargs...) - -Solve the unbalanced optimal transport problem and return the OT plan. -The function solves the following optimization problem: - -```math -W = \\min_{\\gamma \\geq 0} \\langle \\gamma, M \\rangle_F + - \\mathrm{reg_{m1}} \\cdot \\operatorname{div}(\\gamma \\mathbf{1}, a) + - \\mathrm{reg_{m2}} \\cdot \\operatorname{div}(\\gamma^\\mathsf{T} \\mathbf{1}, b) + - \\mathrm{reg} \\cdot \\operatorname{div}(\\gamma, c) -``` - -where - -- `M` is the metric cost matrix, -- `a` and `b` are source and target unbalanced distributions, -- `c` is a reference distribution for the regularization, -- `reg_m` is the marginal relaxation term (if it is a scalar or an indexable object of length 1, then the same term is applied to both marginal relaxations), and -- `reg` is a regularization term. - -This function is a wrapper of the function -[`mm_unbalanced`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.mm_unbalanced) in the -Python Optimal Transport package. Keyword arguments are listed in the documentation of the -Python function. - -# Examples - -```jldoctest -julia> a=[.5, .5]; - -julia> b=[.5, .5]; - -julia> M=[1. 36.; 9. 4.]; - -julia> round.(mm_unbalanced(a, b, M, 5, div="kl"), digits=2) -2×2 Matrix{Float64}: - 0.45 0.0 - 0.0 0.34 - -julia> round.(mm_unbalanced(a, b, M, 5, div="l2"), digits=2) -2×2 Matrix{Float64}: - 0.4 0.0 - 0.0 0.1 -``` - -""" -function mm_unbalanced(a, b, M, reg_m; kwargs...) - return pot.unbalanced.mm_unbalanced(a, b, M, reg_m; kwargs...) -end - - -""" - entropic_partial_wasserstein(a, b, M, reg; kwargs...) - -Solves the partial optimal transport problem and returns the OT plan -The function considers the following problem: - -```math -\\gamma = \\mathop{\\arg \\min}_\\gamma \\quad \\langle \\gamma, -\\mathbf{M} \\rangle_F + \\mathrm{reg} \\cdot\\Omega(\\gamma) -``` - -- `a` and `b` are the sample weights -- `M` is the metric cost matrix -- `reg` is a regularization term > 0 - -This function is a wrapper of the function -[`entropic_partial_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.entropic_partial_wasserstein) in the -Python Optimal Transport package. Keyword arguments are listed in the documentation of the -Python function. - - -# Examples - -```jldoctest -julia> a = [.1, .2]; - -julia> b = [.1, .1]; - -julia> M = [0. 1.; 2. 3.]; - -julia> round.(entropic_partial_wasserstein(a, b, M, 1, m=0.1), digits=2) -2×2 Matrix{Float64}: - 0.06 0.02 - 0.01 0.0 -``` - -""" -function entropic_partial_wasserstein(a, b, M, reg; kwargs...) - return pot.partial.entropic_partial_wasserstein(a, b, M, reg; kwargs...) -end - -function partial_wasserstein(a, b, M; kwargs...) - return pot.partial.partial_wasserstein2(a, b, M; kwargs...) -end - -function partial_wasserstein2(a, b, M; kwargs...) - return pot.partial.partial_wasserstein2(a, b, M; kwargs...) -end - -function entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) - return pot.partial.entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) -end - -function entropic_partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) - return pot.partial.entropic_partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) -end diff --git a/src/lp.jl b/src/lp.jl new file mode 100644 index 0000000..2d53e28 --- /dev/null +++ b/src/lp.jl @@ -0,0 +1,207 @@ +""" + emd(μ, ν, C; kwargs...) + +Compute the optimal transport plan for the Monge-Kantorovich problem with source and target +marginals `μ` and `ν` and cost matrix `C` of size `(length(μ), length(ν))`. + +The optimal transport plan `γ` is of the same size as `C` and solves +```math +\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle. +``` + +This function is a wrapper of the function +[`emd`](https://pythonot.github.io/all.html#ot.emd) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. + +# Examples + +```jldoctest +julia> μ = [0.5, 0.2, 0.3]; + +julia> ν = [0.0, 1.0]; + +julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; + +julia> emd(μ, ν, C) +3×2 Matrix{Float64}: + 0.0 0.5 + 0.0 0.2 + 0.0 0.3 +``` + +See also: [`emd2`](@ref) +""" +function emd(μ, ν, C; kwargs...) + return pot.lp.emd(μ, ν, PyCall.PyReverseDims(permutedims(C)); kwargs...) +end + +""" + emd2(μ, ν, C; kwargs...) + +Compute the optimal transport cost for the Monge-Kantorovich problem with source and target +marginals `μ` and `ν` and cost matrix `C` of size `(length(μ), length(ν))`. + +The optimal transport cost is the scalar value +```math +\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle. +``` + +This function is a wrapper of the function +[`emd2`](https://pythonot.github.io/all.html#ot.emd2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. + +# Examples + +```jldoctest +julia> μ = [0.5, 0.2, 0.3]; + +julia> ν = [0.0, 1.0]; + +julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; + +julia> emd2(μ, ν, C) +0.95 +``` + +See also: [`emd`](@ref) +""" +function emd2(μ, ν, C; kwargs...) + return pot.lp.emd2(μ, ν, PyCall.PyReverseDims(permutedims(C)); kwargs...) +end + +""" + emd_1d(xsource, xtarget; kwargs...) + +Compute the optimal transport plan for the Monge-Kantorovich problem with univariate +discrete measures with support `xsource` and `xtarget` as source and target marginals. + +This function is a wrapper of the function +[`emd_1d`](https://pythonot.github.io/all.html#ot.emd_1d) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. + +# Examples + +```jldoctest +julia> xsource = [0.2, 0.5]; + +julia> xtarget = [0.8, 0.3]; + +julia> emd_1d(xsource, xtarget) +2×2 Matrix{Float64}: + 0.0 0.5 + 0.5 0.0 + +julia> histogram_source = [0.8, 0.2]; + +julia> histogram_target = [0.7, 0.3]; + +julia> emd_1d(xsource, xtarget; a=histogram_source, b=histogram_target) +2×2 Matrix{Float64}: + 0.5 0.3 + 0.2 0.0 +``` + +See also: [`emd`](@ref), [`emd2_1d`](@ref) +""" +function emd_1d(xsource, xtarget; kwargs...) + return pot.lp.emd_1d(xsource, xtarget; kwargs...) +end + +""" + emd2_1d(xsource, xtarget; kwargs...) + +Compute the optimal transport cost for the Monge-Kantorovich problem with univariate +discrete measures with support `xsource` and `xtarget` as source and target marginals. + +This function is a wrapper of the function +[`emd2_1d`](https://pythonot.github.io/all.html#ot.emd2_1d) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. + +# Examples + +```jldoctest +julia> xsource = [0.2, 0.5]; + +julia> xtarget = [0.8, 0.3]; + +julia> round(emd2_1d(xsource, xtarget); sigdigits=6) +0.05 + +julia> histogram_source = [0.8, 0.2]; + +julia> histogram_target = [0.7, 0.3]; + +julia> round(emd2_1d(xsource, xtarget; a=histogram_source, b=histogram_target); sigdigits=6) +0.201 +``` + +See also: [`emd2`](@ref), [`emd2_1d`](@ref) +""" +function emd2_1d(xsource, xtarget; kwargs...) + return pot.lp.emd2_1d(xsource, xtarget; kwargs...) +end + +""" + wasserstein_1d(u_values, v_values; kwargs...) + +Compute the 1D Wasserstein distance (power `p`) between two weighted empirical measures. + +This function is a wrapper of the function +[`ot.lp.wasserstein_1d`](https://pythonot.github.io/gen_modules/ot.lp.html#ot.lp.wasserstein_1d) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function wasserstein_1d(u_values, v_values; kwargs...) + return pot.lp.wasserstein_1d(u_values, v_values; kwargs...) +end + +""" + wasserstein_circle(u_values, v_values; kwargs...) + +Compute the Wasserstein distance on the circle (values in [0, 1)) between two weighted empirical measures. + +This function is a wrapper of the function +[`ot.lp.wasserstein_circle`](https://pythonot.github.io/gen_modules/ot.lp.html#ot.lp.wasserstein_circle) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function wasserstein_circle(u_values, v_values; kwargs...) + return pot.lp.wasserstein_circle(u_values, v_values; kwargs...) +end + +""" + binary_search_circle(u_values, v_values; kwargs...) + +Compute the Wasserstein distance on the circle using a binary search. + +This function is a wrapper of the function +[`ot.lp.binary_search_circle`](https://pythonot.github.io/gen_modules/ot.lp.html#ot.lp.binary_search_circle) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function binary_search_circle(u_values, v_values; kwargs...) + return pot.lp.binary_search_circle(u_values, v_values; kwargs...) +end + +""" + linear_circular_ot(u_values, v_values=nothing; kwargs...) + +Compute the linear circular OT embedding / distance between measures on the circle. + +This function is a wrapper of the function +[`ot.lp.linear_circular_ot`](https://pythonot.github.io/gen_modules/ot.lp.html#ot.lp.linear_circular_ot) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function linear_circular_ot(u_values, v_values=nothing; kwargs...) + return pot.lp.linear_circular_ot(u_values, v_values; kwargs...) +end + +""" + free_support_barycenter(measures_locations, measures_weights, X_init; kwargs...) + +Compute the Wasserstein barycenter of measures with free support, starting from `X_init`. + +This function is a wrapper of the function +[`ot.lp.free_support_barycenter`](https://pythonot.github.io/gen_modules/ot.lp.html#ot.lp.free_support_barycenter) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function free_support_barycenter(measures_locations, measures_weights, X_init; kwargs...) + return pot.lp.free_support_barycenter(measures_locations, measures_weights, X_init; kwargs...) +end diff --git a/src/partial.jl b/src/partial.jl new file mode 100644 index 0000000..d98daa8 --- /dev/null +++ b/src/partial.jl @@ -0,0 +1,131 @@ +""" + entropic_partial_wasserstein(a, b, M, reg; kwargs...) + +Solves the partial optimal transport problem and returns the OT plan +The function considers the following problem: + +```math +\\gamma = \\mathop{\\arg \\min}_\\gamma \\quad \\langle \\gamma, +\\mathbf{M} \\rangle_F + \\mathrm{reg} \\cdot\\Omega(\\gamma) +``` + +- `a` and `b` are the sample weights +- `M` is the metric cost matrix +- `reg` is a regularization term > 0 + +This function is a wrapper of the function +[`entropic_partial_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.entropic_partial_wasserstein) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. + + +# Examples + +```jldoctest +julia> a = [.1, .2]; + +julia> b = [.1, .1]; + +julia> M = [0. 1.; 2. 3.]; + +julia> round.(entropic_partial_wasserstein(a, b, M, 1, m=0.1), digits=2) +2×2 Matrix{Float64}: + 0.06 0.02 + 0.01 0.0 +``` + +""" +function entropic_partial_wasserstein(a, b, M, reg; kwargs...) + return pot.partial.entropic_partial_wasserstein(a, b, M, reg; kwargs...) +end + +""" + partial_wasserstein(a, b, M; kwargs...) + +Solve the partial optimal transport problem and return the OT plan. + +This function is a wrapper of the function +[`ot.partial.partial_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.partial_wasserstein) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function partial_wasserstein(a, b, M; kwargs...) + return pot.partial.partial_wasserstein(a, b, M; kwargs...) +end + +""" + partial_wasserstein2(a, b, M; kwargs...) + +Solve the partial optimal transport problem and return the OT cost. + +This function is a wrapper of the function +[`ot.partial.partial_wasserstein2`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.partial_wasserstein2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function partial_wasserstein2(a, b, M; kwargs...) + return pot.partial.partial_wasserstein2(a, b, M; kwargs...) +end + +""" + partial_wasserstein_lagrange(a, b, M; kwargs...) + +Solve the partial optimal transport problem with a Lagrangian relaxation (`reg_m`) and return the OT plan. + +This function is a wrapper of the function +[`ot.partial.partial_wasserstein_lagrange`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.partial_wasserstein_lagrange) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function partial_wasserstein_lagrange(a, b, M; kwargs...) + return pot.partial.partial_wasserstein_lagrange(a, b, M; kwargs...) +end + +""" + partial_gromov_wasserstein(C1, C2, p, q; kwargs...) + +Solve the partial Gromov-Wasserstein problem and return the transport plan. + +This function is a wrapper of the function +[`ot.partial.partial_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.partial_gromov_wasserstein) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function partial_gromov_wasserstein(C1, C2, p, q; kwargs...) + return pot.partial.partial_gromov_wasserstein(C1, C2, p, q; kwargs...) +end + +""" + partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) + +Solve the partial Gromov-Wasserstein problem and return the cost. + +This function is a wrapper of the function +[`ot.partial.partial_gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.partial_gromov_wasserstein2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) + return pot.partial.partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) +end + +""" + entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) + +Solve the entropic partial Gromov-Wasserstein problem and return the transport plan. + +This function is a wrapper of the function +[`ot.partial.entropic_partial_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.entropic_partial_gromov_wasserstein) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) + return pot.partial.entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) +end + +""" + entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) + +Solve the entropic partial Gromov-Wasserstein problem and return the cost. + +This function is a wrapper of the function +[`ot.partial.entropic_partial_gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.entropic_partial_gromov_wasserstein2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) + return pot.partial.entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) +end diff --git a/src/sliced.jl b/src/sliced.jl new file mode 100644 index 0000000..d2e9624 --- /dev/null +++ b/src/sliced.jl @@ -0,0 +1,51 @@ +""" + sliced_wasserstein_distance(xsource, xtarget; kwargs...) + +Compute the sliced Wasserstein distance between empirical samples (rows are samples). + +This function is a wrapper of the function +[`ot.sliced.sliced_wasserstein_distance`](https://pythonot.github.io/gen_modules/ot.sliced.html#ot.sliced.sliced_wasserstein_distance) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function sliced_wasserstein_distance(xsource, xtarget; kwargs...) + return pot.sliced.sliced_wasserstein_distance(xsource, xtarget; kwargs...) +end + +""" + max_sliced_wasserstein_distance(xsource, xtarget; kwargs...) + +Compute the max-sliced Wasserstein distance between empirical samples (rows are samples). + +This function is a wrapper of the function +[`ot.sliced.max_sliced_wasserstein_distance`](https://pythonot.github.io/gen_modules/ot.sliced.html#ot.sliced.max_sliced_wasserstein_distance) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function max_sliced_wasserstein_distance(xsource, xtarget; kwargs...) + return pot.sliced.max_sliced_wasserstein_distance(xsource, xtarget; kwargs...) +end + +""" + sliced_wasserstein_sphere(xsource, xtarget; kwargs...) + +Compute the spherical sliced Wasserstein distance between samples on the sphere. + +This function is a wrapper of the function +[`ot.sliced.sliced_wasserstein_sphere`](https://pythonot.github.io/gen_modules/ot.sliced.html#ot.sliced.sliced_wasserstein_sphere) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function sliced_wasserstein_sphere(xsource, xtarget; kwargs...) + return pot.sliced.sliced_wasserstein_sphere(xsource, xtarget; kwargs...) +end + +""" + get_random_projections(d, n_projections; kwargs...) + +Generate `n_projections` random unit directions in dimension `d` (as columns). + +This function is a wrapper of the function +[`ot.sliced.get_random_projections`](https://pythonot.github.io/gen_modules/ot.sliced.html#ot.sliced.get_random_projections) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function get_random_projections(d, n_projections; kwargs...) + return pot.sliced.get_random_projections(d, n_projections; kwargs...) +end diff --git a/src/smooth.jl b/src/smooth.jl index eef6981..12d31b5 100644 --- a/src/smooth.jl +++ b/src/smooth.jl @@ -3,7 +3,7 @@ module Smooth using ..PythonOT: PythonOT using ..PyCall: PyCall -export smooth_ot_dual +export smooth_ot_dual, smooth_ot_semi_dual """ smooth_ot_dual(μ, ν, C, ε; reg_type="l2", kwargs...) @@ -56,4 +56,19 @@ julia> smooth_ot_dual(μ, ν, C, 0.01) function smooth_ot_dual(μ, ν, C, ε; kwargs...) return PythonOT.pot.smooth.smooth_ot_dual(μ, ν, C, ε; kwargs...) end + +""" + smooth_ot_semi_dual(μ, ν, C, ε; reg_type="l2", kwargs...) + +Compute the optimal transport plan for a regularized optimal transport problem by solving +its semi-dual formulation (see [`smooth_ot_dual`](@ref)). + +This function is a wrapper of the function +[`smooth_ot_semi_dual`](https://pythonot.github.io/gen_modules/ot.smooth.html#ot.smooth.smooth_ot_semi_dual) +in the Python Optimal Transport package. Keyword arguments are listed in the documentation +of the Python function. +""" +function smooth_ot_semi_dual(μ, ν, C, ε; kwargs...) + return PythonOT.pot.smooth.smooth_ot_semi_dual(μ, ν, C, ε; kwargs...) +end end diff --git a/src/unbalanced.jl b/src/unbalanced.jl new file mode 100644 index 0000000..416756b --- /dev/null +++ b/src/unbalanced.jl @@ -0,0 +1,252 @@ +""" + sinkhorn_unbalanced(μ, ν, C, ε, λ; kwargs...) + +Compute the optimal transport plan for the unbalanced entropic regularization optimal +transport problem with source and target marginals `μ` and `ν`, cost matrix `C` of size +`(length(μ), length(ν))`, entropic regularization parameter `ε`, and marginal relaxation +term `λ`. + +The optimal transport plan `γ` is of the same size as `C` and solves +```math +\\inf_{\\gamma} \\langle \\gamma, C \\rangle ++ \\varepsilon \\Omega(\\gamma) ++ \\lambda \\mathrm{KL}(\\gamma 1, \\mu) ++ \\lambda \\mathrm{KL}(\\gamma^{\\mathsf{T}} 1, \\nu), +``` +where ``\\Omega(\\gamma) = \\sum_{i,j} \\gamma_{i,j} \\log \\gamma_{i,j}`` is the entropic +regularization term and ``\\mathrm{KL}`` is the Kullback-Leibler divergence. + +This function is a wrapper of the function +[`sinkhorn_unbalanced`](https://pythonot.github.io/all.html#ot.sinkhorn_unbalanced) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. + +# Examples + +```jldoctest sinkhorn_unbalanced +julia> μ = [0.5, 0.5]; + +julia> ν = [0.5, 0.5]; + +julia> C = [0.0 1.0; 1.0 0.0]; + +julia> round.(sinkhorn_unbalanced(μ, ν, C, 1, 1); sigdigits=7) +2×2 Matrix{Float64}: + 0.322054 0.118477 + 0.118477 0.322054 +``` + +``` + +See also: [`sinkhorn_unbalanced2`](@ref) +""" +function sinkhorn_unbalanced(μ, ν, C, ε, λ; kwargs...) + return pot.sinkhorn_unbalanced( + μ, ν, PyCall.PyReverseDims(permutedims(C)), ε, λ; kwargs... + ) +end + +""" + sinkhorn_unbalanced2(μ, ν, C, ε, λ; kwargs...) + +Compute the optimal transport cost for the unbalanced entropic regularization optimal +transport problem with source and target marginals `μ` and `ν`, cost matrix `C` of size +`(length(μ), length(ν))`, entropic regularization parameter `ε`, and marginal relaxation +term `λ`. + +The optimal transport cost is the scalar value +```math +\\inf_{\\gamma} \\langle \\gamma, C \\rangle ++ \\varepsilon \\Omega(\\gamma) ++ \\lambda \\mathrm{KL}(\\gamma 1, \\mu) ++ \\lambda \\mathrm{KL}(\\gamma^{\\mathsf{T}} 1, \\nu), +``` +where ``\\Omega(\\gamma) = \\sum_{i,j} \\gamma_{i,j} \\log \\gamma_{i,j}`` is the entropic +regularization term and ``\\mathrm{KL}`` is the Kullback-Leibler divergence. + +This function is a wrapper of the function +[`sinkhorn_unbalanced2`](https://pythonot.github.io/all.html#ot.sinkhorn_unbalanced2) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. + +# Examples + +```jldoctest sinkhorn_unbalanced2 +julia> μ = [0.5, 0.1]; + +julia> ν = [0.5, 0.5]; + +julia> C = [0.0 1.0; 1.0 0.0]; + +julia> round.(sinkhorn_unbalanced2(μ, ν, C, 1., 1.); sigdigits=8) +0.19600125 +``` + +See also: [`sinkhorn_unbalanced`](@ref) +""" +function sinkhorn_unbalanced2(μ, ν, C, ε, λ; kwargs...) + return pot.sinkhorn_unbalanced2( + μ, ν, PyCall.PyReverseDims(permutedims(C)), ε, λ; kwargs... + ) +end + +""" + barycenter_unbalanced(A, C, ε, λ; kwargs...) + +Compute the entropically regularized unbalanced Wasserstein barycenter with histograms `A`, cost matrix +`C`, entropic regularization parameter `ε` and marginal relaxation parameter `λ`. + +The Wasserstein barycenter is a histogram and solves +```math +\\inf_{a} \\sum_{i} W_{\\varepsilon,C,\\lambda}(a, a_i), +``` +where the histograms ``a_i`` are columns of matrix `A` and ``W_{\\varepsilon,C,\\lambda}(a, a_i)}`` +is the optimal transport cost for the entropically regularized optimal transport problem +with marginals ``a`` and ``a_i``, cost matrix ``C``, entropic regularization parameter +``\\varepsilon`` and marginal relaxation parameter ``\\lambda``. Optionally, weights of the histograms ``a_i`` can be provided with the +keyword argument `weights`. + +This function is a wrapper of the function +[`barycenter_unbalanced`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.barycenter_unbalanced) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. + +# Examples + +```jldoctest +julia> A = rand(10, 3); + +julia> A ./= sum(A; dims=1); + +julia> C = rand(10, 10); + +julia> isapprox(sum(barycenter_unbalanced(A, C, 0.01, 1; method="sinkhorn_stabilized")), 1; atol=1e-4) +false + +julia> isapprox(sum(barycenter_unbalanced( + A, C, 0.01, 10_000; method="sinkhorn_stabilized", numItermax=5_000 + )), 1; atol=1e-4) +true +``` + +See also: [`barycenter`](@ref) +""" +function barycenter_unbalanced(A, C, ε, λ; kwargs...) + return pot.barycenter_unbalanced(A, C, ε, λ; kwargs...) +end + +""" + mm_unbalanced(a, b, M, reg_m; reg=0, c=a*b', kwargs...) + +Solve the unbalanced optimal transport problem and return the OT plan. +The function solves the following optimization problem: + +```math +W = \\min_{\\gamma \\geq 0} \\langle \\gamma, M \\rangle_F + + \\mathrm{reg_{m1}} \\cdot \\operatorname{div}(\\gamma \\mathbf{1}, a) + + \\mathrm{reg_{m2}} \\cdot \\operatorname{div}(\\gamma^\\mathsf{T} \\mathbf{1}, b) + + \\mathrm{reg} \\cdot \\operatorname{div}(\\gamma, c) +``` + +where + +- `M` is the metric cost matrix, +- `a` and `b` are source and target unbalanced distributions, +- `c` is a reference distribution for the regularization, +- `reg_m` is the marginal relaxation term (if it is a scalar or an indexable object of length 1, then the same term is applied to both marginal relaxations), and +- `reg` is a regularization term. + +This function is a wrapper of the function +[`mm_unbalanced`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.mm_unbalanced) in the +Python Optimal Transport package. Keyword arguments are listed in the documentation of the +Python function. + +# Examples + +```jldoctest +julia> a=[.5, .5]; + +julia> b=[.5, .5]; + +julia> M=[1. 36.; 9. 4.]; + +julia> round.(mm_unbalanced(a, b, M, 5, div="kl"), digits=2) +2×2 Matrix{Float64}: + 0.45 0.0 + 0.0 0.34 + +julia> round.(mm_unbalanced(a, b, M, 5, div="l2"), digits=2) +2×2 Matrix{Float64}: + 0.4 0.0 + 0.0 0.1 +``` + +""" +function mm_unbalanced(a, b, M, reg_m; kwargs...) + return pot.unbalanced.mm_unbalanced(a, b, M, reg_m; kwargs...) +end + +""" + mm_unbalanced2(a, b, M, reg_m; kwargs...) + +Solve the unbalanced optimal transport problem with the majorization-minimization algorithm and return the cost. + +This function is a wrapper of the function +[`ot.unbalanced.mm_unbalanced2`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.mm_unbalanced2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function mm_unbalanced2(a, b, M, reg_m; kwargs...) + return pot.unbalanced.mm_unbalanced2(a, b, M, reg_m; kwargs...) +end + +""" + sinkhorn_unbalanced_translation_invariant(a, b, M, reg, reg_m; kwargs...) + +Solve the translation-invariant unbalanced entropic OT problem and return the plan. + +This function is a wrapper of the function +[`ot.unbalanced.sinkhorn_unbalanced_translation_invariant`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.sinkhorn_unbalanced_translation_invariant) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function sinkhorn_unbalanced_translation_invariant(a, b, M, reg, reg_m; kwargs...) + return pot.unbalanced.sinkhorn_unbalanced_translation_invariant(a, b, M, reg, reg_m; kwargs...) +end + +""" + lbfgsb_unbalanced(a, b, M, reg, reg_m; kwargs...) + +Solve the unbalanced OT problem with L-BFGS-B and return the plan. + +This function is a wrapper of the function +[`ot.unbalanced.lbfgsb_unbalanced`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.lbfgsb_unbalanced) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function lbfgsb_unbalanced(a, b, M, reg, reg_m; kwargs...) + return pot.unbalanced.lbfgsb_unbalanced(a, b, M, reg, reg_m; kwargs...) +end + +""" + lbfgsb_unbalanced2(a, b, M, reg, reg_m; kwargs...) + +Solve the unbalanced OT problem with L-BFGS-B and return the cost. + +This function is a wrapper of the function +[`ot.unbalanced.lbfgsb_unbalanced2`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.lbfgsb_unbalanced2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function lbfgsb_unbalanced2(a, b, M, reg, reg_m; kwargs...) + return pot.unbalanced.lbfgsb_unbalanced2(a, b, M, reg, reg_m; kwargs...) +end + +""" + uot_1d(u_values, v_values, reg_m; kwargs...) + +Solve the unbalanced OT problem between 1D measures. + +This function is a wrapper of the function +[`ot.unbalanced.uot_1d`](https://pythonot.github.io/gen_modules/ot.unbalanced.html#ot.unbalanced.uot_1d) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function uot_1d(u_values, v_values, reg_m; kwargs...) + return pot.unbalanced.uot_1d(u_values, v_values, reg_m; kwargs...) +end diff --git a/src/utils.jl b/src/utils.jl new file mode 100644 index 0000000..c817181 --- /dev/null +++ b/src/utils.jl @@ -0,0 +1,51 @@ +""" + dist(x1, x2=nothing; kwargs...) + +Compute the pairwise distance matrix between the rows of `x1` and `x2` (default `x1`); use `metric` to select the metric. + +This function is a wrapper of the function +[`ot.utils.dist`](https://pythonot.github.io/gen_modules/ot.utils.html#ot.utils.dist) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function dist(x1, x2=nothing; kwargs...) + return pot.utils.dist(x1, x2; kwargs...) +end + +""" + unif(n) + +Return the uniform histogram of length `n`. + +This function is a wrapper of the function +[`ot.utils.unif`](https://pythonot.github.io/gen_modules/ot.utils.html#ot.utils.unif) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function unif(n) + return pot.utils.unif(n) +end + +""" + solve(M, a=nothing, b=nothing; kwargs...) + +Generic OT solver for cost matrix `M`; returns a Python `OTResult` object. + +This function is a wrapper of the function +[`ot.solve`](https://pythonot.github.io/gen_modules/all.html#ot.solve) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function solve(M, a=nothing, b=nothing; kwargs...) + return pot.solve(M, a, b; kwargs...) +end + +""" + solve_sample(xsource, xtarget, a=nothing, b=nothing; kwargs...) + +Generic OT solver for empirical samples; returns a Python `OTResult` object. + +This function is a wrapper of the function +[`ot.solve_sample`](https://pythonot.github.io/gen_modules/all.html#ot.solve_sample) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function solve_sample(xsource, xtarget, a=nothing, b=nothing; kwargs...) + return pot.solve_sample(xsource, xtarget, a, b; kwargs...) +end diff --git a/test/runtests.jl b/test/runtests.jl index a803048..7ee9024 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,6 +8,7 @@ using Pkg end using PythonOT +using PythonOT: dist, unif, solve, get_random_projections using Documenter using Test @@ -23,4 +24,49 @@ using Test ], ) end + + @testset "wrappers" begin + μ = [0.5, 0.2, 0.3] + ν = [0.4, 0.6] + C = [0.0 1.0; 2.0 0.0; 0.5 1.5] + @test partial_wasserstein(μ, ν, C; m=0.5) isa Matrix + @test size(partial_wasserstein(μ, ν, C; m=0.5)) == (3, 2) + @test partial_wasserstein2(μ, ν, C; m=0.5) isa Real + @test sum(partial_wasserstein(μ, ν, C; m=0.5)) ≈ 0.5 + @test sinkhorn_log(μ, ν, C, 0.1) ≈ sinkhorn(μ, ν, C, 0.1) atol = 1e-6 + @test sinkhorn_stabilized(μ, ν, C, 0.1) ≈ sinkhorn(μ, ν, C, 0.1) atol = 1e-6 + @test sum(greenkhorn(μ, ν, C, 0.1)) ≈ 1 atol = 1e-4 + @test unif(4) == fill(0.25, 4) + D = dist([0.0 0.0; 1.0 1.0], [0.0 0.0; 2.0 0.0]) + @test D == [0.0 4.0; 2.0 2.0] + @test wasserstein_1d([0.0, 1.0], [1.0, 2.0]) ≈ 1 + @test mm_unbalanced2(μ[1:2], ν, C[1:2, :], 5) isa Real + @test lbfgsb_unbalanced(μ, ν, C, 0.1, 1.0) isa Matrix + @test size(empirical_sinkhorn([0.0 0.0; 1.0 1.0], [0.0 1.0; 2.0 2.0; 3.0 3.0], 1.0)) == + (2, 3) + @test empirical_sinkhorn2([0.0; 1.0;;], [2.0; 3.0;;], 0.1) isa Any + + xs = [0.0 0.0; 1.0 0.0; 0.0 1.0] + Cs = dist(xs, xs) + p = unif(3) + @test gromov_wasserstein2(p, p, Cs, Cs) ≈ 0 atol = 1e-8 + @test fused_gromov_wasserstein2(p, p, zeros(3, 3), Cs, Cs) ≈ 0 atol = 1e-8 + @test size(fused_gromov_wasserstein(p, p, zeros(3, 3), Cs, Cs)) == (3, 3) + @test entropic_gromov_wasserstein2(p, p, Cs, Cs, 0.1) isa Real + @test size(semirelaxed_gromov_wasserstein(Cs, Cs, p)) == (3, 3) + @test size(partial_gromov_wasserstein(Cs, Cs, p, p; m=0.5)) == (3, 3) + @test partial_gromov_wasserstein2(Cs, Cs, p, p; m=0.5) isa Real + @test size(entropic_partial_gromov_wasserstein(Cs, Cs, p, p, 1.0; m=0.5)) == (3, 3) + @test entropic_partial_gromov_wasserstein2(Cs, Cs, p, p, 1.0; m=0.5) isa Real + @test size(gromov_barycenters(3, [Cs, Cs]; max_iter=5)) == (3, 3) + + X = [0.0 0.0; 1.0 1.0; 2.0 0.5] + @test sliced_wasserstein_distance(X, X; seed=0) ≈ 0 atol = 1e-8 + @test max_sliced_wasserstein_distance(X, X; seed=0) ≈ 0 atol = 1e-8 + @test size(get_random_projections(3, 5; seed=0)) == (3, 5) + @test only(wasserstein_circle([0.1, 0.5], [0.1, 0.5])) ≈ 0 atol = 1e-8 + + res = solve(C, μ, ν) + @test res.value isa Real + end end From f1a0fa2687f440c897322ccdc36026986a37e3de Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 10:28:05 +0100 Subject: [PATCH 09/16] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1d38470..edfce28 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PythonOT" uuid = "3c485715-4278-42b2-9b5f-8f00e43c12ef" authors = ["David Widmann"] -version = "0.1.9" +version = "0.1.7" [deps] PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" From 1f9a21dc99034ff911edfe9e5c4562b9e9f9e5b2 Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 10:40:03 +0100 Subject: [PATCH 10/16] Move partial Gromov wrappers to ot.gromov Update the wrappers and documentation to use POT's gromov module. Suppress SciPy solver-option warnings during initialization and Python warnings in tests. --- src/PythonOT.jl | 5 ++++- src/gromov.jl | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/partial.jl | 52 ------------------------------------------------ test/runtests.jl | 3 +++ 4 files changed, 59 insertions(+), 53 deletions(-) diff --git a/src/PythonOT.jl b/src/PythonOT.jl index 44826fc..47fa1d6 100644 --- a/src/PythonOT.jl +++ b/src/PythonOT.jl @@ -66,7 +66,10 @@ end include("smooth.jl") function __init__() - return copy!(pot, PyCall.pyimport_conda("ot", "pot", "conda-forge")) + copy!(pot, PyCall.pyimport_conda("ot", "pot", "conda-forge")) + # SciPy warns about the unused `disp` option that POT passes to `minimize` + PyCall.pyimport("warnings").filterwarnings("ignore"; message="Unknown solver options") + return nothing end end diff --git a/src/gromov.jl b/src/gromov.jl index 2a0703d..928bde3 100644 --- a/src/gromov.jl +++ b/src/gromov.jl @@ -142,3 +142,55 @@ package. Keyword arguments are listed in the documentation of the Python functio function fgw_barycenters(N, Ys, Cs; kwargs...) return pot.gromov.fgw_barycenters(N, Ys, Cs; kwargs...) end + +""" + partial_gromov_wasserstein(C1, C2, p, q; kwargs...) + +Solve the partial Gromov-Wasserstein problem and return the transport plan. + +This function is a wrapper of the function +[`ot.gromov.partial_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.partial_gromov_wasserstein) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function partial_gromov_wasserstein(C1, C2, p, q; kwargs...) + return pot.gromov.partial_gromov_wasserstein(C1, C2, p, q; kwargs...) +end + +""" + partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) + +Solve the partial Gromov-Wasserstein problem and return the cost. + +This function is a wrapper of the function +[`ot.gromov.partial_gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.partial_gromov_wasserstein2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) + return pot.gromov.partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) +end + +""" + entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) + +Solve the entropic partial Gromov-Wasserstein problem and return the transport plan. + +This function is a wrapper of the function +[`ot.gromov.entropic_partial_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.entropic_partial_gromov_wasserstein) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) + return pot.gromov.entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) +end + +""" + entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) + +Solve the entropic partial Gromov-Wasserstein problem and return the cost. + +This function is a wrapper of the function +[`ot.gromov.entropic_partial_gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.entropic_partial_gromov_wasserstein2) in the Python Optimal Transport +package. Keyword arguments are listed in the documentation of the Python function. +""" +function entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) + return pot.gromov.entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) +end diff --git a/src/partial.jl b/src/partial.jl index d98daa8..244dd80 100644 --- a/src/partial.jl +++ b/src/partial.jl @@ -77,55 +77,3 @@ package. Keyword arguments are listed in the documentation of the Python functio function partial_wasserstein_lagrange(a, b, M; kwargs...) return pot.partial.partial_wasserstein_lagrange(a, b, M; kwargs...) end - -""" - partial_gromov_wasserstein(C1, C2, p, q; kwargs...) - -Solve the partial Gromov-Wasserstein problem and return the transport plan. - -This function is a wrapper of the function -[`ot.partial.partial_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.partial_gromov_wasserstein) in the Python Optimal Transport -package. Keyword arguments are listed in the documentation of the Python function. -""" -function partial_gromov_wasserstein(C1, C2, p, q; kwargs...) - return pot.partial.partial_gromov_wasserstein(C1, C2, p, q; kwargs...) -end - -""" - partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) - -Solve the partial Gromov-Wasserstein problem and return the cost. - -This function is a wrapper of the function -[`ot.partial.partial_gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.partial_gromov_wasserstein2) in the Python Optimal Transport -package. Keyword arguments are listed in the documentation of the Python function. -""" -function partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) - return pot.partial.partial_gromov_wasserstein2(C1, C2, p, q; kwargs...) -end - -""" - entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) - -Solve the entropic partial Gromov-Wasserstein problem and return the transport plan. - -This function is a wrapper of the function -[`ot.partial.entropic_partial_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.entropic_partial_gromov_wasserstein) in the Python Optimal Transport -package. Keyword arguments are listed in the documentation of the Python function. -""" -function entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) - return pot.partial.entropic_partial_gromov_wasserstein(C1, C2, p, q, reg; kwargs...) -end - -""" - entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) - -Solve the entropic partial Gromov-Wasserstein problem and return the cost. - -This function is a wrapper of the function -[`ot.partial.entropic_partial_gromov_wasserstein2`](https://pythonot.github.io/gen_modules/ot.partial.html#ot.partial.entropic_partial_gromov_wasserstein2) in the Python Optimal Transport -package. Keyword arguments are listed in the documentation of the Python function. -""" -function entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) - return pot.partial.entropic_partial_gromov_wasserstein2(C1, C2, p, q, reg; kwargs...) -end diff --git a/test/runtests.jl b/test/runtests.jl index 7ee9024..8831f6f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,6 +12,9 @@ using PythonOT: dist, unif, solve, get_random_projections using Documenter using Test +# Keep the test output quiet: POT emits deprecation and convergence warnings +PythonOT.PyCall.pyimport("warnings").filterwarnings("ignore") + @testset "PythonOT.jl" begin @testset "doctests" begin DocMeta.setdocmeta!(PythonOT, :DocTestSetup, :(using PythonOT); recursive=true) From 13f26bd006a8832f9b1c6aaf35ed22f1b8f4631f Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 10:54:37 +0100 Subject: [PATCH 11/16] Require Julia 1.10 and adopt Dependabot Raise the minimum Julia version to 1.10 and update the CI matrix accordingly. Replace CompatHelper with weekly Dependabot updates for root, docs, and test dependencies. --- .github/dependabot.yml | 8 ++++++++ .github/workflows/CI.yml | 8 +------- .github/workflows/CompatHelper.yml | 16 ---------------- Project.toml | 2 +- src/gromov.jl | 4 +++- src/lp.jl | 4 +++- src/unbalanced.jl | 4 +++- test/Project.toml | 2 +- test/runtests.jl | 5 +++-- 9 files changed, 23 insertions(+), 30 deletions(-) delete mode 100644 .github/workflows/CompatHelper.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml index d8a1c91..904bf86 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -6,3 +6,11 @@ updates: schedule: interval: monthly open-pull-requests-limit: 99 + - package-ecosystem: julia + directories: + - / + - /docs + - /test + schedule: + interval: weekly + open-pull-requests-limit: 99 diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index f756474..6b307cd 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -22,8 +22,7 @@ jobs: fail-fast: false matrix: version: - - '1.0' - - '1.6' + - '1.10' - '1' os: - ubuntu-latest @@ -31,11 +30,6 @@ jobs: - windows-latest arch: - x64 - exclude: - # Conda installation fails: https://github.com/JuliaPy/Conda.jl/issues/230 - - version: '1.0' - os: windows-latest - arch: x64 steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 diff --git a/.github/workflows/CompatHelper.yml b/.github/workflows/CompatHelper.yml deleted file mode 100644 index 6c1f6e2..0000000 --- a/.github/workflows/CompatHelper.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: CompatHelper -on: - schedule: - - cron: 0 0 * * * - workflow_dispatch: -jobs: - CompatHelper: - runs-on: ubuntu-latest - steps: - - name: Pkg.add("CompatHelper") - run: julia -e 'using Pkg; Pkg.add("CompatHelper")' - - name: CompatHelper.main() - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }} - run: julia -e 'using CompatHelper; CompatHelper.main(; subdirs=["", "docs", "test"])' diff --git a/Project.toml b/Project.toml index edfce28..d515483 100644 --- a/Project.toml +++ b/Project.toml @@ -8,7 +8,7 @@ PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" [compat] PyCall = "1" -julia = "1" +julia = "1.10" [extras] Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d" diff --git a/src/gromov.jl b/src/gromov.jl index 928bde3..00f8a2e 100644 --- a/src/gromov.jl +++ b/src/gromov.jl @@ -100,7 +100,9 @@ This function is a wrapper of the function [`ot.gromov.entropic_fused_gromov_wasserstein`](https://pythonot.github.io/gen_modules/ot.gromov.html#ot.gromov.entropic_fused_gromov_wasserstein) in the Python Optimal Transport package. Keyword arguments are listed in the documentation of the Python function. """ -function entropic_fused_gromov_wasserstein(μ, ν, M, Cμ, Cν, ε, loss="square_loss"; kwargs...) +function entropic_fused_gromov_wasserstein( + μ, ν, M, Cμ, Cν, ε, loss="square_loss"; kwargs... +) return pot.gromov.entropic_fused_gromov_wasserstein(M, Cμ, Cν, μ, ν, loss, ε; kwargs...) end diff --git a/src/lp.jl b/src/lp.jl index 2d53e28..fc471f7 100644 --- a/src/lp.jl +++ b/src/lp.jl @@ -203,5 +203,7 @@ This function is a wrapper of the function package. Keyword arguments are listed in the documentation of the Python function. """ function free_support_barycenter(measures_locations, measures_weights, X_init; kwargs...) - return pot.lp.free_support_barycenter(measures_locations, measures_weights, X_init; kwargs...) + return pot.lp.free_support_barycenter( + measures_locations, measures_weights, X_init; kwargs... + ) end diff --git a/src/unbalanced.jl b/src/unbalanced.jl index 416756b..1486d03 100644 --- a/src/unbalanced.jl +++ b/src/unbalanced.jl @@ -209,7 +209,9 @@ This function is a wrapper of the function package. Keyword arguments are listed in the documentation of the Python function. """ function sinkhorn_unbalanced_translation_invariant(a, b, M, reg, reg_m; kwargs...) - return pot.unbalanced.sinkhorn_unbalanced_translation_invariant(a, b, M, reg, reg_m; kwargs...) + return pot.unbalanced.sinkhorn_unbalanced_translation_invariant( + a, b, M, reg, reg_m; kwargs... + ) end """ diff --git a/test/Project.toml b/test/Project.toml index 522c06f..545d959 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -5,4 +5,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] Documenter = "0.27, 1" -julia = "1" +julia = "1.10" diff --git a/test/runtests.jl b/test/runtests.jl index 8831f6f..17bd799 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -45,8 +45,9 @@ PythonOT.PyCall.pyimport("warnings").filterwarnings("ignore") @test wasserstein_1d([0.0, 1.0], [1.0, 2.0]) ≈ 1 @test mm_unbalanced2(μ[1:2], ν, C[1:2, :], 5) isa Real @test lbfgsb_unbalanced(μ, ν, C, 0.1, 1.0) isa Matrix - @test size(empirical_sinkhorn([0.0 0.0; 1.0 1.0], [0.0 1.0; 2.0 2.0; 3.0 3.0], 1.0)) == - (2, 3) + @test size( + empirical_sinkhorn([0.0 0.0; 1.0 1.0], [0.0 1.0; 2.0 2.0; 3.0 3.0], 1.0) + ) == (2, 3) @test empirical_sinkhorn2([0.0; 1.0;;], [2.0; 3.0;;], 0.1) isa Any xs = [0.0 0.0; 1.0 0.0; 0.0 1.0] From 6639880595082f0fcb79cddc7ae3f192ed5bce43 Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 11:01:45 +0100 Subject: [PATCH 12/16] Expand and organize transport wrapper tests Split wrapper tests by module and add coverage for transport solvers, barycenters, marginals, and utility functions. --- test/bregman.jl | 45 +++++++++++++++++++++++++++++++++++++++++++ test/da.jl | 17 ++++++++++++++++ test/gromov.jl | 42 ++++++++++++++++++++++++++++++++++++++++ test/lp.jl | 27 ++++++++++++++++++++++++++ test/partial.jl | 15 +++++++++++++++ test/runtests.jl | 48 +++------------------------------------------- test/sliced.jl | 15 +++++++++++++++ test/smooth.jl | 14 ++++++++++++++ test/unbalanced.jl | 24 +++++++++++++++++++++++ test/utils.jl | 17 ++++++++++++++++ 10 files changed, 219 insertions(+), 45 deletions(-) create mode 100644 test/bregman.jl create mode 100644 test/da.jl create mode 100644 test/gromov.jl create mode 100644 test/lp.jl create mode 100644 test/partial.jl create mode 100644 test/sliced.jl create mode 100644 test/smooth.jl create mode 100644 test/unbalanced.jl create mode 100644 test/utils.jl diff --git a/test/bregman.jl b/test/bregman.jl new file mode 100644 index 0000000..a8c4859 --- /dev/null +++ b/test/bregman.jl @@ -0,0 +1,45 @@ +@testset "bregman" begin + n = 20 + x = collect(range(0, 1; length=n)) + a = fill(1 / n, n) + b = fill(1 / n, n) + M = [(xi - xj)^2 for xi in x, xj in x] + M ./= maximum(M) + reg = 1.0 + + # test_bregman.py: all solvers agree and satisfy the marginals + G = sinkhorn(a, b, M, reg) + for solver in (sinkhorn_log, sinkhorn_stabilized) + Gs = solver(a, b, M, reg) + @test Gs ≈ G atol = 1e-5 + @test vec(sum(Gs; dims=2)) ≈ a atol = 1e-5 + @test vec(sum(Gs; dims=1)) ≈ b atol = 1e-5 + end + Ge = sinkhorn_epsilon_scaling(a, b, M, reg) + @test vec(sum(Ge; dims=2)) ≈ a atol = 1e-4 + Gg = greenkhorn(a, b, M, reg) + @test vec(sum(Gg; dims=2)) ≈ a atol = 1e-3 + Gscreen = screenkhorn(a, b, M, reg; ns_budget=15, nt_budget=15) + @test size(Gscreen) == (n, n) + + # empirical Sinkhorn (test_empirical_sinkhorn) + Xs = reshape(x, n, 1) + Xt = reshape(x .+ 0.5, n, 1) + Me = [(xi - xj)^2 for xi in Xs[:, 1], xj in Xt[:, 1]] + @test empirical_sinkhorn(Xs, Xt, reg) ≈ sinkhorn(a, b, Me, reg) atol = 1e-6 + @test empirical_sinkhorn2(Xs, Xt, reg) ≈ sinkhorn2(a, b, Me, reg) atol = 1e-6 + @test empirical_sinkhorn_divergence(Xs, Xt, reg) isa Real + + # barycenters (test_bregman.py::test_barycenter, test_convolutional_barycenter) + A = hcat(fill(1 / n, n), [exp(-(xi - 0.5)^2 / 0.02) for xi in x]) + A ./= sum(A; dims=1) + bary = barycenter_debiased(A, M, reg; weights=[0.5, 0.5]) + @test sum(bary) ≈ 1 atol = 1e-3 + img = zeros(2, 8, 8) + img[1, 2:3, 2:3] .= 1 + img[2, 6:7, 6:7] .= 1 + img ./= sum(img; dims=(2, 3)) + cb = convolutional_barycenter2d(img, 0.05) + @test size(cb) == (8, 8) + @test sum(cb) ≈ 1 atol = 1e-3 +end diff --git a/test/da.jl b/test/da.jl new file mode 100644 index 0000000..8a4bfd1 --- /dev/null +++ b/test/da.jl @@ -0,0 +1,17 @@ +@testset "da" begin + # test_da.py::test_sinkhorn_l1l2_transport_class / test_emd_laplace + n = 10 + xs = [Float64(i) for i in 1:n, _ in 1:2] + xt = xs .+ 0.1 + labels = repeat([1, 2]; inner=n ÷ 2) + a = fill(1 / n, n) + b = fill(1 / n, n) + M = [sum(abs2, xs[i, :] - xt[j, :]) for i in 1:n, j in 1:n] + M ./= maximum(M) + + G = sinkhorn_l1l2_gl(a, labels, b, M, 1.0; eta=1.0) + @test vec(sum(G; dims=2)) ≈ a atol = 1e-3 + G = sinkhorn_lpl1_mm(a, labels, b, M, 1.0; eta=1.0) + @test vec(sum(G; dims=2)) ≈ a atol = 1e-3 + # emd_laplace needs scikit-learn, which is not a dependency of POT +end diff --git a/test/gromov.jl b/test/gromov.jl new file mode 100644 index 0000000..76682ab --- /dev/null +++ b/test/gromov.jl @@ -0,0 +1,42 @@ +@testset "gromov" begin + # test_gromov.py: identical structures have zero cost + xs = [0.0 0.0; 1.0 0.0; 0.0 1.0; 1.0 1.0] + C1 = [sqrt(sum(abs2, xs[i, :] - xs[j, :])) for i in 1:4, j in 1:4] + p = fill(0.25, 4) + @test gromov_wasserstein2(p, p, C1, C1) ≥ 0 + G = gromov_wasserstein(p, p, C1, C1) + @test vec(sum(G; dims=2)) ≈ p atol = 1e-8 + @test vec(sum(G; dims=1)) ≈ p atol = 1e-8 + Ge = entropic_gromov_wasserstein(p, p, C1, C1, 0.1) + @test vec(sum(Ge; dims=2)) ≈ p atol = 1e-5 + @test entropic_gromov_wasserstein2(p, p, C1, C1, 0.1) isa Real + + # fused GW with a zero feature cost reduces to GW + M = zeros(4, 4) + @test fused_gromov_wasserstein2(p, p, M, C1, C1) ≥ 0 + Gf = fused_gromov_wasserstein(p, p, M, C1, C1) + @test vec(sum(Gf; dims=2)) ≈ p atol = 1e-8 + Gef = entropic_fused_gromov_wasserstein(p, p, M, C1, C1, 0.1) + @test vec(sum(Gef; dims=2)) ≈ p atol = 1e-5 + + # semi-relaxed GW keeps the source marginal + Gs = semirelaxed_gromov_wasserstein(C1, C1, p) + @test vec(sum(Gs; dims=2)) ≈ p atol = 1e-8 + + # barycenters (test_gromov.py::test_gromov_barycenter, test_fgw_barycenter) + Cb = gromov_barycenters(4, [C1, C1]; max_iter=20) + @test size(Cb) == (4, 4) + Ys = [xs[:, 1:1], xs[:, 1:1]] + X, Cf = fgw_barycenters(4, Ys, [C1, C1]; max_iter=5) + @test size(X) == (4, 1) + @test size(Cf) == (4, 4) + + # partial GW (test_partial.py::test_partial_gromov_wasserstein) + m = 0.5 + Gp = partial_gromov_wasserstein(C1, C1, p, p; m=m) + @test sum(Gp) ≈ m atol = 1e-6 + @test partial_gromov_wasserstein2(C1, C1, p, p; m=m) isa Real + Gep = entropic_partial_gromov_wasserstein(C1, C1, p, p, 10.0; m=m) + @test sum(Gep) ≈ m atol = 1e-3 + @test entropic_partial_gromov_wasserstein2(C1, C1, p, p, 10.0; m=m) isa Real +end diff --git a/test/lp.jl b/test/lp.jl new file mode 100644 index 0000000..0bc7fc4 --- /dev/null +++ b/test/lp.jl @@ -0,0 +1,27 @@ +@testset "lp" begin + # ot.lp.emd_1d / wasserstein_1d (test_1d_solver.py) + x = [0.0, 1.0, 2.0, 3.0] + y = [1.0, 2.0, 3.0, 4.0] + @test wasserstein_1d(x, y; p=1) ≈ 1 + @test wasserstein_1d(x, y; p=2) ≈ 1 + @test wasserstein_1d(x, x) ≈ 0 atol = 1e-12 + + # circle distances (test_1d_solver.py) + u = [0.1, 0.4, 0.7] + v = [0.2, 0.5, 0.9] + w = only(wasserstein_circle(u, v)) + @test w ≥ 0 + @test only(wasserstein_circle(u, u)) ≈ 0 atol = 1e-8 + @test only(first(binary_search_circle(u, v; p=2))) ≈ + only(wasserstein_circle(u, v; p=2)) atol = 1e-2 + @test only(wasserstein_circle(u, u; p=2)) ≈ 0 atol = 1e-8 + @test only(linear_circular_ot(u, v)) ≥ 0 + + # free support barycenter (test_ot.py::test_free_support_barycenter) + measures_locations = [[0.0; 1.0;;], [1.0; 2.0;;]] + measures_weights = [[0.5, 0.5], [0.5, 0.5]] + X_init = [0.5; 1.5;;] + X = free_support_barycenter(measures_locations, measures_weights, X_init) + @test size(X) == (2, 1) + @test sort(vec(X)) ≈ [0.5, 1.5] atol = 1e-6 +end diff --git a/test/partial.jl b/test/partial.jl new file mode 100644 index 0000000..c90ffc4 --- /dev/null +++ b/test/partial.jl @@ -0,0 +1,15 @@ +@testset "partial" begin + # test_partial.py::test_partial_wasserstein + a = [0.5, 0.2, 0.3] + b = [0.4, 0.6] + M = [0.0 1.0; 2.0 0.0; 0.5 1.5] + G = partial_wasserstein(a, b, M; m=0.5) + @test sum(G) ≈ 0.5 + @test all(vec(sum(G; dims=2)) .≤ a .+ 1e-8) + @test all(vec(sum(G; dims=1)) .≤ b .+ 1e-8) + @test partial_wasserstein2(a, b, M; m=0.5) ≈ sum(G .* M) + Gl = partial_wasserstein_lagrange(a, b, M; reg_m=1.0) + @test size(Gl) == (3, 2) + Ge = entropic_partial_wasserstein(a, b, M, 0.1; m=0.5) + @test sum(Ge) ≈ 0.5 atol = 1e-3 +end diff --git a/test/runtests.jl b/test/runtests.jl index 17bd799..421604d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,7 +8,7 @@ using Pkg end using PythonOT -using PythonOT: dist, unif, solve, get_random_projections +using PythonOT: dist, unif, solve, solve_sample, get_random_projections using Documenter using Test @@ -28,49 +28,7 @@ PythonOT.PyCall.pyimport("warnings").filterwarnings("ignore") ) end - @testset "wrappers" begin - μ = [0.5, 0.2, 0.3] - ν = [0.4, 0.6] - C = [0.0 1.0; 2.0 0.0; 0.5 1.5] - @test partial_wasserstein(μ, ν, C; m=0.5) isa Matrix - @test size(partial_wasserstein(μ, ν, C; m=0.5)) == (3, 2) - @test partial_wasserstein2(μ, ν, C; m=0.5) isa Real - @test sum(partial_wasserstein(μ, ν, C; m=0.5)) ≈ 0.5 - @test sinkhorn_log(μ, ν, C, 0.1) ≈ sinkhorn(μ, ν, C, 0.1) atol = 1e-6 - @test sinkhorn_stabilized(μ, ν, C, 0.1) ≈ sinkhorn(μ, ν, C, 0.1) atol = 1e-6 - @test sum(greenkhorn(μ, ν, C, 0.1)) ≈ 1 atol = 1e-4 - @test unif(4) == fill(0.25, 4) - D = dist([0.0 0.0; 1.0 1.0], [0.0 0.0; 2.0 0.0]) - @test D == [0.0 4.0; 2.0 2.0] - @test wasserstein_1d([0.0, 1.0], [1.0, 2.0]) ≈ 1 - @test mm_unbalanced2(μ[1:2], ν, C[1:2, :], 5) isa Real - @test lbfgsb_unbalanced(μ, ν, C, 0.1, 1.0) isa Matrix - @test size( - empirical_sinkhorn([0.0 0.0; 1.0 1.0], [0.0 1.0; 2.0 2.0; 3.0 3.0], 1.0) - ) == (2, 3) - @test empirical_sinkhorn2([0.0; 1.0;;], [2.0; 3.0;;], 0.1) isa Any - - xs = [0.0 0.0; 1.0 0.0; 0.0 1.0] - Cs = dist(xs, xs) - p = unif(3) - @test gromov_wasserstein2(p, p, Cs, Cs) ≈ 0 atol = 1e-8 - @test fused_gromov_wasserstein2(p, p, zeros(3, 3), Cs, Cs) ≈ 0 atol = 1e-8 - @test size(fused_gromov_wasserstein(p, p, zeros(3, 3), Cs, Cs)) == (3, 3) - @test entropic_gromov_wasserstein2(p, p, Cs, Cs, 0.1) isa Real - @test size(semirelaxed_gromov_wasserstein(Cs, Cs, p)) == (3, 3) - @test size(partial_gromov_wasserstein(Cs, Cs, p, p; m=0.5)) == (3, 3) - @test partial_gromov_wasserstein2(Cs, Cs, p, p; m=0.5) isa Real - @test size(entropic_partial_gromov_wasserstein(Cs, Cs, p, p, 1.0; m=0.5)) == (3, 3) - @test entropic_partial_gromov_wasserstein2(Cs, Cs, p, p, 1.0; m=0.5) isa Real - @test size(gromov_barycenters(3, [Cs, Cs]; max_iter=5)) == (3, 3) - - X = [0.0 0.0; 1.0 1.0; 2.0 0.5] - @test sliced_wasserstein_distance(X, X; seed=0) ≈ 0 atol = 1e-8 - @test max_sliced_wasserstein_distance(X, X; seed=0) ≈ 0 atol = 1e-8 - @test size(get_random_projections(3, 5; seed=0)) == (3, 5) - @test only(wasserstein_circle([0.1, 0.5], [0.1, 0.5])) ≈ 0 atol = 1e-8 - - res = solve(C, μ, ν) - @test res.value isa Real + for f in ("lp", "bregman", "unbalanced", "partial", "gromov", "sliced", "da", "smooth", "utils") + include(f * ".jl") end end diff --git a/test/sliced.jl b/test/sliced.jl new file mode 100644 index 0000000..35dd2d2 --- /dev/null +++ b/test/sliced.jl @@ -0,0 +1,15 @@ +@testset "sliced" begin + # test_sliced.py + X = [0.0 0.0; 1.0 1.0; 2.0 0.5; 0.5 2.0] + Y = X .+ 1.0 + @test sliced_wasserstein_distance(X, X; seed=0) ≈ 0 atol = 1e-8 + @test sliced_wasserstein_distance(X, Y; seed=0, n_projections=500) > 0 + @test max_sliced_wasserstein_distance(X, X; seed=0) ≈ 0 atol = 1e-8 + @test max_sliced_wasserstein_distance(X, Y; seed=0) > 0 + P = get_random_projections(3, 5; seed=0) + @test size(P) == (3, 5) + @test vec(sqrt.(sum(abs2, P; dims=1))) ≈ ones(5) atol = 1e-8 + S = X ./ sqrt.(sum(abs2, X; dims=2)) + S = S[2:end, :] + @test sliced_wasserstein_sphere(S, S; seed=0) ≈ 0 atol = 1e-6 +end diff --git a/test/smooth.jl b/test/smooth.jl new file mode 100644 index 0000000..9f02f3b --- /dev/null +++ b/test/smooth.jl @@ -0,0 +1,14 @@ +@testset "smooth" begin + using PythonOT.Smooth + # test_smooth.py: the dual and semi-dual solutions satisfy the marginals + a = [0.5, 0.2, 0.3] + b = [0.4, 0.6] + M = [0.0 1.0; 2.0 0.0; 0.5 1.5] + for reg_type in ("l2", "kl") + G = smooth_ot_dual(a, b, M, 1.0; reg_type=reg_type) + @test vec(sum(G; dims=2)) ≈ a atol = 1e-4 + @test vec(sum(G; dims=1)) ≈ b atol = 1e-4 + Gs = smooth_ot_semi_dual(a, b, M, 1.0; reg_type=reg_type) + @test vec(sum(Gs; dims=2)) ≈ a atol = 1e-3 + end +end diff --git a/test/unbalanced.jl b/test/unbalanced.jl new file mode 100644 index 0000000..cecd502 --- /dev/null +++ b/test/unbalanced.jl @@ -0,0 +1,24 @@ +@testset "unbalanced" begin + n = 10 + x = collect(range(0, 1; length=n)) + a = fill(1 / n, n) + b = fill(1 / n, n) + M = [(xi - xj)^2 for xi in x, xj in x] + + # test_unbalanced.py + G = mm_unbalanced(a, b, M, 1.0) + @test size(G) == (n, n) + @test mm_unbalanced2(a, b, M, 1.0) ≈ sum(G .* M) atol = 1e-6 + Gl = lbfgsb_unbalanced(a, b, M, 0.1, 1.0) + @test size(Gl) == (n, n) + @test lbfgsb_unbalanced2(a, b, M, 0.1, 1.0) isa Real + Gti = sinkhorn_unbalanced_translation_invariant(a, b, M, 1.0, 10.0) + @test size(Gti) == (n, n) + @test sum(Gti) ≈ 1 atol = 1e-1 + @test sinkhorn_unbalanced(a, b, M, 1.0, 10.0) isa Matrix + + # test_unbalanced.py::test_uot_1d + u = [0.0, 1.0, 2.0] + # the NumPy backend is rejected by POT; only torch and jax are supported + @test_throws PythonOT.PyCall.PyError uot_1d(u, u, 1.0) +end diff --git a/test/utils.jl b/test/utils.jl new file mode 100644 index 0000000..c9571e8 --- /dev/null +++ b/test/utils.jl @@ -0,0 +1,17 @@ +@testset "utils" begin + @test unif(4) == fill(0.25, 4) + @test dist([0.0 0.0; 1.0 1.0], [0.0 0.0; 2.0 0.0]) == [0.0 4.0; 2.0 2.0] + @test dist([0.0 0.0; 3.0 4.0]; metric="euclidean") ≈ [0.0 5.0; 5.0 0.0] + + # test_solvers.py + M = [0.0 1.0; 2.0 0.0; 0.5 1.5] + a = [0.5, 0.2, 0.3] + b = [0.4, 0.6] + res = solve(M, a, b) + @test res.value ≈ emd2(a, b, M) + @test res.plan ≈ emd(a, b, M) + xs = [0.0 0.0; 1.0 0.0; 0.0 1.0] + xt = [0.0 0.0; 2.0 0.0] + res2 = solve_sample(xs, xt) + @test size(res2.plan) == (3, 2) +end From db00172104c2af9010649aa874467f242594f62b Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 11:29:58 +0100 Subject: [PATCH 13/16] Run Julia CI on macOS ARM64 Update CI and nightly matrices to test ARM64 on macOS while retaining x64 on Ubuntu and Windows. --- .github/workflows/CI.yml | 8 ++++++++ .github/workflows/JuliaNightly.yml | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 6b307cd..9670391 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -30,6 +30,14 @@ jobs: - windows-latest arch: - x64 + - arm64 + exclude: + - os: ubuntu-latest + arch: arm64 + - os: windows-latest + arch: arm64 + - os: macOS-latest + arch: x64 steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 diff --git a/.github/workflows/JuliaNightly.yml b/.github/workflows/JuliaNightly.yml index 5fb0206..6bdc41e 100644 --- a/.github/workflows/JuliaNightly.yml +++ b/.github/workflows/JuliaNightly.yml @@ -30,6 +30,14 @@ jobs: - windows-latest arch: - x64 + - arm64 + exclude: + - os: ubuntu-latest + arch: arm64 + - os: windows-latest + arch: arm64 + - os: macOS-latest + arch: x64 python: - '' - python From 3f229448940232cc47eefb33cea9a623a48a897b Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 11:44:40 +0100 Subject: [PATCH 14/16] Make barycenter doctests deterministic Replace random inputs with fixed distributions and distance costs. Check the unbalanced barycenter's shape and nonnegativity instead of relying on its total mass. --- src/bregman.jl | 8 +++++--- src/unbalanced.jl | 16 +++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/bregman.jl b/src/bregman.jl index 2430d36..42ae7d2 100644 --- a/src/bregman.jl +++ b/src/bregman.jl @@ -161,13 +161,15 @@ Python function. # Examples ```jldoctest -julia> A = rand(10, 3); +julia> x = range(0, 1; length=10); + +julia> A = hcat([exp.(-(x .- m) .^ 2 ./ 0.02) for m in (0.2, 0.5, 0.8)]...); julia> A ./= sum(A; dims=1); -julia> C = rand(10, 10); +julia> C = [(xi - xj)^2 for xi in x, xj in x]; -julia> isapprox(sum(barycenter(A, C, 0.01; method="sinkhorn_stabilized")), 1; atol=1e-4) +julia> isapprox(sum(barycenter(A, C, 0.05; method="sinkhorn_stabilized")), 1; atol=1e-3) true ``` """ diff --git a/src/unbalanced.jl b/src/unbalanced.jl index 1486d03..40edd70 100644 --- a/src/unbalanced.jl +++ b/src/unbalanced.jl @@ -114,18 +114,20 @@ Python function. # Examples ```jldoctest -julia> A = rand(10, 3); +julia> x = range(0, 1; length=10); + +julia> A = hcat([exp.(-(x .- m) .^ 2 ./ 0.02) for m in (0.2, 0.5, 0.8)]...); julia> A ./= sum(A; dims=1); -julia> C = rand(10, 10); +julia> C = [(xi - xj)^2 for xi in x, xj in x]; + +julia> B = barycenter_unbalanced(A, C, 0.01, 10; method="sinkhorn_stabilized"); -julia> isapprox(sum(barycenter_unbalanced(A, C, 0.01, 1; method="sinkhorn_stabilized")), 1; atol=1e-4) -false +julia> size(B) +(10,) -julia> isapprox(sum(barycenter_unbalanced( - A, C, 0.01, 10_000; method="sinkhorn_stabilized", numItermax=5_000 - )), 1; atol=1e-4) +julia> all(B .>= 0) true ``` From 72dde85cb9702c710725b12d7cffd8432b1c8774 Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 12:09:05 +0100 Subject: [PATCH 15/16] Format Julia test files --- test/lp.jl | 4 ++-- test/runtests.jl | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/lp.jl b/test/lp.jl index 0bc7fc4..53f41cf 100644 --- a/test/lp.jl +++ b/test/lp.jl @@ -12,8 +12,8 @@ w = only(wasserstein_circle(u, v)) @test w ≥ 0 @test only(wasserstein_circle(u, u)) ≈ 0 atol = 1e-8 - @test only(first(binary_search_circle(u, v; p=2))) ≈ - only(wasserstein_circle(u, v; p=2)) atol = 1e-2 + @test only(first(binary_search_circle(u, v; p=2))) ≈ only(wasserstein_circle(u, v; p=2)) atol = + 1e-2 @test only(wasserstein_circle(u, u; p=2)) ≈ 0 atol = 1e-8 @test only(linear_circular_ot(u, v)) ≥ 0 diff --git a/test/runtests.jl b/test/runtests.jl index 421604d..cc363f1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -28,7 +28,17 @@ PythonOT.PyCall.pyimport("warnings").filterwarnings("ignore") ) end - for f in ("lp", "bregman", "unbalanced", "partial", "gromov", "sliced", "da", "smooth", "utils") + for f in ( + "lp", + "bregman", + "unbalanced", + "partial", + "gromov", + "sliced", + "da", + "smooth", + "utils", + ) include(f * ".jl") end end From 6181358add78ff4e3c95dc456c4b449beb1a15b5 Mon Sep 17 00:00:00 2001 From: Pierre Navaro Date: Wed, 25 Nov 2026 12:12:08 +0100 Subject: [PATCH 16/16] Update GitHub Actions versions Update checkout, Python, Julia, caching, and coverage actions across CI and documentation workflows. Pin Coveralls and Julia setup to versioned releases. --- .github/workflows/CI.yml | 12 ++++++------ .github/workflows/Docs.yml | 4 ++-- .github/workflows/DocsPreviewCleanup.yml | 2 +- .github/workflows/Format.yml | 4 ++-- .github/workflows/JuliaNightly.yml | 8 ++++---- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 9670391..fccf6d2 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -39,12 +39,12 @@ jobs: - os: macOS-latest arch: x64 steps: - - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 + - uses: actions/checkout@v7 + - uses: julia-actions/setup-julia@v3 with: version: ${{ matrix.version }} arch: ${{ matrix.arch }} - - uses: julia-actions/cache@v2 + - uses: julia-actions/cache@v3 with: cache-packages: "false" # Weird interaction with PyCall settings? - uses: julia-actions/julia-buildpkg@v1 @@ -63,12 +63,12 @@ jobs: shell: julia --project=. --color=yes {0} - uses: julia-actions/julia-runtest@v1 - uses: julia-actions/julia-processcoverage@v1 - - uses: codecov/codecov-action@v4 + - uses: codecov/codecov-action@v7 with: files: lcov.info fail_ci_if_error: true token: ${{ secrets.CODECOV_TOKEN }} - - uses: coverallsapp/github-action@master + - uses: coverallsapp/github-action@v2 with: github-token: ${{ secrets.GITHUB_TOKEN }} path-to-lcov: ./lcov.info @@ -79,7 +79,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Coveralls Finished - uses: coverallsapp/github-action@master + uses: coverallsapp/github-action@v2 with: github-token: ${{ secrets.github_token }} parallel-finished: true diff --git a/.github/workflows/Docs.yml b/.github/workflows/Docs.yml index f6e1398..0bcfd5a 100644 --- a/.github/workflows/Docs.yml +++ b/.github/workflows/Docs.yml @@ -18,8 +18,8 @@ jobs: name: Documentation runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 + - uses: actions/checkout@v7 + - uses: julia-actions/setup-julia@v3 with: version: '1' - uses: julia-actions/julia-docdeploy@v1 diff --git a/.github/workflows/DocsPreviewCleanup.yml b/.github/workflows/DocsPreviewCleanup.yml index c5c8c8c..3b391c0 100644 --- a/.github/workflows/DocsPreviewCleanup.yml +++ b/.github/workflows/DocsPreviewCleanup.yml @@ -9,7 +9,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout gh-pages branch - uses: actions/checkout@v4 + uses: actions/checkout@v7 with: ref: gh-pages - name: Delete preview and history + push changes diff --git a/.github/workflows/Format.yml b/.github/workflows/Format.yml index fc1a7c2..dfa8131 100644 --- a/.github/workflows/Format.yml +++ b/.github/workflows/Format.yml @@ -7,8 +7,8 @@ jobs: format: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@latest + - uses: actions/checkout@v7 + - uses: julia-actions/setup-julia@v3 with: version: 1 - name: Install JuliaFormatter and format code diff --git a/.github/workflows/JuliaNightly.yml b/.github/workflows/JuliaNightly.yml index 6bdc41e..6e8b553 100644 --- a/.github/workflows/JuliaNightly.yml +++ b/.github/workflows/JuliaNightly.yml @@ -42,20 +42,20 @@ jobs: - '' - python steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - name: Install python - uses: actions/setup-python@v5 + uses: actions/setup-python@v7 with: python-version: '3.x' architecture: ${{ matrix.arch }} if: matrix.python - run: python -m pip install pot if: matrix.python - - uses: julia-actions/setup-julia@v2 + - uses: julia-actions/setup-julia@v3 with: version: ${{ matrix.version }} arch: ${{ matrix.arch }} - - uses: julia-actions/cache@v2 + - uses: julia-actions/cache@v3 with: cache-packages: "false" # Weird interaction with PyCall settings? - uses: julia-actions/julia-buildpkg@v1