|
1 | 1 | import copy
|
2 | 2 | import itertools
|
3 |
| - |
4 | 3 | import numpy as np
|
5 | 4 | import pytest
|
6 | 5 |
|
@@ -221,6 +220,208 @@ def test_Normalize():
|
221 | 220 | assert 0 < norm(1 + 50 * eps) < 1
|
222 | 221 |
|
223 | 222 |
|
| 223 | +class BaseNormMixin(object): |
| 224 | + def test_call(self): |
| 225 | + normed_vals = self.norm(self.vals) |
| 226 | + assert_array_almost_equal(normed_vals, self.expected) |
| 227 | + |
| 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 | + assert_equal(norm.vmin, 10.) |
| 244 | + 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 | + assert_equal(norm.vmin, 0.) |
| 250 | + 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 | + assert_equal(norm.vmin, 1.) |
| 256 | + assert_equal(norm.vmax, 10.) |
| 257 | + |
| 258 | + def test_scale(self): |
| 259 | + norm = self.normclass() |
| 260 | + assert_false(norm.scaled()) |
| 261 | + |
| 262 | + norm([1, 2, 3, 4]) |
| 263 | + assert_true(norm.scaled()) |
| 264 | + |
| 265 | + def test_process_value_scalar(self): |
| 266 | + res, is_scalar = mcolors.Normalize.process_value(5) |
| 267 | + 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 | + assert_false(is_scalar) |
| 273 | + 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 | + assert_false(is_scalar) |
| 278 | + 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 | + assert_false(is_scalar) |
| 283 | + 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 | + assert_true(self.norm.vmin is None) |
| 335 | + self.norm(self.vals) |
| 336 | + assert_equal(self.norm.vmin, self.expected_vmin) |
| 337 | + |
| 338 | + def test_vcenter(self): |
| 339 | + assert_true(self.norm.vcenter is None) |
| 340 | + self.norm(self.vals) |
| 341 | + assert_equal(self.norm.vcenter, self.expected_vcenter) |
| 342 | + |
| 343 | + def test_vmax(self): |
| 344 | + assert_true(self.norm.vmax is None) |
| 345 | + self.norm(self.vals) |
| 346 | + 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])) |
| 404 | + |
| 405 | + |
| 406 | +@image_comparison(baseline_images=['test_offset_norm'], extensions=['png'], |
| 407 | + style='mpl20') |
| 408 | +def test_offset_norm_img(): |
| 409 | + x = np.linspace(-2, 7) |
| 410 | + y = np.linspace(-1*np.pi, np.pi) |
| 411 | + X, Y = np.meshgrid(x, y) |
| 412 | + Z = x * np.sin(Y)**2 |
| 413 | + |
| 414 | + fig, (ax1, ax2) = plt.subplots(ncols=2) |
| 415 | + cmap = plt.cm.coolwarm |
| 416 | + norm = mcolors.DivergingNorm(vmin=-2, vcenter=0, vmax=7) |
| 417 | + |
| 418 | + img1 = ax1.pcolormesh(Z, cmap=cmap, norm=None) |
| 419 | + cbar1 = fig.colorbar(img1, ax=ax1) |
| 420 | + |
| 421 | + img2 = ax2.pcolormesh(Z, cmap=cmap, norm=norm) |
| 422 | + cbar2 = fig.colorbar(img2, ax=ax2) |
| 423 | + |
| 424 | + |
224 | 425 | def test_SymLogNorm():
|
225 | 426 | """
|
226 | 427 | Test SymLogNorm behavior
|
|
0 commit comments