Skip to content

Commit 9bcbfc4

Browse files
authored
Merge pull request #7494 from dstansby/numpy1.6-workarounds
MNT: Remove some numpy 1.6 workarounds
2 parents 5d1cd3c + 430f789 commit 9bcbfc4

File tree

6 files changed

+16
-87
lines changed

6 files changed

+16
-87
lines changed

lib/matplotlib/category.py

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,6 @@
1414
import matplotlib.ticker as ticker
1515

1616

17-
# pure hack for numpy 1.6 support
18-
from distutils.version import LooseVersion
19-
20-
NP_NEW = (LooseVersion(np.version.version) >= LooseVersion('1.7'))
21-
22-
23-
def to_array(data, maxlen=100):
24-
if NP_NEW:
25-
return np.array(data, dtype=np.unicode)
26-
if cbook.is_scalar_or_string(data):
27-
data = [data]
28-
try:
29-
vals = np.array(data, dtype=('|S', maxlen))
30-
except UnicodeEncodeError:
31-
# this yields gibberish
32-
vals = np.array([convert_to_string(d) for d in data])
33-
return vals
34-
35-
3617
class StrCategoryConverter(units.ConversionInterface):
3718
@staticmethod
3819
def convert(value, unit, axis):
@@ -44,7 +25,7 @@ def convert(value, unit, axis):
4425
if isinstance(value, six.string_types):
4526
return vmap[value]
4627

47-
vals = to_array(value)
28+
vals = np.array(value, dtype=np.unicode)
4829
for lab, loc in vmap.items():
4930
vals[vals == lab] = loc
5031

@@ -79,25 +60,6 @@ def __init__(self, seq):
7960
self.offset_string = ''
8061

8162

82-
def convert_to_string(value):
83-
"""Helper function for numpy 1.6, can be replaced with
84-
np.array(...,dtype=unicode) for all later versions of numpy"""
85-
86-
if isinstance(value, six.string_types):
87-
pass
88-
elif np.isfinite(value):
89-
value = np.asarray(value, dtype=str)[np.newaxis][0]
90-
elif np.isnan(value):
91-
value = 'nan'
92-
elif np.isposinf(value):
93-
value = 'inf'
94-
elif np.isneginf(value):
95-
value = '-inf'
96-
else:
97-
raise ValueError("Unconvertable {}".format(value))
98-
return value
99-
100-
10163
class UnitData(object):
10264
# debatable makes sense to special code missing values
10365
spdict = {'nan': -1.0, 'inf': -2.0, '-inf': -3.0}
@@ -119,12 +81,11 @@ def update(self, new_data):
11981
self._set_seq_locs(new_data, value)
12082

12183
def _set_seq_locs(self, data, value):
122-
# magic to make it work under np1.6
123-
strdata = to_array(data)
84+
strdata = np.array(data, dtype=np.unicode)
12485
# np.unique makes dateframes work
12586
new_s = [d for d in np.unique(strdata) if d not in self.seq]
12687
for ns in new_s:
127-
self.seq.append(convert_to_string(ns))
88+
self.seq.append(ns)
12889
if ns in UnitData.spdict.keys():
12990
self.locs.append(UnitData.spdict[ns])
13091
else:

lib/matplotlib/cbook.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,23 +2582,6 @@ def get_label(y, default_name):
25822582
except AttributeError:
25832583
return default_name
25842584

2585-
# Numpy > 1.6.x deprecates putmask in favor of the new copyto.
2586-
# So long as we support versions 1.6.x and less, we need the
2587-
# following local version of putmask. We choose to make a
2588-
# local version of putmask rather than of copyto because the
2589-
# latter includes more functionality than the former. Therefore
2590-
# it is easy to make a local version that gives full putmask
2591-
# behavior, but duplicating the full copyto behavior would be
2592-
# more difficult.
2593-
2594-
try:
2595-
np.copyto
2596-
except AttributeError:
2597-
_putmask = np.putmask
2598-
else:
2599-
def _putmask(a, mask, values):
2600-
return np.copyto(a, values, where=mask)
2601-
26022585
_lockstr = """\
26032586
LOCKERROR: matplotlib is trying to acquire the lock
26042587
{!r}

lib/matplotlib/colors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def __call__(self, X, alpha=None, bytes=False):
473473
# Treat 1.0 as slightly less than 1.
474474
vals = np.array([1, 0], dtype=xa.dtype)
475475
almost_one = np.nextafter(*vals)
476-
cbook._putmask(xa, xa == 1.0, almost_one)
476+
np.copyto(xa, almost_one, where=xa == 1.0)
477477
# The following clip is fast, and prevents possible
478478
# conversion of large positive values to negative integers.
479479

@@ -482,15 +482,15 @@ def __call__(self, X, alpha=None, bytes=False):
482482

483483
# ensure that all 'under' values will still have negative
484484
# value after casting to int
485-
cbook._putmask(xa, xa < 0.0, -1)
485+
np.copyto(xa, -1, where=xa < 0.0)
486486
xa = xa.astype(int)
487487
# Set the over-range indices before the under-range;
488488
# otherwise the under-range values get converted to over-range.
489-
cbook._putmask(xa, xa > self.N - 1, self._i_over)
490-
cbook._putmask(xa, xa < 0, self._i_under)
489+
np.copyto(xa, self._i_over, where=xa > self.N - 1)
490+
np.copyto(xa, self._i_under, where=xa < 0)
491491
if mask_bad is not None:
492492
if mask_bad.shape == xa.shape:
493-
cbook._putmask(xa, mask_bad, self._i_bad)
493+
np.copyto(xa, self._i_bad, where=mask_bad)
494494
elif mask_bad:
495495
xa.fill(self._i_bad)
496496
if bytes:
@@ -990,7 +990,7 @@ def __call__(self, value, clip=None):
990990
mask = (resdat <= 0)
991991
else:
992992
mask |= resdat <= 0
993-
cbook._putmask(resdat, mask, 1)
993+
np.copyto(resdat, 1, where=mask)
994994
np.log(resdat, resdat)
995995
resdat -= np.log(vmin)
996996
resdat /= (np.log(vmax) - np.log(vmin))

lib/matplotlib/quiver.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,8 @@ def _h_arrows(self, length):
707707
Y0 = shrink * Y0[np.newaxis, :]
708708
short = np.repeat(length < minsh, 8, axis=1)
709709
# Now select X0, Y0 if short, otherwise X, Y
710-
cbook._putmask(X, short, X0)
711-
cbook._putmask(Y, short, Y0)
710+
np.copyto(X, X0, where=short)
711+
np.copyto(Y, Y0, where=short)
712712
if self.pivot == 'middle':
713713
X -= 0.5 * X[:, 3, np.newaxis]
714714
elif self.pivot == 'tip':
@@ -728,8 +728,8 @@ def _h_arrows(self, length):
728728
X1 = np.repeat(x1[np.newaxis, :], N, axis=0)
729729
Y1 = np.repeat(y1[np.newaxis, :], N, axis=0)
730730
tooshort = np.repeat(tooshort, 8, 1)
731-
cbook._putmask(X, tooshort, X1)
732-
cbook._putmask(Y, tooshort, Y1)
731+
np.copyto(X, X1, where=tooshort)
732+
np.copyto(Y, Y1, where=tooshort)
733733
# Mask handling is deferred to the caller, _make_verts.
734734
return X, Y
735735

lib/matplotlib/tests/test_category.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@
1313
import unittest
1414

1515

16-
class TestConvertToString(object):
17-
testdata = [("abc", "abc"), ("Здравствуйте мир", "Здравствуйте мир"),
18-
("3.14", 3.14), ("nan", np.nan),
19-
("inf", np.inf), ("-inf", -np.inf)]
20-
ids = ["string", "unicode", "decimal", "nan", "posinf", "neginf", ]
21-
22-
@pytest.mark.parametrize("expected, test", testdata, ids=ids)
23-
def test_convert_to_string(self, expected, test):
24-
assert expected == cat.convert_to_string(test)
25-
26-
2716
class TestUnitData(object):
2817
testdata = [("hello world", ["hello world"], [0]),
2918
("Здравствуйте мир", ["Здравствуйте мир"], [0]),
@@ -157,7 +146,6 @@ def axis_test(self, axis, ticks, labels, unit_data):
157146

158147
@cleanup
159148
def test_plot_unicode(self):
160-
# Image test would fail on numpy 1.6
161149
words = ['Здравствуйте', 'привет']
162150
locs = [0.0, 1.0]
163151
unit_data = MockUnitData(zip(words, locs))

lib/matplotlib/tri/triinterpolate.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,12 +1265,9 @@ def dot(self, V):
12651265
*V* dense vector of shape (self.m,)
12661266
"""
12671267
assert V.shape == (self.m,)
1268-
# For a more generic implementation we could use below kw argument
1269-
# minlength=self.m of bincount ; however:
1270-
# - it is new in numpy 1.6
1271-
# - it is unecessary when each row have at least 1 entry in global
1272-
# matrix, which is the case here.
1273-
return np.bincount(self.rows, weights=self.vals*V[self.cols])
1268+
return np.bincount(self.rows,
1269+
weights=self.vals*V[self.cols],
1270+
minlength=self.m)
12741271

12751272
def compress_csc(self):
12761273
"""

0 commit comments

Comments
 (0)