From 3ed55f480ccc62eba4177d21f7a0cf065eed9bd5 Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Fri, 17 Mar 2017 10:01:36 +0530 Subject: [PATCH 1/9] Added __copy__ to Colormap --- lib/matplotlib/colors.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 8b0e8350d6af..2a9e339bd779 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -529,7 +529,18 @@ def __call__(self, X, alpha=None, bytes=False): if vtype == 'scalar': rgba = tuple(rgba[0, :]) return rgba - + + def __copy__(self): + """Create new object with the same class and update attributes + If object is initialized, copy the elements of _lut list + """ + cls = self.__class__ + newCMapObj = cls.__new__(cls) + newCMapObj.__dict__.update(self.__dict__) + if self._isinit: + newCMapObj._lut = np.copy(self._lut) + return newCMapObj + def set_bad(self, color='k', alpha=None): """Set color to be used for masked values. """ From 50030479fa726d64da92eeedc5c27e520fe4146c Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Fri, 17 Mar 2017 10:05:16 +0530 Subject: [PATCH 2/9] Updated __copy__ in Colormap --- lib/matplotlib/colors.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 2a9e339bd779..17933caa4893 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -532,14 +532,15 @@ def __call__(self, X, alpha=None, bytes=False): def __copy__(self): """Create new object with the same class and update attributes - If object is initialized, copy the elements of _lut list - """ - cls = self.__class__ - newCMapObj = cls.__new__(cls) - newCMapObj.__dict__.update(self.__dict__) - if self._isinit: - newCMapObj._lut = np.copy(self._lut) - return newCMapObj + If object is initialized, copy the elements of _lut list + """ + cls = self.__class__ + newCMapObj = cls.__new__(cls) + newCMapObj.__dict__.update(self.__dict__) + if self._isinit: + newCMapObj._lut = np.copy(self._lut) + return newCMapObj + def set_bad(self, color='k', alpha=None): """Set color to be used for masked values. From 82cccd003e1a9eff6fcca7de9f24fcd44a2b4657 Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Fri, 17 Mar 2017 10:09:41 +0530 Subject: [PATCH 3/9] Fixed __copy__ indentation --- lib/matplotlib/colors.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 17933caa4893..253b4f975d3e 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -532,15 +532,14 @@ def __call__(self, X, alpha=None, bytes=False): def __copy__(self): """Create new object with the same class and update attributes - If object is initialized, copy the elements of _lut list - """ - cls = self.__class__ - newCMapObj = cls.__new__(cls) - newCMapObj.__dict__.update(self.__dict__) - if self._isinit: - newCMapObj._lut = np.copy(self._lut) - return newCMapObj - + If object is initialized, copy the elements of _lut list + """ + cls = self.__class__ + newCMapObj = cls.__new__(cls) + newCMapObj.__dict__.update(self.__dict__) + if self._isinit: + newCMapObj._lut = np.copy(self._lut) + return newCMapObj def set_bad(self, color='k', alpha=None): """Set color to be used for masked values. From 3ece4cb11f6bfbb39b96e55509e647bb95f6f48a Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Sat, 18 Mar 2017 01:24:56 +0530 Subject: [PATCH 4/9] Updated __copy__ indentation --- lib/matplotlib/colors.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 253b4f975d3e..627d8b96c54e 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -532,14 +532,14 @@ def __call__(self, X, alpha=None, bytes=False): def __copy__(self): """Create new object with the same class and update attributes - If object is initialized, copy the elements of _lut list - """ - cls = self.__class__ - newCMapObj = cls.__new__(cls) - newCMapObj.__dict__.update(self.__dict__) - if self._isinit: - newCMapObj._lut = np.copy(self._lut) - return newCMapObj + If object is initialized, copy the elements of _lut list + """ + cls = self.__class__ + newCMapObj = cls.__new__(cls) + newCMapObj.__dict__.update(self.__dict__) + if self._isinit: + newCMapObj._lut = np.copy(self._lut) + return newCMapObj def set_bad(self, color='k', alpha=None): """Set color to be used for masked values. From 7e8a9403903daed2b0cf8f8de1d70fb9a39c740d Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Sun, 19 Mar 2017 11:15:26 +0530 Subject: [PATCH 5/9] Added tests for Colormap copy --- lib/matplotlib/tests/test_colors.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 0d1243c94552..3b499c213c1e 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1,6 +1,7 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) +import copy import six import itertools import warnings @@ -21,7 +22,19 @@ import matplotlib.pyplot as plt from matplotlib.testing.decorators import image_comparison - +def test_copy(): + cm = plt.cm.Reds + cm([-1, 0, .5, np.nan, np.inf]) + cm.set_bad('y') + cm2 = copy.copy(cm) + cm2.set_bad('b') + cm.set_under('k') + cm2.set_under('r') + cm.set_over('c') + cm2.set_over('m') + + assert_equal(np.not_equal(cm._lut, cm2._lut), True) + def test_resample(): """ Github issue #6025 pointed to incorrect ListedColormap._resample; From 5cafb57499aa40de9f81a67a5599bdb7da97deeb Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Sun, 19 Mar 2017 16:18:11 +0530 Subject: [PATCH 6/9] Update test_colors.py --- lib/matplotlib/tests/test_colors.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 3b499c213c1e..d71efca014c2 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -21,19 +21,6 @@ import matplotlib.cbook as cbook import matplotlib.pyplot as plt from matplotlib.testing.decorators import image_comparison - -def test_copy(): - cm = plt.cm.Reds - cm([-1, 0, .5, np.nan, np.inf]) - cm.set_bad('y') - cm2 = copy.copy(cm) - cm2.set_bad('b') - cm.set_under('k') - cm2.set_under('r') - cm.set_over('c') - cm2.set_over('m') - - assert_equal(np.not_equal(cm._lut, cm2._lut), True) def test_resample(): """ @@ -56,6 +43,18 @@ def test_resample(): assert_array_almost_equal(lsc3([0, 0.5, 1]), expected) assert_array_almost_equal(lc3([0, 0.5, 1]), expected) +def test_colormap_copy(): + cm = plt.cm.Reds + cm([-1, 0, .5, np.nan, np.inf]) + cm.set_bad('y') + cm2 = copy.copy(cm) + cm2.set_bad('b') + cm.set_under('k') + cm2.set_under('r') + cm.set_over('c') + cm2.set_over('m') + assert_equal(np.not_equal(cm._lut, cm2._lut), True) + def test_colormap_endian(): """ From 3e6a6899035c25ef361a8c093171e89bdaa3d7ac Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Sun, 19 Mar 2017 16:34:56 +0530 Subject: [PATCH 7/9] Fixed copy test function --- lib/matplotlib/tests/test_colors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index d71efca014c2..7ac1d93c7f57 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -52,8 +52,8 @@ def test_colormap_copy(): cm.set_under('k') cm2.set_under('r') cm.set_over('c') - cm2.set_over('m') - assert_equal(np.not_equal(cm._lut, cm2._lut), True) + cm2.set_over('m') + assert_equal(np.array_equal(cm._lut, cm2._lut), False) def test_colormap_endian(): From f7eadbf45fd37e3fd8afb37db45c7ff7c50db827 Mon Sep 17 00:00:00 2001 From: Vidur Satija Date: Sun, 19 Mar 2017 18:11:19 +0530 Subject: [PATCH 8/9] Fixed indentation --- lib/matplotlib/tests/test_colors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 7ac1d93c7f57..5ad8dd0d2600 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -21,7 +21,8 @@ import matplotlib.cbook as cbook import matplotlib.pyplot as plt from matplotlib.testing.decorators import image_comparison - + + def test_resample(): """ Github issue #6025 pointed to incorrect ListedColormap._resample; @@ -43,6 +44,7 @@ def test_resample(): assert_array_almost_equal(lsc3([0, 0.5, 1]), expected) assert_array_almost_equal(lc3([0, 0.5, 1]), expected) + def test_colormap_copy(): cm = plt.cm.Reds cm([-1, 0, .5, np.nan, np.inf]) From eeae70da0f74dd04e88385da6701c69e809bf2ed Mon Sep 17 00:00:00 2001 From: vidursatija Date: Fri, 24 Mar 2017 21:04:53 +0530 Subject: [PATCH 9/9] Changed copy test --- lib/matplotlib/tests/test_colors.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 5ad8dd0d2600..5cfc5a2a3c42 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -55,7 +55,20 @@ def test_colormap_copy(): cm2.set_under('r') cm.set_over('c') cm2.set_over('m') - assert_equal(np.array_equal(cm._lut, cm2._lut), False) + + expected1 = plt.cm.Reds + expected2 = plt.cm.Reds + expected1([-1, 0, .5, np.nan, np.inf]) + expected2([-1, 0, .5, np.nan, np.inf]) + expected1.set_bad('y') + expected2.set_bad('b') + expected1.set_under('k') + expected2.set_under('r') + expected1.set_over('c') + expected2.set_over('m') + + assert_array_equal(cm._lut, expected1._lut) + assert_array_equal(cm2._lut, expected2._lut) def test_colormap_endian():