-
-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1277 from ayshih/hash
Hash-based figure verification for tests
- Loading branch information
Showing
9 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"sunpy.map.tests.test_mapbase.test_plot_aia171": "62b5b9fc1a423e3cbeeea85d3b8daf1a7877099fdeda2ce9352746be1dd451fc" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
import io | ||
import hashlib | ||
import json | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
HASH_LIBRARY_NAME = 'figure_hashes.json' | ||
|
||
# Load the hash library if it exists | ||
try: | ||
with open(os.path.join(os.path.dirname(__file__), HASH_LIBRARY_NAME)) as infile: | ||
hash_library = json.load(infile) | ||
except IOError: | ||
hash_library = {} | ||
|
||
def hash_figure(figure=None): | ||
""" | ||
For a matplotlib.figure.Figure, returns the SHA256 hash as a hexadecimal string. | ||
Parameters | ||
---------- | ||
figure : matplotlib.figure.Figure | ||
If None is specified, the current figure is used (as determined by matplotlib.pyplot.gcf()) | ||
Returns | ||
------- | ||
out : string | ||
The SHA256 hash in hexadecimal representation | ||
""" | ||
|
||
if figure is None: | ||
figure = plt.gcf() | ||
|
||
imgdata = io.BytesIO() | ||
figure.savefig(imgdata, format='png') | ||
|
||
imgdata.seek(0) | ||
buf = imgdata.read() | ||
imgdata.close() | ||
|
||
hasher = hashlib.sha256() | ||
hasher.update(buf) | ||
return hasher.hexdigest() | ||
|
||
def verify_figure_hash(name, figure=None): | ||
""" | ||
Verifies whether a figure has the same hash as the named hash in the current hash library. | ||
If the hash library does not contain the specified name, the hash is added to the library. | ||
Parameters | ||
---------- | ||
name : string | ||
The identifier for the hash in the hash library | ||
figure : matplotlib.figure.Figure | ||
If None is specified, the current figure is used (as determined by matplotlib.pyplot.gcf()) | ||
Returns | ||
------- | ||
out : bool | ||
False if the figure's hash does not match the named hash, otherwise True | ||
""" | ||
if name not in hash_library: | ||
hash_library[name] = hash_figure(figure) | ||
return True | ||
return hash_library[name] == hash_figure(figure) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters