Skip to content

Commit

Permalink
Merge 0b23f3f into 76aa4f0
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky committed Jun 30, 2019
2 parents 76aa4f0 + 0b23f3f commit bf58d98
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 176 deletions.
5 changes: 2 additions & 3 deletions xarray/backends/pseudonetcdf_.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ class PseudoNetCDFDataStore(AbstractDataStore):
"""Store for accessing datasets via PseudoNetCDF
"""
@classmethod
def open(cls, filename, lock=None, **format_kwds):
def open(cls, filename, lock=None, mode=None, **format_kwargs):
from PseudoNetCDF import pncopen

keywords = dict(kwargs=format_kwds)
keywords = {'kwargs': format_kwargs}
# only include mode if explicitly passed
mode = format_kwds.pop('mode', None)
if mode is not None:
keywords['mode'] = mode

Expand Down
7 changes: 1 addition & 6 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def reindex_variables(
return reindexed, new_indexes


def broadcast(*args, **kwargs):
def broadcast(*args, exclude=None):
"""Explicitly broadcast any number of DataArray or Dataset objects against
one another.
Expand Down Expand Up @@ -466,13 +466,8 @@ def broadcast(*args, **kwargs):
from .dataarray import DataArray
from .dataset import Dataset

exclude = kwargs.pop('exclude', None)
if exclude is None:
exclude = set()
if kwargs:
raise TypeError('broadcast() got unexpected keyword arguments: %s'
% list(kwargs))

args = align(*args, join='outer', copy=False, exclude=exclude)

common_coords = OrderedDict()
Expand Down
3 changes: 1 addition & 2 deletions xarray/core/dask_array_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ def _gradient_kernel(x, block_id, coord, axis, array_locs, grad_kwargs):
grad = np.gradient(x, coord, axis=axis, **grad_kwargs)
return grad

def gradient(f, *varargs, **kwargs):
def gradient(f, *varargs, axis=None, **kwargs):
f = da.asarray(f)

kwargs["edge_order"] = math.ceil(kwargs.get("edge_order", 1))
if kwargs["edge_order"] > 2:
raise ValueError("edge_order must be less than or equal to 2.")

drop_result_list = False
axis = kwargs.pop("axis", None)
if axis is None:
axis = tuple(range(f.ndim))
elif isinstance(axis, Integral):
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ def wrapped_func(self, dim=DEFAULT_DIMS, axis=None, # type: ignore


class DatasetGroupBy(GroupBy, ImplementsDatasetReduce):
def apply(self, func, args=(), **kwargs):
def apply(self, func, args=(), shortcut=None, **kwargs):
"""Apply a function over each Dataset in the group and concatenate them
together into a new Dataset.
Expand Down Expand Up @@ -756,7 +756,7 @@ def apply(self, func, args=(), **kwargs):
applied : Dataset or DataArray
The result of splitting, applying and combining this dataset.
"""
kwargs.pop('shortcut', None) # ignore shortcut if set (for now)
# ignore shortcut if set (for now)
applied = (func(ds, *args, **kwargs) for ds in self._iter_grouped())
return self._combine(applied)

Expand Down
64 changes: 28 additions & 36 deletions xarray/core/missing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import warnings
from collections.abc import Iterable
from functools import partial
from typing import Any, Callable, Dict
from typing import Any, Callable, Dict, Sequence

import numpy as np
import pandas as pd
Expand All @@ -15,9 +14,10 @@


class BaseInterpolator:
'''gerneric interpolator class for normalizing interpolation methods'''
cons_kwargs = {} # type: Dict[str, Any]
call_kwargs = {} # type: Dict[str, Any]
"""Generic interpolator class for normalizing interpolation methods
"""
cons_kwargs = None # type: Dict[str, Any]
call_kwargs = None # type: Dict[str, Any]
f = None # type: Callable
method = None # type: str

Expand All @@ -30,35 +30,30 @@ def __repr__(self):


class NumpyInterpolator(BaseInterpolator):
'''One-dimensional linear interpolation.
"""One-dimensional linear interpolation.
See Also
--------
numpy.interp
'''

def __init__(self, xi, yi, method='linear', fill_value=None, **kwargs):
"""
def __init__(self, xi, yi, method='linear', fill_value=None, period=None):

if method != 'linear':
raise ValueError(
'only method `linear` is valid for the NumpyInterpolator')

self.method = method
self.f = np.interp
self.cons_kwargs = kwargs
self.call_kwargs = {'period': self.cons_kwargs.pop('period', None)}
self.cons_kwargs = {}
self.call_kwargs = {'period': period}

self._xi = xi
self._yi = yi

if self.cons_kwargs:
raise ValueError(
'received invalid kwargs: %r' % self.cons_kwargs.keys())

if fill_value is None:
self._left = np.nan
self._right = np.nan
elif isinstance(fill_value, Iterable) and len(fill_value) == 2:
elif isinstance(fill_value, Sequence) and len(fill_value) == 2:
self._left = fill_value[0]
self._right = fill_value[1]
elif is_scalar(fill_value):
Expand All @@ -73,33 +68,33 @@ def __call__(self, x):


class ScipyInterpolator(BaseInterpolator):
'''Interpolate a 1-D function using Scipy interp1d
"""Interpolate a 1-D function using Scipy interp1d
See Also
--------
scipy.interpolate.interp1d
'''

"""
def __init__(self, xi, yi, method=None, fill_value=None,
assume_sorted=True, copy=False, bounds_error=False, **kwargs):
assume_sorted=True, copy=False, bounds_error=False,
order=None, **kwargs):
from scipy.interpolate import interp1d

if method is None:
raise ValueError('method is a required argument, please supply a '
'valid scipy.inter1d method (kind)')

if method == 'polynomial':
method = kwargs.pop('order', None)
if method is None:
if order is None:
raise ValueError('order is required when method=polynomial')
method = order

self.method = method

self.cons_kwargs = kwargs
self.call_kwargs = {}

if fill_value is None and method == 'linear':
fill_value = kwargs.pop('fill_value', (np.nan, np.nan))
fill_value = np.nan, np.nan
elif fill_value is None:
fill_value = np.nan

Expand All @@ -109,15 +104,14 @@ def __init__(self, xi, yi, method=None, fill_value=None,


class SplineInterpolator(BaseInterpolator):
'''One-dimensional smoothing spline fit to a given set of data points.
"""One-dimensional smoothing spline fit to a given set of data points.
See Also
--------
scipy.interpolate.UnivariateSpline
'''

"""
def __init__(self, xi, yi, method='spline', fill_value=None, order=3,
**kwargs):
nu=0, ext=None, **kwargs):
from scipy.interpolate import UnivariateSpline

if method != 'spline':
Expand All @@ -126,8 +120,7 @@ def __init__(self, xi, yi, method='spline', fill_value=None, order=3,

self.method = method
self.cons_kwargs = kwargs
self.call_kwargs['nu'] = kwargs.pop('nu', 0)
self.call_kwargs['ext'] = kwargs.pop('ext', None)
self.call_kwargs = {'nu': nu, 'ext': ext}

if fill_value is not None:
raise ValueError('SplineInterpolator does not support fill_value')
Expand All @@ -136,8 +129,8 @@ def __init__(self, xi, yi, method='spline', fill_value=None, order=3,


def _apply_over_vars_with_dim(func, self, dim=None, **kwargs):
'''wrapper for datasets'''

"""Wrapper for datasets
"""
ds = type(self)(coords=self.coords, attrs=self.attrs)

for name, var in self.data_vars.items():
Expand All @@ -149,7 +142,7 @@ def _apply_over_vars_with_dim(func, self, dim=None, **kwargs):
return ds


def get_clean_interp_index(arr, dim, use_coordinate=True, **kwargs):
def get_clean_interp_index(arr, dim, use_coordinate=True):
'''get index to use for x values in interpolation.
If use_coordinate is True, the coordinate that shares the name of the
Expand Down Expand Up @@ -189,17 +182,16 @@ def get_clean_interp_index(arr, dim, use_coordinate=True, **kwargs):

def interp_na(self, dim=None, use_coordinate=True, method='linear', limit=None,
**kwargs):
'''Interpolate values according to different methods.'''

"""Interpolate values according to different methods.
"""
if dim is None:
raise NotImplementedError('dim is a required argument')

if limit is not None:
valids = _get_valid_fill_mask(self, dim, limit)

# method
index = get_clean_interp_index(self, dim, use_coordinate=use_coordinate,
**kwargs)
index = get_clean_interp_index(self, dim, use_coordinate=use_coordinate)
interp_class, kwargs = _get_interpolator(method, **kwargs)
interpolator = partial(func_interpolate_na, interp_class, **kwargs)

Expand Down
13 changes: 5 additions & 8 deletions xarray/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def nansum(a, axis=None, dtype=None, out=None, min_count=None):
return result


def _nanmean_ddof_object(ddof, value, axis=None, **kwargs):
def _nanmean_ddof_object(ddof, value, axis=None, dtype=None, **kwargs):
""" In house nanmean. ddof argument will be used in _nanvar method """
from .duck_array_ops import (count, fillna, _dask_or_eager_func,
where_method)
Expand All @@ -142,7 +142,6 @@ def _nanmean_ddof_object(ddof, value, axis=None, **kwargs):
value = fillna(value, 0)
# As dtype inference is impossible for object dtype, we assume float
# https://github.com/dask/dask/issues/3162
dtype = kwargs.pop('dtype', None)
if dtype is None and value.dtype.kind == 'O':
dtype = value.dtype if value.dtype.kind in ['cf'] else float

Expand All @@ -165,14 +164,12 @@ def nanmedian(a, axis=None, out=None):
return _dask_or_eager_func('nanmedian', eager_module=nputils)(a, axis=axis)


def _nanvar_object(value, axis=None, **kwargs):
ddof = kwargs.pop('ddof', 0)
kwargs_mean = kwargs.copy()
kwargs_mean.pop('keepdims', None)
def _nanvar_object(value, axis=None, ddof=0, keepdims=False, **kwargs):
value_mean = _nanmean_ddof_object(ddof=0, value=value, axis=axis,
keepdims=True, **kwargs_mean)
keepdims=True, **kwargs)
squared = (value.astype(value_mean.dtype) - value_mean)**2
return _nanmean_ddof_object(ddof, squared, axis=axis, **kwargs)
return _nanmean_ddof_object(ddof, squared, axis=axis,
keepdims=keepdims, **kwargs)


def nanvar(a, axis=None, dtype=None, out=None, ddof=0):
Expand Down
10 changes: 4 additions & 6 deletions xarray/core/npcompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ def normalize_axis_tuple(axes, N):
axes = (axes, )
return tuple([N + a if a < 0 else a for a in axes])

def gradient(f, *varargs, **kwargs):
def gradient(f, *varargs, axis=None, edge_order=1):
f = np.asanyarray(f)
N = f.ndim # number of dimensions

axes = kwargs.pop('axis', None)
axes = axis
del axis

if axes is None:
axes = tuple(range(N))
else:
Expand Down Expand Up @@ -146,10 +148,6 @@ def gradient(f, *varargs, **kwargs):
else:
raise TypeError("invalid number of arguments")

edge_order = kwargs.pop('edge_order', 1)
if kwargs:
raise TypeError('"{}" are not valid keyword arguments.'.format(
'", "'.join(kwargs.keys())))
if edge_order > 2:
raise ValueError("'edge_order' greater than 2 not supported")

Expand Down
10 changes: 5 additions & 5 deletions xarray/core/nputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ def _rolling_window(a, window, axis=-1):


def _create_bottleneck_method(name, npmodule=np):
def f(values, axis=None, **kwds):
dtype = kwds.get('dtype', None)
def f(values, axis=None, **kwargs):
dtype = kwargs.get('dtype', None)
bn_func = getattr(bn, name, None)

if (_USE_BOTTLENECK and bn_func is not None and
Expand All @@ -214,10 +214,10 @@ def f(values, axis=None, **kwds):
values.dtype.isnative and
(dtype is None or np.dtype(dtype) == values.dtype)):
# bottleneck does not take care dtype, min_count
kwds.pop('dtype', None)
result = bn_func(values, axis=axis, **kwds)
kwargs.pop('dtype', None)
result = bn_func(values, axis=axis, **kwargs)
else:
result = getattr(npmodule, name)(values, axis=axis, **kwds)
result = getattr(npmodule, name)(values, axis=axis, **kwargs)

return result

Expand Down
28 changes: 14 additions & 14 deletions xarray/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ class DataArrayResample(DataArrayGroupBy, Resample):
specified dimension
"""

def __init__(self, *args, **kwargs):
def __init__(self, *args, dim=None, resample_dim=None, **kwargs):

self._dim = kwargs.pop('dim', None)
self._resample_dim = kwargs.pop('resample_dim', None)

if self._dim == self._resample_dim:
if dim == resample_dim:
raise ValueError("Proxy resampling dimension ('{}') "
"cannot have the same name as actual dimension "
"('{}')! ".format(self._resample_dim, self._dim))
"('{}')! ".format(resample_dim, dim))
self._dim = dim
self._resample_dim = resample_dim

super(DataArrayResample, self).__init__(*args, **kwargs)

def apply(self, func, shortcut=False, args=(), **kwargs):
Expand Down Expand Up @@ -227,18 +227,18 @@ class DatasetResample(DatasetGroupBy, Resample):
"""DatasetGroupBy object specialized to resampling a specified dimension
"""

def __init__(self, *args, **kwargs):
def __init__(self, *args, dim=None, resample_dim=None, **kwargs):

self._dim = kwargs.pop('dim', None)
self._resample_dim = kwargs.pop('resample_dim', None)

if self._dim == self._resample_dim:
if dim == resample_dim:
raise ValueError("Proxy resampling dimension ('{}') "
"cannot have the same name as actual dimension "
"('{}')! ".format(self._resample_dim, self._dim))
"('{}')! ".format(resample_dim, dim))
self._dim = dim
self._resample_dim = resample_dim

super(DatasetResample, self).__init__(*args, **kwargs)

def apply(self, func, args=(), **kwargs):
def apply(self, func, args=(), shortcut=None, **kwargs):
"""Apply a function over each Dataset in the groups generated for
resampling and concatenate them together into a new Dataset.
Expand Down Expand Up @@ -267,7 +267,7 @@ def apply(self, func, args=(), **kwargs):
applied : Dataset or DataArray
The result of splitting, applying and combining this dataset.
"""
kwargs.pop('shortcut', None) # ignore shortcut if set (for now)
# ignore shortcut if set (for now)
applied = (func(ds, *args, **kwargs) for ds in self._iter_grouped())
combined = self._combine(applied)

Expand Down

0 comments on commit bf58d98

Please sign in to comment.