1
|
|
|
import numpy as np |
|
|
|
|
2
|
|
|
from ..util import cartesian |
3
|
|
|
import scipy.spatial.qhull as qhull |
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
# Mark this as no cover because they are not used at the moment |
7
|
|
|
def interp_weights(xy, uv, d=2): # pragma: no cover |
|
|
|
|
8
|
|
|
|
9
|
|
|
tri = qhull.Delaunay(xy) |
|
|
|
|
10
|
|
|
|
11
|
|
|
simplex = tri.find_simplex(uv) |
12
|
|
|
|
13
|
|
|
vertices = np.take(tri.simplices, simplex, axis=0) |
14
|
|
|
|
15
|
|
|
temp = np.take(tri.transform, simplex, axis=0) |
16
|
|
|
|
17
|
|
|
delta = uv - temp[:, d] |
18
|
|
|
|
19
|
|
|
bary = np.einsum('njk,nk->nj', temp[:, :d, :], delta) |
20
|
|
|
|
21
|
|
|
return vertices, np.hstack((bary, 1 - bary.sum(axis=1, keepdims=True))) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def interpolate(values, vtx, wts): # pragma: no cover |
|
|
|
|
25
|
|
|
|
26
|
|
|
return np.einsum('nj,nj->n', np.take(values, vtx), wts) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class FastLinearInterpolatorIrregularGrid(object): # pragma: no cover |
|
|
|
|
30
|
|
|
|
31
|
|
|
def __init__(self, data_shape, new_coords): |
32
|
|
|
|
33
|
|
|
old_coords = cartesian([np.arange(data_shape[0]), np.arange(data_shape[1])]) |
34
|
|
|
|
35
|
|
|
self._vtx, self._wts = interp_weights(old_coords, new_coords) |
36
|
|
|
|
37
|
|
|
def __call__(self, data): |
38
|
|
|
|
39
|
|
|
return interpolate(data, self._vtx, self._wts) |
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.