From 82613638c197b04735be5b83d01f9f91a94456f8 Mon Sep 17 00:00:00 2001 From: Anna Petrasova Date: Wed, 25 Sep 2024 09:49:07 -0400 Subject: [PATCH] grass.jupyter: move save() to BaseSeriesMap class to reduce redundancy (#4378) --- python/grass/jupyter/baseseriesmap.py | 48 ++++++++++++++++++- python/grass/jupyter/seriesmap.py | 47 ------------------ .../grass/jupyter/tests/timeseriesmap_test.py | 5 -- python/grass/jupyter/timeseriesmap.py | 47 ------------------ 4 files changed, 47 insertions(+), 100 deletions(-) diff --git a/python/grass/jupyter/baseseriesmap.py b/python/grass/jupyter/baseseriesmap.py index 634e172f3c5..4497b16593d 100644 --- a/python/grass/jupyter/baseseriesmap.py +++ b/python/grass/jupyter/baseseriesmap.py @@ -25,7 +25,7 @@ import grass.script as gs from .map import Map -from .utils import get_number_of_cores +from .utils import get_number_of_cores, save_gif class BaseSeriesMap: @@ -210,3 +210,49 @@ def change_image(index): width="100%", display="inline-flex", flex_flow="row wrap" ) return widgets.HBox([play, slider, out_img], layout=layout) + + def save( + self, + filename, + duration=500, + label=True, + font=None, + text_size=12, + text_color="gray", + ): + """ + Creates a GIF animation of rendered layers. + + Text color must be in a format accepted by PIL ImageColor module. For supported + formats, visit: + https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names + + param str filename: name of output GIF file + param int duration: time to display each frame; milliseconds + param bool label: include label on each frame + param str font: font file + param int text_size: size of label text + param str text_color: color to use for the text. + """ + + # Render images if they have not been already + if not self._layers_rendered: + self.render() + + input_files = [] + for index in self._indices: + input_files.append(self._base_filename_dict[index]) + + save_gif( + input_files, + filename, + duration=duration, + label=label, + labels=self._labels, + font=font, + text_size=text_size, + text_color=text_color, + ) + + # Display the GIF + return filename diff --git a/python/grass/jupyter/seriesmap.py b/python/grass/jupyter/seriesmap.py index 8615ad488fb..ae6bde911b6 100644 --- a/python/grass/jupyter/seriesmap.py +++ b/python/grass/jupyter/seriesmap.py @@ -20,7 +20,6 @@ from .map import Map from .region import RegionManagerForSeries -from .utils import save_gif from .baseseriesmap import BaseSeriesMap @@ -165,49 +164,3 @@ def render(self): ) tasks = [(i,) for i in range(self.baseseries)] self._render(tasks) - - def save( - self, - filename, - duration=500, - label=True, - font=None, - text_size=12, - text_color="gray", - ): - """ - Creates a GIF animation of rendered layers. - - Text color must be in a format accepted by PIL ImageColor module. For supported - formats, visit: - https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names - - param str filename: name of output GIF file - param int duration: time to display each frame; milliseconds - param bool label: include label on each frame - param str font: font file - param int text_size: size of label text - param str text_color: color to use for the text - """ - - # Render images if they have not been already - if not self._layers_rendered: - self.render() - - tmp_files = [] - for file in self._base_filename_dict.values(): - tmp_files.append(file) - - save_gif( - tmp_files, - filename, - duration=duration, - label=label, - labels=self._labels, - font=font, - text_size=text_size, - text_color=text_color, - ) - - # Display the GIF - return filename diff --git a/python/grass/jupyter/tests/timeseriesmap_test.py b/python/grass/jupyter/tests/timeseriesmap_test.py index c4e20f7704e..d65f7fcb700 100644 --- a/python/grass/jupyter/tests/timeseriesmap_test.py +++ b/python/grass/jupyter/tests/timeseriesmap_test.py @@ -1,7 +1,6 @@ """Test TimeSeriesMap functions""" from pathlib import Path -import sys import pytest @@ -67,10 +66,6 @@ def test_render_layers(space_time_raster_dataset, fill_gaps): assert Path(filename).is_file() -@pytest.mark.xfail( - sys.platform == "win32", - reason="DejaVuSans.ttf file isn't found and not installed with GRASS", -) @pytest.mark.needs_solo_run def test_save(space_time_raster_dataset, tmp_path): """Test returns from animate and time_slider are correct object types""" diff --git a/python/grass/jupyter/timeseriesmap.py b/python/grass/jupyter/timeseriesmap.py index 3ac94bea93e..2acfa575126 100644 --- a/python/grass/jupyter/timeseriesmap.py +++ b/python/grass/jupyter/timeseriesmap.py @@ -20,7 +20,6 @@ from .map import Map from .region import RegionManagerForTimeSeries -from .utils import save_gif from .baseseriesmap import BaseSeriesMap @@ -312,49 +311,3 @@ def render(self): filename = os.path.join(self._tmpdir.name, f"{layer}.png") tasks.append((date, layer, filename)) self._render(tasks) - - def save( - self, - filename, - duration=500, - label=True, - font="DejaVuSans.ttf", - text_size=12, - text_color="gray", - ): - """ - Creates a GIF animation of rendered layers. - - Text color must be in a format accepted by PIL ImageColor module. For supported - formats, visit: - https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names - - param str filename: name of output GIF file - param int duration: time to display each frame; milliseconds - param bool label: include date/time stamp on each frame - param str font: font file - param int text_size: size of date/time text - param str text_color: color to use for the text. - """ - - # Render images if they have not been already - if not self._layers_rendered: - self.render() - - input_files = [] - for date in self._labels: - input_files.append(self._base_filename_dict[date]) - - save_gif( - input_files, - filename, - duration=duration, - label=label, - labels=self._labels, - font=font, - text_size=text_size, - text_color=text_color, - ) - - # Display the GIF - return filename