econ-ark / HARK

Heterogenous Agents Resources & toolKit
Apache License 2.0
315 stars 195 forks source link

utilities.py: metadata bug in make_figs() #1375

Closed kennetheva closed 3 months ago

kennetheva commented 3 months ago

When calling make_figs(), an unrecognized argument error occurs at:

plt.savefig(
            os.path.join(target_dir, f"{figure_name}.jpg"),
            metadata={"CreationDate": None},
) 

The matplotlib.pyplot.savefig documentation says metadata is currently not supported for .jpeg files. Commenting out the metadata argument stops the error and the figure is produced as expected.

Should metadata={"CreationDate": None} be removed from the JPEG savefig call until support is added in a later version of the package?

llorracc commented 3 months ago

The reason for suppressing the CreationDate was that when the code gets run, even if the image part of a regenerated figure is bit-by-bit identical to the corresponding part of the former figure, if each of them is stamped with a different CreationDate git will declare that the files have changed.

Omitting the CreationDate from the metadata therefore enormously reduced the number of files that git identified as different.

If matplotlib has been modified to exclude the creation date and other metadata by default, this workaround to prevent it from including CreationDate should be no longer needed.

From: kennetheva @.> Date: Monday, January 29, 2024 at 15:50 To: econ-ark/HARK @.> Cc: Subscribed @.***> Subject: [econ-ark/HARK] utilities.py: metadata bug in make_figs() (Issue #1375)

When calling make_figs(), an unrecognized argument error occurs at:

plt.savefig(

        os.path.join(target_dir, f"{figure_name}.jpg"),

        metadata={"CreationDate": None},

)

The matplotlib.pyplot.savefig documentationhttps://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html says metadata is currently not supported for .jpeg files. Commenting out the metadata argument stops the error and the figure is produced as expected.

Should metadata={"CreationDate": None} be removed from the JPEG savefig call until support is added in a later version of the package?

— Reply to this email directly, view it on GitHubhttps://github.com/econ-ark/HARK/issues/1375, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAKCK74X7ZEOV766IUAMVHLYRADQ3AVCNFSM6AAAAABCQDEK5GVHI2DSMVQWIX3LMV43ASLTON2WKOZSGEYDMMZZGY3DQNA. You are receiving this because you are subscribed to this thread.Message ID: @.***>

sbenthall commented 3 months ago

It might be better to use .gitignore to suppress version control over the jpegs entirely.

llorracc commented 3 months ago

Matt,

As the original author of savefig, can you look into this? I'm puzzled that a lot more examples have not cropped up.

Also, my preference would be that whenever a figure is saved, it gets saved in jpg, pdf, svg, and png forms.

mnwhite commented 3 months ago

Sure, but I don't actually remember writing it. Maybe it was just a very, very long time ago?

On Fri, Feb 2, 2024 at 10:12 AM Christopher Llorracc Carroll < @.***> wrote:

Matt,

As the original author of savefig, can you look into this? I'm puzzled that a lot more examples have not cropped up.

Also, my preference would be that whenever a figure is saved, it gets saved in jpg, pdf, svg, and png forms.

— Reply to this email directly, view it on GitHub https://github.com/econ-ark/HARK/issues/1375#issuecomment-1924075074, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKRAFNZGSCFSYCES7L6MGDYRT65TAVCNFSM6AAAAABCQDEK5GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRUGA3TKMBXGQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

llorracc commented 3 months ago

Well, I don't remember writing it either but as you say it was in a galaxy far far away so maybe I did.

I do remember adding some code to suppress metadata, but I think that that was in a version that saved all four kinds of figures automatically (jpg, svg, pdf, png) and I worked out how to suppress the metadata for all of them (because there would be no point in suppressing it for just one).

In any case, I'm puzzled that the problem has not cropped up for any of our REMARKs or other tests, so maybe none of them uses savefig. Actually, @edmundcrawley, can you point Matt to the place in the HAFiscal-Latest code where the savefig command is invoked, so he can see the content (I've just invited him as a collaborator on HAFiscal-Latest; please let Matt know the name of the branch you've been testing against HARK 0.13 on.)

edmundcrawley commented 3 months ago

@mnwhite here is a minimal working example that shows the problem - that is probably easier than pointing you to the code in HAFiscal-Latest. Let me know if this doesn't work for you.

from HARK.utilities import make_figs import matplotlib.pyplot as plt import numpy as np

plt.figure() plt.plot(np.linspace(1,5,40),np.linspace(4,8,40) ) make_figs('test', True , False, target_dir="C:/Downloads") plt.show()

I get the error:

TypeError: print_jpg() got an unexpected keyword argument 'metadata'

mnwhite commented 3 months ago

Thanks @edmundcrawley , that was a perfect example. I've made a PR that fixes this in #1386 and will merge ASAP, then try to do a minor release.