Skip to content

Commit 743bbc0

Browse files
committed
minor fixes
1 parent 3d8f150 commit 743bbc0

File tree

1 file changed

+39
-44
lines changed

1 file changed

+39
-44
lines changed

pygem/radial.py

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
1212
As reference please consult M. D. Buhmann. Radial Basis Functions, volume 12 of Cambridge
1313
monographs on applied and computational mathematics. Cambridge University Press, UK, 2003.
14-
RBF shape parametrization technique is based on the definition of a map,
14+
RBF shape parametrization technique is based on the definition of a map,
1515
:math:`\\mathcal{M}(\\boldsymbol{x}) : \\mathbb{R}^n \\rightarrow \\mathbb{R}^n`, that allows the
16-
possibility of transferring data across non-matching grids and facing the dynamic mesh handling.
16+
possibility of transferring data across non-matching grids and facing the dynamic mesh handling.
1717
The map introduced is defines as follows
1818
1919
.. math::
@@ -23,10 +23,10 @@
2323
where :math:`p(\\boldsymbol{x})` is a low_degree polynomial term, :math:`\\gamma_i` is the weight,
2424
corresponding to the a-priori selected :math:`\\mathcal{N}_C` control points, associated to the
2525
:math:`i`-th basis function, and :math:`\\varphi(\\| \\boldsymbol{x} - \\boldsymbol{x_{C_i}} \\|)`
26-
a radial function based on the Euclidean distance between the control points position
26+
a radial function based on the Euclidean distance between the control points position
2727
:math:`\\boldsymbol{x_{C_i}}` and :math:`\\boldsymbol{x}`. A radial basis function, generally, is
28-
a real-valued function whose value depends only on the distance from the origin, so that
29-
:math:`\\varphi(\\boldsymbol{x}) = \\tilde{\\varphi}(\\| \\boldsymbol{x} \\|)`.
28+
a real-valued function whose value depends only on the distance from the origin, so that
29+
:math:`\\varphi(\\boldsymbol{x}) = \\tilde{\\varphi}(\\| \\boldsymbol{x} \\|)`.
3030
3131
The matrix version of the formula above is:
3232
@@ -41,11 +41,7 @@
4141
Gaussian splines, Multi-quadratic biharmonic splines, Inverted multi-quadratic biharmonic splines,
4242
Thin-plate splines and Beckert and Wendland :math:`C^2` basis all defined and implemented below.
4343
"""
44-
import os
45-
import params as rbfp
4644
import numpy as np
47-
from mpl_toolkits.mplot3d import axes3d
48-
import matplotlib.pyplot as plt
4945

5046

5147
class RBF(object):
@@ -64,7 +60,7 @@ class RBF(object):
6460
implemented to the actual implementation.
6561
:cvar numpy.matrix weights: the matrix formed by the weights corresponding to the a-priori
6662
selected N control points, associated to the basis functions and c and Q terms that
67-
describe the polynomial of order one p(x) = c + Qx. The shape is
63+
describe the polynomial of order one p(x) = c + Qx. The shape is
6864
(n_control_points+1+3)-by-3. It is computed internally.
6965
7066
:Example:
@@ -73,7 +69,7 @@ class RBF(object):
7369
>>> import pygem.params as rbfp
7470
>>> import numpy as np
7571
76-
>>> rbf_parameters = rbfp.FFDParameters()
72+
>>> rbf_parameters = rbfp.RBFParameters()
7773
>>> rbf_parameters.read_parameters('tests/test_datasets/parameters_rbf_cube.prm')
7874
7975
>>> nx, ny, nz = (20, 20, 20)
@@ -84,7 +80,7 @@ class RBF(object):
8480
>>> z, y, x = np.meshgrid(zv, yv, xv)
8581
>>> mesh = np.array([x.ravel(), y.ravel(), z.ravel()])
8682
>>> original_mesh_points = mesh.T
87-
83+
8884
>>> radial_trans = rbf.RBF(rbf_parameters, original_mesh_points)
8985
>>> radial_trans.perform()
9086
>>> new_mesh_points = radial_trans.modified_mesh_points
@@ -93,26 +89,25 @@ def __init__(self, rbf_parameters, original_mesh_points):
9389
self.parameters = rbf_parameters
9490
self.original_mesh_points = original_mesh_points
9591
self.modified_mesh_points = None
96-
97-
self.bases = {
98-
'gaussian_spline': self.gaussian_spline,
99-
'multi_quadratic_biharmonic_spline': self.multi_quadratic_biharmonic_spline,
100-
'inv_multi_quadratic_biharmonic_spline': self.inv_multi_quadratic_biharmonic_spline,
101-
'thin_plate_spline': self.thin_plate_spline,
102-
'beckert_wendland_c2_basis': self.beckert_wendland_c2_basis
103-
}
104-
105-
# to make the str callable we have to use a dictionary with all the implemented radial basis functions
106-
if params.basis in self.bases:
107-
self.basis = self.bases[params.basis]
92+
93+
self.bases = {'gaussian_spline': self.gaussian_spline, \
94+
'multi_quadratic_biharmonic_spline': self.multi_quadratic_biharmonic_spline, \
95+
'inv_multi_quadratic_biharmonic_spline': self.inv_multi_quadratic_biharmonic_spline, \
96+
'thin_plate_spline': self.thin_plate_spline, \
97+
'beckert_wendland_c2_basis': self.beckert_wendland_c2_basis}
98+
99+
# to make the str callable we have to use a dictionary with all the implemented
100+
# radial basis functions
101+
if self.parameters.basis in self.bases:
102+
self.basis = self.bases[self.parameters.basis]
108103
else:
109104
raise NameError('The name of the basis function in the parameters file is not correct ' + \
110105
'or not implemented. Check the documentation for all the available functions.')
111106

112107
self.weights = self._get_weights(self.parameters.original_control_points, \
113108
self.parameters.deformed_control_points)
114109

115-
110+
116111
@staticmethod
117112
def gaussian_spline(X, r):
118113
"""
@@ -128,10 +123,10 @@ def gaussian_spline(X, r):
128123
:rtype: float
129124
"""
130125
norm = np.linalg.norm(X)
131-
result = np.exp( -(norm * norm) / (r * r) )
126+
result = np.exp(-(norm * norm) / (r * r))
132127
return result
133-
134-
128+
129+
135130
@staticmethod
136131
def multi_quadratic_biharmonic_spline(X, r):
137132
"""
@@ -147,10 +142,10 @@ def multi_quadratic_biharmonic_spline(X, r):
147142
:rtype: float
148143
"""
149144
norm = np.linalg.norm(X)
150-
result = np.sqrt( (norm * norm) + (r * r) )
145+
result = np.sqrt((norm * norm) + (r * r))
151146
return result
152-
153-
147+
148+
154149
@staticmethod
155150
def inv_multi_quadratic_biharmonic_spline(X, r):
156151
"""
@@ -165,17 +160,18 @@ def inv_multi_quadratic_biharmonic_spline(X, r):
165160
:return: result: the result of the formula above.
166161
:rtype: float
167162
"""
168-
result = 1.0/multi_quadratic_biharmonic_spline(X, r)
163+
norm = np.linalg.norm(X)
164+
result = 1.0 / (np.sqrt((norm * norm) + (r * r)))
169165
return result
170-
171-
166+
167+
172168
@staticmethod
173169
def thin_plate_spline(X, r):
174170
"""
175171
It implements the following formula:
176172
177173
.. math::
178-
\\varphi(\\| \\boldsymbol{x} \\|) = \\left\\| \\frac{\\boldsymbol{x} }{r} \\right\\|^2
174+
\\varphi(\\| \\boldsymbol{x} \\|) = \\left\\| \\frac{\\boldsymbol{x} }{r} \\right\\|^2
179175
\\ln \\left\\| \\frac{\\boldsymbol{x} }{r} \\right\\|
180176
181177
:param numpy.ndarray X: the vector x in the formula above.
@@ -190,8 +186,8 @@ def thin_plate_spline(X, r):
190186
if norm > 0:
191187
result *= np.log(norm)
192188
return result
193-
194-
189+
190+
195191
@staticmethod
196192
def beckert_wendland_c2_basis(X, r):
197193
"""
@@ -215,11 +211,11 @@ def beckert_wendland_c2_basis(X, r):
215211
second = (4 * arg) + 1
216212
result = first * second
217213
return result
218-
214+
219215

220216
def _distance_matrix(self, X1, X2):
221217
"""
222-
This private method returns the following matrix:
218+
This private method returns the following matrix:
223219
:math:`\\boldsymbol{D_{ij}} = \\varphi(\\| \\boldsymbol{x_i} - \\boldsymbol{y_j} \\|)`
224220
225221
:param numpy.ndarray X1: the vector x in the formula above.
@@ -234,8 +230,8 @@ def _distance_matrix(self, X1, X2):
234230
for j in range(0, n):
235231
matrix[i][j] = self.basis(X1[i] - X2[j], self.parameters.radius)
236232
return matrix
237-
238-
233+
234+
239235
def _get_weights(self, X, Y):
240236
"""
241237
This private method, given the original control points and the deformed ones, returns the matrix
@@ -260,15 +256,14 @@ def _get_weights(self, X, Y):
260256
inv_H = np.linalg.inv(H)
261257
weights = np.dot(inv_H, rhs)
262258
return weights
263-
264-
259+
260+
265261
def perform(self):
266262
"""
267263
This method performs the deformation of the mesh points. After the execution
268264
it sets `self.modified_mesh_points`.
269265
"""
270266
n_points = self.original_mesh_points.shape[0]
271-
dim = self.original_mesh_points.shape[1]
272267
dist = self._distance_matrix(self.original_mesh_points, self.parameters.original_control_points)
273268
identity = np.ones(n_points).reshape(n_points, 1)
274269
H = np.bmat([[dist, identity, self.original_mesh_points]])

0 commit comments

Comments
 (0)