From c98a90d192bced1671024098726988e5d9b0c8ac Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 30 Jan 2015 11:02:18 -0500 Subject: [PATCH 1/3] BUG : fix PowerNorm with scalars closes #4053 --- lib/matplotlib/colors.py | 4 +++- lib/matplotlib/tests/test_colors.py | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 5d165b8f69a0..d1e6f84b5644 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -1168,6 +1168,7 @@ def __call__(self, value, clip=None): elif vmin == vmax: result.fill(0) else: + res_mask = result.data < 0 if clip: mask = ma.getmask(result) val = ma.array(np.clip(result.filled(vmax), vmin, vmax), @@ -1176,8 +1177,9 @@ def __call__(self, value, clip=None): resdat -= vmin np.power(resdat, gamma, resdat) resdat /= (vmax - vmin) ** gamma + result = np.ma.array(resdat, mask=result.mask, copy=False) - result[value < 0] = 0 + result[res_mask] = 0 if is_scalar: result = result[0] return result diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 38f013cc9407..ba52bc374dc7 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -3,7 +3,7 @@ import six -from nose.tools import assert_raises +from nose.tools import assert_raises, assert_equal import numpy as np from numpy.testing.utils import assert_array_equal, assert_array_almost_equal @@ -62,9 +62,11 @@ def test_PowerNorm(): assert_array_almost_equal(norm(a), pnorm(a)) a = np.array([-0.5, 0, 2, 4, 8], dtype=np.float) - expected = [0, 0, 1./16, 1./4, 1] + expected = [0, 0, 1/16, 1/4, 1] pnorm = mcolors.PowerNorm(2, vmin=0, vmax=8) assert_array_almost_equal(pnorm(a), expected) + assert_equal(pnorm(a[0]), expected[0]) + assert_equal(pnorm(a[2]), expected[2]) assert_array_almost_equal(a[1:], pnorm.inverse(pnorm(a))[1:]) From 571091baf8a2a6fa85c41d6302d48b4c3a2018c6 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 1 Feb 2015 11:42:43 +0000 Subject: [PATCH 2/3] Add tests for Clip of powernorm. These tests are currently failing and should be fixed by the next commit. --- lib/matplotlib/tests/test_colors.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index ba52bc374dc7..eb7c24725f7a 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -69,6 +69,22 @@ def test_PowerNorm(): assert_equal(pnorm(a[2]), expected[2]) assert_array_almost_equal(a[1:], pnorm.inverse(pnorm(a))[1:]) + # Clip = True + a = np.array([-0.5, 0, 1, 8, 16], dtype=np.float) + expected = [0, 0, 0, 1, 1] + pnorm = mcolors.PowerNorm(2, vmin=2, vmax=8, clip=True) + assert_array_almost_equal(pnorm(a), expected) + assert_equal(pnorm(a[0]), expected[0]) + assert_equal(pnorm(a[-1]), expected[-1]) + + # Clip = True at call time + a = np.array([-0.5, 0, 1, 8, 16], dtype=np.float) + expected = [0, 0, 0, 1, 1] + pnorm = mcolors.PowerNorm(2, vmin=2, vmax=8, clip=False) + assert_array_almost_equal(pnorm(a, clip=True), expected) + assert_equal(pnorm(a[0], clip=True), expected[0]) + assert_equal(pnorm(a[-1], clip=True), expected[-1]) + def test_Normalize(): norm = mcolors.Normalize() From e2c421933b701a142db7a3f00403a09e4b256343 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 1 Feb 2015 11:50:23 +0000 Subject: [PATCH 3/3] Correct issue with Powernorm Clip --- lib/matplotlib/colors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index d1e6f84b5644..47844f5bd465 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -1171,8 +1171,8 @@ def __call__(self, value, clip=None): res_mask = result.data < 0 if clip: mask = ma.getmask(result) - val = ma.array(np.clip(result.filled(vmax), vmin, vmax), - mask=mask) + result = ma.array(np.clip(result.filled(vmax), vmin, vmax), + mask=mask) resdat = result.data resdat -= vmin np.power(resdat, gamma, resdat)