Skip to content

Implementation of polyfit and polyval #3733

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 23 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
80936fd
[WIP] Implementation of polyfit and polyval - minimum testing - no docs
aulemahal Jan 30, 2020
05355af
Formatting with black, flake8
aulemahal Jan 30, 2020
7f26cb1
Fix failing test
aulemahal Jan 30, 2020
fdc1819
More intelligent skipna switching
aulemahal Jan 30, 2020
924f4eb
Add docs | Change coeff order to fit numpy | move polyval
aulemahal Jan 31, 2020
1f6030a
Move doc patching to class
aulemahal Jan 31, 2020
0f922e7
conditional doc patching
aulemahal Jan 31, 2020
bfec9c8
Fix windows fail - more efficient nan skipping
aulemahal Jan 31, 2020
2d3235a
Fix typo in least_squares
aulemahal Jan 31, 2020
31440ae
Move polyfit to dataset
aulemahal Feb 11, 2020
da831a5
Add more tests | fix some edge cases
aulemahal Feb 12, 2020
f13123f
Skip test without dask
aulemahal Feb 12, 2020
896313f
Fix 1D case | add docs
aulemahal Feb 12, 2020
5b70971
skip polyval test without dask
aulemahal Feb 12, 2020
5af3c64
Explicit docs | More restrictive polyval
aulemahal Feb 13, 2020
d731fd9
Merge branch 'master' into implement-polyfit-polyval
aulemahal Mar 13, 2020
3400dea
Small typo in polyfit docstrings
aulemahal Mar 13, 2020
198ff81
Apply suggestions from code review
aulemahal Mar 13, 2020
34726a1
Polyfit : fix style in docstring | add see also section
aulemahal Mar 13, 2020
860a892
Merge branch 'implement-polyfit-polyval' of github.com:aulemahal/xarr…
aulemahal Mar 13, 2020
3f7fe4c
Clean up docstrings and documentation.
aulemahal Mar 13, 2020
31e92bb
Merge master
aulemahal Mar 25, 2020
7eeba59
Move whats new entry to 0.16 | fix PEP8 issue in test_dataarray
aulemahal Mar 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Top-level functions
zeros_like
ones_like
dot
polyval
map_blocks
show_versions
set_options
Expand Down Expand Up @@ -171,6 +172,7 @@ Computation
Dataset.quantile
Dataset.differentiate
Dataset.integrate
Dataset.polyfit

**Aggregation**:
:py:attr:`~Dataset.all`
Expand Down Expand Up @@ -349,6 +351,7 @@ Computation
DataArray.quantile
DataArray.differentiate
DataArray.integrate
DataArray.polyfit
DataArray.str

**Aggregation**:
Expand Down
26 changes: 26 additions & 0 deletions doc/computation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,32 @@ trapezoidal rule using their coordinates,
and integration along multidimensional coordinate are not supported.


.. _compute.polyfit:

Fitting polynomials
===================

Xarray objects provide an interface for performing linear or polynomial regressions
using the least-squares method. :py:meth:`~xarray.DataArray.polyfit` computes the
best fitting coefficients along a given dimension and for a given order,

.. ipython:: python

x = np.arange(10)
a = xr.DataArray(3 + 4 * x, dims=['x'], coords={'x': x})
out = a.polyfit(dim='x', deg=1, full=True)
out

The method outputs a dataset containing the coefficients (and more if `full=True`).
The inverse operation is done with :py:meth:`~xarray.polyval`,

.. ipython:: python

xr.polyval(coord=x, coeffs=out.polyfit_coefficients)

.. note::
These methods replicate the behaviour of :py:func:`numpy.polyfit` and :py:func:`numpy.polyval`.

.. _compute.broadcasting:

Broadcasting by dimension name
Expand Down
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ Breaking changes

New Features
~~~~~~~~~~~~
- Added :py:meth:`DataArray.polyfit` and :py:func:`xarray.polyval` for fitting polynomials. (:issue:`3349`)
By `Pascal Bourgault <https://github.com/aulemahal>`_.
- :py:meth:`DataArray.sel` and :py:meth:`Dataset.sel` now support :py:class:`pandas.CategoricalIndex`. (:issue:`3669`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
- Support using an existing, opened h5netcdf ``File`` with
Expand Down
3 changes: 2 additions & 1 deletion xarray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .core.alignment import align, broadcast
from .core.combine import auto_combine, combine_by_coords, combine_nested
from .core.common import ALL_DIMS, full_like, ones_like, zeros_like
from .core.computation import apply_ufunc, dot, where
from .core.computation import apply_ufunc, dot, polyval, where
from .core.concat import concat
from .core.dataarray import DataArray
from .core.dataset import Dataset
Expand Down Expand Up @@ -65,6 +65,7 @@
"open_mfdataset",
"open_rasterio",
"open_zarr",
"polyval",
"register_dataarray_accessor",
"register_dataset_accessor",
"save_mfdataset",
Expand Down
32 changes: 32 additions & 0 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,3 +1299,35 @@ def where(cond, x, y):
dataset_join="exact",
dask="allowed",
)


def polyval(coord, coeffs, degree_dim="degree"):
"""Evaluate a polynomial at specific values

Parameters
----------
coord : DataArray
The 1D coordinate along which to evaluate the polynomial.
coeffs : DataArray
Coefficients of the polynomials.
degree_dim : str, default "degree"
Name of the polynomial degree dimension in `coeffs`.

See also
--------
xarray.DataArray.polyfit
numpy.polyval
"""
from .dataarray import DataArray
from .missing import get_clean_interp_index

x = get_clean_interp_index(coord, coord.name)

deg_coord = coeffs[degree_dim]

lhs = DataArray(
np.vander(x, int(deg_coord.max()) + 1),
dims=(coord.name, degree_dim),
coords={coord.name: coord, degree_dim: np.arange(deg_coord.max() + 1)[::-1]},
)
return (lhs * coeffs).sum(degree_dim)
27 changes: 27 additions & 0 deletions xarray/core/dask_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,30 @@ def func(x, window, axis=-1):
# crop boundary.
index = (slice(None),) * axis + (slice(drop_size, drop_size + orig_shape[axis]),)
return out[index]


def least_squares(lhs, rhs, rcond=None, skipna=False):
import dask.array as da

lhs_da = da.from_array(lhs, chunks=(rhs.chunks[0], lhs.shape[1]))
if skipna:
added_dim = rhs.ndim == 1
if added_dim:
rhs = rhs.reshape(rhs.shape[0], 1)
results = da.apply_along_axis(
nputils._nanpolyfit_1d,
0,
rhs,
lhs_da,
dtype=float,
shape=(lhs.shape[1] + 1,),
rcond=rcond,
)
coeffs = results[:-1, ...]
residuals = results[-1, ...]
if added_dim:
coeffs = coeffs.reshape(coeffs.shape[0])
residuals = residuals.reshape(residuals.shape[0])
else:
coeffs, residuals, _, _ = da.linalg.lstsq(lhs_da, rhs)
return coeffs, residuals
53 changes: 53 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3245,6 +3245,59 @@ def map_blocks(

return map_blocks(func, self, args, kwargs)

def polyfit(
self,
dim: Hashable,
deg: int,
skipna: bool = None,
rcond: float = None,
w: Union[Hashable, Any] = None,
full: bool = False,
cov: bool = False,
):
"""
Least squares polynomial fit.

This replicates the behaviour of `numpy.polyfit` but differs by skipping
invalid values when `skipna = True`.

Parameters
----------
dim : hashable
Coordinate along which to fit the polynomials.
deg : int
Degree of the fitting polynomial.
skipna : bool, optional
If True, removes all invalid values before fitting each 1D slices of the array
Default is True if data is stored in a dask.array or if there is any
invalid values, False otherwise.
rcond : float, optional
Relative condition number to the fit.
w : Union[Hashable, Any], optional
Weights to apply to the y-coordinate of the sample points.
Can be an array-like object or the name of a coordinate in the dataset.
full : bool, optional
Whether to return the residuals, matrix rank and singular values in addition
to the coefficients.
cov : Union[bool, str], optional
Whether to return to the covariance matrix in addition to the coefficients.
The matrix is not scaled if `cov = 'unscaled'`.

See documentation of `numpy.polyfit`.

Returns
-------
A single dataset which containts:
polyfit_coefficients : The coefficients of the best fit.
polyfit_residuals : The residuals of the least-square computation (only included if `full=True`)
[dim]_matrix_rank : The effective rank of the scaled Vandermonde coefficient matrix (only included if `full=True`)
[dim]_singular_values : The singular values of the scaled Vandermonde coefficient matrix (only included if `full=True`)
polyfit_covariance : The covariance matrix of the polynomial coefficient estimates (only included if `full=False` and `cov=True`)
"""
return self._to_temp_dataset().polyfit(
dim, deg, skipna=skipna, rcond=rcond, w=w, full=full, cov=cov
)

# this needs to be at the end, or mypy will confuse with `str`
# https://mypy.readthedocs.io/en/latest/common_issues.html#dealing-with-conflicting-names
str = property(StringAccessor)
Expand Down
169 changes: 169 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
merge_coordinates_without_align,
merge_data_and_coords,
)
from .missing import get_clean_interp_index
from .options import OPTIONS, _get_keep_attrs
from .pycompat import dask_array_type
from .utils import (
Expand Down Expand Up @@ -5724,5 +5725,173 @@ def map_blocks(

return map_blocks(func, self, args, kwargs)

def polyfit(
self,
dim: Hashable,
deg: int,
skipna: bool = None,
rcond: float = None,
w: Union[Hashable, Any] = None,
full: bool = False,
cov: Union[bool, str] = False,
):
"""
Least squares polynomial fit.

This replicates the behaviour of `numpy.polyfit` but differs by skipping
invalid values when `skipna = True`.

Parameters
----------
dim : hashable
Coordinate along which to fit the polynomials.
deg : int
Degree of the fitting polynomial.
skipna : bool, optional
If True, removes all invalid values before fitting each 1D slices of the array
Default is True if data is stored in a dask.array or if there is any
invalid values, False otherwise.
rcond : float, optional
Relative condition number to the fit.
w : Union[Hashable, Any], optional
Weights to apply to the y-coordinate of the sample points.
Can be an array-like object or the name of a coordinate in the dataset.
full : bool, optional
Whether to return the residuals, matrix rank and singular values in addition
to the coefficients.
cov : Union[bool, str], optional
Whether to return to the covariance matrix in addition to the coefficients.
The matrix is not scaled if `cov = 'unscaled'`.

See documentation of `numpy.polyfit`.

Returns
-------
A single dataset which containts:
[var_]polyfit_coefficients : The coefficients of the best fit for each variable in this dataset.
[var_]polyfit_residuals : The residuals of the least-square computation for each variable (only included if `full=True`)
[dim]_matrix_rank : The effective rank of the scaled Vandermonde coefficient matrix (only included if `full=True`)
[dim]_singular_values : The singular values of the scaled Vandermonde coefficient matrix (only included if `full=True`)
[var_]polyfit_covariance : The covariance matrix of the polynomial coefficient estimates (only included if `full=False` and `cov=True`)
"""
variables = {}
skipna_da = skipna

x = get_clean_interp_index(self, dim)
xname = "{}_".format(self[dim].name)
order = int(deg) + 1
lhs = np.vander(x, order)

if rcond is None:
rcond = x.shape[0] * np.core.finfo(x.dtype).eps

# Weights:
if w is not None:
if isinstance(w, Hashable):
w = self.coords[w]
w = np.asarray(w)
if w.ndim != 1:
raise TypeError("Expected a 1-d array for weights.")
if w.shape[0] != lhs.shape[0]:
raise TypeError("Expected w and {} to have the same length".format(dim))
lhs *= w[:, np.newaxis]

# Scaling
scale = np.sqrt((lhs * lhs).sum(axis=0))
lhs /= scale

degree_dim = utils.get_temp_dimname(self.dims, "degree")

rank = np.linalg.matrix_rank(lhs)
if rank != order and not full:
warnings.warn(
"Polyfit may be poorly conditioned", np.RankWarning, stacklevel=4
)

if full:
rank = xr.DataArray(rank, name=xname + "matrix_rank")
variables[rank.name] = rank
sing = np.linalg.svd(lhs, compute_uv=False)
sing = xr.DataArray(
sing,
dims=(degree_dim,),
coords={degree_dim: np.arange(order)[::-1]},
name=xname + "singular_values",
)
variables[sing.name] = sing

for name, da in self.data_vars.items():
if dim not in da.dims:
continue

if skipna is None:
if isinstance(da.data, dask_array_type):
skipna_da = True
else:
skipna_da = np.any(da.isnull())

dims_to_stack = [dimname for dimname in da.dims if dimname != dim]
stacked_coords = {}
if dims_to_stack:
stacked_dim = utils.get_temp_dimname(dims_to_stack, "stacked")
rhs = da.transpose(dim, *dims_to_stack).stack(
{stacked_dim: dims_to_stack}
)
stacked_coords = {stacked_dim: rhs[stacked_dim]}
scale_da = scale[:, np.newaxis]
else:
rhs = da
scale_da = scale

if w is not None:
rhs *= w[:, np.newaxis]

coeffs, residuals = duck_array_ops.least_squares(
lhs, rhs.data, rcond=rcond, skipna=skipna_da
)

if isinstance(name, str):
name = "{}_".format(name)
else:
# Thus a ReprObject => polyfit was called on a DataArray
name = ""

coeffs = xr.DataArray(
coeffs / scale_da,
dims=[degree_dim] + list(stacked_coords.keys()),
coords={degree_dim: np.arange(order)[::-1], **stacked_coords},
name=name + "polyfit_coefficients",
)
if dims_to_stack:
coeffs = coeffs.unstack(stacked_dim)
variables[coeffs.name] = coeffs

if full or (cov is True):
residuals = xr.DataArray(
residuals if dims_to_stack else residuals.squeeze(),
dims=list(stacked_coords.keys()),
coords=stacked_coords,
name=name + "polyfit_residuals",
)
if dims_to_stack:
residuals = residuals.unstack(stacked_dim)
variables[residuals.name] = residuals

if cov:
Vbase = np.linalg.inv(np.dot(lhs.T, lhs))
Vbase /= np.outer(scale, scale)
if cov == "unscaled":
fac = 1
else:
if x.shape[0] <= order:
raise ValueError(
"The number of data points must exceed order to scale the covariance matrix."
)
fac = residuals / (x.shape[0] - order)
covariance = xr.DataArray(Vbase, dims=("cov_i", "cov_j"),) * fac
variables[name + "polyfit_covariance"] = covariance

return Dataset(data_vars=variables, attrs=self.attrs.copy())


ops.inject_all_ops_and_reduce_methods(Dataset, array_only=False)
Loading