Skip to content

Commit ed4a2ff

Browse files
committed
more tests
1 parent ba980d2 commit ed4a2ff

6 files changed

+537
-4
lines changed

mplotutils/test/test_colorbar_utils.py

+199-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
resize_colorbar_horz,
66
_parse_shift_shrink)
77

8-
8+
import matplotlib.pyplot as plt
9+
import numpy as np
910

1011

1112
def test_parse_shift_shrink():
@@ -32,3 +33,200 @@ def test_parse_shift_shrink():
3233

3334
with raises(AssertionError):
3435
_parse_shift_shrink(0, 1.1)
36+
37+
38+
def _easy_cbar_vert(**kwargs):
39+
40+
f, ax = plt.subplots()
41+
42+
f.subplots_adjust(left=0, bottom=0, right=0.8, top=1)
43+
44+
# simplest 'mappable'
45+
h = ax.pcolormesh([[0, 1]])
46+
47+
# create colorbar
48+
cbax = f.add_axes([0, 0, 0.1, 0.1])
49+
50+
cbar = plt.colorbar(h, orientation='vertical', cax=cbax)
51+
52+
func = resize_colorbar_vert(cbax, ax, **kwargs)
53+
f.canvas.mpl_connect('draw_event', func)
54+
55+
f.canvas.draw()
56+
57+
return f, cbar
58+
59+
60+
def test_resize_colorbar_vert():
61+
62+
63+
# test pad=0, size=0.2
64+
f, cbar = _easy_cbar_vert(size=0.2, pad=0)
65+
66+
pos = cbar.ax.get_position()
67+
res = [pos.x0, pos.y0, pos.width, pos.height]
68+
exp = [0.8, 0, 0.2, 1.]
69+
70+
np.testing.assert_allclose(res, exp)
71+
72+
# -----------------------------------------------------------
73+
74+
f.subplots_adjust(left=0, bottom=0.1, right=0.8, top=0.9)
75+
f.canvas.draw()
76+
77+
pos = cbar.ax.get_position()
78+
res = [pos.x0, pos.y0, pos.width, pos.height]
79+
exp = [0.8, 0.1, 0.2, 0.8]
80+
81+
np.testing.assert_allclose(res, exp)
82+
83+
# ===========================================================
84+
85+
# pad=0.05, size=0.1
86+
87+
f, cbar = _easy_cbar_vert(size=0.1, pad=0.05)
88+
89+
pos = cbar.ax.get_position()
90+
res = [pos.x0, pos.y0, pos.width, pos.height]
91+
exp = [0.85, 0, 0.1, 1.]
92+
93+
np.testing.assert_allclose(res, exp)
94+
95+
# ===========================================================
96+
97+
# shift='symmetric', shrink=0.1
98+
# --> colorbar is 10 % smaller, and centered
99+
100+
f, cbar = _easy_cbar_vert(size=0.2, pad=0., shrink=0.1)
101+
102+
pos = cbar.ax.get_position()
103+
res = [pos.x0, pos.y0, pos.width, pos.height]
104+
exp = [0.8, 0.05, 0.2, 0.9]
105+
106+
np.testing.assert_allclose(res, exp)
107+
108+
# ===========================================================
109+
110+
# shift=0., shrink=0.1
111+
# --> colorbar is 10 % smaller, and aligned with the bottom
112+
113+
f, cbar = _easy_cbar_vert(size=0.2, pad=0., shrink=0.1, shift=0.)
114+
115+
pos = cbar.ax.get_position()
116+
res = [pos.x0, pos.y0, pos.width, pos.height]
117+
exp = [0.8, 0.0, 0.2, 0.9]
118+
119+
np.testing.assert_allclose(res, exp)
120+
121+
# ===========================================================
122+
123+
# shift=0.1, shrink=None
124+
# --> colorbar is 10 % smaller, and aligned with the top
125+
126+
f, cbar = _easy_cbar_vert(size=0.2, pad=0., shrink=None, shift=0.1)
127+
128+
pos = cbar.ax.get_position()
129+
res = [pos.x0, pos.y0, pos.width, pos.height]
130+
exp = [0.8, 0.1, 0.2, 0.9]
131+
132+
np.testing.assert_allclose(res, exp)
133+
134+
# =============================================================================
135+
# =============================================================================
136+
137+
def _easy_cbar_horz(**kwargs):
138+
139+
f, ax = plt.subplots()
140+
141+
f.subplots_adjust(left=0, bottom=0.2, right=1, top=1)
142+
143+
# simplest 'mappable'
144+
h = ax.pcolormesh([[0, 1]])
145+
146+
# create colorbar
147+
cbax = f.add_axes([0, 0, 0.1, 0.1])
148+
149+
cbar = plt.colorbar(h, orientation='horizontal', cax=cbax)
150+
151+
func = resize_colorbar_horz(cbax, ax, **kwargs)
152+
f.canvas.mpl_connect('draw_event', func)
153+
154+
f.canvas.draw()
155+
156+
return f, cbar
157+
158+
159+
def test_resize_colorbar_horz():
160+
161+
162+
# test pad=0, size=0.2
163+
f, cbar = _easy_cbar_horz(size=0.2, pad=0)
164+
165+
pos = cbar.ax.get_position()
166+
res = [pos.x0, pos.y0, pos.width, pos.height]
167+
exp = [0, 0, 1, 0.2]
168+
169+
# adding atol because else 5e-17 != 0
170+
np.testing.assert_allclose(res, exp, atol=1e-08)
171+
172+
# -----------------------------------------------------------
173+
174+
f.subplots_adjust(left=0.1, bottom=0.2, right=0.9, top=1)
175+
f.canvas.draw()
176+
177+
pos = cbar.ax.get_position()
178+
res = [pos.x0, pos.y0, pos.width, pos.height]
179+
exp = [0.1, 0, 0.8, 0.2]
180+
181+
np.testing.assert_allclose(res, exp, atol=1e-08)
182+
183+
# ===========================================================
184+
185+
# pad=0.05, size=0.1
186+
187+
f, cbar = _easy_cbar_horz(size=0.1, pad=0.05)
188+
189+
pos = cbar.ax.get_position()
190+
res = [pos.x0, pos.y0, pos.width, pos.height]
191+
exp = [0.0, 0.05, 1, 0.1]
192+
193+
np.testing.assert_allclose(res, exp, atol=1e-08)
194+
195+
# ===========================================================
196+
197+
# shift='symmetric', shrink=0.1
198+
# --> colorbar is 10 % smaller, and centered
199+
200+
f, cbar = _easy_cbar_horz(size=0.2, pad=0., shrink=0.1)
201+
202+
pos = cbar.ax.get_position()
203+
res = [pos.x0, pos.y0, pos.width, pos.height]
204+
exp = [0.05, 0, 0.9, 0.2]
205+
206+
np.testing.assert_allclose(res, exp, atol=1e-08)
207+
208+
# ===========================================================
209+
210+
# shift=0., shrink=0.1
211+
# --> colorbar is 10 % smaller, and aligned with lhs
212+
213+
f, cbar = _easy_cbar_horz(size=0.2, pad=0., shrink=0.1, shift=0.)
214+
215+
pos = cbar.ax.get_position()
216+
res = [pos.x0, pos.y0, pos.width, pos.height]
217+
exp = [0.0, 0, 0.9, 0.2]
218+
219+
np.testing.assert_allclose(res, exp, atol=1e-08)
220+
221+
# ===========================================================
222+
223+
# shift=0.1, shrink=None
224+
# --> colorbar is 10 % smaller, and aligned with rhs
225+
226+
f, cbar = _easy_cbar_horz(size=0.2, pad=0., shrink=None, shift=0.1)
227+
228+
pos = cbar.ax.get_position()
229+
res = [pos.x0, pos.y0, pos.width, pos.height]
230+
exp = [0.1, 0, 0.9, 0.2]
231+
232+
np.testing.assert_allclose(res, exp, atol=1e-08)
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
import numpy as np
3+
import xarray as xr
4+
5+
from mplotutils.cartopy_utils import (cyclic_dataarray)
6+
7+
8+
def test_cyclic_dataarray():
9+
10+
data = xr.DataArray([[1, 2, 3], [4, 5, 6]],
11+
coords={'x': [1, 2], 'y': range(3)},
12+
dims=['x', 'y'])
13+
14+
res = cyclic_dataarray(data, 'y')
15+
16+
expected_data = np.asarray([[1, 2, 3, 1], [4, 5, 6, 4]])
17+
18+
np.testing.assert_allclose(res, expected_data)
19+
20+
np.testing.assert_allclose(res.y, [0, 1, 2, 3])
21+
np.testing.assert_allclose(res.x, [1, 2])
22+
23+
24+
# per default use 'lon'
25+
data = xr.DataArray([[1, 2, 3], [4, 5, 6]],
26+
coords={'x': [1, 2], 'lon': range(3)},
27+
dims=['x', 'lon'])
28+
29+
res = cyclic_dataarray(data)
30+
np.testing.assert_allclose(res, expected_data)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import numpy as np
2+
3+
from numpy.testing import assert_array_equal # noqa: F401
4+
5+
from pytest import raises
6+
7+
from mplotutils.cartopy_utils import (infer_interval_breaks,
8+
_infer_interval_breaks)
9+
10+
11+
def test__infer_interval_breaks():
12+
assert_array_equal([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1]))
13+
assert_array_equal([-0.5, 0.5, 5.0, 9.5, 10.5],
14+
_infer_interval_breaks([0, 1, 9, 10]))
15+
16+
# make a bounded 2D array that we will center and re-infer
17+
xref, yref = np.meshgrid(np.arange(6), np.arange(5))
18+
cx = (xref[1:, 1:] + xref[:-1, :-1]) / 2
19+
cy = (yref[1:, 1:] + yref[:-1, :-1]) / 2
20+
x = _infer_interval_breaks(cx, axis=1)
21+
x = _infer_interval_breaks(x, axis=0)
22+
y = _infer_interval_breaks(cy, axis=1)
23+
y = _infer_interval_breaks(y, axis=0)
24+
np.testing.assert_allclose(xref, x)
25+
np.testing.assert_allclose(yref, y)
26+
27+
# test that warning is raised for non-monotonic inputs
28+
# with raises(ValueError):
29+
# _infer_interval_breaks(np.array([0, 2, 1]))
30+
31+
32+
def test_infer_interval_breaks():
33+
34+
# 1D
35+
lon = np.arange(5, 356, 10)
36+
lat = np.arange(-85, 86, 10)
37+
38+
lon_expected = np.arange(0, 361, 10)
39+
lat_expected = np.arange(-90, 91, 10)
40+
41+
lon_result, lat_result = infer_interval_breaks(lon, lat)
42+
43+
np.testing.assert_allclose(lon_expected, lon_result)
44+
np.testing.assert_allclose(lat_expected, lat_result)
45+
46+
# 2D, as above
47+
48+
xref, yref = np.meshgrid(np.arange(6), np.arange(5))
49+
cx = (xref[1:, 1:] + xref[:-1, :-1]) / 2
50+
cy = (yref[1:, 1:] + yref[:-1, :-1]) / 2
51+
52+
x, y = infer_interval_breaks(cx, cy)
53+
54+
np.testing.assert_allclose(xref, x)
55+
np.testing.assert_allclose(yref, y)
56+
57+
58+
def test_infer_interval_breaks_clip():
59+
60+
# no clip
61+
lon = np.arange(5, 356, 10)
62+
lat = np.arange(-90, 91, 10)
63+
64+
lon_expected = np.arange(0, 361, 10)
65+
lat_expected = np.arange(-95, 96, 10)
66+
67+
lon_result, lat_result = infer_interval_breaks(lon, lat)
68+
69+
np.testing.assert_allclose(lon_expected, lon_result)
70+
np.testing.assert_allclose(lat_expected, lat_result)
71+
72+
# clip
73+
lat_expected[0] = -90
74+
lat_expected[-1] = 90
75+
76+
lon_result, lat_result = infer_interval_breaks(lon, lat, clip=True)
77+
78+
np.testing.assert_allclose(lon_expected, lon_result)
79+
np.testing.assert_allclose(lat_expected, lat_result)
80+
81+
82+
83+
# 2D, as above
84+
85+
86+
87+
# def test_is_monotonic():
88+
89+
# _is_monotonic(coord, axis=0):
90+
"""
91+
>>> _is_monotonic(np.array([0, 1, 2]))
92+
True
93+
>>> _is_monotonic(np.array([2, 1, 0]))
94+
True
95+
>>> _is_monotonic(np.array([0, 2, 1]))
96+
False
97+
"""
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from mplotutils import sample_data_3d
2+
3+
import numpy as np
4+
5+
def test_data_shape():
6+
7+
nlons = 10
8+
nlats = 20
9+
10+
lon, lat, data = sample_data_3d(nlons, nlats)
11+
12+
assert len(lon) == nlons
13+
assert len(lat) == nlats
14+
15+
assert data.shape == (nlats, nlons)
16+
17+
nlons = 5
18+
nlats = 10
19+
20+
lon, lat, data = sample_data_3d(nlons, nlats)
21+
22+
assert len(lon) == nlons
23+
assert len(lat) == nlats
24+
25+
assert data.shape == (nlats, nlons)
26+
27+
28+
def test_lat():
29+
30+
__, lat, __ = sample_data_3d(36, 9)
31+
32+
expected_lat = np.arange(-80, 81, 20)
33+
assert np.allclose(lat, expected_lat)
34+
35+
36+
def test_lon():
37+
38+
lon, __, __ = sample_data_3d(36, 9)
39+
40+
expected_lon = np.arange(0, 351, 10)
41+
assert np.allclose(lon, expected_lon)

0 commit comments

Comments
 (0)