Skip to content

Commit

Permalink
Merge pull request #1242 from jhamman/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
Joe Hamman authored Jan 31, 2017
2 parents 588339c + 4d94858 commit 93d6963
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 48 deletions.
16 changes: 9 additions & 7 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ def _validate_attrs(dataset):
def check_attr(name, value):
if isinstance(name, basestring):
if not name:
raise ValueError('Invalid name for attr: string must be length '
'1 or greater for serialization to netCDF '
'files')
raise ValueError('Invalid name for attr: string must be '
'length 1 or greater for serialization to '
'netCDF files')
else:
raise TypeError("Invalid name for attr: {} must be a string for "
"serialization to netCDF files".format(name))

if not isinstance(value, (basestring, Number, np.ndarray, np.number,
list, tuple)):
raise TypeError('Invalid value for attr: {} must be a number '
'string, ndarray or a list/tuple of numbers/strings '
'for serialization to netCDF '
'string, ndarray or a list/tuple of '
'numbers/strings for serialization to netCDF '
'files'.format(value))

# Check attrs on the dataset itself
Expand Down Expand Up @@ -228,9 +228,11 @@ def maybe_decode_store(store, lock=False):
try:
from dask.base import tokenize
except ImportError:
import dask # raise the usual error if dask is entirely missing
# raise the usual error if dask is entirely missing
import dask
if StrictVersion(dask.__version__) < StrictVersion('0.6'):
raise ImportError('xarray requires dask version 0.6 or newer')
raise ImportError(
'xarray requires dask version 0.6 or newer')
else:
raise

Expand Down
1 change: 0 additions & 1 deletion xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from collections import defaultdict

import numpy as np
import pandas as pd

from . import ops, utils
from .common import _maybe_promote
Expand Down
21 changes: 12 additions & 9 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import pandas as pd

from .pycompat import (basestring, suppress, dask_array_type, OrderedDict)
from .pycompat import basestring, suppress, dask_array_type, OrderedDict
from . import formatting
from .utils import SortedKeysDict, not_implemented, Frozen

Expand Down Expand Up @@ -599,9 +599,11 @@ def where(self, cond, other=None, drop=False):
----------
cond : boolean DataArray or Dataset
other : unimplemented, optional
Unimplemented placeholder for compatibility with future numpy / pandas versions
Unimplemented placeholder for compatibility with future
numpy / pandas versions
drop : boolean, optional
Coordinate labels that only correspond to NA values should be dropped
Coordinate labels that only correspond to NA values should be
dropped
Returns
-------
Expand Down Expand Up @@ -633,7 +635,8 @@ def where(self, cond, other=None, drop=False):
* y (y) int64 0 1 2 3 4
"""
if other is not None:
raise NotImplementedError("The optional argument 'other' has not yet been implemented")
raise NotImplementedError("The optional argument 'other' has not "
"yet been implemented")

if drop:
from .dataarray import DataArray
Expand Down Expand Up @@ -675,11 +678,11 @@ def __enter__(self):
def __exit__(self, exc_type, exc_value, traceback):
self.close()

# this has no runtime function - these are listed so IDEs know these methods
# are defined and don't warn on these operations
__lt__ = __le__ =__ge__ = __gt__ = __add__ = __sub__ = __mul__ = \
__truediv__ = __floordiv__ = __mod__ = __pow__ = __and__ = __xor__ = \
__or__ = __div__ = __eq__ = __ne__ = not_implemented
# this has no runtime function - these are listed so IDEs know these
# methods are defined and don't warn on these operations
__lt__ = __le__ = __ge__ = __gt__ = __add__ = __sub__ = __mul__ = \
__truediv__ = __floordiv__ = __mod__ = __pow__ = __and__ = __xor__ = \
__or__ = __div__ = __eq__ = __ne__ = not_implemented


def _maybe_promote(dtype):
Expand Down
11 changes: 4 additions & 7 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
broadcast_variables)
from .pycompat import (iteritems, basestring, OrderedDict,
dask_array_type, range)
from .combine import concat
from .formatting import ensure_valid_repr
from .options import OPTIONS

# list of attributes of pd.DatetimeIndex that are ndarrays of time info
Expand Down Expand Up @@ -154,8 +152,8 @@ def merge_indexes(
for n in var_names:
names.append(n)
var = variables[n]
if (current_index_variable is not None and
var.dims != current_index_variable.dims):
if ((current_index_variable is not None) and
(var.dims != current_index_variable.dims)):
raise ValueError(
"dimension mismatch between %r %s and %r %s"
% (dim, current_index_variable.dims, n, var.dims))
Expand Down Expand Up @@ -1222,7 +1220,6 @@ def isel_points(self, dim='points', **indexers):
Dataset.sel_points
DataArray.isel_points
"""
from .dataarray import DataArray

indexer_dims = set(indexers)

Expand Down Expand Up @@ -2098,7 +2095,7 @@ def reduce(self, func, dim=None, keep_attrs=False, numeric_only=False,
if name not in self.coords:
if (not numeric_only or
np.issubdtype(var.dtype, np.number) or
var.dtype == np.bool_):
(var.dtype == np.bool_)):
if len(reduce_dims) == 1:
# unpack dimensions for the benefit of functions
# like np.argmin which can't handle tuple arguments
Expand Down Expand Up @@ -2810,7 +2807,7 @@ def filter_by_attrs(self, **kwargs):
for attr_name, pattern in kwargs.items():
attr_value = variable.attrs.get(attr_name)
if ((callable(pattern) and pattern(attr_value))
or attr_value == pattern):
or attr_value == pattern):
selection.append(var_name)
return self[selection]

Expand Down
5 changes: 2 additions & 3 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
from pandas.tslib import OutOfBoundsDatetime

from .options import OPTIONS
from .pycompat import (
PY2, unicode_type, bytes_type, dask_array_type, OrderedDict, basestring)
from .pycompat import PY2, unicode_type, bytes_type, dask_array_type


def pretty_print(x, numchars):
Expand Down Expand Up @@ -49,7 +48,7 @@ def ensure_valid_repr(string):
On Python 2, this means we need to convert unicode to bytes. We won't need
this function once we drop Python 2.7 support.
"""
if PY2 and isinstance(string, unicode):
if PY2 and isinstance(string, unicode_type):
string = string.encode('utf-8')
return string

Expand Down
1 change: 1 addition & 0 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def _consolidate_slices(slices):
"""Consolidate adjacent slices in a list of slices.
"""
result = []
last_slice = slice(None)
for slice_ in slices:
if not isinstance(slice_, slice):
raise ValueError('list element is not a slice: %r' % slice_)
Expand Down
19 changes: 6 additions & 13 deletions xarray/core/npcompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def _maybe_view_as_subclass(original_array, new_array):
new_array.__array_finalize__(original_array)
return new_array


def _broadcast_to(array, shape, subok, readonly):
shape = tuple(shape) if np.iterable(shape) else (shape,)
array = np.array(array, copy=False, subok=subok)
Expand All @@ -41,7 +40,6 @@ def _broadcast_to(array, shape, subok, readonly):
result.flags.writeable = True
return result


def broadcast_to(array, shape, subok=False):
"""Broadcast an array to a new shape.
Expand Down Expand Up @@ -78,7 +76,6 @@ def broadcast_to(array, shape, subok=False):
"""
return _broadcast_to(array, shape, subok=subok, readonly=True)


def stack(arrays, axis=0):
"""
Join a sequence of arrays along a new axis.
Expand Down Expand Up @@ -143,7 +140,6 @@ def stack(arrays, axis=0):
expanded_arrays = [arr[sl] for arr in arrays]
return np.concatenate(expanded_arrays, axis=axis)


def _replace_nan(a, val):
"""
If `a` is of inexact type, make a copy of `a`, replace NaNs with
Expand Down Expand Up @@ -185,7 +181,6 @@ def _replace_nan(a, val):
np.copyto(a, val, where=mask)
return a, mask


def nanprod(a, axis=None, dtype=None, out=None, keepdims=0):
"""
Return the product of array elements over a given axis treating Not a
Expand Down Expand Up @@ -255,12 +250,11 @@ def nanprod(a, axis=None, dtype=None, out=None, keepdims=0):
a, mask = _replace_nan(a, 1)
return np.prod(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims)


def nancumsum(a, axis=None, dtype=None, out=None):
"""
Return the cumulative sum of array elements over a given axis treating Not a
Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are
encountered and leading NaNs are replaced by zeros.
Return the cumulative sum of array elements over a given axis treating
Not a Numbers (NaNs) as zero. The cumulative sum does not change when
NaNs are encountered and leading NaNs are replaced by zeros.
Zeros are returned for slices that are all-NaN or empty.
Expand Down Expand Up @@ -320,12 +314,11 @@ def nancumsum(a, axis=None, dtype=None, out=None):
a, mask = _replace_nan(a, 0)
return np.cumsum(a, axis=axis, dtype=dtype, out=out)


def nancumprod(a, axis=None, dtype=None, out=None):
"""
Return the cumulative product of array elements over a given axis treating Not a
Numbers (NaNs) as one. The cumulative product does not change when NaNs are
encountered and leading NaNs are replaced by ones.
Return the cumulative product of array elements over a given axis
treating Not a Numbers (NaNs) as one. The cumulative product does not
change when NaNs are encountered and leading NaNs are replaced by ones.
Ones are returned for slices that are all-NaN or empty.
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def equivalent(first, second):
if isinstance(first, np.ndarray) or isinstance(second, np.ndarray):
return ops.array_equiv(first, second)
else:
return (first is second or
first == second or
return ((first is second) or
(first == second) or
(pd.isnull(first) and pd.isnull(second)))


Expand Down
4 changes: 2 additions & 2 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,8 @@ def reduce(self, func, dim=None, axis=None, keep_attrs=False,

if getattr(func, 'keep_dims', False):
if dim is None and axis is None:
raise ValueError("must supply either single 'dim' or 'axis' argument to %s"
% (func.__name__))
raise ValueError("must supply either single 'dim' or 'axis' "
"argument to %s" % (func.__name__))

if dim is not None:
axis = self.get_axis_num(dim)
Expand Down
2 changes: 0 additions & 2 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ def hist(darray, figsize=None, size=None, aspect=None, ax=None, **kwargs):
Additional keyword arguments to matplotlib.pyplot.hist
"""
import matplotlib.pyplot as plt

ax = get_axis(figsize, size, aspect, ax)

no_nan = np.ravel(darray.values)
Expand Down
3 changes: 1 addition & 2 deletions xarray/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from numpy.testing import assert_array_equal
import pytest

from xarray.core import utils, nputils, ops
from xarray.core.variable import as_variable
from xarray.core import utils
from xarray.core.pycompat import PY3
from xarray.testing import assert_equal, assert_identical, assert_allclose

Expand Down

0 comments on commit 93d6963

Please sign in to comment.