diff --git a/opencsp/common/lib/render/test/data/input/figure_management/test_plot_arrows.png b/opencsp/common/lib/render/test/data/input/figure_management/test_plot_arrows.png new file mode 100644 index 000000000..79e6f1dbe Binary files /dev/null and b/opencsp/common/lib/render/test/data/input/figure_management/test_plot_arrows.png differ diff --git a/opencsp/common/lib/render/test/test_figure_management.py b/opencsp/common/lib/render/test/test_figure_management.py index 61cf5ce33..7af9eb5c6 100644 --- a/opencsp/common/lib/render/test/test_figure_management.py +++ b/opencsp/common/lib/render/test/test_figure_management.py @@ -1,18 +1,18 @@ -import os -import subprocess import sys -import time import unittest import matplotlib.pyplot as plt +import numpy as np +import numpy.testing +from PIL import Image -import opencsp.common.lib.opencsp_path.opencsp_root_path as root_path -import opencsp.common.lib.process.subprocess_tools as st import opencsp.common.lib.render.figure_management as fm import opencsp.common.lib.render.test.lib.RenderControlFigureRecordInfSave as rcfr_is +import opencsp.common.lib.render.view_spec as vs +import opencsp.common.lib.render_control.RenderControlAxis as rca import opencsp.common.lib.render_control.RenderControlFigure as rcfg +import opencsp.common.lib.render_control.RenderControlPointSeq as rcps import opencsp.common.lib.tool.file_tools as ft -import opencsp.common.lib.tool.log_tools as lt is_original_call = "--funcname" in sys.argv """ Because we call this file again but with arguments, we need to know if @@ -21,22 +21,21 @@ class test_figure_management(unittest.TestCase): - dir_in = os.path.join('common', 'lib', 'render', 'test', 'data', 'input', 'figure_management') - dir_out = os.path.join('common', 'lib', 'render', 'test', 'data', 'output', 'figure_management') - - def __init__(self, *vargs, **kwargs): - super().__init__(*vargs, **kwargs) - self.dir_in = test_figure_management.dir_in - self.dir_out = test_figure_management.dir_out - @classmethod def setUpClass(cls) -> None: + path, name, _ = ft.path_components(__file__) + cls.dir_in = ft.join(path, 'data/input', name.split('test_')[-1]) + cls.dir_out = ft.join(path, 'data/output', name.split('test_')[-1]) + ret = super().setUpClass() ft.create_directories_if_necessary(cls.dir_out) if is_original_call: ft.delete_files_in_directory(cls.dir_out, "*") return ret + def setUp(self) -> None: + self.test_name = self.id().split('.')[-1] + def tearDown(self): # Make sure we release all matplotlib resources. plt.close('all') @@ -113,6 +112,38 @@ def _figure_manager_timeout_1(self): return fm + def test_plot_arrows(self): + fm.reset_figure_management() + + # setup + axis_control = rca.meters(grid=False) + figure_control = rcfg.RenderControlFigure() + view_spec_2d = vs.view_spec_xy() + fig_record = fm.setup_figure( + figure_control, + axis_control, + view_spec_2d, + title=self.test_name, + code_tag=f"{__file__}.{self.test_name}()", + equal=False, + ) + + # draw + square_corners = [(0, 0), (1, 0), (1, 1), (0, 1)] + arrow_style = rcps.RenderControlPointSeq(marker='arrow', markersize=0.1) + fig_record.view.draw_pq_list(square_corners, close=True, style=arrow_style) + fig_record.view.show(equal=True, block=False) + actual = fig_record.to_array() + fig_record.close() + + # load and compare + expected = np.array(Image.open(ft.join(self.dir_in, f"{self.test_name}.png"))) + np.testing.assert_array_equal(expected, actual) + + # save + img = Image.fromarray(actual) + img.save(ft.join(self.dir_out, f"{self.test_name}.png")) + if __name__ == '__main__': import argparse