Skip to content

Commit

Permalink
add test for drawing arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
bbean23 committed Jul 31, 2024
1 parent 286b021 commit 8c20276
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 45 additions & 14 deletions opencsp/common/lib/render/test/test_figure_management.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8c20276

Please sign in to comment.