-
Notifications
You must be signed in to change notification settings - Fork 280
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
enh: store basic software info in png metadata #4553
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,13 @@ | |
from copy import deepcopy | ||
from functools import lru_cache, wraps | ||
from numbers import Number as numeric_type | ||
from typing import Any, Callable, Optional, Type | ||
from typing import Any, Callable, Dict, Optional, Type | ||
|
||
import numpy as np | ||
from more_itertools import always_iterable, collapse, first | ||
|
||
from yt._maintenance.deprecation import issue_deprecation_warning | ||
from yt._version import __version__ as yt_version | ||
from yt.config import ytcfg | ||
from yt.units import YTArray, YTQuantity | ||
from yt.utilities.exceptions import YTFieldNotFound, YTInvalidWidthError | ||
|
@@ -566,7 +567,7 @@ def get_version_stack(): | |
import matplotlib | ||
|
||
version_info = {} | ||
version_info["yt"] = get_yt_version() | ||
version_info["yt"] = yt_version | ||
version_info["numpy"] = np.version.version | ||
version_info["matplotlib"] = matplotlib.__version__ | ||
return version_info | ||
|
@@ -1447,3 +1448,25 @@ def validate_moment(moment, weight_field): | |
"Weighted projections can only be made of averages " | ||
"(moment = 1) or standard deviations (moment = 2)!" | ||
) | ||
|
||
|
||
def setdefault_mpl_metadata(mpl_kwargs: Dict[str, Any], name: str) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. took me a minute to realise but in this context, I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's specifically a matplotlib kwarg |
||
""" | ||
Set a default Software metadata entry for use with Matplotlib outputs. | ||
""" | ||
_, ext = os.path.splitext(name.lower()) | ||
if ext in (".eps", ".ps", ".svg", ".pdf"): | ||
key = "Creator" | ||
elif ext == ".png": | ||
key = "Software" | ||
else: | ||
return | ||
default_software = ( | ||
"Matplotlib version{matplotlib}, https://matplotlib.org|NumPy-{numpy}|yt-{yt}" | ||
) | ||
if "metadata" in mpl_kwargs: | ||
mpl_kwargs["metadata"].setdefault( | ||
key, default_software.format(**get_version_stack()) | ||
) | ||
else: | ||
mpl_kwargs["metadata"] = {key: default_software.format(**get_version_stack())} | ||
Xarthisius marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
from io import BytesIO | ||
|
||
import PIL | ||
from PIL import Image | ||
from PIL.PngImagePlugin import PngInfo | ||
|
||
from .._version import __version__ as yt_version | ||
|
||
|
||
def call_png_write_png(buffer, fileobj, dpi): | ||
Image.fromarray(buffer).save(fileobj, dpi=(dpi, dpi), format="png") | ||
metadata = PngInfo() | ||
metadata.add_text("Software", f"{PIL.__name__}-{PIL.__version__}|yt-{yt_version}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't |
||
Image.fromarray(buffer).save( | ||
fileobj, dpi=(dpi, dpi), format="png", pnginfo=metadata | ||
) | ||
|
||
|
||
def write_png(buffer, filename, dpi=100): | ||
|
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 would prefer we nest this import in
get_version_stack
to avoid having to support people relying onfrom yt.funcs import yt_version
in the future. (I know people already use the indirectionfrom yt.funcs import mylog
)