Skip to content
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

Some general cleanups #6573

Merged
merged 3 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
cycler)

import numpy
import numpy.ma
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What effect does this have?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that everyone can directly use np.ma.<xyz> without importing numpy.ma.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it doesn't do anything at all. It's superfluous.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, removed.

from matplotlib.externals.six.moves.urllib.request import urlopen
from matplotlib.externals.six.moves import reload_module as reload

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5387,7 +5387,7 @@ def pcolor(self, *args, **kwargs):

if t and any(t.contains_branch_seperately(self.transData)):
trans_to_data = t - self.transData
pts = np.vstack([x, y]).T.astype(np.float)
pts = np.vstack([x, y]).T.astype(float)
transformed_pts = trans_to_data.transform(pts)
x = transformed_pts[..., 0]
y = transformed_pts[..., 1]
Expand Down Expand Up @@ -5537,7 +5537,7 @@ def pcolormesh(self, *args, **kwargs):

if t and any(t.contains_branch_seperately(self.transData)):
trans_to_data = t - self.transData
pts = np.vstack([X, Y]).T.astype(np.float)
pts = np.vstack([X, Y]).T.astype(float)
transformed_pts = trans_to_data.transform(pts)
X = transformed_pts[..., 0]
Y = transformed_pts[..., 1]
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from operator import itemgetter

import numpy as np
from numpy import ma

import matplotlib

Expand Down Expand Up @@ -1917,7 +1916,7 @@ def update_datalim(self, xys, updatex=True, updatey=True):

if iterable(xys) and not len(xys):
return
if not ma.isMaskedArray(xys):
if not isinstance(xys, np.ma.MaskedArray):
xys = np.asarray(xys)
self.dataLim.update_from_data_xy(xys, self.ignore_existing_data_limits,
updatex=updatex, updatey=updatey)
Expand Down
9 changes: 1 addition & 8 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
def _fn_name(): return sys._getframe(1).f_code.co_name
import io

try:
from hashlib import md5
except ImportError:
from md5 import md5 #Deprecated in 2.5
from hashlib import md5

from tempfile import mkstemp
from matplotlib import verbose, __version__, rcParams, checkdep_ghostscript
Expand Down Expand Up @@ -44,10 +41,6 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
import numpy as np
import binascii
import re
try:
set
except NameError:
from sets import Set as set

if sys.platform.startswith('win'): cmd_split = '&'
else: cmd_split = ';'
Expand Down
15 changes: 7 additions & 8 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from weakref import ref, WeakKeyDictionary

import numpy as np
import numpy.ma as ma


class MatplotlibDeprecationWarning(UserWarning):
Expand Down Expand Up @@ -677,7 +676,7 @@ def is_string_like(obj):
if isinstance(obj, six.string_types):
return True
# numpy strings are subclass of str, ma strings are not
if ma.isMaskedArray(obj):
if isinstance(obj, np.ma.MaskedArray):
if obj.ndim == 0 and obj.dtype.kind in 'SU':
return True
else:
Expand Down Expand Up @@ -1728,7 +1727,7 @@ def delete_masked_points(*args):
for i, x in enumerate(args):
if (not is_string_like(x)) and iterable(x) and len(x) == nrecs:
seqlist[i] = True
if ma.isMA(x):
if isinstance(x, np.ma.MaskedArray):
if x.ndim > 1:
raise ValueError("Masked arrays must be 1-D")
else:
Expand All @@ -1739,8 +1738,8 @@ def delete_masked_points(*args):
if seqlist[i]:
if x.ndim > 1:
continue # Don't try to get nan locations unless 1-D.
if ma.isMA(x):
masks.append(~ma.getmaskarray(x)) # invert the mask
if isinstance(x, np.ma.MaskedArray):
masks.append(~np.ma.getmaskarray(x)) # invert the mask
xd = x.data
else:
xd = x
Expand All @@ -1758,7 +1757,7 @@ def delete_masked_points(*args):
if seqlist[i]:
margs[i] = x.take(igood, axis=0)
for i, x in enumerate(margs):
if seqlist[i] and ma.isMA(x):
if seqlist[i] and isinstance(x, np.ma.MaskedArray):
margs[i] = x.filled()
return margs

Expand Down Expand Up @@ -2344,7 +2343,7 @@ def pts_to_poststep(x, *args):
# do normalization
vertices = _step_validation(x, *args)
# create the output array
steps = ma.zeros((vertices.shape[0], 2 * len(x) - 1), np.float)
steps = np.zeros((vertices.shape[0], 2 * len(x) - 1), np.float)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These got missed in the np.float -> float conversion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also not the masked array zeros, if that makes a difference.

Copy link
Contributor Author

@anntzer anntzer Jun 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's quite a few np.float left; this one is safe to replace but I'd need to do some more research to figure out how this changes calls to np.issubdtype and others.
WRT to np.zeros vs ma.zeros, see below.

# do the to step conversion logic
steps[0, ::2], steps[0, 1:-1:2] = vertices[0, :], vertices[0, 1:]
steps[1:, 0::2], steps[1:, 1::2] = vertices[1:, :], vertices[1:, :-1]
Expand Down Expand Up @@ -2385,7 +2384,7 @@ def pts_to_midstep(x, *args):
# do normalization
vertices = _step_validation(x, *args)
# create the output array
steps = ma.zeros((vertices.shape[0], 2 * len(x)), np.float)
steps = np.zeros((vertices.shape[0], 2 * len(x)), np.float)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the similar change in pts_to_poststep is changing the behavior, and I suspect it will break something. _step_validation accepts subclasses including masked arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pts_to_prestep was already using np.zeros so if there was a bug it was already there, if any; but in fact Line2D handles replacing masked arrays by nan-filled arrays (see Line2D.recache).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change on this line is ma -> np, so I don't understand what you mean, unless you are referring to somewhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this patch, pts_to_prestep was using np.zeros whereas pts_to_midstep and pts_to_poststep were using ma.zeros, so the behavior was inconsistent to start with. I guess that the use of ma.zeros was to handle the hypothetical case where a masked array would be passed to a step-plotting function, but Line2D.recache (which ultimately dispatches to pts_to_*step) already replaces masked arrays by nan-filled regular arrays, so there's no reason to return a masked array here.

steps[0, 1:-1:2] = 0.5 * (vertices[0, :-1] + vertices[0, 1:])
steps[0, 2::2] = 0.5 * (vertices[0, :-1] + vertices[0, 1:])
steps[0, 0] = vertices[0, 0]
Expand Down
17 changes: 8 additions & 9 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from matplotlib.externals.six.moves import zip
import warnings
import numpy as np
import numpy.ma as ma
import matplotlib as mpl
import matplotlib.cbook as cbook
import matplotlib.colors as mcolors
Expand Down Expand Up @@ -199,7 +198,7 @@ def get_datalim(self, transData):
transOffset = transOffset.get_affine()

offsets = np.asanyarray(offsets, np.float_)
if np.ma.isMaskedArray(offsets):
if isinstance(offsets, np.ma.MaskedArray):
offsets = offsets.filled(np.nan)
# get_path_collection_extents handles nan but not masked arrays
offsets.shape = (-1, 2) # Make it Nx2
Expand Down Expand Up @@ -252,7 +251,7 @@ def _prepare_points(self):
# This might have changed an ndarray into a masked array.
transOffset = transOffset.get_affine()

if np.ma.isMaskedArray(offsets):
if isinstance(offsets, np.ma.MaskedArray):
offsets = offsets.filled(np.nan)
# Changing from a masked array to nan-filled ndarray
# is probably most efficient at this point.
Expand Down Expand Up @@ -875,14 +874,14 @@ def __init__(self, verts, sizes=None, closed=True, **kwargs):

def set_verts(self, verts, closed=True):
'''This allows one to delay initialization of the vertices.'''
if np.ma.isMaskedArray(verts):
verts = verts.astype(np.float_).filled(np.nan)
if isinstance(verts, np.ma.MaskedArray):
verts = verts.astype(float).filled(np.nan)
# This is much faster than having Path do it one at a time.
if closed:
self._paths = []
for xy in verts:
if len(xy):
if np.ma.isMaskedArray(xy):
if isinstance(xy, np.ma.MaskedArray):
xy = np.ma.concatenate([xy, xy[0:1]])
else:
xy = np.asarray(xy)
Expand Down Expand Up @@ -1162,7 +1161,7 @@ def set_segments(self, segments):
_segments = []

for seg in segments:
if not np.ma.isMaskedArray(seg):
if not isinstance(seg, np.ma.MaskedArray):
seg = np.asarray(seg, np.float_)
_segments.append(seg)

Expand Down Expand Up @@ -1779,7 +1778,7 @@ def convert_mesh_to_paths(meshWidth, meshHeight, coordinates):
"""
Path = mpath.Path

if ma.isMaskedArray(coordinates):
if isinstance(coordinates, np.ma.MaskedArray):
c = coordinates.data
else:
c = coordinates
Expand All @@ -1800,7 +1799,7 @@ def convert_mesh_to_triangles(self, meshWidth, meshHeight, coordinates):
with its own color. This is useful for experiments using
`draw_qouraud_triangle`.
"""
if ma.isMaskedArray(coordinates):
if isinstance(coordinates, np.ma.MaskedArray):
p = coordinates.data
else:
p = coordinates
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,12 +891,12 @@ def process_value(value):
# (list, tuple, deque, ndarray, Series, ...)
result = result.copy()
elif result.dtype.itemsize > 2:
result = result.astype(np.float)
result = result.astype(float)
else:
result = result.astype(np.float32)
else:
is_scalar = True
result = ma.array([value]).astype(np.float)
result = ma.array([value]).astype(float)
Copy link
Member

@efiring efiring Jun 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are standardizing on np.ma, here is another opportunity for it. I used to use ma, but now I always use np.ma. (But after looking at things farther along, I am wondering whether the extra verbosity does more harm than good.)

return result, is_scalar

def __call__(self, value, clip=None):
Expand Down Expand Up @@ -1121,7 +1121,7 @@ def _transform_vmin_vmax(self):
Calculates vmin and vmax in the transformed system.
"""
vmin, vmax = self.vmin, self.vmax
arr = np.array([vmax, vmin]).astype(np.float)
arr = np.array([vmax, vmin]).astype(float)
self._upper, self._lower = self._transform(arr)

def inverse(self, value):
Expand Down
4 changes: 0 additions & 4 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@

import json
import os, sys, warnings
try:
set
except NameError:
from sets import Set as set
from collections import Iterable
import matplotlib
from matplotlib import afm
Expand Down
9 changes: 4 additions & 5 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import warnings

import numpy as np
from numpy import ma
from matplotlib import verbose
from . import artist, colors as mcolors
from .artist import Artist
Expand Down Expand Up @@ -628,17 +627,17 @@ def recache_always(self):
def recache(self, always=False):
if always or self._invalidx:
xconv = self.convert_xunits(self._xorig)
if ma.isMaskedArray(self._xorig):
x = ma.asarray(xconv, np.float_).filled(np.nan)
if isinstance(self._xorig, np.ma.MaskedArray):
x = np.ma.asarray(xconv, np.float_).filled(np.nan)
else:
x = np.asarray(xconv, np.float_)
x = x.ravel()
else:
x = self._x
if always or self._invalidy:
yconv = self.convert_yunits(self._yorig)
if ma.isMaskedArray(self._yorig):
y = ma.asarray(yconv, np.float_).filled(np.nan)
if isinstance(self._yorig, np.ma.MaskedArray):
y = np.ma.asarray(yconv, np.float_).filled(np.nan)
else:
y = np.asarray(yconv, np.float_)
y = y.ravel()
Expand Down
4 changes: 0 additions & 4 deletions lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
import os, sys
from matplotlib.externals.six import unichr
from math import ceil
try:
set
except NameError:
from sets import Set as set
import unicodedata
from warnings import warn

Expand Down
22 changes: 8 additions & 14 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@
from matplotlib.path import Path
import math

ma = np.ma

if six.PY3:
long = int
Expand Down Expand Up @@ -1570,7 +1569,7 @@ def entropy(y, bins):
Sanalytic = 0.5 * ( 1.0 + log(2*pi*sigma**2.0) )
"""
n, bins = np.histogram(y, bins)
n = n.astype(np.float_)
n = n.astype(float)

n = np.take(n, np.nonzero(n)[0]) # get the positive

Expand Down Expand Up @@ -2980,13 +2979,7 @@ def get_converters(reader, comments):
return None

if use_mrecords and np.any(rowmasks):
try:
from numpy.ma import mrecords
except ImportError:
raise RuntimeError('numpy 1.05 or later is required for masked '
'array support')
else:
r = mrecords.fromrecords(rows, names=names, mask=rowmasks)
r = np.ma.mrecords.fromrecords(rows, names=names, mask=rowmasks)
else:
r = np.rec.fromrecords(rows, names=names)
return r
Expand Down Expand Up @@ -3824,8 +3817,8 @@ def poly_below(xmin, xs, ys):
xv, yv = poly_below(0, x, y)
ax.fill(xv, yv)
"""
if ma.isMaskedArray(xs) or ma.isMaskedArray(ys):
numpy = ma
if isinstance(xs, np.ma.MaskedArray) or isinstance(ys, np.ma.MaskedArray):
numpy = np.ma
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (previous and present) use of numpy for np.ma or ma looks odd. Maybe switch it to nx or num? Long ago, in the days when we had to support Numeric, numarray, and numpy, we used nx as the shim module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that there's another variable called Nx in the same function ("size of xs"), which makes things a bit awkward... and num sounds too generic, doesn't it?

else:
numpy = np

Expand Down Expand Up @@ -3853,9 +3846,10 @@ def poly_between(x, ylower, yupper):
Return value is *x*, *y* arrays for use with
:meth:`matplotlib.axes.Axes.fill`.
"""
if (ma.isMaskedArray(ylower) or ma.isMaskedArray(yupper) or
ma.isMaskedArray(x)):
numpy = ma
if (isinstance(ylower, np.ma.MaskedArray) or
isinstance(yupper, np.ma.MaskedArray) or
isinstance(x, np.ma.MaskedArray)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting pretty clunky. I'm almost ready to argue for standardizing on isMA as a readability win. Also, there is nothing wrong with making code a bit more concise and readable by importing frequently used symbols at the top so as to avoid so much np.ma. repetition in a case like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like:

    if any(isinstance(var, np.ma.MaskedArray) for var in (ylower, yupper, x)):  

will fit on one line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed following @QuLogic's suggestion.

numpy = np.ma
else:
numpy = np

Expand Down
9 changes: 4 additions & 5 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from weakref import WeakValueDictionary

import numpy as np
from numpy import ma

from matplotlib import _path
from matplotlib.cbook import simple_linear_interpolation, maxdict
Expand Down Expand Up @@ -131,8 +130,8 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1,
Makes the path behave in an immutable way and sets the vertices
and codes as read-only arrays.
"""
if ma.isMaskedArray(vertices):
vertices = vertices.astype(np.float_).filled(np.nan)
if isinstance(vertices, np.ma.MaskedArray):
vertices = vertices.astype(float).filled(np.nan)
else:
vertices = np.asarray(vertices, np.float_)

Expand Down Expand Up @@ -187,8 +186,8 @@ def _fast_from_codes_and_verts(cls, verts, codes, internals=None):
"""
internals = internals or {}
pth = cls.__new__(cls)
if ma.isMaskedArray(verts):
verts = verts.astype(np.float_).filled(np.nan)
if isinstance(verts, np.ma.MaskedArray):
verts = verts.astype(float).filled(np.nan)
else:
verts = np.asarray(verts, np.float_)
pth._vertices = verts
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ def save_diff_image(expected, actual, output):
actualImage = _png.read_png(actual)
actualImage, expectedImage = crop_to_same(
actual, actualImage, expected, expectedImage)
expectedImage = np.array(expectedImage).astype(np.float)
actualImage = np.array(actualImage).astype(np.float)
expectedImage = np.array(expectedImage).astype(float)
actualImage = np.array(actualImage).astype(float)
assert expectedImage.ndim == actualImage.ndim
assert expectedImage.shape == actualImage.shape
absDiffImage = abs(expectedImage - actualImage)
Expand Down