Skip to content

Commit

Permalink
Fix imagehash hex_to_hash (#2989)
Browse files Browse the repository at this point in the history
* Fix imagehash hex_to_hash

* expose hamming distance vector

* fix idiff

* update imagerepo

* fix repo hashes (bjlittle)

* fix repo hashed (scitools)

* fix test_image_json
  • Loading branch information
bjlittle authored and pelson committed Apr 30, 2018
1 parent 12b25a2 commit 73220b2
Show file tree
Hide file tree
Showing 5 changed files with 537 additions and 535 deletions.
16 changes: 8 additions & 8 deletions lib/iris/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2010 - 2017, Met Office
# (C) British Crown Copyright 2010 - 2018, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -552,7 +552,6 @@ def assertWarnsRegexp(self, expected_regexp=''):
msg = msg.format(expected_regexp)
self.assertTrue(matches, msg)


@contextlib.contextmanager
def assertNoWarningsRegexp(self, expected_regexp=''):
# Check that no warning matching the given expression is raised.
Expand Down Expand Up @@ -782,7 +781,7 @@ def check_graphic(self):
def _create_missing():
fname = '{}.png'.format(phash)
base_uri = ('https://scitools.github.io/test-iris-imagehash/'
'images/{}')
'images/v4/{}')
uri = base_uri.format(fname)
hash_fname = os.path.join(image_output_directory, fname)
uris = repo.setdefault(unique_id, [])
Expand Down Expand Up @@ -817,13 +816,14 @@ def _create_missing():
raise AssertionError(emsg.format(unique_id))
else:
uris = repo[unique_id]
# Extract the hex basename strings from the uris.
hexes = [os.path.splitext(os.path.basename(uri))[0]
for uri in uris]
# Create the expected perceptual image hashes from the uris.
to_hash = imagehash.hex_to_hash
expected = [to_hash(os.path.splitext(os.path.basename(uri))[0],
hash_size=_HASH_SIZE)
for uri in uris]
expected = [to_hash(uri_hex) for uri_hex in hexes]

# Calculate the hamming distance vector for the result hash.
# Calculate hamming distance vector for the result hash.
distances = [e - phash for e in expected]

if np.all([hd > _HAMMING_DISTANCE for hd in distances]):
Expand Down Expand Up @@ -1104,7 +1104,7 @@ def assertGribMessageDifference(self, filename1, filename2, diffs,
# for each message.
self.assertEqual(m1_value, diffs[key][0],
msg=msg.format(key, m1_value,
diffs[key][0]))
diffs[key][0]))

self.assertEqual(m2_value, diffs[key][1],
msg=msg.format(key, m2_value,
Expand Down
16 changes: 9 additions & 7 deletions lib/iris/tests/idiff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# (C) British Crown Copyright 2010 - 2017, Met Office
# (C) British Crown Copyright 2010 - 2018, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -83,7 +83,7 @@ def diff_viewer(repo, key, repo_fname, phash, status,

result_dir = os.path.dirname(result_fname)
fname = '{}.png'.format(phash)
base_uri = 'https://scitools.github.io/test-iris-imagehash/images/{}'
base_uri = 'https://scitools.github.io/test-iris-imagehash/images/v4/{}'
uri = base_uri.format(fname)
phash_fname = os.path.join(result_dir, fname)

Expand Down Expand Up @@ -139,24 +139,26 @@ def skip(event):


def _calculate_hit(uris, phash, action):
# Extract the hex basename strings from the uris.
hexes = [os.path.splitext(os.path.basename(uri))[0] for uri in uris]
# Create the expected perceptual image hashes from the uris.
to_hash = imagehash.hex_to_hash
expected = [to_hash(os.path.splitext(os.path.basename(uri))[0],
hash_size=iris.tests._HASH_SIZE)
for uri in uris]
expected = [to_hash(uri_hex) for uri_hex in hexes]
# Calculate the hamming distance vector for the result hash.
distances = [e - phash for e in expected]

if action == 'first':
index = 0
elif action == 'last':
index = -1
elif action == 'similar':
index = np.argmin(distances)
elif action == 'difference':
elif action == 'different':
index = np.argmax(distances)
else:
emsg = 'Unknown action: {!r}'
raise ValueError(emsg.format(action))

return index, distances[index]


Expand Down Expand Up @@ -230,7 +232,7 @@ def step_over_diffs(result_dir, action, display=True):
os.path.basename(uri))
if not os.path.isfile(local_fname):
emsg = 'Bad URI {!r} for test {!r}.'
raise ValueError(uri, key)
raise ValueError(emsg.format(uri, key))
else:
# The temporary expected filename has the test name
# baked into it, and is used in the diff plot title.
Expand Down
Loading

0 comments on commit 73220b2

Please sign in to comment.