From 207edcfc5b9566162e92fcacb687626144b44db0 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 11:10:57 +0000 Subject: [PATCH 01/11] Remove local definition of putmask --- lib/matplotlib/cbook.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 32ddabbb5a73..4706255530e9 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2582,23 +2582,6 @@ def get_label(y, default_name): except AttributeError: return default_name -# Numpy > 1.6.x deprecates putmask in favor of the new copyto. -# So long as we support versions 1.6.x and less, we need the -# following local version of putmask. We choose to make a -# local version of putmask rather than of copyto because the -# latter includes more functionality than the former. Therefore -# it is easy to make a local version that gives full putmask -# behavior, but duplicating the full copyto behavior would be -# more difficult. - -try: - np.copyto -except AttributeError: - _putmask = np.putmask -else: - def _putmask(a, mask, values): - return np.copyto(a, values, where=mask) - _lockstr = """\ LOCKERROR: matplotlib is trying to acquire the lock {!r} From 8ce11f283467e4bcf9ddd80734981b38796fc082 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 11:15:14 +0000 Subject: [PATCH 02/11] Use minlength kwarg --- lib/matplotlib/tri/triinterpolate.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/tri/triinterpolate.py b/lib/matplotlib/tri/triinterpolate.py index f907e642f423..5dd75207975c 100644 --- a/lib/matplotlib/tri/triinterpolate.py +++ b/lib/matplotlib/tri/triinterpolate.py @@ -1265,12 +1265,9 @@ def dot(self, V): *V* dense vector of shape (self.m,) """ assert V.shape == (self.m,) - # For a more generic implementation we could use below kw argument - # minlength=self.m of bincount ; however: - # - it is new in numpy 1.6 - # - it is unecessary when each row have at least 1 entry in global - # matrix, which is the case here. - return np.bincount(self.rows, weights=self.vals*V[self.cols]) + return np.bincount(self.rows, + weights=self.vals*V[self.cols], + minlength=self.m) def compress_csc(self): """ From 79b9ee07924592cd9bc7854937b5be02bcdb4fe6 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 11:21:10 +0000 Subject: [PATCH 03/11] Simplifiy to_array for numpy >= 1.7 --- lib/matplotlib/category.py | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 5c252e2306fd..eedc57400c8d 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -14,23 +14,8 @@ import matplotlib.ticker as ticker -# pure hack for numpy 1.6 support -from distutils.version import LooseVersion - -NP_NEW = (LooseVersion(np.version.version) >= LooseVersion('1.7')) - - def to_array(data, maxlen=100): - if NP_NEW: - return np.array(data, dtype=np.unicode) - if cbook.is_scalar_or_string(data): - data = [data] - try: - vals = np.array(data, dtype=('|S', maxlen)) - except UnicodeEncodeError: - # this yields gibberish - vals = np.array([convert_to_string(d) for d in data]) - return vals + return np.array(data, dtype=np.unicode) class StrCategoryConverter(units.ConversionInterface): From 3a4213110754a2d61038402adaef850704bb587e Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 13:05:27 +0000 Subject: [PATCH 04/11] Replace _putmask calls with copyto calls --- lib/matplotlib/colors.py | 12 ++++++------ lib/matplotlib/quiver.py | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 8a7e99faa988..cfc1e0caa534 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -473,7 +473,7 @@ def __call__(self, X, alpha=None, bytes=False): # Treat 1.0 as slightly less than 1. vals = np.array([1, 0], dtype=xa.dtype) almost_one = np.nextafter(*vals) - cbook._putmask(xa, xa == 1.0, almost_one) + np.copyto(xa, almost_one, where=xa == 1.0) # The following clip is fast, and prevents possible # conversion of large positive values to negative integers. @@ -482,15 +482,15 @@ def __call__(self, X, alpha=None, bytes=False): # ensure that all 'under' values will still have negative # value after casting to int - cbook._putmask(xa, xa < 0.0, -1) + np.copyto(xa, -1, where=xa < 0.0) xa = xa.astype(int) # Set the over-range indices before the under-range; # otherwise the under-range values get converted to over-range. - cbook._putmask(xa, xa > self.N - 1, self._i_over) - cbook._putmask(xa, xa < 0, self._i_under) + np.copyto(xa, self._i_over, where=xa > self.N - 1) + np.copyto(xa, self._i_under, where=xa < 0) if mask_bad is not None: if mask_bad.shape == xa.shape: - cbook._putmask(xa, mask_bad, self._i_bad) + np.copyto(xa, self._i_bad, where=mask_bad) elif mask_bad: xa.fill(self._i_bad) if bytes: @@ -990,7 +990,7 @@ def __call__(self, value, clip=None): mask = (resdat <= 0) else: mask |= resdat <= 0 - cbook._putmask(resdat, mask, 1) + np.copyto(resdat, 1, where=mask) np.log(resdat, resdat) resdat -= np.log(vmin) resdat /= (np.log(vmax) - np.log(vmin)) diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index da4622f48bf3..5d4caab29918 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -707,8 +707,8 @@ def _h_arrows(self, length): Y0 = shrink * Y0[np.newaxis, :] short = np.repeat(length < minsh, 8, axis=1) # Now select X0, Y0 if short, otherwise X, Y - cbook._putmask(X, short, X0) - cbook._putmask(Y, short, Y0) + np.copyto(X, X0, where=short) + np.copyto(Y, Y0, where=short) if self.pivot == 'middle': X -= 0.5 * X[:, 3, np.newaxis] elif self.pivot == 'tip': @@ -728,8 +728,8 @@ def _h_arrows(self, length): X1 = np.repeat(x1[np.newaxis, :], N, axis=0) Y1 = np.repeat(y1[np.newaxis, :], N, axis=0) tooshort = np.repeat(tooshort, 8, 1) - cbook._putmask(X, tooshort, X1) - cbook._putmask(Y, tooshort, Y1) + np.copyto(X, X1, where=tooshort) + np.copyto(Y, Y1, where=tooshort) # Mask handling is deferred to the caller, _make_verts. return X, Y From 77dbcccf2a35ec4003ad29e0f25def8947b2fe72 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 13:15:53 +0000 Subject: [PATCH 05/11] Remove old to_array method --- lib/matplotlib/category.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index eedc57400c8d..e11b0323396e 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -14,10 +14,6 @@ import matplotlib.ticker as ticker -def to_array(data, maxlen=100): - return np.array(data, dtype=np.unicode) - - class StrCategoryConverter(units.ConversionInterface): @staticmethod def convert(value, unit, axis): @@ -29,7 +25,7 @@ def convert(value, unit, axis): if isinstance(value, six.string_types): return vmap[value] - vals = to_array(value) + vals = np.array(value, dtype=np.unicode) for lab, loc in vmap.items(): vals[vals == lab] = loc @@ -105,7 +101,7 @@ def update(self, new_data): def _set_seq_locs(self, data, value): # magic to make it work under np1.6 - strdata = to_array(data) + strdata = np.array(data, dtype=np.unicode) # np.unique makes dateframes work new_s = [d for d in np.unique(strdata) if d not in self.seq] for ns in new_s: From 74d2d333275540bb82cb6f1380bec431557108f8 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 13:28:24 +0000 Subject: [PATCH 06/11] Simplify convert_to_string for numpy >= 1.7 --- lib/matplotlib/category.py | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index e11b0323396e..343a5190776e 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -61,22 +61,7 @@ def __init__(self, seq): def convert_to_string(value): - """Helper function for numpy 1.6, can be replaced with - np.array(...,dtype=unicode) for all later versions of numpy""" - - if isinstance(value, six.string_types): - pass - elif np.isfinite(value): - value = np.asarray(value, dtype=str)[np.newaxis][0] - elif np.isnan(value): - value = 'nan' - elif np.isposinf(value): - value = 'inf' - elif np.isneginf(value): - value = '-inf' - else: - raise ValueError("Unconvertable {}".format(value)) - return value + return np.array(value, dtype=unicode) class UnitData(object): From fcef8fdbf39488b9c8c12209a2c204f4db975e85 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 14:07:21 +0000 Subject: [PATCH 07/11] Use numpy's unicode type in covert_to_string --- lib/matplotlib/category.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 343a5190776e..8c70f0c19587 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -61,7 +61,7 @@ def __init__(self, seq): def convert_to_string(value): - return np.array(value, dtype=unicode) + return np.array(value, dtype=np.unicode) class UnitData(object): From aa0e899ff12c7b8830297e9ac198f9d0b3f1b4cb Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 15:03:31 +0000 Subject: [PATCH 08/11] Fix convert_to_string method --- lib/matplotlib/category.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 8c70f0c19587..6dc4c8ea00bc 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -61,7 +61,7 @@ def __init__(self, seq): def convert_to_string(value): - return np.array(value, dtype=np.unicode) + return np.asarray(value, dtype=str)[np.newaxis][0] class UnitData(object): From 13c965db037151164039f5687af55f58d7a5d4fe Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 17:31:26 +0000 Subject: [PATCH 09/11] Remove convert_to_string method --- lib/matplotlib/category.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 6dc4c8ea00bc..4d812b568075 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -60,10 +60,6 @@ def __init__(self, seq): self.offset_string = '' -def convert_to_string(value): - return np.asarray(value, dtype=str)[np.newaxis][0] - - class UnitData(object): # debatable makes sense to special code missing values spdict = {'nan': -1.0, 'inf': -2.0, '-inf': -3.0} @@ -90,7 +86,7 @@ def _set_seq_locs(self, data, value): # np.unique makes dateframes work new_s = [d for d in np.unique(strdata) if d not in self.seq] for ns in new_s: - self.seq.append(convert_to_string(ns)) + self.seq.append(ns) if ns in UnitData.spdict.keys(): self.locs.append(UnitData.spdict[ns]) else: From 2983fbd9da0c95790073117f0b4344ad8243706d Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 17:31:55 +0000 Subject: [PATCH 10/11] Remove category string conversion test --- lib/matplotlib/tests/test_category.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index 031611cd03c7..66d69e0c7d88 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -13,17 +13,6 @@ import unittest -class TestConvertToString(object): - testdata = [("abc", "abc"), ("Здравствуйте мир", "Здравствуйте мир"), - ("3.14", 3.14), ("nan", np.nan), - ("inf", np.inf), ("-inf", -np.inf)] - ids = ["string", "unicode", "decimal", "nan", "posinf", "neginf", ] - - @pytest.mark.parametrize("expected, test", testdata, ids=ids) - def test_convert_to_string(self, expected, test): - assert expected == cat.convert_to_string(test) - - class TestUnitData(object): testdata = [("hello world", ["hello world"], [0]), ("Здравствуйте мир", ["Здравствуйте мир"], [0]), @@ -157,7 +146,6 @@ def axis_test(self, axis, ticks, labels, unit_data): @cleanup def test_plot_unicode(self): - # Image test would fail on numpy 1.6 words = ['Здравствуйте', 'привет'] locs = [0.0, 1.0] unit_data = MockUnitData(zip(words, locs)) From 430f789419979f4a16a51f22f9015d2c4e6799f7 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 21 Nov 2016 17:34:24 +0000 Subject: [PATCH 11/11] Remove old numpy 1.6 comment --- lib/matplotlib/category.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 4d812b568075..5a425f890e2d 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -81,7 +81,6 @@ def update(self, new_data): self._set_seq_locs(new_data, value) def _set_seq_locs(self, data, value): - # magic to make it work under np1.6 strdata = np.array(data, dtype=np.unicode) # np.unique makes dateframes work new_s = [d for d in np.unique(strdata) if d not in self.seq]