Skip to content

Commit ea02532

Browse files
committed
fix repo hashes (bjlittle)
1 parent cbd7226 commit ea02532

File tree

3 files changed

+320
-402
lines changed

3 files changed

+320
-402
lines changed

lib/iris/tests/__init__.py

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ def check_graphic(self):
781781
def _create_missing():
782782
fname = '{}.png'.format(phash)
783783
base_uri = ('https://scitools.github.io/test-iris-imagehash/'
784-
'images/{}')
784+
'images/v4/{}')
785785
uri = base_uri.format(fname)
786786
hash_fname = os.path.join(image_output_directory, fname)
787787
uris = repo.setdefault(unique_id, [])
@@ -819,46 +819,14 @@ def _create_missing():
819819
# Extract the hex basename strings from the uris.
820820
hexes = [os.path.splitext(os.path.basename(uri))[0]
821821
for uri in uris]
822-
# See https://github.com/JohannesBuchner/imagehash#changelog
823-
# for details on imagehash release v4.0 old_hex_to_hash() fix.
824-
hex_fix_available = hasattr(imagehash, 'old_hex_to_hash')
822+
# Create the expected perceptual image hashes from the uris.
825823
to_hash = imagehash.hex_to_hash
824+
expected = [to_hash(uri_hex) for uri_hex in hexes]
826825

827-
def _image_matcher(expected):
828-
"""
829-
Returns whether at least one expected image is similar
830-
enough to the actual image, and the vector of hamming
831-
distances of each expected image from the actual image.
832-
833-
"""
834-
# Calculate hamming distance vector for the result hash.
835-
distances = [e - phash for e in expected]
836-
result = np.all([d > _HAMMING_DISTANCE for d in distances])
837-
return not result, distances
838-
839-
if hex_fix_available:
840-
# Create expected perceptual image hashes from the uris.
841-
# Note that, the "hash_size" kwarg is unavailable and old
842-
# style hashes will not be corrected.
843-
expected = [to_hash(uri_hex) for uri_hex in hexes]
844-
matching, distances = _image_matcher(expected)
845-
846-
if not matching:
847-
# Retry, correcting old hex strings. Note that,
848-
# the "hash_size" kwarg is required.
849-
to_hash = imagehash.old_hex_to_hash
850-
expected = [to_hash(uri_hex, hash_size=_HASH_SIZE)
851-
for uri_hex in hexes]
852-
matching, distances = _image_matcher(expected)
853-
else:
854-
# Create expected perceptual image hashes from the uris.
855-
# The version of imagehash will be prior to v4.0, so the
856-
# "hash_size" kwarg is required.
857-
expected = [to_hash(uri_hex, hash_size=_HASH_SIZE)
858-
for uri_hex in hexes]
859-
matching, distances = _image_matcher(expected)
860-
861-
if not matching:
826+
# Calculate hamming distance vector for the result hash.
827+
distances = [e - phash for e in expected]
828+
829+
if np.all([hd > _HAMMING_DISTANCE for hd in distances]):
862830
if dev_mode:
863831
_create_missing()
864832
else:

lib/iris/tests/idiff.py

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def diff_viewer(repo, key, repo_fname, phash, status,
8383

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

@@ -139,77 +139,27 @@ def skip(event):
139139

140140

141141
def _calculate_hit(uris, phash, action):
142+
# Extract the hex basename strings from the uris.
142143
hexes = [os.path.splitext(os.path.basename(uri))[0] for uri in uris]
143-
# See https://github.com/JohannesBuchner/imagehash#changelog
144-
# for details on imagehash release v4.0 old_hex_to_hash() fix.
145-
hex_fix_available = hasattr(imagehash, 'old_hex_to_hash')
144+
# Create the expected perceptual image hashes from the uris.
146145
to_hash = imagehash.hex_to_hash
147-
148-
if hex_fix_available:
149-
# Note that, the "hash_size" kwarg is unavailable and old
150-
# style hashes will not be corrected.
151-
expected = [to_hash(uri_hex) for uri_hex in hexes]
152-
# Calculate the hamming distance vector for the result hash.
153-
dnew = [e - phash for e in expected]
154-
# Retry, correcting old hex strings. Note that, the
155-
# "hash_size" kwarg is required.
156-
to_hash = imagehash.old_hex_to_hash
157-
expected = [to_hash(uri_hex, hash_size=iris.tests._HASH_SIZE)
158-
for uri_hex in hexes]
159-
# Calculate the hamming distance vector for the result hash.
160-
dold = [e - phash for e in expected]
161-
else:
162-
# The version of imagehash will be prior to v4.0, so the
163-
# "hash_size" kwarg is required.
164-
expected = [to_hash(uri_hex, hash_size=iris.tests._HASH_SIZE)
165-
for uri_hex in hexes]
166-
dold = [e - phash for e in expected]
167-
168-
dmsg = '{} (old={})' if hex_fix_available else '{}'
146+
expected = [to_hash(uri_hex) for uri_hex in hexes]
147+
# Calculate the hamming distance vector for the result hash.
148+
distances = [e - phash for e in expected]
169149

170150
if action == 'first':
171151
index = 0
172-
if hex_fix_available:
173-
distance = dmsg.format(dnew[index], dold[index])
174-
else:
175-
distance = dmsg.format(dold[index])
176152
elif action == 'last':
177153
index = -1
178-
if hex_fix_available:
179-
distance = dmsg.format(dnew[index], dold[index])
180-
else:
181-
distance = dmsg.format(dold[index])
182154
elif action == 'similar':
183-
if hex_fix_available:
184-
inew, iold = np.argmin(dnew), np.argmin(dold)
185-
vnew, vold = dnew[inew], dold[iold]
186-
if vnew <= vold:
187-
index = inew
188-
distance = '{} (old={})'.format(vnew, vold)
189-
else:
190-
index = iold
191-
distance = '{} (new={})'.format(vold, vnew)
192-
else:
193-
index = np.argmin(dold)
194-
distance = '{}'.format(dold[index])
155+
index = np.argmin(distances)
195156
elif action == 'different':
196-
if hex_fix_available:
197-
inew, iold = np.argmax(dnew), np.argmax(dold)
198-
vnew, vold = dnew[inew], dold[iold]
199-
if vnew >= vold:
200-
index = inew
201-
distance = '{} (old={})'.format(vnew, vold)
202-
else:
203-
index = iold
204-
distance = '{} (new={})'.format(vold, vnew)
205-
else:
206-
index = np.argmax(dold)
207-
distance = '{}'.format(dold[index])
157+
index = np.argmax(distances)
208158
else:
209159
emsg = 'Unknown action: {!r}'
210160
raise ValueError(emsg.format(action))
211161

212-
return index, distance
162+
return index, distances[index]
213163

214164

215165
def step_over_diffs(result_dir, action, display=True):
@@ -282,7 +232,7 @@ def step_over_diffs(result_dir, action, display=True):
282232
os.path.basename(uri))
283233
if not os.path.isfile(local_fname):
284234
emsg = 'Bad URI {!r} for test {!r}.'
285-
raise ValueError(uri, key)
235+
raise ValueError(emsg.format(uri, key))
286236
else:
287237
# The temporary expected filename has the test name
288238
# baked into it, and is used in the diff plot title.

0 commit comments

Comments
 (0)