From 57702ac398e61f58a86f4cfd2965ed48a7f9ab95 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Mon, 17 Jun 2024 20:48:38 -0400 Subject: [PATCH 1/4] ndarray.ptp -> np.ptp for numpy v2 --- fastplotlib/widgets/histogram_lut.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastplotlib/widgets/histogram_lut.py b/fastplotlib/widgets/histogram_lut.py index a3edffcbd..02c21aa38 100644 --- a/fastplotlib/widgets/histogram_lut.py +++ b/fastplotlib/widgets/histogram_lut.py @@ -163,7 +163,7 @@ def _calculate_histogram(self, data): # used if data ptp <= 10 because event things get weird # with tiny world objects due to floating point error # so if ptp <= 10, scale up by a factor - self._scale_factor: int = max(1, 100 * int(10 / data_ss.ptp())) + self._scale_factor: int = max(1, 100 * int(10 / np.ptp(data_ss))) edges = edges * self._scale_factor From fed6040db5ae7d9c201ae90a9f76aa1d6d8c49ea Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Tue, 18 Jun 2024 18:58:14 -0400 Subject: [PATCH 2/4] Figure.export --- fastplotlib/layouts/_figure.py | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/fastplotlib/layouts/_figure.py b/fastplotlib/layouts/_figure.py index 17bb28095..19bdbb6a6 100644 --- a/fastplotlib/layouts/_figure.py +++ b/fastplotlib/layouts/_figure.py @@ -128,6 +128,7 @@ def __init__( # if controller instances have been specified for each subplot if controllers is not None: + # one controller for all subplots if isinstance(controllers, pygfx.Controller): controllers = [controllers] * len(self) @@ -475,6 +476,11 @@ def show( _maintain_aspect = maintain_aspect subplot.auto_scale(maintain_aspect=_maintain_aspect, zoom=0.95) + # used for generating images in docs using nbsphinx + if "NB_SNAPSHOT" in os.environ.keys(): + if os.environ["NB_SNAPSHOT"] == "1": + return self.canvas.snapshot() + # return the appropriate OutputContext based on the current canvas if self.canvas.__class__.__name__ == "JupyterWgpuCanvas": from .output.jupyter_output import ( @@ -579,6 +585,52 @@ def clear(self): for subplot in self: subplot.clear() + def export(self, uri: str | Path | bytes, **kwargs): + """ + Use ``imageio`` for writing the current Figure to a file, or return a byte string. + Must have ``imageio`` installed. + + Parameters + ---------- + uri: str | Path | bytes + + kwargs: passed to imageio.v3.imwrite, see: https://imageio.readthedocs.io/en/stable/_autosummary/imageio.v3.imwrite.html + + Returns + ------- + None | bytes + see https://imageio.readthedocs.io/en/stable/_autosummary/imageio.v3.imwrite.html + """ + try: + import imageio.v3 as iio + except ModuleNotFoundError: + raise ImportError( + "imageio is required to use Figure.export(). Install it using pip or conda:\n" + "pip install imageio\n" + "conda install -c conda-forge imageio\n" + ) + else: + snapshot = self.renderer.snapshot() + remove_alpha = True + + # image formats that support alpha channel: + # https://en.wikipedia.org/wiki/Alpha_compositing#Image_formats_supporting_alpha_channels + alpha_support = [".png", ".exr", ".tiff", ".tif", ".gif", ".jxl", ".svg"] + + if isinstance(uri, str): + if any([uri.endswith(ext) for ext in alpha_support]): + remove_alpha = False + + elif isinstance(uri, Path): + if uri.suffix in alpha_support: + remove_alpha = False + + if remove_alpha: + # remove alpha channel if it's not supported + snapshot = snapshot[..., :-1].shape + + return iio.imwrite(uri, snapshot, **kwargs) + def _get_iterator(self): return product(range(self.shape[0]), range(self.shape[1])) From fd3b48fcd418e4ec93c902d69df51b7f3ad2944f Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Tue, 18 Jun 2024 18:58:54 -0400 Subject: [PATCH 3/4] docs --- docs/source/api/layouts/figure.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/api/layouts/figure.rst b/docs/source/api/layouts/figure.rst index a2d5e5758..817284e18 100644 --- a/docs/source/api/layouts/figure.rst +++ b/docs/source/api/layouts/figure.rst @@ -37,6 +37,7 @@ Methods Figure.add_animations Figure.clear Figure.close + Figure.export Figure.remove_animation Figure.render Figure.show From 6446584899f87b27238de29d493122dfd11de7f8 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Tue, 18 Jun 2024 19:01:51 -0400 Subject: [PATCH 4/4] remove nbsphinx code that snuck in --- fastplotlib/layouts/_figure.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/fastplotlib/layouts/_figure.py b/fastplotlib/layouts/_figure.py index 19bdbb6a6..d330c6928 100644 --- a/fastplotlib/layouts/_figure.py +++ b/fastplotlib/layouts/_figure.py @@ -476,11 +476,6 @@ def show( _maintain_aspect = maintain_aspect subplot.auto_scale(maintain_aspect=_maintain_aspect, zoom=0.95) - # used for generating images in docs using nbsphinx - if "NB_SNAPSHOT" in os.environ.keys(): - if os.environ["NB_SNAPSHOT"] == "1": - return self.canvas.snapshot() - # return the appropriate OutputContext based on the current canvas if self.canvas.__class__.__name__ == "JupyterWgpuCanvas": from .output.jupyter_output import (