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..fccf6d2 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,18 +30,21 @@ jobs: - windows-latest arch: - x64 + - arm64 exclude: - # Conda installation fails: https://github.com/JuliaPy/Conda.jl/issues/230 - - version: '1.0' - os: windows-latest + - 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 + - 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 @@ -61,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 @@ -77,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/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/.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 5fb0206..6e8b553 100644 --- a/.github/workflows/JuliaNightly.yml +++ b/.github/workflows/JuliaNightly.yml @@ -30,24 +30,32 @@ jobs: - windows-latest arch: - x64 + - arm64 + exclude: + - os: ubuntu-latest + arch: arm64 + - os: windows-latest + arch: arm64 + - os: macOS-latest + arch: x64 python: - '' - 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 diff --git a/Project.toml b/Project.toml index d8894ac..d515483 100644 --- a/Project.toml +++ b/Project.toml @@ -1,14 +1,14 @@ 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" [compat] PyCall = "1" -julia = "1" +julia = "1.10" [extras] Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d" diff --git a/docs/src/api.md b/docs/src/api.md index 00409b1..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,4 +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 d801f3d..47fa1d6 100644 --- a/src/PythonOT.jl +++ b/src/PythonOT.jl @@ -13,15 +13,63 @@ export emd, sinkhorn_unbalanced, sinkhorn_unbalanced2, empirical_sinkhorn_divergence, - mm_unbalanced + mm_unbalanced, + entropic_partial_wasserstein, + entropic_partial_gromov_wasserstein, + entropic_partial_gromov_wasserstein2, + partial_wasserstein, + 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__() - 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/bregman.jl b/src/bregman.jl new file mode 100644 index 0000000..42ae7d2 --- /dev/null +++ b/src/bregman.jl @@ -0,0 +1,293 @@ +""" + 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> 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 = [(xi - xj)^2 for xi in x, xj in x]; + +julia> isapprox(sum(barycenter(A, C, 0.05; method="sinkhorn_stabilized")), 1; atol=1e-3) +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..00f8a2e --- /dev/null +++ b/src/gromov.jl @@ -0,0 +1,198 @@ +""" + 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 + +""" + 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/lib.jl b/src/lib.jl deleted file mode 100644 index 07bb9af..0000000 --- a/src/lib.jl +++ /dev/null @@ -1,568 +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.2, 0.3]; - -julia> ν = [0.0, 1.0]; - -julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; - -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 -``` - -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) -""" -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.2, 0.3]; - -julia> ν = [0.0, 1.0]; - -julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5]; - -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: - -```jldoctest sinkhorn_unbalanced2 -julia> ν = [0.0 0.5; 1.0 0.5]; - -julia> round.(sinkhorn_unbalanced2(μ, ν, C, 0.01, 1_000); sigdigits=4) -2-element Vector{Float64}: - 0.9497 - 0.4494 -``` - -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 diff --git a/src/lp.jl b/src/lp.jl new file mode 100644 index 0000000..fc471f7 --- /dev/null +++ b/src/lp.jl @@ -0,0 +1,209 @@ +""" + 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..244dd80 --- /dev/null +++ b/src/partial.jl @@ -0,0 +1,79 @@ +""" + 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 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..40edd70 --- /dev/null +++ b/src/unbalanced.jl @@ -0,0 +1,256 @@ +""" + 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> 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 = [(xi - xj)^2 for xi in x, xj in x]; + +julia> B = barycenter_unbalanced(A, C, 0.01, 10; method="sinkhorn_stabilized"); + +julia> size(B) +(10,) + +julia> all(B .>= 0) +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/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/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..53f41cf --- /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 a803048..cc363f1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,9 +8,13 @@ using Pkg end using PythonOT +using PythonOT: dist, unif, solve, solve_sample, 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) @@ -23,4 +27,18 @@ using Test ], ) end + + 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