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

Add nearest neighbor interpolation to fill gaps #87

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"skimage": ("https://scikit-image.org/docs/stable/", None),
"pooch": ("https://www.fatiando.org/pooch/latest/", None),
"matplotlib": ("https://matplotlib.org/stable/", None),
"scipy": ("https://docs.scipy.org/doc/scipy/", None),
}

# Autosummary pages will be generated by sphinx-autogen instead of sphinx-build
Expand Down
20 changes: 17 additions & 3 deletions xlandsat/_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import scipy as sp


def interpolate_missing(scene, pixel_radius=20):
def interpolate_missing(scene, pixel_radius=20, method="cubic"):
"""
Fill missing values (NaNs) in a scene by cubic interpolation
Fill missing values (NaNs) in a scene by interpolation

Each missing value is filled by interpolating the pixels within a
neighboring region (controlled by ``pixel_radius``) using a piecewise cubic
Expand All @@ -28,12 +28,26 @@
that will be used for interpolation. Smaller values make for faster
interpolation but may lead to bad results if many missing values are
grouped together.
method : str
The interpolation method used. Can be one of: ``"cubic"`` for cubic
interpolation with
:class:`scipy.interpolate.CloughTocher2DInterpolator`, ``"neighbors"``
for nearest neighbor interpolation with
:class:`scipy.interpolate.NearestNDInterpolator`.

Returns
-------
filled_scene : :class:`xarray.Dataset`
The scene with missing values filled in.
"""
valid_methods = {

Check warning on line 43 in xlandsat/_interpolation.py

View check run for this annotation

Codecov / codecov/patch

xlandsat/_interpolation.py#L43

Added line #L43 was not covered by tests
"cubic": sp.interpolate.CloughTocher2DInterpolator,
"neighbors": sp.interpolate.NearestNDInterpolator,
}
if method not in valid_methods:
raise ValueError(

Check warning on line 48 in xlandsat/_interpolation.py

View check run for this annotation

Codecov / codecov/patch

xlandsat/_interpolation.py#L48

Added line #L48 was not covered by tests
f"Invalid interpolation method '{method}'. Must be one of: {tuple(valid_methods.keys())}"
)
filled_scene = scene.copy(deep=True)
rows, columns = np.meshgrid(
np.arange(scene.northing.size),
Expand All @@ -47,7 +61,7 @@
imin, imax = _search_range(i, pixel_radius, scene.northing.size)
jmin, jmax = _search_range(j, pixel_radius, scene.easting.size)
valid = ~np.isnan(values[imin:imax, jmin:jmax])
interpolator = sp.interpolate.CloughTocher2DInterpolator(
interpolator = valid_methods[method](

Check warning on line 64 in xlandsat/_interpolation.py

View check run for this annotation

Codecov / codecov/patch

xlandsat/_interpolation.py#L64

Added line #L64 was not covered by tests
(
rows[imin:imax, jmin:jmax][valid],
columns[imin:imax, jmin:jmax][valid],
Expand Down
Loading