-
Notifications
You must be signed in to change notification settings - Fork 72
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 a function to fill NaNs in grids by interpolation #440
base: main
Are you sure you want to change the base?
Changes from all commits
1b58b40
3cdc024
dae71a0
b547d40
cfd2b31
40e9290
05fe6c7
cb812cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -13,8 +13,10 @@ | |||||||
import numpy as np | ||||||||
import pandas as pd | ||||||||
import xarray as xr | ||||||||
import verde as vd | ||||||||
from scipy.spatial import cKDTree | ||||||||
|
||||||||
|
||||||||
try: | ||||||||
from pykdtree.kdtree import KDTree as pyKDTree | ||||||||
except ImportError: | ||||||||
|
@@ -681,6 +683,37 @@ def kdtree(coordinates, use_pykdtree=True, **kwargs): | |||||||
return tree | ||||||||
|
||||||||
|
||||||||
def fill_nans(grid): | ||||||||
""" | ||||||||
Fill missing values in a grid by nearest neighbor interpolation | ||||||||
|
||||||||
Parameters | ||||||||
---------- | ||||||||
grid : :class:`xarray.DataArray` | ||||||||
A 2D grid with one or more data variables. | ||||||||
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.
Suggested change
|
||||||||
Returns | ||||||||
------- | ||||||||
grid : :class:`xarray.DataArray` | ||||||||
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.
Suggested change
|
||||||||
A 2D grid with the NaN values filled. | ||||||||
""" | ||||||||
|
||||||||
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.
Suggested change
|
||||||||
filled_grid = grid.copy() | ||||||||
|
||||||||
not_nan_values = np.argwhere(~np.isnan(grid.values)) | ||||||||
unknown_indices = np.argwhere(np.isnan(grid.values)) | ||||||||
Comment on lines
+702
to
+703
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. Use the |
||||||||
|
||||||||
knn = vd.KNeighbors() | ||||||||
easting, northing = not_nan_values[:, 0], not_nan_values[:, 1] | ||||||||
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. Use the actual coordinates of the grid instead of generating indices. This makes the interpolation work even if the grid is not uniform. |
||||||||
knn.fit((easting, northing), grid.values[not_nan_values[:, 0], | ||||||||
not_nan_values[:, 1]]) | ||||||||
predicted_values = knn_imputer.predict((easting, northing)) | ||||||||
|
||||||||
for i, idx in enumerate(unknown_indices): | ||||||||
filled_grid[tuple(idx)] = predicted_values[i] | ||||||||
Comment on lines
+709
to
+712
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. Instead of this, you could use |
||||||||
|
||||||||
return filled_grid | ||||||||
|
||||||||
|
||||||||
def partition_by_sum(array, parts): | ||||||||
""" | ||||||||
Partition an array into parts of approximately equal sum. | ||||||||
|
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.
The
DataArray
s should contain coordinates as well. They should be as close as possible to the format of a real dataset. That's how we make the tests more robust.