Skip to content

Commit c93ab4c

Browse files
committed
Clean code
- rewrite method `save` from `rbfparams` class - code formatter
1 parent 3f4afc3 commit c93ab4c

File tree

10 files changed

+177
-141
lines changed

10 files changed

+177
-141
lines changed

pygem/filehandler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def _check_extension(self, filename):
5050
if file_ext not in self.extensions:
5151
raise ValueError(
5252
'The input file does not have the proper extension. \
53-
It is {0!s}, instead of {1!s}.'
54-
.format(file_ext, self.extensions))
53+
It is {0!s}, instead of {1!s}.'.format(file_ext,
54+
self.extensions))
5555

5656
@staticmethod
5757
def _check_filename_type(filename):

pygem/freeform.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ def perform(self):
9898
self.original_mesh_points - translation, transformation)
9999

100100
# select mesh points inside bounding box
101-
mesh_points = reference_frame_mesh_points[(
102-
reference_frame_mesh_points[:, 0] >= 0.) & (
103-
reference_frame_mesh_points[:, 0] <= 1.) & (
104-
reference_frame_mesh_points[:, 1] >= 0.) & (
105-
reference_frame_mesh_points[:, 1] <= 1.) & (
106-
reference_frame_mesh_points[:, 2] >= 0.) & (
107-
reference_frame_mesh_points[:, 2] <= 1.)]
101+
mesh_points = reference_frame_mesh_points[
102+
(reference_frame_mesh_points[:, 0] >= 0.)
103+
& (reference_frame_mesh_points[:, 0] <= 1.) &
104+
(reference_frame_mesh_points[:, 1] >= 0.) &
105+
(reference_frame_mesh_points[:, 1] <= 1.) &
106+
(reference_frame_mesh_points[:, 2] >= 0.) &
107+
(reference_frame_mesh_points[:, 2] <= 1.)]
108108
(n_rows_mesh, n_cols_mesh) = mesh_points.shape
109109

110110
# Initialization. In order to exploit the contiguity in memory the
@@ -156,12 +156,13 @@ def perform(self):
156156

157157
# merge non-shifted mesh points with shifted ones
158158
self.modified_mesh_points = np.copy(self.original_mesh_points)
159-
self.modified_mesh_points[(reference_frame_mesh_points[:, 0] >= 0.) & (
160-
reference_frame_mesh_points[:, 0] <= 1.
161-
) & (reference_frame_mesh_points[:, 1] >= 0.) & (
162-
reference_frame_mesh_points[:, 1] <= 1.) & (
163-
reference_frame_mesh_points[:, 2] >= 0.) & (
164-
reference_frame_mesh_points[:, 2] <= 1.)] = new_mesh_points
159+
self.modified_mesh_points[(reference_frame_mesh_points[:, 0] >= 0.)
160+
& (reference_frame_mesh_points[:, 0] <= 1.) &
161+
(reference_frame_mesh_points[:, 1] >= 0.) &
162+
(reference_frame_mesh_points[:, 1] <= 1.) &
163+
(reference_frame_mesh_points[:, 2] >= 0.) &
164+
(reference_frame_mesh_points[:, 2] <=
165+
1.)] = new_mesh_points
165166

166167
@staticmethod
167168
def _transform_points(original_points, transformation):

pygem/nurbshandler.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ def load_shape_from_file(self, filename):
7979
8080
Not implemented, it has to be implemented in subclasses.
8181
"""
82-
raise NotImplementedError('Subclass must implement abstract method ' +
83-
self.__class__.__name__ +
84-
'.load_shape_from_file')
82+
raise NotImplementedError(
83+
'Subclass must implement abstract method ' +
84+
self.__class__.__name__ + '.load_shape_from_file')
8585

8686
def parse(self, filename):
8787
"""
@@ -138,8 +138,8 @@ def parse(self, filename):
138138
# pushing the control points coordinates to the mesh_points array (used for FFD)
139139
mesh_points = np.append(
140140
mesh_points, control_polygon_coordinates, axis=0)
141-
control_point_position.append(control_point_position[-1] + n_poles_u
142-
* n_poles_v)
141+
control_point_position.append(
142+
control_point_position[-1] + n_poles_u * n_poles_v)
143143

144144
n_faces += 1
145145
faces_explorer.Next()
@@ -299,8 +299,8 @@ def parse_face(topo_face):
299299
bspline_converter = BRepBuilderAPI_NurbsConvert(edge)
300300
bspline_converter.Perform(edge)
301301
bspline_tshape_edge = bspline_converter.Shape()
302-
h_geom_edge = BRep_Tool_Curve(topods_Edge(bspline_tshape_edge))[
303-
0]
302+
h_geom_edge = BRep_Tool_Curve(
303+
topods_Edge(bspline_tshape_edge))[0]
304304
h_bspline_edge = geomconvert_CurveToBSplineCurve(h_geom_edge)
305305
bspline_geom_edge = h_bspline_edge.GetObject()
306306

@@ -314,7 +314,9 @@ def parse_face(topo_face):
314314
for i in range(1, nb_poles + 1):
315315
ctrlpt = edge_ctrlpts.Value(i)
316316
ctrlpt_position = np.array(
317-
[[ctrlpt.Coord(1), ctrlpt.Coord(2), ctrlpt.Coord(3)]])
317+
[[ctrlpt.Coord(1),
318+
ctrlpt.Coord(2),
319+
ctrlpt.Coord(3)]])
318320
points_single_edge = np.append(
319321
points_single_edge, ctrlpt_position, axis=0)
320322

@@ -342,8 +344,10 @@ def parse_face(topo_face):
342344
for indice_u_direction in range(1, nb_u + 1):
343345
for indice_v_direction in range(1, nb_v + 1):
344346
ctrlpt = ctrlpts.Value(indice_u_direction, indice_v_direction)
345-
ctrlpt_position = np.array([[ctrlpt.Coord(1), ctrlpt.Coord(2),
346-
ctrlpt.Coord(3)]])
347+
ctrlpt_position = np.array(
348+
[[ctrlpt.Coord(1),
349+
ctrlpt.Coord(2),
350+
ctrlpt.Coord(3)]])
347351
mesh_points_face = np.append(
348352
mesh_points_face, ctrlpt_position, axis=0)
349353

@@ -548,8 +552,8 @@ def write_face(self, points_face, list_points_edge, topo_face, toledge):
548552
stol.SetTolerance(new_edge_toadd, toledge * 10.0)
549553
new_bspline_twire.Add(new_edge_toadd)
550554
else:
551-
deformed_edge_revers = deformed_edges[np.abs(
552-
deformed_edge_i) - 1]
555+
deformed_edge_revers = deformed_edges[
556+
np.abs(deformed_edge_i) - 1]
553557
stol.SetTolerance(deformed_edge_revers, toledge)
554558
new_bspline_twire.Add(deformed_edge_revers)
555559
if new_bspline_twire.Error() != 0:

pygem/params/ffdparams.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ def position_vertices(self):
123123
:rtype: numpy.ndarray
124124
"""
125125
return self.origin_box + np.vstack([
126-
np.zeros(
127-
(1, 3)), self.rotation_matrix.dot(np.diag(self.lenght_box)).T
126+
np.zeros((1, 3)),
127+
self.rotation_matrix.dot(np.diag(self.lenght_box)).T
128128
])
129129

130130
def read_parameters(self, filename='parameters.prm'):
@@ -203,12 +203,12 @@ def write_parameters(self, filename='parameters.prm'):
203203
output_string += ' points in each direction (x, y, z).\n'
204204
output_string += '# For example, to create a 2 x 3 x 2 grid, use the'
205205
output_string += ' following: n control points: 2, 3, 2\n'
206-
output_string += 'n control points x: ' + str(self.n_control_points[
207-
0]) + '\n'
208-
output_string += 'n control points y: ' + str(self.n_control_points[
209-
1]) + '\n'
210-
output_string += 'n control points z: ' + str(self.n_control_points[
211-
2]) + '\n'
206+
output_string += 'n control points x: ' + str(
207+
self.n_control_points[0]) + '\n'
208+
output_string += 'n control points y: ' + str(
209+
self.n_control_points[1]) + '\n'
210+
output_string += 'n control points z: ' + str(
211+
self.n_control_points[2]) + '\n'
212212

213213
output_string += '\n# box lenght indicates the length of the FFD '
214214
output_string += 'bounding box along the three canonical directions '
@@ -282,8 +282,8 @@ def write_parameters(self, filename='parameters.prm'):
282282
for j in range(0, self.n_control_points[1]):
283283
for k in range(0, self.n_control_points[2]):
284284
output_string += offset * ' ' + str(i) + ' ' + str(
285-
j) + ' ' + str(k) + ' ' + str(self.array_mu_x[i][j][
286-
k]) + '\n'
285+
j) + ' ' + str(k) + ' ' + str(
286+
self.array_mu_x[i][j][k]) + '\n'
287287
offset = 13
288288

289289
output_string += '\n# parameter y collects the displacements along y, '
@@ -295,8 +295,8 @@ def write_parameters(self, filename='parameters.prm'):
295295
for j in range(0, self.n_control_points[1]):
296296
for k in range(0, self.n_control_points[2]):
297297
output_string += offset * ' ' + str(i) + ' ' + str(
298-
j) + ' ' + str(k) + ' ' + str(self.array_mu_y[i][j][
299-
k]) + '\n'
298+
j) + ' ' + str(k) + ' ' + str(
299+
self.array_mu_y[i][j][k]) + '\n'
300300
offset = 13
301301

302302
output_string += '\n# parameter z collects the displacements along z, '
@@ -308,8 +308,8 @@ def write_parameters(self, filename='parameters.prm'):
308308
for j in range(0, self.n_control_points[1]):
309309
for k in range(0, self.n_control_points[2]):
310310
output_string += offset * ' ' + str(i) + ' ' + str(
311-
j) + ' ' + str(k) + ' ' + str(self.array_mu_z[i][j][
312-
k]) + '\n'
311+
j) + ' ' + str(k) + ' ' + str(
312+
self.array_mu_z[i][j][k]) + '\n'
313313
offset = 13
314314

315315
with open(filename, 'w') as f:
@@ -359,28 +359,29 @@ def save(self, filename, write_deformed=True):
359359
y = np.linspace(0, self.lenght_box[1], self.n_control_points[1])
360360
z = np.linspace(0, self.lenght_box[2], self.n_control_points[2])
361361

362-
lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(y, x,
363-
z)
362+
lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(
363+
y, x, z)
364364

365365
if write_deformed:
366366
box_points = np.array([
367-
lattice_x_coords.ravel() + self.array_mu_x.ravel() *
368-
self.lenght_box[0], lattice_y_coords.ravel() +
367+
lattice_x_coords.ravel() +
368+
self.array_mu_x.ravel() * self.lenght_box[0],
369+
lattice_y_coords.ravel() +
369370
self.array_mu_y.ravel() * self.lenght_box[1],
370-
lattice_z_coords.ravel() + self.array_mu_z.ravel() *
371-
self.lenght_box[2]
371+
lattice_z_coords.ravel() +
372+
self.array_mu_z.ravel() * self.lenght_box[2]
372373
])
373374
else:
374375
box_points = np.array([
375-
lattice_x_coords.ravel(), lattice_y_coords.ravel(),
376+
lattice_x_coords.ravel(),
377+
lattice_y_coords.ravel(),
376378
lattice_z_coords.ravel()
377379
])
378380

379381
n_rows = box_points.shape[1]
380382

381-
box_points = np.dot(
382-
self.rotation_matrix,
383-
box_points) + np.transpose(np.tile(self.origin_box, (n_rows, 1)))
383+
box_points = np.dot(self.rotation_matrix, box_points) + np.transpose(
384+
np.tile(self.origin_box, (n_rows, 1)))
384385

385386
points = vtk.vtkPoints()
386387

pygem/params/rbfparams.py

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ def read_parameters(self, filename='parameters_rbf.prm'):
8888
for line, i in zip(lines, list(range(0, self.n_control_points))):
8989
values = line.split()
9090
self.original_control_points[i] = np.array(
91-
[float(values[0]), float(values[1]), float(values[2])])
91+
[float(values[0]),
92+
float(values[1]),
93+
float(values[2])])
9294

9395
mod_points = config.get('Control points', 'deformed control points')
9496
lines = mod_points.split('\n')
@@ -103,7 +105,9 @@ def read_parameters(self, filename='parameters_rbf.prm'):
103105
for line, i in zip(lines, list(range(0, self.n_control_points))):
104106
values = line.split()
105107
self.deformed_control_points[i] = np.array(
106-
[float(values[0]), float(values[1]), float(values[2])])
108+
[float(values[0]),
109+
float(values[1]),
110+
float(values[2])])
107111

108112
def write_parameters(self, filename='parameters_rbf.prm'):
109113
"""
@@ -116,55 +120,63 @@ def write_parameters(self, filename='parameters_rbf.prm'):
116120
if not isinstance(filename, str):
117121
raise TypeError("filename must be a string")
118122

119-
with open(filename, 'w') as output_file:
120-
output_file.write('\n[Radial Basis Functions]\n')
121-
output_file.write(
122-
'# This section describes the radial basis functions shape.\n')
123-
124-
output_file.write(
125-
'\n# basis funtion is the name of the basis functions to use in the transformation. The functions\n')
126-
output_file.write(
127-
'# implemented so far are: gaussian_spline, multi_quadratic_biharmonic_spline,\n')
128-
output_file.write(
129-
'# inv_multi_quadratic_biharmonic_spline, thin_plate_spline, beckert_wendland_c2_basis, polyharmonic_spline.\n')
130-
output_file.write(
131-
'# For a comprehensive list with details see the class RBF.\n')
132-
output_file.write('basis function: ' + str(self.basis) + '\n')
133-
134-
output_file.write(
135-
'\n# radius is the scaling parameter r that affects the shape of the basis functions. See the documentation\n')
136-
output_file.write('# of the class RBF for details.\n')
137-
output_file.write('radius: ' + str(self.radius) + '\n')
138-
output_file.write(
139-
'\n# The power parameter k for polyharmonic spline')
140-
output_file.write('\n# See the documentation for details\n')
141-
output_file.write('power: ' + str(self.power) + '\n')
142-
143-
output_file.write('\n\n[Control points]\n')
144-
output_file.write(
145-
'# This section describes the RBF control points.\n')
146-
147-
output_file.write(
148-
'\n# original control points collects the coordinates of the interpolation control points before the deformation.\n')
149-
output_file.write('original control points:')
150-
offset = 1
151-
for i in range(0, self.n_control_points):
152-
output_file.write(offset * ' ' + str(
153-
self.original_control_points[i][0]) + ' ' + str(
154-
self.original_control_points[i][1]) + ' ' + str(
155-
self.original_control_points[i][2]) + '\n')
156-
offset = 25
157-
158-
output_file.write(
159-
'\n# deformed control points collects the coordinates of the interpolation control points after the deformation.\n')
160-
output_file.write('deformed control points:')
161-
offset = 1
162-
for i in range(0, self.n_control_points):
163-
output_file.write(offset * ' ' + str(
164-
self.deformed_control_points[i][0]) + ' ' + str(
165-
self.deformed_control_points[i][1]) + ' ' + str(
166-
self.deformed_control_points[i][2]) + '\n')
167-
offset = 25
123+
output_string = ""
124+
output_string += '\n[Radial Basis Functions]\n'
125+
output_string += '# This section describes the radial basis functions'
126+
output_string += ' shape.\n'
127+
128+
output_string += '\n# basis funtion is the name of the basis functions'
129+
output_string += ' to use in the transformation. The functions\n'
130+
output_string += '# implemented so far are: gaussian_spline,'
131+
output_string += ' multi_quadratic_biharmonic_spline,\n'
132+
output_string += '# inv_multi_quadratic_biharmonic_spline,'
133+
output_string += ' thin_plate_spline, beckert_wendland_c2_basis,'
134+
output_string += ' polyharmonic_spline.\n'
135+
output_string += '# For a comprehensive list with details see the'
136+
output_string += ' class RBF.\n'
137+
output_string += 'basis function: {}\n'.format(str(self.basis))
138+
139+
output_string += '\n# radius is the scaling parameter r that affects'
140+
output_string += ' the shape of the basis functions. See the'
141+
output_string += ' documentation\n'
142+
output_string += '# of the class RBF for details.\n'
143+
output_string += 'radius: {}\n'.format(str(self.radius))
144+
145+
output_string += '\n# The power parameter k for polyharmonic spline'
146+
output_string += '\n# See the documentation for details\n'
147+
output_string += 'power: {}\n'.format(self.power)
148+
149+
output_string += '\n\n[Control points]\n'
150+
output_string += '# This section describes the RBF control points.\n'
151+
152+
output_string += '\n# original control points collects the coordinates'
153+
output_string += ' of the interpolation control points before the'
154+
output_string += ' deformation.\n'
155+
156+
output_string += 'original control points:'
157+
offset = 1
158+
for i in range(0, self.n_control_points):
159+
output_string += offset * ' ' + str(
160+
self.original_control_points[i][0]) + ' ' + str(
161+
self.original_control_points[i][1]) + ' ' + str(
162+
self.original_control_points[i][2]) + '\n'
163+
offset = 25
164+
165+
output_string += '\n# deformed control points collects the coordinates'
166+
output_string += ' of the interpolation control points after the'
167+
output_string += ' deformation.\n'
168+
169+
output_string += 'deformed control points:'
170+
offset = 1
171+
for i in range(0, self.n_control_points):
172+
output_string += offset * ' ' + str(
173+
self.deformed_control_points[i][0]) + ' ' + str(
174+
self.deformed_control_points[i][1]) + ' ' + str(
175+
self.deformed_control_points[i][2]) + '\n'
176+
offset = 25
177+
178+
with open(filename, 'w') as f:
179+
f.write(output_string)
168180

169181
def __str__(self):
170182
"""

pygem/radial.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,18 @@ def __init__(self, rbf_parameters, original_mesh_points):
108108
self.modified_mesh_points = None
109109

110110
self.bases = {
111-
'gaussian_spline': self.gaussian_spline,
111+
'gaussian_spline':
112+
self.gaussian_spline,
112113
'multi_quadratic_biharmonic_spline':
113114
self.multi_quadratic_biharmonic_spline,
114115
'inv_multi_quadratic_biharmonic_spline':
115116
self.inv_multi_quadratic_biharmonic_spline,
116-
'thin_plate_spline': self.thin_plate_spline,
117-
'beckert_wendland_c2_basis': self.beckert_wendland_c2_basis,
118-
'polyharmonic_spline': self.polyharmonic_spline
117+
'thin_plate_spline':
118+
self.thin_plate_spline,
119+
'beckert_wendland_c2_basis':
120+
self.beckert_wendland_c2_basis,
121+
'polyharmonic_spline':
122+
self.polyharmonic_spline
119123
}
120124

121125
# to make the str callable we have to use a dictionary with all the
@@ -315,9 +319,12 @@ def _get_weights(self, X, Y):
315319
dim = X.shape[1]
316320
identity = np.ones((n_points, 1))
317321
dist = self._distance_matrix(X, X)
318-
H = np.bmat([[dist, identity, X], [identity.T, np.zeros(
319-
(1, 1)), np.zeros((1, dim))], [X.T, np.zeros((dim, 1)), np.zeros(
320-
(dim, dim))]])
322+
H = np.bmat([[dist, identity,
323+
X], [identity.T,
324+
np.zeros((1, 1)),
325+
np.zeros((1, dim))],
326+
[X.T, np.zeros((dim, 1)),
327+
np.zeros((dim, dim))]])
321328
rhs = np.bmat([[Y], [np.zeros((1, dim))], [np.zeros((dim, dim))]])
322329
weights = np.linalg.solve(H, rhs)
323330
return weights

0 commit comments

Comments
 (0)