HammingDistance.py 1.33 KB
Newer Older
1 2
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
3

4 5

from bob.bio.base.algorithm import Distance
6
import scipy.spatial.distance
7 8 9


class HammingDistance (Distance):
10 11 12 13 14 15 16 17 18 19 20 21 22
  """This class calculates the Hamming distance between two binary images.

  Each binary image is first flattened by concatenating its rows to form a one-dimensional vector.  The Hamming distance is then calculated between the two binary vectors.
  The Hamming distance is computed using :py:func:`scipy.spatial.distance.hamming`, which returns a scalar ``float`` to represent the proportion of mismatching corresponding bits between the two binary vectors.

  **Parameters:**

  ``distance_function`` : function
    Set this parameter to ``scipy.spatial.distance.hamming`` to ensure we are calculating the Hamming distance

  ``is_distance_function`` : bool
    Set this flag to ``False`` to ensure that Hamming distances are returned as positive values rather than negative 

23 24
  """

25

26 27 28 29 30 31 32 33 34 35 36 37
  def __init__(
      self,
      distance_function = scipy.spatial.distance.hamming,
      is_distance_function = False  # setting this to False ensures that Hamming distances are returned as positive values rather than negative
  ):

    # Call base class constructor
    Distance.__init__(
        self,
        distance_function = distance_function,
        is_distance_function = is_distance_function
    )