-
Notifications
You must be signed in to change notification settings - Fork 2
/
interpolate.py
232 lines (197 loc) · 7.74 KB
/
interpolate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from __future__ import division, print_function, absolute_import
import numpy as np
from scipy import ndimage
__all__ = ["interp_grid", "EqualGridInterpolator"]
def interp_grid(coord, grids, value, order=1, padding='constant',
fill_value=np.nan):
"""Interpolation on a equal spaced regular grid in arbitrary dimensions.
Parameters
----------
coord : tuple of ndarray
The coordinates to interpolate.
grids : tuple of ndarray, shape (m1,), ..., (mn,)
The points defining the equal regular grid in n dimensions.
value : array_like, shape (m1, ..., mn)
The data on the regular grid in n dimensions.
order : int
The order of the spline interpolation, default is 1.
The order has to be in the range [0, 5].
0 means nearest interpolation.
padding : str
Points outside the boundaries of the input are filled according
to the given mode ('constant', 'nearest', 'reflect' or 'wrap').
fill_value : number, optional
If provided, the value to use for points outside of the
interpolation domain.
Examples
--------
1D example:
x = np.linspace(-3, 3, 2000)
y = np.sin(x)
xi = np.random.rand(10000) * 6 -3
yi = interp_grid(xi, x, y)
y2 = np.interp(xi, x, y)
print(np.allclose(yi, y2))
# True
# Timing
%timeit -n10 -r1 interp_grid(xi, x, y)
%timeit -n10 -r1 np.interp(xi, x, y)
2D example:
f = lambda x, y: x**2- y**2
x, y = np.linspace(-2, 3, 5), np.linspace(-3, 2, 6)
z = f(*np.meshgrid(x, y, indexing='ij'))
xi, yi = np.random.rand(100), np.random.rand(100)
zi = interp_grid((xi, yi), (x, y), z, order=1)
See also
--------
numpy.interp
scipy.interpolate.RegularGridInterpolator
scipy.ndimage.map_coordinates
References
----------
NI_GeometricTransform at
https://github.com/scipy/scipy/blob/master/scipy/ndimage/src/ni_interpolation.c
"""
#coord = [np.asarray(xi) for xi in coord]
if value.ndim == 1:
if len(coord) != 1:
coord = [coord]
if grids is not None and len(grids) != 1:
grids = [grids]
if grids is None:
xi = coord
else:
xi = [(x - b[0]) / (b[1] - b[0]) for x, b in zip(coord, grids)]
if len(xi) == 1:
xi = xi[0][np.newaxis]
else:
xi = np.asarray(xi)
yi = ndimage.map_coordinates(value, xi, order=order, mode=padding,
cval=fill_value)
return yi
class EqualGridInterpolator(object):
"""
Interpolation on a equal spaced regular grid in arbitrary dimensions.
Fork from https://github.com/JohannesBuchner/regulargrid
"""
def __init__(self, points, values, order=1, padding='constant',
fill_value=np.nan):
'''
Parameters
----------
points : tuple of ndarray, shape (m1, ), ..., (mn, )
The points defining the equal regular grid in n dimensions.
values : array_like, shape (m1, ..., mn)
The data on the regular grid in n dimensions.
order : int
The order of the spline interpolation, default is 1.
The order has to be in the range 0 to 5.
0 means nearest interpolation.
padding : str
Points outside the boundaries of the input are filled according
to the given mode ('constant', 'nearest', 'reflect' or 'wrap').
fill_value : number, optional
If provided, the value to use for points outside of the
interpolation domain.
Examples
--------
import numpy as np
f = lambda x, y: 1 - x + y
x, y = np.linspace(-2, 3, 5), np.linspace(-3, 2, 6)
z = f(*np.meshgrid(x, y, indexing='ij'))
f_interp = EqualGridInterpolator((x, y), z, order=1)
xi, yi = np.meshgrid(np.linspace(-2, 3, 50), np.linspace(-3, 2, 60),
indexing='ij')
zi_true = f(xi, yi)
zi_interp = f_interp([xi, yi])
np.allclose(zi_true, zi_interp)
'''
values = np.asfarray(values)
if len(points) != values.ndim:
raise ValueError('invalid shape for points array')
points = [np.asarray(p) for p in points]
for i, p in enumerate(points):
if p.ndim != 1 or p.size <= 1:
raise ValueError('invalid shape for points array')
if p[0] == p[1] or not np.allclose(np.diff(p), p[1] - p[0]):
raise ValueError('points array should be equally spaced!')
if p.size != values.shape[i]:
raise ValueError('inconsistent shape for points and values')
self.order = order
self.padding = padding
self.fill_value = fill_value
self.ndim = len(points)
self.grid = tuple(points)
self.values = values
self.edges = tuple([p[0] for p in points])
self.steps = tuple([p[1] - p[0] for p in points])
self.coeffs = {0: self.values, 1: self.values}
def __call__(self, xi, order=None):
'''
xi : tuple of ndarray
The coordinates to sample the gridded data at.
order : int
The order of the spline interpolation.
'''
if len(xi) != self.ndim:
raise ValueError("input array has unmatched shape!")
xi = [(xi[i] - self.edges[i]) / self.steps[i] for i in range(self.ndim)]
xi = np.broadcast_arrays(*xi)
xi = np.array(xi, dtype='float')
scalar = (xi.ndim == 1)
if scalar:
xi = xi[..., np.newaxis]
order = self.order if order is None else order
values = self._coeffs(order)
yi = ndimage.map_coordinates(values, xi, order=order,
prefilter=False,
mode=self.padding,
cval=self.fill_value)
if scalar:
return yi[0]
else:
return yi
def _coeffs(self, order):
if order not in self.coeffs:
coeff = ndimage.spline_filter(self.values, order=order)
self.coeffs[order] = coeff
return self.coeffs[order]
if __name__ == "__main__":
import numpy as np
from scipy.interpolate import RegularGridInterpolator
from matplotlib import pyplot as plt
# Example 1
f = lambda x, y: np.sin(x / 2) - np.sin(y)
x, y = np.linspace(-2, 3, 5), np.linspace(-3, 2, 6)
z = f(*np.meshgrid(x, y))
xi, yi = np.meshgrid(np.linspace(-2, 3, 50), np.linspace(-3, 2, 60))
zi1 = EqualGridInterpolator((x, y), z.T, order=1)((xi, yi))
zi2 = RegularGridInterpolator((x, y), z.T)((xi, yi))
assert np.allclose(zi1, zi2)
zi1 = EqualGridInterpolator((x, y), z.T, order=0)((xi, yi))
zi2 = RegularGridInterpolator((x, y), z.T, method='nearest')((xi, yi))
assert np.allclose(zi1, zi2)
f = lambda x, y: x * y
mid = lambda x: (x[1:] + x[:-1]) / 2.
x = np.linspace(0, 2, 10)
y = np.linspace(0, 2, 15)
x_, y_ = mid(x), mid(y)
z = f(*np.meshgrid(x_, y_))
# Example 2
xi = np.linspace(0, 2, 40)
yi = np.linspace(0, 2, 60)
xi_, yi_ = mid(xi), mid(yi)
zi1 = EqualGridInterpolator((x_, y_), z.T, order=3)(np.meshgrid(xi_, yi_))
zi2 = EqualGridInterpolator((x_, y_), z.T)(np.meshgrid(xi_, yi_))
zi3 = EqualGridInterpolator((x_, y_), z.T, padding='nearest')(np.meshgrid(xi_, yi_))
plt.figure(figsize=(9, 9))
plt.viridis()
plt.subplot(221)
plt.pcolormesh(x, y, z)
plt.subplot(222)
plt.pcolormesh(xi, yi, np.ma.array(zi1, mask=np.isnan(zi1)))
plt.subplot(223)
plt.pcolormesh(xi, yi, np.ma.array(zi2, mask=np.isnan(zi2)))
plt.subplot(224)
plt.pcolormesh(xi, yi, zi3)
plt.show()