### Install POT using Pip Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Install the POT library from PyPI. This is the standard method for most users. ```console pip install POT ``` -------------------------------- ### Install Latest POT Version using Pip Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Install the latest development version of POT directly from its GitHub repository. Use '--user' for installations without root privileges. ```console pip install -U git+https://github.com/PythonOT/POT.git # with --user for user install (no root) ``` -------------------------------- ### Install and Configure Pre-commit Hooks Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Install the pre-commit package and set up the hooks to run on every commit. If hooks fail, the commit will be aborted. ```bash $ pip install pre-commit $ pre-commit install ``` -------------------------------- ### Build POT Project with Pip Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Install the POT project in editable mode using pip. Use the '[all]' extra to install all dependencies. ```bash pip install -e . # Or to install all dependencies: pip install -e .[all] ``` -------------------------------- ### Install POT with All Optional Dependencies Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Install POT along with all optional dependencies, including cvxopt. Note that cvxopt is licensed under GPL 3.0. ```console pip install POT[all] ``` -------------------------------- ### Import POT Module Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Verify a successful installation by importing the POT module. The module is conventionally imported as 'ot'. ```python import ot ``` -------------------------------- ### Install POT using Conda Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Install POT and its required dependencies using conda from the conda-forge channel. This is recommended for users of the Anaconda distribution. ```console conda install -c conda-forge pot ``` -------------------------------- ### Install Dependencies for ot.dr Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Install the specific dependencies required for the 'ot.dr' (Wasserstein dimensionality reduction) submodule, namely autograd and pymanopt. ```shell pip install pymanopt autograd ``` -------------------------------- ### Get POT and Environment Version Information Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Run this snippet to gather essential version information for your operating system, Python, POT, NumPy, and SciPy. This is crucial for bug reports. ```python import platform; print(platform.platform()) import sys; print("Python", sys.version) import numpy; print("NumPy", numpy.__version__) import scipy; print("SciPy", scipy.__version__) import ot; print("POT", ot.__version__) ``` -------------------------------- ### Install LLVM OpenMP on macOS Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Install the LLVM OpenMP library using Homebrew on macOS and set the CC and CXX environment variables to use the LLVM clang compiler. ```bash $ brew install libomp $ export CC=/usr/local/opt/llvm/bin/clang $ export CXX=/usr/local/opt/llvm/bin/clang++ ``` -------------------------------- ### Clone POT Repository Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Clone your fork of the POT repository to your local machine and navigate into the project directory. ```bash $ git clone git@github.com:YourLogin/POT.git $ cd POT ``` -------------------------------- ### Recover Solutions Objects Source: https://github.com/pythonot/pot/blob/master/RELEASES.md Demonstrates how to recover the OT plan, dual variables, and the OT value from a solution object obtained from OT solvers. ```python # recover solutions objects P = sol.plan # OT plan u, v = sol.potentials # dual variables value = sol.value # OT value # for GW and FGW value_linear = sol.value_linear # linear part of the loss value_quad = sol.value_quad # quadratic part of the loss ``` -------------------------------- ### Solve OT with Empirical Samples using ot.solve_sample Source: https://github.com/pythonot/pot/blob/master/docs/source/releases.md Demonstrates various ways to use the `ot.solve_sample` function for solving Optimal Transport problems with empirical samples. It covers exact OT with uniform or custom weights, Sinkhorn regularization, faster solvers like `geomloss`, factored and low-rank approximations, and Bures-Wasserstein distance calculation. ```python # Generate random data xs, xt = np.random.randn(100, 2), np.random.randn(50, 2) # Solve OT problem with empirical samples sol = ot.solve_sample(xs, xt) # Exact OT betwen smaples with uniform weights sol = ot.solve_sample(xs, xt, wa, wb) # Exact OT with weights given by user sol = ot.solve_sample(xs, xt, reg= 1, metric='euclidean') # sinkhorn with euclidean metric sol = ot.solve_sample(xs, xt, reg= 1, method='geomloss') # faster sinkhorn solver on CPU/GPU sol = ot.solve_sample(x,x2, method='factored', rank=10) # compute factored OT sol = ot.solve_sample(x,x2, method='lowrank', rank=10) # compute lowrank sinkhorn OT value_bw = ot.solve_sample(xs, xt, method='gaussian').value # Bures-Wasserstein distance ``` ```python # Solve GW problem Cs, Ct = ot.dist(xs, xs), ot.dist(xt, xt) # compute cost matrices sol = ot.solve_gromov(Cs,Ct) # Exact GW between samples with uniform weights ``` ```python # Solve FGW problem M = ot.dist(xs, xt) # compute cost matrix # Exact FGW between samples with uniform weights sol = ot.solve_gromov(Cs, Ct, M, loss='KL', alpha=0.7) # FGW with KL data fitting ``` ```python # recover solutions objects P = sol.plan # OT plan u, v = sol.potentials # dual variables value = sol.value # OT value # for GW and FGW value_linear = sol.value_linear # linear part of the loss value_quad = sol.value_quad # quadratic part of the loss ``` -------------------------------- ### Solve Fat-Gromov-Wasserstein Problem Source: https://github.com/pythonot/pot/blob/master/RELEASES.md Illustrates solving the Fat-Gromov-Wasserstein problem with KL data fitting loss, specifying cost matrices and alpha parameter. ```python # Solve FGW problem M = ot.dist(xs, xt) # compute cost matrix # Exact FGW between samples with uniform weights sol = ot.solve_gromov(Cs, Ct, M, loss='KL', alpha=0.7) # FGW with KL data fitting ``` -------------------------------- ### Add and Commit Changes Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Stage modified files using 'git add' and commit them to record your changes. Then, push the changes to your GitHub account. ```bash $ git add modified_files $ git commit $ git push -u origin my-feature ``` -------------------------------- ### Solve OT Problem with Empirical Samples Source: https://github.com/pythonot/pot/blob/master/RELEASES.md Demonstrates solving exact Optimal Transport problems between empirical samples with uniform weights and with user-defined weights. ```python # Generate random data xs, xt = np.random.randn(100, 2), np.random.randn(50, 2) # Solve OT problem with empirical samples sol = ot.solve_sample(xs, xt) # Exact OT betwen smaples with uniform weights sol = ot.solve_sample(xs, xt, wa, wb) # Exact OT with weights given by user ``` -------------------------------- ### Create and Activate Conda Environment Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Create a new conda environment named 'dev-pot-env' with Python 3.12 and activate it for development. ```bash $ conda create -n dev-pot-env python=3.12 $ conda activate dev-pot-env ``` -------------------------------- ### Run Pre-commit Checks Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Execute pre-commit hooks to ensure adherence to project coding standards and identify common programming errors. ```bash $ pre-commit run --all-files ``` -------------------------------- ### Solve Gromov-Wasserstein Problem Source: https://github.com/pythonot/pot/blob/master/RELEASES.md Shows how to solve the exact Gromov-Wasserstein problem between samples with uniform weights, requiring cost matrices as input. ```python # Solve GW problem Cs, Ct = ot.dist(xs, xs), ot.dist(xt, xt) # compute cost matrices sol = ot.solve_gromov(Cs,Ct) # Exact GW between samples with uniform weights ``` -------------------------------- ### Cite POT Library (Default Format) Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Use these references when citing the POT toolbox in your research. This format is suitable for general academic use. ```default Flamary R., Vincent-Cuaz C., Courty N., Gramfort A., Kachaiev O., Quang Tran H., David L., Bonet C., Cassereau N., Gnassounou T., Tanguy E., Delon J., Collas A., Mazelet S., Chapel L., Kerdoncuff T., Yu X., Feickert M., Krzakala P., Liu T., Fernandes Montesuma E. POT Python Optimal Transport (version 0.9.5). URL: https://github.com/PythonOT/POT Rémi Flamary, Nicolas Courty, Alexandre Gramfort, Mokhtar Z. Alaya, Aurélie Boisbunon, Stanislas Chambon, Laetitia Chapel, Adrien Corenflos, Kilian Fatras, Nemo Fournier, Léo Gautheron, Nathalie T.H. Gayraud, Hicham Janati, Alain Rakotomamonjy, Ievgen Redko, Antoine Rolet, Antony Schutz, Vivien Seguy, Danica J. Sutherland, Romain Tavenard, Alexander Tong, Titouan Vayer, POT Python Optimal Transport library, Journal of Machine Learning Research, 22(78):1−8, 2021. URL: https://pythonot.github.io/ ``` -------------------------------- ### Solving OT with Different Array Types Source: https://github.com/pythonot/pot/blob/master/docs/source/user_guide.md POT's backend system enables the use of OT solvers like ot.emd and ot.emd2 with various input array types, including NumPy, PyTorch, and JAX. The output type and device will match the input. ```python # a and b are 1D histograms (sum to 1 and positive) # M is the ground cost matrix T = ot.emd(a, b, M) # exact linear program w = ot.emd2(a, b, M) # Wasserstein computation ``` -------------------------------- ### Compute OT Matrix (Old API) Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Compute the exact linear program (LP) solved optimal transport plan and the entropic regularized optimal transport plan using the older API. Assumes M is the ground cost matrix and a, b are 1D histograms. ```python # a,b are 1D histograms (sum to 1 and positive) # M is the ground cost matrix # With the old API : T = ot.emd(a, b, M) # exact linear program T_reg = ot.sinkhorn(a, b, M, reg) # entropic regularized OT ``` -------------------------------- ### Solve Optimal Transport Matrix (Classical API) Source: https://github.com/pythonot/pot/blob/master/docs/source/user_guide.md Use the classical API to solve the exact linear program for optimal transport and obtain the transport matrix. Assumes 'M' is the cost matrix and 'a', 'b' are 1D histograms. ```python # a and b are 1D histograms (sum to 1 and positive) # M is the ground cost matrix # classical API T = ot.emd(a, b, M) # exact linear program ``` -------------------------------- ### Cite POT Library (BibTeX Format) Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md BibTeX formatted references for citing the POT toolbox, commonly used in LaTeX documents. ```bibtex @misc{flamary2024pot, author = {Flamary, R{\'e}mi and Vincent-Cuaz, C{\'e}dric and Courty, Nicolas and Gramfort, Alexandre and Kachaiev, Oleksii and Quang Tran, Huy and David, Laur{\`e}ne and Bonet, Cl{\'e}ment and Cassereau, Nathan and Gnassounou, Th{\'e}o and Tanguy, Eloi and Delon, Julie and Collas, Antoine and Mazelet, Sonia and Chapel, Laetitia and Kerdoncuff, Tanguy and Yu, Xizheng and Feickert, Matthew and Krzakala, Paul and Liu, Tianlin and Fernandes Montesuma, Eduardo}, title = {POT Python Optimal Transport (version 0.9.5)}, url = {https://github.com/PythonOT/POT}, year = {2024} } @article{flamary2021pot, author = {R{\'e}mi Flamary and Nicolas Courty and Alexandre Gramfort and Mokhtar Z. Alaya and Aur{\'e}lie Boisbunon and Stanislas Chambon and Laetitia Chapel and Adrien Corenflos and Kilian Fatras and Nemo Fournier and L{\'e}o Gautheron and Nathalie T.H. Gayraud and Hicham Janati and Alain Rakotomamonjy and Ievgen Redko and Antoine Rolet and Antony Schutz and Vivien Seguy and Danica J. Sutherland and Romain Tavenard and Alexander Tong and Titouan Vayer}, title = {POT: Python Optimal Transport}, journal = {Journal of Machine Learning Research}, year = {2021}, volume = {22}, number = {78}, pages = {1-8}, url = {http://jmlr.org/papers/v22/20-451.html} } ``` -------------------------------- ### Compute OT Matrix (Unified API) Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Compute the exact linear program (LP) solved optimal transport plan and the entropic regularized optimal transport plan using the unified API. Assumes M is the ground cost matrix and a, b are 1D histograms. ```python # a,b are 1D histograms (sum to 1 and positive) # M is the ground cost matrix # With the unified API : T = ot.solve(M, a, b).plan # exact linear program T_reg = ot.solve(M, a, b, reg=reg).plan # entropic regularized OT ``` -------------------------------- ### Run Pytest and Doctests Source: https://github.com/pythonot/pot/blob/master/docs/source/contributing.md Execute all tests, including module-level doctests, to verify code correctness and ensure no regressions. ```bash $ pytest --durations=20 -v test/ --doctest-modules ``` -------------------------------- ### Solve Optimal Transport Matrix (Unified API) Source: https://github.com/pythonot/pot/blob/master/docs/source/user_guide.md Use the unified API to solve the exact linear program for optimal transport and obtain the transport matrix. Assumes 'M' is the cost matrix and 'a', 'b' are 1D histograms. ```python # a and b are 1D histograms (sum to 1 and positive) # M is the ground cost matrix # unified API T = ot.solve(M, a, b).plan # exact linear program ``` -------------------------------- ### Compute Wasserstein Distances (Old API) Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Calculate exact linear program (LP) solved Wasserstein distance and entropic regularized Wasserstein distance using the older API. Assumes M is the ground cost matrix and a, b are 1D histograms. ```python # a,b are 1D histograms (sum to 1 and positive) # M is the ground cost matrix # With the old API : Wd = ot.emd2(a, b, M) # exact linear program Wd_reg = ot.sinkhorn2(a, b, M, reg) # entropic regularized OT # if b is a matrix compute all distances to a and return a vector ``` -------------------------------- ### Compute Factored and Low Rank OT Source: https://github.com/pythonot/pot/blob/master/RELEASES.md Illustrates the computation of factored Optimal Transport plans and low-rank Sinkhorn Optimal Transport plans. ```python sol = ot.solve_sample(x,x2, method='factored', rank=10) # compute factored OT sol = ot.solve_sample(x,x2, method='lowrank', rank=10) # compute lowrank sinkhorn OT ``` -------------------------------- ### Compute Wasserstein Distances (Unified API) Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Calculate exact linear program (LP) solved Wasserstein distance and entropic regularized Wasserstein distance using the unified API. Assumes M is the ground cost matrix and a, b are 1D histograms. ```python # a,b are 1D histograms (sum to 1 and positive) # M is the ground cost matrix # With the unified API : Wd = ot.solve(M, a, b).value # exact linear program Wd_reg = ot.solve(M, a, b, reg=reg).value # entropic regularized OT ``` -------------------------------- ### Compute OT on Empirical Distributions (Unified API) Source: https://github.com/pythonot/pot/blob/master/docs/source/index.md Calculate the exact linear program (LP) solved transport plan and the entropic regularized transport plan for empirical distributions represented by arrays X and Y. Also computes the corresponding Wasserstein distances. ```python # X and Y are two 2D arrays of shape (n_samples, n_features) # with squared euclidean metric T = ot.solve_sample(X, Y).plan # exact linear program T_reg = ot.solve_sample(X, Y, reg=reg).plan # entropic regularized OT Wass_2 = ot.solve_sample(X, Y).value # Squared Wasserstein_2 Wass_1 = ot.solve_sample(X, Y, metric='euclidean').value # Wasserstein 1 ``` -------------------------------- ### Partial Gromov-Wasserstein Solver Source: https://github.com/pythonot/pot/blob/master/docs/source/user_guide.md Compute partial Gromov-Wasserstein distances using `ot.partial.partial_gromov_wasserstein` or its entropic regularized version `ot.partial.entropic_partial_gromov_wasserstein`. ```python import ot # Example usage (assuming C1, C2 are cost matrices, a, b are distributions, m is mass) # ot.partial.entropic_partial_gromov_wasserstein(C1, C2, a, b, m=1.0, reg=0.1) ``` -------------------------------- ### Solve OT Problem with Sinkhorn and Geomloss Source: https://github.com/pythonot/pot/blob/master/RELEASES.md Shows how to solve Optimal Transport problems using Sinkhorn regularization with Euclidean metric and a faster Sinkhorn solver on CPU/GPU via geomloss. ```python sol = ot.solve_sample(xs, xt, reg= 1, metric='euclidean') # sinkhorn with euclidean metric sol = ot.solve_sample(xs, xt, reg= 1, method='geomloss') # faster sinkhorn solver on CPU/GPU ```