-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_base.py
More file actions
executable file
·173 lines (141 loc) · 5.62 KB
/
Copy pathtest_base.py
File metadata and controls
executable file
·173 lines (141 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""Test module of the :mod:`psyplot.plotter.baseplotter` module"""
# SPDX-FileCopyrightText: 2016-2024 University of Lausanne
# SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht
# SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum hereon GmbH
#
# SPDX-License-Identifier: LGPL-3.0-only
import unittest
from itertools import chain
import _base_testing as bt
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import psyplot
from psy_simple.base import BasePlotter
from psyplot import InteractiveList, open_dataset
if mpl.__version__ >= "1.5" and mpl.__version__ < "2.1":
from matplotlib.font_manager import weight_dict
bold = weight_dict["bold"]
else:
bold = "bold"
class BasePlotterTest(bt.PsyPlotTestCase):
"""Test :class:`psyplot.plotter.baseplotter.BasePlotter` class"""
var = "t2m"
@classmethod
def setUpClass(cls):
cls.ds = open_dataset(cls.ncfile)
cls.data = InteractiveList.from_dataset(
cls.ds, y=[0, 1], z=0, t=0, name=cls.var, auto_update=True
)
cls.plotter = BasePlotter(cls.data)
@classmethod
def tearDownClass(cls):
super(BasePlotterTest, cls).tearDownClass()
cls.ds.close()
plt.close(cls.plotter.ax.get_figure().number)
def tearDown(self):
self.data.psy.update(t=0, todefault=True, replot=True)
def update(self, *args, **kwargs):
"""Update the plotter of this instance"""
self.plotter.update(*args, **kwargs)
def _label_test(self, key, label_func, has_time=True):
kwargs = {
key: "Test plot at %Y-%m-%d, {tinfo} o'clock of %(long_name)s"
}
self.update(**kwargs)
t_str = "1979-01-31, 18:00" if has_time else "%Y-%m-%d, %H:%M"
self.assertEqual(
"Test plot at %s o'clock of %s"
% (t_str, self.data.attrs.get("long_name", "Temperature")),
label_func().get_text(),
)
self.data.psy.update(t=1)
t_str = "1979-02-28, 18:00" if has_time else "%Y-%m-%d, %H:%M"
self.assertEqual(
"Test plot at %s o'clock of %s"
% (t_str, self.data.attrs.get("long_name", "Temperature")),
label_func().get_text(),
)
self.data.psy.update(t=0)
def test_title(self):
"""Test title, titlesize, titleweight, titleprops formatoptions"""
def get_title():
return self.plotter.ax.title
self._label_test("title", get_title)
self.update(
titlesize=22, titleweight="bold", titleprops={"ha": "left"}
)
self.assertEqual(get_title().get_size(), 22)
self.assertEqual(get_title().get_weight(), bold)
self.assertEqual(get_title().get_ha(), "left")
def test_figtitle(self):
"""Test figtitle, figtitlesize, figtitleweight, figtitleprops
formatoptions"""
def get_figtitle():
fig = plt.gcf()
for text in fig.texts:
if text.get_position() == (0.5, 0.98):
return text
self._label_test("figtitle", get_figtitle)
self.update(
figtitlesize=22,
figtitleweight="bold",
figtitleprops={"ha": "left"},
)
self.assertEqual(get_figtitle().get_size(), 22)
self.assertEqual(get_figtitle().get_weight(), bold)
self.assertEqual(get_figtitle().get_ha(), "left")
def test_text(self):
"""Test text formatoption"""
def get_default_text():
for text in chain(*self.plotter.text._texts.values()):
if text.get_position() == tuple(
psyplot.rcParams["texts.default_position"]
):
return text
self._label_test("text", get_default_text)
self.update(text=(0.5, 0.5, "%(name)s", "fig", {"fontsize": 16}))
for t in self.plotter.text._texts["fig"]:
if t.get_position() == (0.5, 0.5):
text = t
break
else:
text = False
self.assertTrue(text is not False)
if not text:
return
self.assertEqual(text.get_text(), getattr(self.data, "name", self.var))
self.assertEqual(text.get_fontsize(), 16)
def test_maskgreater(self):
"""Test maskgreater formatoption"""
self.update(maskgreater=250)
for arr in self.plotter.maskgreater.iter_data:
self.assertLessEqual(arr.max().values, 250)
def test_maskgeq(self):
"""Test maskgeq formatoption"""
self.update(maskgeq=250)
for arr in self.plotter.maskgeq.iter_data:
self.assertLessEqual(arr.max().values, 250)
def test_maskless(self):
"""Test maskless formatoption"""
self.update(maskless=250)
for arr in self.plotter.maskless.iter_data:
self.assertGreaterEqual(arr.min().values, 250)
def test_maskleq(self):
"""Test maskleq formatoption"""
self.update(maskleq=250)
for arr in self.plotter.maskleq.iter_data:
self.assertGreaterEqual(arr.min().values, 250)
def test_maskbetween(self):
"""Test maskbetween formatoption"""
self.update(maskbetween=[250, 251])
for arr in self.plotter.maskbetween.iter_data:
data = arr.values[~np.isnan(arr.values)]
self.assertLessEqual(data[data < 251].max(), 250)
self.assertGreaterEqual(data[data > 250].max(), 251)
class BasePlotterTest2D(bt.TestBase2D, BasePlotterTest):
"""Test :class:`psyplot.plotter.baseplotter.BasePlotter` class without time
and vertical dimension"""
var = "t2m_2d"
if __name__ == "__main__":
unittest.main()