A Python wrapper for Ofir Pele and Michael Werman's implementation of the Earth Mover's Distance.
Project description
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}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pyemd-0.0.10-cp34-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 663629bc5dfc0df8346f58cd9968d6b890320655c4f4cc254266f057ebbf07f2 |
|
MD5 | 85f4064f0fa877e4b2ec937cfdd7b89c |
|
BLAKE2b-256 | 30464a4de9ce41dc22f413ffdb26a276f766f61df51c0743d3b5b8912f1215a5 |
Hashes for pyemd-0.0.10-cp34-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ec3104e1b11ce7fa99ec54760046012b7923a8b9a4cfdf640cfa8bba79d2b9d |
|
MD5 | 2388f83efbe08f1ee0aa5195852fa267 |
|
BLAKE2b-256 | d8291cf748d01033d2a1d369f6d0a1e3b3a9955b3bbe983fe544d96e972505e8 |
Hashes for pyemd-0.0.10-cp34-cp34m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c32647920b6c86ad7198a1f6890c8e9a57d183b00dad7271f6615e5a37ddd323 |
|
MD5 | c730a6e7f23909c2e5f9827f7c6d3e4e |
|
BLAKE2b-256 | 52ec1960377012ff17ea8d950daf0e82cbb0f9c6009f2cbf881ee5ea011de596 |