Skip to main content

A Python wrapper for Ofir Pele and Michael Werman's implementation of the Earth Mover's Distance.

Project description

https://img.shields.io/travis/wmayner/pyemd/develop.svg?style=flat-square&maxAge=3600 Python versions badge

PyEMD: Fast EMD for Python

PyEMD is a Python wrapper for Ofir Pele and Michael Werman’s implementation of the Earth Mover’s Distance that allows it to be used with NumPy. If you use this code, please cite the papers listed at the end of this document.

Installation

To install the latest release:

pip install pyemd

Before opening an issue related to installation, please try to install PyEMD in a fresh, empty Python 3 virtual environment and check that the problem persists.

Usage

>>> from pyemd import emd
>>> import numpy as np
>>> first_histogram = np.array([0.0, 1.0])
>>> second_histogram = np.array([5.0, 3.0])
>>> distance_matrix = np.array([[0.0, 0.5],
...                             [0.5, 0.0]])
>>> emd(first_histogram, second_histogram, distance_matrix)
3.5

You can also get the associated minimum-cost flow:

>>> from pyemd import emd_with_flow
>>> emd_with_flow(first_histogram, second_histogram, distance_matrix)
(3.5, [[0.0, 0.0], [0.0, 1.0]])

API

emd(first_histogram, second_histogram, distance_matrix)
  • first_histogram: A 1-dimensional numpy array of type np.float64, of length \(N\).

  • second_histogram: A 1-dimensional numpy array of type np.float64, of length \(N\).

  • distance_matrix: A 2-dimensional array of type np.float64, of size at least \(N \times N\). This defines the underlying metric, or ground distance, by giving the pairwise distances between the histogram bins. It must represent a metric; there is no warning if it doesn’t.

The arguments to emd_with_flow are the same.

Limitations and Caveats

  • distance_matrix is assumed to represent a metric; there is no check to ensure that this is true. See the documentation in pyemd/lib/emd_hat.hpp for more information.

  • The flow matrix does not contain the flows to/from the extra mass bin.

  • The histograms and distance matrix must be numpy arrays of type np.float64. The original C++ template function can accept any numerical C++ type, but this wrapper only instantiates the template with double (Cython converts np.float64 to double). If there’s demand, I can add support for other types.

Contributing

To help develop PyEMD, fork the project on GitHub and install the requirements with pip.

The Makefile defines some tasks to help with development:

  • default: compile the Cython code into C++ and build the C++ into a Python extension, using the setup.py build command

  • build: same as default, but using the cython command

  • clean: remove the build directory and the compiled C++ extension

  • test: run unit tests with py.test

Tests for different Python environments can be run by installing tox with pip install tox and running the tox command.

Credit

  • All credit for the actual algorithm and implementation goes to Ofir Pele and Michael Werman. See the relevant paper.

  • Thanks to the Cython devlopers for making this kind of wrapper relatively easy to write.

Please cite these papers if you use this code:

Ofir Pele and Michael Werman, “A linear time histogram metric for improved SIFT matching,” in Computer Vision - ECCV 2008, Marseille, France, 2008, pp. 495-508.

@INPROCEEDINGS{pele2008,
  title={A linear time histogram metric for improved sift matching},
  author={Pele, Ofir and Werman, Michael},
  booktitle={Computer Vision--ECCV 2008},
  pages={495--508},
  year={2008},
  month={October},
  publisher={Springer}
}

Ofir Pele and Michael Werman, “Fast and robust earth mover’s distances,” in Proc. 2009 IEEE 12th Int. Conf. on Computer Vision, Kyoto, Japan, 2009, pp. 460-467.

@INPROCEEDINGS{pele2009,
  title={Fast and robust earth mover's distances},
  author={Pele, Ofir and Werman, Michael},
  booktitle={2009 IEEE 12th International Conference on Computer Vision},
  pages={460--467},
  year={2009},
  month={September},
  organization={IEEE}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyemd-0.4.4.tar.gz (67.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyemd-0.4.4-cp36-cp36m-macosx_10_12_x86_64.whl (73.5 kB view details)

Uploaded CPython 3.6mmacOS 10.12+ x86-64

File details

Details for the file pyemd-0.4.4.tar.gz.

File metadata

  • Download URL: pyemd-0.4.4.tar.gz
  • Upload date:
  • Size: 67.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyemd-0.4.4.tar.gz
Algorithm Hash digest
SHA256 2457a0066fed668a6d9a0dd89ea6171110dd5d210f0c92c9662799908f37c08f
MD5 5aad19a920239edadc424dd6789d26c8
BLAKE2b-256 34610f2803463c695bdde498e63a6300f7878829427ecdb9144b6306883ce7f9

See more details on using hashes here.

File details

Details for the file pyemd-0.4.4-cp36-cp36m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyemd-0.4.4-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4baa0d9fca040e3a9b63559f5cef0400219a41c8c3069cf33f3777d901461ec1
MD5 a9d97be913c6685c0f44fd90bda73005
BLAKE2b-256 a9025914b1603fae81fe735982fab370bbd8c11d7ac039bfdb38ee21fc72c29b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page