Skip to content

Remove some numpy 1.6 workarounds #7494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 24, 2016
45 changes: 3 additions & 42 deletions lib/matplotlib/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@
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


class StrCategoryConverter(units.ConversionInterface):
@staticmethod
def convert(value, unit, axis):
Expand All @@ -44,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

Expand Down Expand Up @@ -79,25 +60,6 @@ def __init__(self, seq):
self.offset_string = ''


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


class UnitData(object):
# debatable makes sense to special code missing values
spdict = {'nan': -1.0, 'inf': -2.0, '-inf': -3.0}
Expand All @@ -119,12 +81,11 @@ 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 = 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:
self.seq.append(convert_to_string(ns))
self.seq.append(ns)
if ns in UnitData.spdict.keys():
self.locs.append(UnitData.spdict[ns])
else:
Expand Down
17 changes: 0 additions & 17 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 6 additions & 6 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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:
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/quiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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

Expand Down
12 changes: 0 additions & 12 deletions lib/matplotlib/tests/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 3 additions & 6 deletions lib/matplotlib/tri/triinterpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy