Skip to content

Commit

Permalink
Merge branch 'release/0.6.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
wmayner committed Feb 24, 2023
2 parents e9fb657 + bada206 commit 1e11dc3
Show file tree
Hide file tree
Showing 28 changed files with 495 additions and 454 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [wmayner]
27 changes: 27 additions & 0 deletions .github/workflows/build_wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Build wheels & run tests

on: ['push', 'pull_request']

jobs:
build_wheels:
name: Build wheels on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-20.04, windows-2019, macos-11]

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Build wheels
uses: pypa/cibuildwheel@v2.12.0
with:
package-dir: .
output-dir: wheelhouse
config-file: "{package}/pyproject.toml"

- uses: actions/upload-artifact@v3
with:
path: ./wheelhouse/*.whl
19 changes: 19 additions & 0 deletions .github/workflows/make_sdist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Make source distribution

on: ['push', 'pull_request']

jobs:
make_sdist:
name: Make source distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Build source distribution
run: pipx run build --sdist

- uses: actions/upload-artifact@v3
with:
path: dist/*.tar.gz
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
__pycache__
.gitconfig
.cache
.pytest_cache
.tox
.env
.ropeproject
*.so
*.pyc
MANIFEST
*.egg*
src/pyemd/emd.cpp
src/pyemd/_version.py
build
dist
pyemd/emd.cpp
wheelhouse
ignore
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014-2017 Will Mayner
Copyright (c) 2014-2023 Will Mayner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
graft pyemd
graft src/pyemd
graft test

include README.rst
Expand Down
55 changes: 37 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
.PHONY: default test build clean dist test-dist check-dist build-dist clean-dist
.PHONY: default clean develop test dist-clean build-local build dist-upload dist-test-upload dist-sign dist-check

src = pyemd
dist_dir = dist
src = src/pyemd
test = test
dist = dist
wheelhouse = wheelhouse

default: build
default: test

test: build
test: develop
py.test

build: clean
python setup.py build_ext -b .
develop: clean
python -m pip install -e ".[test,dist]"

clean:
rm -f pyemd/*.so
rm -rf $(shell find . -name '__pycache__')
rm -rf $(shell find . -name '*.so')
rm -rf .eggs
rm -rf pyemd.egg-info
rm -rf build

dist: build-dist check-dist
twine upload $(dist_dir)/*
dist-build-local:
python -m build

test-dist: build-dist check-dist
twine upload --repository-url https://test.pypi.org/legacy/ $(dist_dir)/*
dist-build-wheels:
cibuildwheel --platform linux --config-file pyproject.toml

check-dist:
python setup.py check --restructuredtext --strict
dist-upload: dist-sign
twine upload $(dist)/*
twine upload $(wheelhouse)/*

build-dist: clean-dist
python setup.py sdist bdist_wheel --dist-dir=$(dist_dir)
dist-test-upload: dist-check
twine upload --repository-url https://test.pypi.org/simple/ testpypi $(dist)/*
twine upload --repository-url https://test.pypi.org/simple/ testpypi $(wheelhouse)/*

clean-dist:
rm -rf $(dist_dir)
dist-sign: dist-check
gpg --detach-sign -a $(dist)/*.tar.gz
gpg --detach-sign -a $(wheelhouse)/*.whl

dist-check:
twine check --strict $(dist)/*
twine check --strict $(wheelhouse)/*

dist-clean:
rm -rf $(dist)

dist-test-install:
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple pyemd
104 changes: 46 additions & 58 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
.. image:: https://img.shields.io/travis/wmayner/pyemd/develop.svg?style=flat-square&maxAge=3600
:target: https://travis-ci.org/wmayner/pyemd
.. image:: https://img.shields.io/github/actions/workflow/status/wmayner/pyemd/build_wheels.yml?style=flat-square&maxAge=86400
:target: https://github.com/wmayner/pyemd/actions/workflows/build_wheels.yml
:alt: Build status badge
.. image:: https://img.shields.io/pypi/pyversions/pyemd.svg?style=flat-square&maxAge=86400
:target: https://wiki.python.org/moin/Python2orPython3
:target: https://pypi.org/project/pyemd/
:alt: Python versions badge

PyEMD: Fast EMD for Python
==========================

PyEMD is a Python wrapper for `Ofir Pele and Michael Werman's implementation
<http://ofirpele.droppages.com/>`_ of the `Earth Mover's
Distance <http://en.wikipedia.org/wiki/Earth_mover%27s_distance>`_ that allows
<https://ofirpele.droppages.com/>`_ of the `Earth Mover's
Distance <https://en.wikipedia.org/wiki/Earth_mover%27s_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.**

Expand Down Expand Up @@ -54,8 +55,9 @@ You can also calculate the EMD directly from two arrays of observations:
>>> emd_samples(first_array, second_array, bins=2)
0.5
Documentation
-------------
API Documentation
-----------------

emd()
~~~~~
Expand All @@ -75,17 +77,17 @@ emd()
*N*.
- ``distance_matrix`` *(np.ndarray)*: A 2D array of ``np.float64,`` of size at
least *N* × *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.
giving the pairwise distances between the histogram bins.
**NOTE: It must represent a metric; there is no warning if it doesn't.**

*Keyword Arguments:*

- ``extra_mass_penalty`` *(float)*: The penalty for extra mass. If you want the
resulting distance to be a metric, it should be at least half the diameter of
the space (maximum possible distance between any two points). If you want
partial matching you can set it to zero (but then the resulting distance is
not guaranteed to be a metric). The default value is ``-1.0``, which means the
maximum value in the distance matrix is used.
not guaranteed to be a metric). The default value is ``-1.0``, which means
the maximum value in the distance matrix is used.

*Returns:* *(float)* The EMD value.

Expand Down Expand Up @@ -123,18 +125,18 @@ emd_samples()
*Arguments:*

- ``first_array`` *(Iterable)*: A 1D array of samples used to generate a
- ``first_array`` *(Iterable)*: An array of samples used to generate a
histogram.
- ``second_array`` *(Iterable)*: A 1D array of samples used to generate a
- ``second_array`` *(Iterable)*: An array of samples used to generate a
histogram.

*Keyword Arguments:*

- ``extra_mass_penalty`` *(float)*: Same as for ``emd()``.
- ``distance`` *(string or function)*: A string or function implementing
a metric on a 1D ``np.ndarray``. Defaults to the Euclidean distance. Currently
limited to 'euclidean' or your own function, which must take a 1D array and
return a square 2D array of pairwise distances.
a metric on a 1D ``np.ndarray``. Defaults to the Euclidean distance.
Currently limited to 'euclidean' or your own function, which must take
a 1D array and return a square 2D array of pairwise distances.
- ``normalized`` (*boolean*): If true (default), treat histograms as fractions
of the dataset. If false, treat histograms as counts. In the latter case the
EMD will vary greatly by array length.
Expand All @@ -147,11 +149,12 @@ emd_samples()
``first_array`` and ``second_array``. Note: if the given range is not a
superset of the default range, no warning will be given.

*Returns:* *(float)* The EMD value between the histograms of ``first_array`` and
``second_array``.
*Returns:* *(float)* The EMD value between the histograms of ``first_array``
and ``second_array``.

----


Limitations and Caveats
-----------------------

Expand All @@ -163,66 +166,36 @@ Limitations and Caveats
- 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.
(Cython converts ``np.float64`` to ``double``). If there's demand, I can
add support for other types.

- ``emd_with_flow()``:

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

- ``emd_samples()``:

- Using the default ``bins='auto'`` results in an extra call to
``np.histogram()`` to determine the bin lengths, since `the NumPy
bin-selectors are not exposed in the public API
- With ``numpy < 1.15.0``, using the default ``bins='auto'`` results in an
extra call to ``np.histogram()`` to determine the bin lengths, since `the
NumPy bin-selectors are not exposed in the public API
<https://github.com/numpy/numpy/issues/10183>`_. For performance, you may
want to set the bins yourself.


Contributing
------------

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

The ``Makefile`` defines some tasks to help with development:

- ``test``: Run the test suite
- ``build`` Generate and compile the Cython extension
- ``clean``: Remove the compiled Cython extension
- ``default``: Run ``build``

Tests for different Python environments can be run with ``tox``.
want to set the bins yourself. If ``numpy >= 1.15`` is available,
``np.histogram_bin_edges()`` is called instead, which is more efficient.


Credit
------

- All credit for the actual algorithm and implementation goes to `Ofir Pele
<http://www.ariel.ac.il/sites/ofirpele/>`_ and `Michael Werman
<http://www.cs.huji.ac.il/~werman/>`_. See the `relevant paper
<http://www.seas.upenn.edu/~ofirpele/publications/ICCV2009.pdf>`_.
<https://ofirpele.droppages.com/>`_ and `Michael Werman
<https://www.cs.huji.ac.il/~werman/>`_. See the `relevant paper
<https://doi.org/10.1109/ICCV.2009.5459199>`_.
- Thanks to the Cython developers 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. *Computer Vision - ECCV 2008*, Marseille, France, 2008, pp. 495-508.

.. code-block:: latex

@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. *Proc.
2009 IEEE 12th Int. Conf. on Computer Vision*, Kyoto, Japan, 2009, pp. 460-467.

Expand All @@ -237,3 +210,18 @@ Ofir Pele and Michael Werman. Fast and robust earth mover's distances. *Proc.
month={September},
organization={IEEE}
}

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

.. code-block:: latex

@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}
}
6 changes: 0 additions & 6 deletions conftest.py

This file was deleted.

1 change: 0 additions & 1 deletion dev_requirements.txt

This file was deleted.

4 changes: 0 additions & 4 deletions dist_requirements.txt

This file was deleted.

20 changes: 0 additions & 20 deletions pyemd/__about__.py

This file was deleted.

Loading

0 comments on commit 1e11dc3

Please sign in to comment.