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
refactor lowess smoothing #193
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ Statistical core functions | |
~core.auto_regression._select_ar_order_xr | ||
~core.auto_regression._fit_auto_regression_xr | ||
~core.auto_regression._draw_auto_regression_correlated_np | ||
~core.smooting.lowess | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
Computation | ||
----------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import numpy as np | ||
import xarray as xr | ||
|
||
from mesmer.core.utils import _check_dataarray_form | ||
|
||
|
||
def lowess(data, dim, *, frac, use_coords_as_x=False, it=0): | ||
"""LOWESS (Locally Weighted Scatterplot Smoothing) for xarray objects | ||
|
||
Parameters | ||
---------- | ||
data : xr.DataArray | ||
Data to smooth (y-values). | ||
dim : str | ||
Dimension along which to smooth (x-dimension) | ||
frac : float | ||
Between 0 and 1. The fraction of the data used when estimating each y-value. | ||
use_coords_as_x : boolean, default: False | ||
If True uses ``data[dim]`` as x-values else uses ``np.arange(data[dim.size)`` | ||
(useful if ``dim`` are time coordinates). | ||
it : int, default: 0 | ||
The number of residual-based reweightings to perform. | ||
""" | ||
|
||
from statsmodels.nonparametric.smoothers_lowess import lowess | ||
|
||
if not isinstance(dim, str): | ||
raise ValueError("Can only pass a single dimension.") | ||
|
||
coords = data[dim] | ||
_check_dataarray_form(coords, name=dim, ndim=1) | ||
|
||
if use_coords_as_x: | ||
x = coords | ||
else: | ||
x = xr.ones_like(coords) | ||
x.data = np.arange(coords.size) | ||
|
||
out = xr.apply_ufunc( | ||
lowess, | ||
data, | ||
x, | ||
input_core_dims=[[dim], [dim]], | ||
output_core_dims=[[dim]], | ||
vectorize=True, | ||
kwargs={"frac": frac, "it": it, "return_sorted": False}, | ||
) | ||
|
||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import pytest | ||
import xarray as xr | ||
from statsmodels.nonparametric.smoothers_lowess import lowess | ||
|
||
import mesmer.core.smoothing | ||
from mesmer.core.utils import _check_dataarray_form | ||
|
||
from .utils import trend_data_1D, trend_data_2D | ||
|
||
|
||
def test_lowess_errors(): | ||
data = trend_data_2D() | ||
|
||
with pytest.raises(ValueError, match="Can only pass a single dimension."): | ||
mesmer.core.smoothing.lowess(data, ("lat", "lon"), frac=0.3) | ||
|
||
with pytest.raises(ValueError, match="data should be 1-dimensional"): | ||
mesmer.core.smoothing.lowess(data.to_dataset(), "data", frac=0.3) | ||
|
||
|
||
@pytest.mark.parametrize("it", [0, 3]) | ||
@pytest.mark.parametrize("frac", [0.3, 0.5]) | ||
def test_lowess(it, frac): | ||
|
||
data = trend_data_1D() | ||
|
||
result = mesmer.core.smoothing.lowess(data, "time", frac=frac, it=it) | ||
|
||
expected = lowess( | ||
data.values, data.time.values, frac=frac, it=it, return_sorted=False | ||
) | ||
expected = xr.DataArray(expected, dims="time", coords={"time": data.time}) | ||
|
||
xr.testing.assert_allclose(result, expected) | ||
|
||
|
||
def test_lowess_dataset(): | ||
|
||
data = trend_data_1D() | ||
|
||
result = mesmer.core.smoothing.lowess(data.to_dataset(), "time", frac=0.3) | ||
|
||
expected = lowess( | ||
data.values, data.time.values, frac=0.3, it=0, return_sorted=False | ||
) | ||
expected = xr.DataArray( | ||
expected, dims="time", coords={"time": data.time}, name="data" | ||
) | ||
expected = expected.to_dataset() | ||
|
||
xr.testing.assert_allclose(result, expected) | ||
|
||
|
||
def test_lowess_2D(): | ||
data = trend_data_2D() | ||
|
||
result = mesmer.core.smoothing.lowess(data, "time", frac=0.3) | ||
|
||
_check_dataarray_form( | ||
result, "result", ndim=2, required_dims=("time", "cells"), shape=data.shape | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
smooting
-->smoothing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Fixed in #194