|
5 | 5 | import itertools
|
6 | 6 | from distutils.version import LooseVersion as V
|
7 | 7 |
|
8 |
| -from nose.tools import assert_raises |
| 8 | +import nose.tools as nt |
9 | 9 |
|
10 | 10 | import numpy as np
|
11 | 11 | from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
|
@@ -80,6 +80,182 @@ def test_Normalize():
|
80 | 80 | _mask_tester(norm, vals)
|
81 | 81 |
|
82 | 82 |
|
| 83 | +class _base_NormMixin(object): |
| 84 | + def test_call(self): |
| 85 | + normed_vals = self.norm(self.vals) |
| 86 | + assert_array_almost_equal(normed_vals, self.expected) |
| 87 | + |
| 88 | + def test_inverse(self): |
| 89 | + _inverse_tester(self.norm, self.vals) |
| 90 | + |
| 91 | + def test_scalar(self): |
| 92 | + _scalar_tester(self.norm, self.vals) |
| 93 | + |
| 94 | + def test_mask(self): |
| 95 | + _mask_tester(self.norm, self.vals) |
| 96 | + |
| 97 | + def test_autoscale(self): |
| 98 | + norm = self.normclass() |
| 99 | + norm.autoscale([10, 20, 30, 40]) |
| 100 | + nt.assert_equal(norm.vmin, 10.) |
| 101 | + nt.assert_equal(norm.vmax, 40.) |
| 102 | + |
| 103 | + def test_autoscale_None_vmin(self): |
| 104 | + norm = self.normclass(vmin=0, vmax=None) |
| 105 | + norm.autoscale_None([1, 2, 3, 4, 5]) |
| 106 | + nt.assert_equal(norm.vmin, 0.) |
| 107 | + nt.assert_equal(norm.vmax, 5.) |
| 108 | + |
| 109 | + def test_autoscale_None_vmax(self): |
| 110 | + norm = self.normclass(vmin=None, vmax=10) |
| 111 | + norm.autoscale_None([1, 2, 3, 4, 5]) |
| 112 | + nt.assert_equal(norm.vmin, 1.) |
| 113 | + nt.assert_equal(norm.vmax, 10.) |
| 114 | + |
| 115 | + def test_scale(self): |
| 116 | + norm = self.normclass() |
| 117 | + nt.assert_false(norm.scaled()) |
| 118 | + |
| 119 | + norm([1, 2, 3, 4]) |
| 120 | + nt.assert_true(norm.scaled()) |
| 121 | + |
| 122 | + def test_process_value_scalar(self): |
| 123 | + res, is_scalar = mcolors.Normalize.process_value(5) |
| 124 | + nt.assert_true(is_scalar) |
| 125 | + assert_array_equal(res, np.array([5.])) |
| 126 | + |
| 127 | + def test_process_value_list(self): |
| 128 | + res, is_scalar = mcolors.Normalize.process_value([5, 10]) |
| 129 | + nt.assert_false(is_scalar) |
| 130 | + assert_array_equal(res, np.array([5., 10.])) |
| 131 | + |
| 132 | + def test_process_value_tuple(self): |
| 133 | + res, is_scalar = mcolors.Normalize.process_value((5, 10)) |
| 134 | + nt.assert_false(is_scalar) |
| 135 | + assert_array_equal(res, np.array([5., 10.])) |
| 136 | + |
| 137 | + def test_process_value_array(self): |
| 138 | + res, is_scalar = mcolors.Normalize.process_value(np.array([5, 10])) |
| 139 | + nt.assert_false(is_scalar) |
| 140 | + assert_array_equal(res, np.array([5., 10.])) |
| 141 | + |
| 142 | + |
| 143 | +class test_OffsetNorm_Even(_base_NormMixin): |
| 144 | + def setup(self): |
| 145 | + self.normclass = mcolors.OffsetNorm |
| 146 | + self.norm = self.normclass(vmin=-1, vcenter=0, vmax=4) |
| 147 | + self.vals = np.array([-1.0, -0.5, 0.0, 1.0, 2.0, 3.0, 4.0]) |
| 148 | + self.expected = np.array([0.0, 0.25, 0.5, 0.625, 0.75, 0.875, 1.0]) |
| 149 | + |
| 150 | + |
| 151 | +class test_OffsetNorm_Odd(_base_NormMixin): |
| 152 | + def setup(self): |
| 153 | + self.normclass = mcolors.OffsetNorm |
| 154 | + self.norm = self.normclass(vmin=-2, vcenter=0, vmax=5) |
| 155 | + self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) |
| 156 | + self.expected = np.array([0.0, 0.25, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]) |
| 157 | + |
| 158 | + |
| 159 | +class test_OffsetNorm_AllNegative(_base_NormMixin): |
| 160 | + def setup(self): |
| 161 | + self.normclass = mcolors.OffsetNorm |
| 162 | + self.norm = self.normclass(vmin=-10, vcenter=-8, vmax=-2) |
| 163 | + self.vals = np.array([-10., -9., -8., -6., -4., -2.]) |
| 164 | + self.expected = np.array([0.0, 0.25, 0.5, 0.666667, 0.833333, 1.0]) |
| 165 | + |
| 166 | + |
| 167 | +class test_OffsetNorm_AllPositive(_base_NormMixin): |
| 168 | + def setup(self): |
| 169 | + self.normclass = mcolors.OffsetNorm |
| 170 | + self.norm = self.normclass(vmin=0, vcenter=3, vmax=9) |
| 171 | + self.vals = np.array([0., 1.5, 3., 4.5, 6.0, 7.5, 9.]) |
| 172 | + self.expected = np.array([0.0, 0.25, 0.5, 0.625, 0.75, 0.875, 1.0]) |
| 173 | + |
| 174 | + |
| 175 | +class test_OffsetNorm_NoVs(_base_NormMixin): |
| 176 | + def setup(self): |
| 177 | + self.normclass = mcolors.OffsetNorm |
| 178 | + self.norm = self.normclass(vmin=None, vcenter=None, vmax=None) |
| 179 | + self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0]) |
| 180 | + self.expected = np.array([0., 0.16666667, 0.33333333, |
| 181 | + 0.5, 0.66666667, 0.83333333, 1.0]) |
| 182 | + self.expected_vmin = -2 |
| 183 | + self.expected_vcenter = 1 |
| 184 | + self.expected_vmax = 4 |
| 185 | + |
| 186 | + def test_vmin(self): |
| 187 | + nt.assert_true(self.norm.vmin is None) |
| 188 | + self.norm(self.vals) |
| 189 | + nt.assert_equal(self.norm.vmin, self.expected_vmin) |
| 190 | + |
| 191 | + def test_vcenter(self): |
| 192 | + nt.assert_true(self.norm.vcenter is None) |
| 193 | + self.norm(self.vals) |
| 194 | + nt.assert_equal(self.norm.vcenter, self.expected_vcenter) |
| 195 | + |
| 196 | + def test_vmax(self): |
| 197 | + nt.assert_true(self.norm.vmax is None) |
| 198 | + self.norm(self.vals) |
| 199 | + nt.assert_equal(self.norm.vmax, self.expected_vmax) |
| 200 | + |
| 201 | + |
| 202 | +class test_OffsetNorm_VminEqualsVcenter(_base_NormMixin): |
| 203 | + def setup(self): |
| 204 | + self.normclass = mcolors.OffsetNorm |
| 205 | + self.norm = self.normclass(vmin=-2, vcenter=-2, vmax=2) |
| 206 | + self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0]) |
| 207 | + self.expected = np.array([0.5, 0.625, 0.75, 0.875, 1.0]) |
| 208 | + |
| 209 | + |
| 210 | +class test_OffsetNorm_VmaxEqualsVcenter(_base_NormMixin): |
| 211 | + def setup(self): |
| 212 | + self.normclass = mcolors.OffsetNorm |
| 213 | + self.norm = self.normclass(vmin=-2, vcenter=2, vmax=2) |
| 214 | + self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0]) |
| 215 | + self.expected = np.array([0.0, 0.125, 0.25, 0.375, 0.5]) |
| 216 | + |
| 217 | + |
| 218 | +class test_OffsetNorm_VsAllEqual(_base_NormMixin): |
| 219 | + def setup(self): |
| 220 | + self.v = 10 |
| 221 | + self.normclass = mcolors.OffsetNorm |
| 222 | + self.norm = self.normclass(vmin=self.v, vcenter=self.v, vmax=self.v) |
| 223 | + self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0]) |
| 224 | + self.expected = np.array([0.0, 0.0, 0.0, 0.0, 0.0]) |
| 225 | + self.expected_inv = self.expected + self.v |
| 226 | + |
| 227 | + def test_inverse(self): |
| 228 | + assert_array_almost_equal( |
| 229 | + self.norm.inverse(self.norm(self.vals)), |
| 230 | + self.expected_inv |
| 231 | + ) |
| 232 | + |
| 233 | + |
| 234 | +class test_OffsetNorm_Errors(object): |
| 235 | + def setup(self): |
| 236 | + self.vals = np.arange(50) |
| 237 | + |
| 238 | + @nt.raises(ValueError) |
| 239 | + def test_VminGTVcenter(self): |
| 240 | + norm = mcolors.OffsetNorm(vmin=10, vcenter=0, vmax=20) |
| 241 | + norm(self.vals) |
| 242 | + |
| 243 | + @nt.raises(ValueError) |
| 244 | + def test_VminGTVmax(self): |
| 245 | + norm = mcolors.OffsetNorm(vmin=10, vcenter=0, vmax=5) |
| 246 | + norm(self.vals) |
| 247 | + |
| 248 | + @nt.raises(ValueError) |
| 249 | + def test_VcenterGTVmax(self): |
| 250 | + norm = mcolors.OffsetNorm(vmin=10, vcenter=25, vmax=20) |
| 251 | + norm(self.vals) |
| 252 | + |
| 253 | + @nt.raises(ValueError) |
| 254 | + def test_premature_scaling(self): |
| 255 | + norm = mcolors.OffsetNorm() |
| 256 | + norm.inverse(np.array([0.1, 0.5, 0.9])) |
| 257 | + |
| 258 | + |
83 | 259 | def test_SymLogNorm():
|
84 | 260 | """
|
85 | 261 | Test SymLogNorm behavior
|
@@ -198,7 +374,12 @@ def test_cmap_and_norm_from_levels_and_colors2():
|
198 | 374 | 'Wih extend={0!r} and data '
|
199 | 375 | 'value={1!r}'.format(extend, d_val))
|
200 | 376 |
|
201 |
| - assert_raises(ValueError, mcolors.from_levels_and_colors, levels, colors) |
| 377 | + nt.assert_raises( |
| 378 | + ValueError, |
| 379 | + mcolors.from_levels_and_colors, |
| 380 | + levels, |
| 381 | + colors |
| 382 | + ) |
202 | 383 |
|
203 | 384 |
|
204 | 385 | def test_rgb_hsv_round_trip():
|
@@ -228,8 +409,8 @@ def gray_from_float_rgb():
|
228 | 409 | def gray_from_float_rgba():
|
229 | 410 | return mcolors.colorConverter.to_rgba(0.4)
|
230 | 411 |
|
231 |
| - assert_raises(ValueError, gray_from_float_rgb) |
232 |
| - assert_raises(ValueError, gray_from_float_rgba) |
| 412 | + nt.assert_raises(ValueError, gray_from_float_rgb) |
| 413 | + nt.assert_raises(ValueError, gray_from_float_rgba) |
233 | 414 |
|
234 | 415 |
|
235 | 416 | @image_comparison(baseline_images=['light_source_shading_topo'],
|
|
0 commit comments