Skip to content
Snippets Groups Projects
Commit 798c0f50 authored by Mayr, Hannes's avatar Mayr, Hannes
Browse files

Add unittests for writing IDs to metadata. The test for mpl still fails as...

Add unittests for writing IDs to metadata. The test for mpl still fails as writing metadata with mpl is still not resolved.
parent 151e2b44
No related branches found
No related tags found
1 merge request!49Draft: Add ID to metadata of the png file
Pipeline #853489 failed
...@@ -110,7 +110,7 @@ class PublishOptions: ...@@ -110,7 +110,7 @@ class PublishOptions:
""" """
# Export plot figure to picture. # Export plot figure to picture.
plot_paths = save_plot(self.figure, self.plot_names) plot_paths = save_plot(self.figure, self.plot_names, figure_ids=self.figure_ids)
match self.data_storage: match self.data_storage:
case "centralized": case "centralized":
self.centralized_data_storage() self.centralized_data_storage()
......
...@@ -12,7 +12,7 @@ import matplotlib ...@@ -12,7 +12,7 @@ import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
def save_plot(figures, plot_names, extension="png"): def save_plot(figures, plot_names, figure_ids=[], extension="png"):
""" """
Export plot(s). Export plot(s).
...@@ -22,8 +22,10 @@ def save_plot(figures, plot_names, extension="png"): ...@@ -22,8 +22,10 @@ def save_plot(figures, plot_names, extension="png"):
Figure that was tagged and now should be saved as picture. Figure that was tagged and now should be saved as picture.
plot_name : str or list of str plot_name : str or list of str
Names of the files where the plots will be saved to. Names of the files where the plots will be saved to.
extension : str figure_ids: str or list of str, optional
File extension for the plot export. IDs of the figures that will be added to the metadata of the exported images.
extension : str, optional
File extension for the plot export. Default is png.
Returns Returns
------- -------
...@@ -71,7 +73,7 @@ def save_plot(figures, plot_names, extension="png"): ...@@ -71,7 +73,7 @@ def save_plot(figures, plot_names, extension="png"):
elif all(x in str(type(fig)) for x in ["PIL", "ImageFile"]): elif all(x in str(type(fig)) for x in ["PIL", "ImageFile"]):
plot_path.append(plot_names[i] + ".tmp." + extension) plot_path.append(plot_names[i] + ".tmp." + extension)
img_exif = fig.getexif() img_exif = fig.getexif()
img_exif[270] = "Identiplier2" img_exif[270] = figure_ids[i]
fig.save(plot_path[i], exif=img_exif) fig.save(plot_path[i], exif=img_exif)
else: else:
raise TypeError(f"Figure number {i} is not a valid figure object.") raise TypeError(f"Figure number {i} is not a valid figure object.")
......
...@@ -14,6 +14,7 @@ FIGURE = plt.figure() ...@@ -14,6 +14,7 @@ FIGURE = plt.figure()
PLOT_NAME = "PLOT_NAME" PLOT_NAME = "PLOT_NAME"
IMG1 = "image1.png" IMG1 = "image1.png"
IMG2 = "image2.jpg" IMG2 = "image2.jpg"
IDS = ["ID0", "ID1", "ID2"]
class TestSavePlot(unittest.TestCase): class TestSavePlot(unittest.TestCase):
...@@ -27,44 +28,57 @@ class TestSavePlot(unittest.TestCase): ...@@ -27,44 +28,57 @@ class TestSavePlot(unittest.TestCase):
def test_save_plot_matplotlib(self): def test_save_plot_matplotlib(self):
""" """
Test if save_plot succeeds with valid arguments Test if save_plot succeeds with valid arguments using the matplotlib engine.
using the matplotlib engine. Check if ID was NOT set in metadata.
""" """
plot_paths = save_plot(FIGURE, [PLOT_NAME], extension="jpg") plot_paths = save_plot(FIGURE, [PLOT_NAME], extension="jpg")
self.assertIsInstance(plot_paths, list) self.assertIsInstance(plot_paths, list)
img_exif = Image.open(plot_paths[0]).getexif()
assert 270 not in img_exif
os.remove(PLOT_NAME + ".tmp.jpg") os.remove(PLOT_NAME + ".tmp.jpg")
def test_save_plot_image_png(self): def test_save_plot_image_png(self):
""" """
Test if save_plot succeeds with valid arguments using the image engine Test if save_plot succeeds with valid arguments using the image engine
and a png file. and a png file. Check if IDs are in metadata.
""" """
img1 = Image.open(IMG1) img1 = Image.open(IMG1)
plot_paths = save_plot(img1, [PLOT_NAME]) plot_paths = save_plot(img1, [PLOT_NAME], figure_ids=[IDS[1]])
self.assertIsInstance(plot_paths, list) self.assertIsInstance(plot_paths, list)
img_exif = Image.open(plot_paths[0]).getexif()
assert img_exif[270] == IDS[1]
os.remove(PLOT_NAME + ".tmp.png") os.remove(PLOT_NAME + ".tmp.png")
def test_save_plot_image_jpg(self): def test_save_plot_image_jpg(self):
""" """
Test if save_plot succeeds with valid arguments using the image engine Test if save_plot succeeds with valid arguments using the image engine
and a list of jpg files. and a list of jpg files. Check if IDs are in metadata.
""" """
img2 = Image.open(IMG2) img2 = Image.open(IMG2)
imgs_as_list = [img2, img2] imgs_as_list = [img2, img2]
plot_paths = save_plot(imgs_as_list, [PLOT_NAME], extension="jpg") plot_paths = save_plot(
imgs_as_list, [PLOT_NAME], extension="jpg", figure_ids=IDS[0:2]
)
self.assertIsInstance(plot_paths, list) self.assertIsInstance(plot_paths, list)
os.remove(PLOT_NAME + "1.tmp.jpg") for i in (1, 2):
os.remove(PLOT_NAME + "2.tmp.jpg") img_exif = Image.open(plot_paths[i - 1]).getexif()
assert img_exif[270] == IDS[i - 1]
os.remove(PLOT_NAME + f"{i}.tmp.jpg")
def test_more_figs_than_names(self): def test_more_figs_than_names(self):
""" """
Test if a warning is raised if more figures than plot names are given. Test if a warning is raised if more figures than plot names are given.
Additionally, check if files are named correctly. Additionally, check if files are named correctly and IDs are set in exif
metadata.
""" """
with self.assertWarns(Warning): with self.assertWarns(Warning):
save_plot([FIGURE, FIGURE, FIGURE], [PLOT_NAME]) plot_paths = save_plot(
[FIGURE, FIGURE, FIGURE], [PLOT_NAME], figure_ids=IDS
)
for i in (1, 2, 3): for i in (1, 2, 3):
assert os.path.isfile(PLOT_NAME + f"{i}.tmp.png") assert os.path.isfile(PLOT_NAME + f"{i}.tmp.png")
img_exif = Image.open(plot_paths[i]).getexif()
assert img_exif[270] == IDS[i - 1]
os.remove(PLOT_NAME + f"{i}.tmp.png") os.remove(PLOT_NAME + f"{i}.tmp.png")
def test_more_names_than_figs(self): def test_more_names_than_figs(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment