|
2 | 2 |
|
3 | 3 | from . import ops
|
4 | 4 | from .groupby import DEFAULT_DIMS, DataArrayGroupBy, DatasetGroupBy
|
5 |
| -from .pycompat import OrderedDict, dask_array_type |
6 | 5 |
|
7 | 6 | RESAMPLE_DIM = '__resample_dim__'
|
8 | 7 |
|
@@ -110,7 +109,16 @@ def interpolate(self, kind='linear'):
|
110 | 109 | return self._interpolate(kind=kind)
|
111 | 110 |
|
112 | 111 | def _interpolate(self, kind='linear'):
|
113 |
| - raise NotImplementedError |
| 112 | + """Apply scipy.interpolate.interp1d along resampling dimension.""" |
| 113 | + # drop any existing non-dimension coordinates along the resampling |
| 114 | + # dimension |
| 115 | + dummy = self._obj.copy() |
| 116 | + for k, v in self._obj.coords.items(): |
| 117 | + if k != self._dim and self._dim in v.dims: |
| 118 | + dummy = dummy.drop(k) |
| 119 | + return dummy.interp(assume_sorted=True, method=kind, |
| 120 | + kwargs={'bounds_error': False}, |
| 121 | + **{self._dim: self._full_index}) |
114 | 122 |
|
115 | 123 |
|
116 | 124 | class DataArrayResample(DataArrayGroupBy, Resample):
|
@@ -182,46 +190,6 @@ def apply(self, func, shortcut=False, args=(), **kwargs):
|
182 | 190 |
|
183 | 191 | return combined
|
184 | 192 |
|
185 |
| - def _interpolate(self, kind='linear'): |
186 |
| - """Apply scipy.interpolate.interp1d along resampling dimension.""" |
187 |
| - from .dataarray import DataArray |
188 |
| - from scipy.interpolate import interp1d |
189 |
| - |
190 |
| - if isinstance(self._obj.data, dask_array_type): |
191 |
| - raise TypeError( |
192 |
| - "Up-sampling via interpolation was attempted on the the " |
193 |
| - "variable '{}', but it is a dask array; dask arrays are not " |
194 |
| - "yet supported in resample.interpolate(). Load into " |
195 |
| - "memory with Dataset.load() before resampling." |
196 |
| - .format(self._obj.data.name) |
197 |
| - ) |
198 |
| - |
199 |
| - x = self._obj[self._dim].astype('float') |
200 |
| - y = self._obj.data |
201 |
| - |
202 |
| - axis = self._obj.get_axis_num(self._dim) |
203 |
| - |
204 |
| - f = interp1d(x, y, kind=kind, axis=axis, bounds_error=True, |
205 |
| - assume_sorted=True) |
206 |
| - new_x = self._full_index.values.astype('float') |
207 |
| - |
208 |
| - # construct new up-sampled DataArray |
209 |
| - dummy = self._obj.copy() |
210 |
| - dims = dummy.dims |
211 |
| - |
212 |
| - # drop any existing non-dimension coordinates along the resampling |
213 |
| - # dimension |
214 |
| - coords = OrderedDict() |
215 |
| - for k, v in dummy.coords.items(): |
216 |
| - # is the resampling dimension |
217 |
| - if k == self._dim: |
218 |
| - coords[self._dim] = self._full_index |
219 |
| - # else, check if resampling dim is in coordinate dimensions |
220 |
| - elif self._dim not in v.dims: |
221 |
| - coords[k] = v |
222 |
| - return DataArray(f(new_x), coords, dims, name=dummy.name, |
223 |
| - attrs=dummy.attrs) |
224 |
| - |
225 | 193 |
|
226 | 194 | ops.inject_reduce_methods(DataArrayResample)
|
227 | 195 | ops.inject_binary_ops(DataArrayResample)
|
@@ -308,50 +276,6 @@ def reduce(self, func, dim=None, keep_attrs=None, **kwargs):
|
308 | 276 | return super(DatasetResample, self).reduce(
|
309 | 277 | func, dim, keep_attrs, **kwargs)
|
310 | 278 |
|
311 |
| - def _interpolate(self, kind='linear'): |
312 |
| - """Apply scipy.interpolate.interp1d along resampling dimension.""" |
313 |
| - from .dataset import Dataset |
314 |
| - from .variable import Variable |
315 |
| - from scipy.interpolate import interp1d |
316 |
| - |
317 |
| - old_times = self._obj[self._dim].astype(float) |
318 |
| - new_times = self._full_index.values.astype(float) |
319 |
| - |
320 |
| - data_vars = OrderedDict() |
321 |
| - coords = OrderedDict() |
322 |
| - |
323 |
| - # Apply the interpolation to each DataArray in our original Dataset |
324 |
| - for name, variable in self._obj.variables.items(): |
325 |
| - if name in self._obj.coords: |
326 |
| - if name == self._dim: |
327 |
| - coords[self._dim] = self._full_index |
328 |
| - elif self._dim not in variable.dims: |
329 |
| - coords[name] = variable |
330 |
| - else: |
331 |
| - if isinstance(variable.data, dask_array_type): |
332 |
| - raise TypeError( |
333 |
| - "Up-sampling via interpolation was attempted on the " |
334 |
| - "variable '{}', but it is a dask array; dask arrays " |
335 |
| - "are not yet supprted in resample.interpolate(). Load " |
336 |
| - "into memory with Dataset.load() before resampling." |
337 |
| - .format(name) |
338 |
| - ) |
339 |
| - |
340 |
| - axis = variable.get_axis_num(self._dim) |
341 |
| - |
342 |
| - # We've previously checked for monotonicity along the |
343 |
| - # re-sampling dimension (in __init__ via the GroupBy |
344 |
| - # constructor), so we can avoid sorting the data again by |
345 |
| - # passing 'assume_sorted=True' |
346 |
| - f = interp1d(old_times, variable.data, kind=kind, |
347 |
| - axis=axis, bounds_error=True, |
348 |
| - assume_sorted=True) |
349 |
| - interpolated = Variable(variable.dims, f(new_times)) |
350 |
| - |
351 |
| - data_vars[name] = interpolated |
352 |
| - |
353 |
| - return Dataset(data_vars, coords) |
354 |
| - |
355 | 279 |
|
356 | 280 | ops.inject_reduce_methods(DatasetResample)
|
357 | 281 | ops.inject_binary_ops(DatasetResample)
|
0 commit comments