-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
default hash savefig format #160
base: main
Are you sure you want to change the base?
Conversation
Okay, wowzers... can anyone explain the test failure behaviour of |
Very strange. It seems that passing |
The hashes are different on |
Yes, the tests pass on MPL 3.5.1. Can you pin this to Line 29 in e387618
Hopefully this is something that the perceptual hashes can solve, as we really should be testing against the latest bug fixes from matplotlib! |
Awesome detective work 🕵️ |
@ConorMacBride If the What are the implications to the community if we bank this new default |
@@ -26,7 +26,7 @@ deps = | |||
mpl32: matplotlib==3.2.* | |||
mpl33: matplotlib==3.3.* | |||
mpl34: matplotlib==3.4.* | |||
mpl35: matplotlib==3.5.* | |||
mpl35: matplotlib==3.5.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ConorMacBride We should make an issue to unpin this, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need to change this.
I presume the default hash size, high frequency factor and hamming tolerance can be tuned to a value that minimises, what an average user would consider, a test passing incorrectly? (I would expect most of the I suspect
From reading the pytest-mpl/pytest_mpl/plugin.py Line 601 in e387618
I've added some code below. In the default case, both hashes are the same so no breaking change. However, if a custom backend has been specified, passing So, if the user asks for a different backend, setting To not interfere with the requested backend, I think we should only set Click for codeimport io
import hashlib
import matplotlib
import matplotlib.pyplot as plt
def _hash_file(in_stream):
"""
Hashes an already opened file.
"""
in_stream.seek(0)
buf = in_stream.read()
hasher = hashlib.sha256()
hasher.update(buf)
return hasher.hexdigest()
def hash(**kwargs):
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 3, 2, 4])
imgdata = io.BytesIO()
fig.savefig(imgdata, **kwargs)
plt.close()
return _hash_file(imgdata)
# Default (pytest-mpl sets agg)
matplotlib.use("agg")
print(hash())
# b7d7754ed51a78949e119e3900b72b4123fcc3674fd6a5750b9a93f7091293ab
print(hash(format="png"))
# b7d7754ed51a78949e119e3900b72b4123fcc3674fd6a5750b9a93f7091293ab
# User passes backend="svg" to pytest-mpl decorator
metadata = {"Creator": None, "Date": None, "Format": None, "Type": None}
matplotlib.use("svg")
print(hash(metadata=metadata))
# 81910e47aa39455d96c41893f73793c65b5405af14af7919dbe8a7172e004fcf
print(hash(metadata=metadata))
# 0bf13ec69c253c6a8d3b6b004aa4855e343e5f40a79106e87fee72c208f7de89
print(hash(format="png"))
# b7d7754ed51a78949e119e3900b72b4123fcc3674fd6a5750b9a93f7091293ab |
Agreed. |
That's pretty much my experience so far. We've opted for a |
I've re-ran and updated a notebook that I wrote to initially investigate and play with To create a conda environment to run it, simply Interestingly, changing the colormap on a plot is detected by |
Overall, I wait for you guys to come to a consensus and advise on what to do next here with this PR. |
I think matplotlib puts metadata into the png including the version iirc. I think we can turn that off. See the metadata keyword argument here: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html and more info on png here: https://matplotlib.org/stable/api/backend_agg_api.html#matplotlib.backends.backend_agg.FigureCanvasAgg.print_png |
Given how effective removing the metadata version number was in #163 I think we should definitely remove it by default and make it a breaking change (for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to set this for the reasons in the second half of #160 (comment). Should we just close this PR?
@@ -26,7 +26,7 @@ deps = | |||
mpl32: matplotlib==3.2.* | |||
mpl33: matplotlib==3.3.* | |||
mpl34: matplotlib==3.4.* | |||
mpl35: matplotlib==3.5.* | |||
mpl35: matplotlib==3.5.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need to change this.
This PR addresses issue #152, by ensuring that a default
format='png'
is injected to thesavefig
kwargs when generating a hash.Closes #152