|
| 1 | +""" |
| 2 | +Module focused on the Inverse Distance Weighting interpolation technique. |
| 3 | +The IDW algorithm is an average moving interpolation that is usually applied to |
| 4 | +highly variable data. The main idea of this interpolation strategy lies in |
| 5 | +fact that it is not desirable to honour local high/low values but rather to look |
| 6 | +at a moving average of nearby data points and estimate the local trends. |
| 7 | +The node value is calculated by averaging the weighted sum of all the points. |
| 8 | +Data points that lie progressively farther from the node inuence much less the |
| 9 | +computed value than those lying closer to the node. |
| 10 | +
|
| 11 | +:Theoretical Insight: |
| 12 | +
|
| 13 | + This implementation is based on the simplest form of inverse distance |
| 14 | + weighting interpolation, proposed by D. Shepard, A two-dimensional |
| 15 | + interpolation function for irregularly-spaced data, Proceedings of the 23 rd |
| 16 | + ACM National Conference. |
| 17 | +
|
| 18 | + The interpolation value :math:`u` of a given point :math:`\\mathrm{x}` |
| 19 | + from a set of samples :math:`u_k = u(\\mathrm{x}_k)`, with |
| 20 | + :math:`k = 1,2,\dotsc,\\mathcal{N}`, is given by: |
| 21 | +
|
| 22 | + .. math:: |
| 23 | + u(\\mathrm{x}) = \\displaystyle\\sum_{k=1}^\\mathcal{N} |
| 24 | + \\frac{w(\\mathrm{x},\\mathrm{x}_k)} |
| 25 | + {\\displaystyle\\sum_{j=1}^\\mathcal{N} w(\\mathrm{x},\\mathrm{x}_j)} |
| 26 | + u_k |
| 27 | +
|
| 28 | +
|
| 29 | + where, in general, :math:`w(\\mathrm{x}, \\mathrm{x}_i)` represents the |
| 30 | + weighting function: |
| 31 | +
|
| 32 | + .. math:: |
| 33 | + w(\\mathrm{x}, \\mathrm{x}_i) = \\| \\mathrm{x} - \\mathrm{x}_i \\|^{-p} |
| 34 | + |
| 35 | + being :math:`\\| \\mathrm{x} - \\mathrm{x}_i \\|^{-p} \\ge 0` is the |
| 36 | + Euclidean distance between :math:`\\mathrm{x}` and data point |
| 37 | + :math:`\\mathrm{x}_i` and :math:`p` is a power parameter, typically equal to |
| 38 | + 2. |
| 39 | +
|
| 40 | +""" |
| 41 | +import numpy as np |
| 42 | + |
| 43 | +from scipy.spatial.distance import cdist |
| 44 | + |
| 45 | + |
| 46 | +class IDW(object): |
| 47 | + """ |
| 48 | + Class that handles the IDW technique. |
| 49 | +
|
| 50 | + :param idw_parameters: the parameters of the IDW |
| 51 | + :type idw_parameters: :class:`IDWParameters` |
| 52 | + :param numpy.ndarray original_mesh_points: coordinates of the original |
| 53 | + points of the mesh. |
| 54 | +
|
| 55 | + :cvar parameters: the parameters of the IDW. |
| 56 | + :vartype parameters: :class:`~pygem.params_idw.IDWParameters` |
| 57 | + :cvar numpy.ndarray original_mesh_points: coordinates of the original |
| 58 | + points of the mesh. |
| 59 | + :cvar numpy.ndarray modified_mesh_points: coordinates of the deformed |
| 60 | + points of the mesh. |
| 61 | +
|
| 62 | + :Example: |
| 63 | +
|
| 64 | + >>> from pygem.idw import IDW |
| 65 | + >>> from pygem.params_idw import IDWParameters |
| 66 | + >>> import numpy as np |
| 67 | + >>> params = IDWParameters() |
| 68 | + >>> params.read_parameters('tests/test_datasets/parameters_idw_cube.prm') |
| 69 | + >>> nx, ny, nz = (20, 20, 20) |
| 70 | + >>> mesh = np.zeros((nx * ny * nz, 3)) |
| 71 | + >>> xv = np.linspace(0, 1, nx) |
| 72 | + >>> yv = np.linspace(0, 1, ny) |
| 73 | + >>> zv = np.linspace(0, 1, nz) |
| 74 | + >>> z, y, x = np.meshgrid(zv, yv, xv) |
| 75 | + >>> mesh = np.array([x.ravel(), y.ravel(), z.ravel()]) |
| 76 | + >>> original_mesh_points = mesh.T |
| 77 | + >>> idw = IDW(rbf_parameters, original_mesh_points) |
| 78 | + >>> idw.perform() |
| 79 | + >>> new_mesh_points = idw.modified_mesh_points |
| 80 | + """ |
| 81 | + |
| 82 | + def __init__(self, idw_parameters, original_mesh_points): |
| 83 | + self.parameters = idw_parameters |
| 84 | + self.original_mesh_points = original_mesh_points |
| 85 | + self.modified_mesh_points = None |
| 86 | + |
| 87 | + def perform(self): |
| 88 | + """ |
| 89 | + This method performs the deformation of the mesh points. After the |
| 90 | + execution it sets `self.modified_mesh_points`. |
| 91 | + """ |
| 92 | + |
| 93 | + def distance(u, v): |
| 94 | + return np.linalg.norm(u - v, self.parameters.power) |
| 95 | + |
| 96 | + # Compute displacement of the control points |
| 97 | + displ = ( |
| 98 | + self.parameters.deformed_control_points - |
| 99 | + self.parameters.original_control_points |
| 100 | + ) |
| 101 | + |
| 102 | + # Compute the distance between the mesh points and the control points |
| 103 | + dist = cdist( |
| 104 | + self.original_mesh_points, |
| 105 | + self.parameters.original_control_points, |
| 106 | + distance |
| 107 | + ) |
| 108 | + |
| 109 | + # Weights are set as the reciprocal of the distance if the distance is |
| 110 | + # not zero, otherwise 1.0 where distance is zero. |
| 111 | + weights = np.zeros(dist.shape) |
| 112 | + for i, d in enumerate(dist): |
| 113 | + weights[i] = 1. / d if d.all() else np.where(d == 0.0, 1.0, 0.0) |
| 114 | + |
| 115 | + |
| 116 | + offset = np.array([ |
| 117 | + np.sum(displ * wi[:, np.newaxis] / np.sum(wi), axis=0) |
| 118 | + for wi in weights |
| 119 | + ]) |
| 120 | + |
| 121 | + self.modified_mesh_points = self.original_mesh_points + offset |
0 commit comments