Skip to main content

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.

This wrapper does not expose the full functionality of that library; it can only used be with the np.float data type, and with a symmetric distance matrix that represents a true metric. See the documentation for the original Pele and Werman library for the other options it provides.

Installation

To install the latest release:

pip install pyemd

To install the latest development version:

pip install “git+https://github.com/wmayner/pyemd@develop#egg=pyemd”

Usage

Use PyEMD like so:

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

API

emd(first_signature, second_signature, distance_matrix)
  • first_signature: A 1-dimensional numpy array of np.float, of size N.

  • second_signature: A 1-dimensional numpy array of np.float, of size N.

  • distance_matrix: A 2-dimensional array of np.float, of size NxN. Must be symmetric and represent a metric.

Limitations and Caveats

  • distance_matrix must be symmetric.

  • distance_matrix is assumed to represent a true metric. This must be enforced by the caller. See the documentation in pyemd/lib/emd_hat.hpp.

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

  • The original C++ functions have optional parameters extra_mass_penalty and F (for flows); this wrapper does not expose those parameters. See the documentation in pyemd/lib/emd_hat.hpp.

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:

  • buildcython: compiles the Cython code into C++ and then builds the C++ into a Python extension

  • runtests: builds everything and then runs the unit tests

  • clean: removes the compiled C++

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}
}

Release files for pyemd 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyemd 0.0.7
File Size Uploaded
pyemd-0.0.7.tar.gz 59.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyemd 0.0.7
File Interpreter ABI Platform
pyemd-0.0.7-cp33-cp33m-macosx_10_9_x86_64.whl CPython 3.3 CPython 3.3 pymalloc macOS 10.9+ x86-64 Details

Total release size: 118.9 kB

Release files / pyemd-0.0.7.tar.gz

Download URL pyemd-0.0.7.tar.gz
Size 59.1 kB
Tags Source
SHA-256 checksum
How to use checksums
70360f9729979bf171a0fd9b16318ace4af5f3c0cd523db58bb9892d103a05bc
BLAKE2b-256 checksum
How to use checksums
4fc6d719791829a7a4989d8c41456f4ea835fd9726f169ed779d591b43ff7901
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pyemd-0.0.7-cp33-cp33m-macosx_10_9_x86_64.whl

Download URL pyemd-0.0.7-cp33-cp33m-macosx_10_9_x86_64.whl
Size 59.7 kB
Tags CPython 3.3 CPython 3.3 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
f51e63ecf0280d9867d8a4fe38f98d73948f5a6a303875fe5131b86124fe7baf
BLAKE2b-256 checksum
How to use checksums
bd55b7dcaf8f9444a476aa82f841c38efbef7535f9c86cf48fe3eaf5c7a15950
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

2.0.0

2 release files

1.1.0

29 release files

1.0.0

36 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.0.9

1 release file

0.0.8

2 release files

This release

0.0.7 This release

2 release files

0.0.6

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page