Skip to content

Commit fb946f8

Browse files
committed
plot: review base plot to support multiple objects
1 parent 3f88a05 commit fb946f8

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

pysits/visualization/base.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
"""Base visualization utilities."""
1919

2020
import os
21+
import shutil
2122
import tempfile
2223
from typing import Any, TypeAlias
2324

25+
from rpy2.robjects import ListVector
26+
2427
from pysits.backend.functions import r_fnc_plot
2528
from pysits.backend.pkgs import r_pkg_grdevices
2629
from pysits.visualization.image import show_local_image
@@ -34,11 +37,12 @@
3437
#
3538
# Utility function
3639
#
37-
def _base_plot(data: Any, image_args: ImageArgs | None = None, **kwargs: Any) -> str:
40+
def _base_plot(data: Any, image_args: ImageArgs | None = None, **kwargs: Any) -> None:
3841
"""Save and show images created using base plot.
3942
40-
This function creates a temporary PNG file from an R plot object and displays it.
43+
This function creates temporary PNG files from R plot objects and displays them.
4144
It handles the configuration of the image device, plotting, and cleanup.
45+
The function handles multiple plots returned by r_fnc_plot as a ListVector.
4246
4347
Args:
4448
data: The R object to be plotted.
@@ -53,15 +57,10 @@ def _base_plot(data: Any, image_args: ImageArgs | None = None, **kwargs: Any) ->
5357
**kwargs: Additional keyword arguments passed to R's base::plot function.
5458
5559
Returns:
56-
str: Absolute path to the saved temporary PNG image file.
57-
58-
Note:
59-
The function creates a temporary directory and file that should be cleaned up
60-
after use. The image is displayed using the ``show_local_image`` function.
60+
None: Nothing.
6161
"""
6262
# Create a temporary directory
6363
temp_dir = tempfile.mkdtemp()
64-
file_path = os.path.join(temp_dir, "base_plot.jpeg")
6564

6665
# Process image args
6766
image_args = image_args if image_args else {}
@@ -71,20 +70,45 @@ def _base_plot(data: Any, image_args: ImageArgs | None = None, **kwargs: Any) ->
7170
image_width = int(image_args.get("width", 10) * image_res)
7271
image_height = int(image_args.get("height", 6) * image_res)
7372

74-
# Enable image device
75-
r_pkg_grdevices.jpeg(
76-
file=file_path, width=image_width, height=image_height, res=image_res
77-
)
73+
# Plot data
74+
plots = r_fnc_plot(data, **kwargs)
75+
76+
# Handle plots
77+
if isinstance(plots, ListVector):
78+
for i, plot in enumerate(plots):
79+
file_path = os.path.join(temp_dir, f"base_plot_{i}.jpeg")
80+
81+
# Enable image device
82+
r_pkg_grdevices.jpeg(
83+
file=file_path, width=image_width, height=image_height, res=image_res
84+
)
85+
86+
# Plot the plot object directly
87+
r_fnc_plot(plot[0])
7888

79-
# Save the tmap plot using R
80-
r_fnc_plot(data, **kwargs)
89+
r_pkg_grdevices.dev_off()
8190

82-
r_pkg_grdevices.dev_off()
91+
# Display saved image
92+
show_local_image(file_path)
8393

84-
# Display the saved image
85-
show_local_image(file_path)
94+
else:
95+
file_path = os.path.join(temp_dir, "base_plot.jpeg")
8696

87-
return file_path
97+
# Enable image device
98+
r_pkg_grdevices.jpeg(
99+
file=file_path, width=image_width, height=image_height, res=image_res
100+
)
101+
102+
# Save plot using R
103+
r_fnc_plot(plots)
104+
105+
r_pkg_grdevices.dev_off()
106+
107+
# Display saved image
108+
show_local_image(file_path)
109+
110+
# Clean up temporary directory
111+
shutil.rmtree(temp_dir)
88112

89113

90114
#
@@ -93,7 +117,7 @@ def _base_plot(data: Any, image_args: ImageArgs | None = None, **kwargs: Any) ->
93117
def plot_base(
94118
instance: Any, image_args: ImageArgs | None = None, **kwargs: Any
95119
) -> None:
96-
"""Generate and display a base R plot.
120+
"""Generate and display base R plots.
97121
98122
This is a high-level function that wraps ``_base_plot`` to create and display
99123
a plot using R's base plotting system. It handles the creation of a temporary
@@ -113,10 +137,6 @@ def plot_base(
113137
114138
Returns:
115139
None: Nothing.
116-
117-
Note:
118-
The function creates a temporary directory and file that should be cleaned up
119-
after use. The image is displayed using the ``show_local_image`` function.
120140
"""
121-
# Save and display the plot
141+
# Save and display plot
122142
_base_plot(instance, image_args=image_args, **kwargs)

0 commit comments

Comments
 (0)