Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit acb0dc7

Browse files
committed
TST: add tests for DivergingNorm
1 parent 512f2d4 commit acb0dc7

File tree

3 files changed

+103
-182
lines changed

3 files changed

+103
-182
lines changed

lib/matplotlib/colors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,13 @@ def __call__(self, value, clip=None):
10361036
self.autoscale_None(result)
10371037
vmin, vmax = self._data_points[0], self._data_points[-1]
10381038

1039+
10391040
# its possible some of the data points are less than vmin
1040-
# or vmax.
1041+
# or vmax. We could error out if some stop points are
1042+
# overrun, but this lets things keep going...
1043+
1044+
if not np.all(np.diff(self._data_points) >= 0):
1045+
raise ValueError("data points must increase monotonically")
10411046
ind = np.where((self._data_points >= vmin) &
10421047
(self._data_points <= vmax))[0]
10431048
result = np.ma.masked_array(np.interp(result,
@@ -1081,6 +1086,8 @@ def autoscale_None(self, A):
10811086

10821087
self._data_points[0] = vmin
10831088
self._data_points[-1] = vmax
1089+
self.vmin = vmin
1090+
self.vmax = vmax
10841091

10851092

10861093
class DivergingNorm(PiecewiseLinearNorm):

lib/matplotlib/tests/test_colors.py

Lines changed: 95 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
from numpy.testing import assert_array_equal, assert_array_almost_equal
6+
from numpy.testing import (assert_array_equal, assert_array_almost_equal)
77

88
from matplotlib import cycler
99
import matplotlib
@@ -220,187 +220,101 @@ def test_Normalize():
220220
assert 0 < norm(1 + 50 * eps) < 1
221221

222222

223-
class BaseNormMixin(object):
224-
def test_call(self):
225-
normed_vals = self.norm(self.vals)
226-
np.assert_array_almost_equal(normed_vals, self.expected)
223+
def test_DivergingNorm_autoscale():
224+
norm = mcolors.DivergingNorm()
225+
norm.autoscale([10, 20, 30, 40])
226+
assert norm.vmin == 10.
227+
assert norm.vmax == 40.
227228

228-
def test_inverse(self):
229-
if self.test_inverse:
230-
_inverse_tester(self.norm, self.vals)
231-
else:
232-
pass
233-
234-
def test_scalar(self):
235-
_scalar_tester(self.norm, self.vals)
236-
237-
def test_mask(self):
238-
_mask_tester(self.norm, self.vals)
239-
240-
def test_autoscale(self):
241-
norm = self.normclass()
242-
norm.autoscale([10, 20, 30, 40])
243-
np.assert_equal(norm.vmin, 10.)
244-
np.assert_equal(norm.vmax, 40.)
245-
246-
def test_autoscale_None_vmin(self):
247-
norm = self.normclass(vmin=0, vmax=None)
248-
norm.autoscale_None([1, 2, 3, 4, 5])
249-
np.assert_equal(norm.vmin, 0.)
250-
np.assert_equal(norm.vmax, 5.)
251-
252-
def test_autoscale_None_vmax(self):
253-
norm = self.normclass(vmin=None, vmax=10)
254-
norm.autoscale_None([1, 2, 3, 4, 5])
255-
np.assert_equal(norm.vmin, 1.)
256-
np.assert_equal(norm.vmax, 10.)
257-
258-
def test_scale(self):
259-
norm = self.normclass()
260-
np.assert_false(norm.scaled())
261-
262-
norm([1, 2, 3, 4])
263-
np.assert_true(norm.scaled())
264-
265-
def test_process_value_scalar(self):
266-
res, is_scalar = mcolors.Normalize.process_value(5)
267-
np.assert_true(is_scalar)
268-
assert_array_equal(res, np.array([5.]))
269-
270-
def test_process_value_list(self):
271-
res, is_scalar = mcolors.Normalize.process_value([5, 10])
272-
np.assert_false(is_scalar)
273-
np.assert_array_equal(res, np.array([5., 10.]))
274-
275-
def test_process_value_tuple(self):
276-
res, is_scalar = mcolors.Normalize.process_value((5, 10))
277-
np.assert_false(is_scalar)
278-
np.assert_array_equal(res, np.array([5., 10.]))
279-
280-
def test_process_value_array(self):
281-
res, is_scalar = mcolors.Normalize.process_value(np.array([5, 10]))
282-
np.assert_false(is_scalar)
283-
np.assert_array_equal(res, np.array([5., 10.]))
284-
285-
286-
class BaseDivergingNorm(BaseNormMixin):
287-
normclass = mcolors.DivergingNorm
288-
test_inverse = False
289-
290-
291-
class test_DivergingNorm_Even(BaseDivergingNorm):
292-
def setup(self):
293-
self.norm = self.normclass(vmin=-1, vcenter=0, vmax=4)
294-
self.vals = np.array([-1.0, -0.5, 0.0, 1.0, 2.0, 3.0, 4.0])
295-
self.expected = np.array([0.0, 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])
296-
297-
298-
class test_DivergingNorm_Odd(BaseDivergingNorm):
299-
def setup(self):
300-
self.normclass = mcolors.DivergingNorm
301-
self.norm = self.normclass(vmin=-2, vcenter=0, vmax=5)
302-
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
303-
self.expected = np.array([0.0, 0.25, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
304-
305-
306-
class test_DivergingNorm_AllNegative(BaseDivergingNorm):
307-
def setup(self):
308-
self.normclass = mcolors.DivergingNorm
309-
self.norm = self.normclass(vmin=-10, vcenter=-8, vmax=-2)
310-
self.vals = np.array([-10., -9., -8., -6., -4., -2.])
311-
self.expected = np.array([0.0, 0.25, 0.5, 0.666667, 0.833333, 1.0])
312-
313-
314-
class test_DivergingNorm_AllPositive(BaseDivergingNorm):
315-
def setup(self):
316-
self.normclass = mcolors.DivergingNorm
317-
self.norm = self.normclass(vmin=0, vcenter=3, vmax=9)
318-
self.vals = np.array([0., 1.5, 3., 4.5, 6.0, 7.5, 9.])
319-
self.expected = np.array([0.0, 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])
320-
321-
322-
class test_DivergingNorm_NoVs(BaseDivergingNorm):
323-
def setup(self):
324-
self.normclass = mcolors.DivergingNorm
325-
self.norm = self.normclass(vmin=None, vcenter=None, vmax=None)
326-
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0])
327-
self.expected = np.array([0., 0.16666667, 0.33333333,
328-
0.5, 0.66666667, 0.83333333, 1.0])
329-
self.expected_vmin = -2
330-
self.expected_vcenter = 1
331-
self.expected_vmax = 4
332-
333-
def test_vmin(self):
334-
np.assert_true(self.norm.vmin is None)
335-
self.norm(self.vals)
336-
np.assert_equal(self.norm.vmin, self.expected_vmin)
337-
338-
def test_vcenter(self):
339-
np.assert_true(self.norm.vcenter is None)
340-
self.norm(self.vals)
341-
np.assert_equal(self.norm.vcenter, self.expected_vcenter)
342-
343-
def test_vmax(self):
344-
np.assert_true(self.norm.vmax is None)
345-
self.norm(self.vals)
346-
np.assert_equal(self.norm.vmax, self.expected_vmax)
347-
348-
349-
class test_DivergingNorm_VminEqualsVcenter(BaseDivergingNorm):
350-
def setup(self):
351-
self.normclass = mcolors.DivergingNorm
352-
self.norm = self.normclass(vmin=-2, vcenter=-2, vmax=2)
353-
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
354-
self.expected = np.array([0.5, 0.625, 0.75, 0.875, 1.0])
355-
356-
357-
class test_DivergingNorm_VmaxEqualsVcenter(BaseDivergingNorm):
358-
def setup(self):
359-
self.normclass = mcolors.DivergingNorm
360-
self.norm = self.normclass(vmin=-2, vcenter=2, vmax=2)
361-
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
362-
self.expected = np.array([0.0, 0.125, 0.25, 0.375, 0.5])
363-
364-
365-
class test_DivergingNorm_VsAllEqual(BaseDivergingNorm):
366-
def setup(self):
367-
self.v = 10
368-
self.normclass = mcolors.DivergingNorm
369-
self.norm = self.normclass(vmin=self.v, vcenter=self.v, vmax=self.v)
370-
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
371-
self.expected = np.array([0.0, 0.0, 0.0, 0.0, 0.0])
372-
self.expected_inv = self.expected + self.v
373-
374-
def test_inverse(self):
375-
assert_array_almost_equal(
376-
self.norm.inverse(self.norm(self.vals)),
377-
self.expected_inv
378-
)
379-
380-
381-
class test_DivergingNorm_Errors(object):
382-
def setup(self):
383-
self.vals = np.arange(50)
384-
385-
def test_VminGTVcenter(self):
386-
with pytest.raises(ValueError):
387-
norm = mcolors.DivergingNorm(vmin=10, vcenter=0, vmax=20)
388-
norm(self.vals)
389-
390-
def test_VminGTVmax(self):
391-
with pytest.raises(ValueError):
392-
norm = mcolors.DivergingNorm(vmin=10, vcenter=0, vmax=5)
393-
norm(self.vals)
394-
395-
def test_VcenterGTVmax(self):
396-
with pytest.raises(ValueError):
397-
norm = mcolors.DivergingNorm(vmin=10, vcenter=25, vmax=20)
398-
norm(self.vals)
399-
400-
def test_premature_scaling(self):
401-
with pytest.raises(ValueError):
402-
norm = mcolors.DivergingNorm()
403-
norm.inverse(np.array([0.1, 0.5, 0.9]))
229+
230+
def test_DivergingNorm_autoscale_None_vmin():
231+
norm = mcolors.DivergingNorm(vmin=0, vmax=None)
232+
norm.autoscale_None([1, 2, 3, 4, 5])
233+
assert norm(5) == 1
234+
assert norm.vmax == 5
235+
236+
237+
def test_DivergingNorm_autoscale_None_vmax():
238+
norm = mcolors.DivergingNorm(vmin=None, vmax=10)
239+
norm.autoscale_None([1, 2, 3, 4, 5])
240+
assert norm(1) == 0
241+
assert norm.vmin == 1
242+
243+
244+
def test_DivergingNorm_scale():
245+
norm = mcolors.DivergingNorm()
246+
assert norm.scaled() is False
247+
norm([1, 2, 3, 4])
248+
assert norm.scaled() is True
249+
250+
251+
def test_DivergingNorm_scaleout_center():
252+
norm = mcolors.DivergingNorm(vcenter=0)
253+
# if the center is defined outside the data limits, should raise an err.
254+
with pytest.raises(ValueError):
255+
norm([1, 2, 3, 5])
256+
257+
258+
def test_DivergingNorm_Even():
259+
norm = mcolors.DivergingNorm(vmin=-1, vcenter=0, vmax=4)
260+
vals = np.array([-1.0, -0.5, 0.0, 1.0, 2.0, 3.0, 4.0])
261+
expected = np.array([0.0, 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])
262+
assert_array_equal(norm(vals), expected)
263+
264+
265+
def test_DivergingNorm_Odd():
266+
norm = mcolors.DivergingNorm(vmin=-2, vcenter=0, vmax=5)
267+
vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
268+
expected = np.array([0.0, 0.25, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
269+
assert_array_equal(norm(vals), expected)
270+
271+
272+
def test_DivergingNorm_VminEqualsVcenter():
273+
norm = mcolors.DivergingNorm(vmin=-2, vcenter=-2, vmax=2)
274+
vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
275+
expected = np.array([0.5, 0.625, 0.75, 0.875, 1.0])
276+
assert_array_equal(norm(vals), expected)
277+
278+
279+
def test_DivergingNorm_VmaxEqualsVcenter():
280+
norm = mcolors.DivergingNorm(vmin=-2, vcenter=2, vmax=2)
281+
vals = np.array([-2.0, -1.0, 0.0, 1.0, 1.999, 2.0])
282+
expected = np.array([0.0, 0.125, 0.25, 0.375, 0.499875, 1.0])
283+
assert_array_equal(norm(vals), expected)
284+
285+
286+
def test_DivergingNorm_VsAllEqual():
287+
v = 10
288+
mcolors.DivergingNorm = mcolors.DivergingNorm
289+
norm = mcolors.DivergingNorm(vmin=v, vcenter=v, vmax=v)
290+
vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
291+
expected = np.array([0.0, 0.0, 0.0, 0.0, 0.0])
292+
assert_array_equal(norm(vals), expected)
293+
294+
295+
def test_DivergingNorm_VminGTVcenter():
296+
vals = np.arange(50)
297+
with pytest.raises(ValueError):
298+
norm = mcolors.DivergingNorm(vmin=10, vcenter=0, vmax=20)
299+
norm(vals)
300+
301+
302+
def test_DivergingNorm_DivergingNorm_VminGTVmax():
303+
with pytest.raises(ValueError):
304+
norm = mcolors.DivergingNorm(vmin=10, vcenter=0, vmax=5)
305+
306+
307+
def test_DivergingNorm_VcenterGTVmax():
308+
vals = np.arange(50)
309+
with pytest.raises(ValueError):
310+
norm = mcolors.DivergingNorm(vmin=10, vcenter=25, vmax=20)
311+
norm(vals)
312+
313+
314+
def test_DivergingNorm_premature_scaling():
315+
with pytest.raises(ValueError):
316+
norm = mcolors.DivergingNorm()
317+
norm.inverse(np.array([0.1, 0.5, 0.9]))
404318

405319

406320
@image_comparison(baseline_images=['test_offset_norm'], extensions=['png'],

0 commit comments

Comments
 (0)