Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bob
bob.bio.vein
Commits
24bc6182
Commit
24bc6182
authored
Oct 18, 2017
by
Vedrana KRIVOKUCA
Browse files
Make HammingDistance class plus add test
parent
6919292f
Pipeline
#13248
passed with stages
in 21 minutes and 4 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
bob/bio/vein/algorithm/HammingDistance.py
View file @
24bc6182
# Calculates the Hamming distance (proportion of mismatching corresponding bits) between two binary vectors
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
import
bob.bio.base
from
bob.bio.base.algorithm
import
Distance
import
scipy.spatial.distance
algorithm
=
bob
.
bio
.
base
.
algorithm
.
Distance
(
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
)
\ No newline at end of file
class
HammingDistance
(
Distance
):
"""Finger vein matching: Hamming Distance between binary fingervein feature vectors
"""
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
)
\ No newline at end of file
bob/bio/vein/tests/test.py
View file @
24bc6182
...
...
@@ -685,3 +685,26 @@ def test_correlation():
total = time.clock() - start
print('scipy+correlate2d, %d iterations - %.2e per iteration' % (N, total/N))
'''
def
test_hamming_distance
():
from
..algorithm.HammingDistance
import
HammingDistance
HD
=
HammingDistance
()
# Tests on simple binary arrays:
# 1.) Maximum HD (1.0):
model_1
=
numpy
.
array
([
0
,
1
,
1
,
1
,
0
,
1
,
1
,
0
,
0
,
0
,
1
,
0
])
probe_1
=
numpy
.
array
([[
1
,
0
,
0
,
0
,
1
,
0
],
[
0
,
1
,
1
,
1
,
0
,
1
]])
score_max
=
HD
.
score
(
model_1
,
probe_1
)
assert
score_max
==
1.0
# 2.) Minimum HD (0.0):
model_2
=
numpy
.
array
([
0
,
1
,
1
,
1
,
0
,
1
,
0
,
1
,
1
,
1
,
0
,
1
])
probe_2
=
numpy
.
array
([[
0
,
1
,
1
,
1
,
0
,
1
],
[
0
,
1
,
1
,
1
,
0
,
1
]])
score_min
=
HD
.
score
(
model_2
,
probe_2
)
assert
score_min
==
0.0
# 3.) HD of exactly half (0.5)
model_3
=
numpy
.
array
([
0
,
1
,
1
,
1
,
0
,
1
,
1
,
0
,
0
,
0
,
1
,
0
])
probe_3
=
numpy
.
array
([[
0
,
1
,
1
,
1
,
0
,
1
],
[
0
,
1
,
1
,
1
,
0
,
1
]])
score_half
=
HD
.
score
(
model_3
,
probe_3
)
assert
score_half
==
0.5
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment