HammingDistance.py 1.33 KB
 Vedrana KRIVOKUCA committed Oct 18, 2017 1 2 ``````#!/usr/bin/env python # vim: set fileencoding=utf-8 : `````` Vedrana KRIVOKUCA committed Oct 18, 2017 3 `````` `````` Vedrana KRIVOKUCA committed Oct 18, 2017 4 5 `````` from bob.bio.base.algorithm import Distance `````` Vedrana KRIVOKUCA committed Oct 18, 2017 6 ``````import scipy.spatial.distance `````` Vedrana KRIVOKUCA committed Oct 18, 2017 7 8 9 `````` class HammingDistance (Distance): `````` Vedrana KRIVOKUCA committed Oct 19, 2017 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 `````` Vedrana KRIVOKUCA committed Oct 18, 2017 23 24 `````` """ `````` Vedrana KRIVOKUCA committed Oct 19, 2017 25 `````` `````` Vedrana KRIVOKUCA committed Oct 18, 2017 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 )``````