Skip to content

Commit 7ae6bf5

Browse files
fixed tutorials and documentation, applied code formatter
1 parent 519512e commit 7ae6bf5

File tree

7 files changed

+256
-277
lines changed

7 files changed

+256
-277
lines changed

docs/source/_tutorials/tutorial-7-cffd.html

Lines changed: 77 additions & 71 deletions
Large diffs are not rendered by default.

docs/source/bffd.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Barycenter Free-form Deformation
22
=====================
33

4-
.. currentmodule:: pygem.bffd
4+
.. currentmodule:: pygem.cffd
55

6-
.. automodule:: pygem.bffd
6+
.. automodule:: pygem.cffd
77

88
.. autosummary::
99
:toctree: _summaries
1010
:nosignatures:
1111

12-
.. autoclass:: pygem.bffd.BFFD
12+
.. autoclass:: pygem.cffd.BFFD
1313
:members:
1414
:special-members: __call__
1515
:private-members:

docs/source/vffd.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Volume Free-form Deformation
22
=====================
33

4-
.. currentmodule:: pygem.vffd
4+
.. currentmodule:: pygem.cffd
55

6-
.. automodule:: pygem.vffd
6+
.. automodule:: pygem.cffd
77

88
.. autosummary::
99
:toctree: _summaries
1010
:nosignatures:
1111

12-
.. autoclass:: pygem.vffd.VFFD
12+
.. autoclass:: pygem.cffd.VFFD
1313
:members:
1414
:special-members: __call__
1515
:private-members:

pygem/cffd.py

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import numpy as np
1414
from scipy.optimize import LinearConstraint, differential_evolution
1515

16+
1617
class CFFD(FFD):
1718
"""
1819
Class that handles the Constrained Free Form Deformation on the mesh points.
@@ -44,77 +45,79 @@ class CFFD(FFD):
4445
on x,y,z respectively. Default is all true. It used only in the triaffine mode.
4546
4647
:Example:
47-
4848
>>> from pygem import CFFD
4949
>>> import numpy as np
50-
>>> original_mesh_points = np.load('tests/test_datasets/meshpoints_sphere_orig.npy')
50+
>>> original_mesh_points = np.random.rand(100, 3)
5151
>>> A = np.random.rand(3, original_mesh_points[:-4].reshape(-1).shape[0])
5252
>>> fun = lambda x: A @ x.reshape(-1)
5353
>>> b = np.random.rand(3)
5454
>>> cffd = CFFD(b, fun, [2, 2, 2])
55-
>>> cffd.read_parameters('tests/test_datasets/parameters_test_ffd_sphere.prm')
55+
>>> cffd.read_parameters('tests/test_datasets/parameters_test_cffd.prm')
5656
>>> cffd.adjust_control_points(original_mesh_points[:-4])
57-
>>> assert np.isclose(np.linalg.norm(fun(cffd.ffd(original_mesh_points[:-4])) - b), np.array([0.]),atol = 1e-06)
57+
>>> assert np.isclose(np.linalg.norm(fun(cffd.ffd(original_mesh_points[:-4])) - b), np.array([0.]), atol = 1e-06)
5858
>>> new_mesh_points = cffd.ffd(original_mesh_points)
59-
"""
6059
60+
"""
6161
def __init__(self,
62-
fixval,
63-
fun,
64-
n_control_points=None,
65-
ffd_mask=None,
66-
fun_mask=None):
62+
fixval,
63+
fun,
64+
n_control_points=None,
65+
ffd_mask=None,
66+
fun_mask=None):
6767
super().__init__(n_control_points)
6868

6969
if ffd_mask is None:
70-
self.ffd_mask = np.full((*self.n_control_points, 3), True, dtype=bool)
70+
self.ffd_mask = np.full((*self.n_control_points, 3),
71+
True,
72+
dtype=bool)
7173
else:
7274
self.ffd_mask = ffd_mask
7375

74-
self.num_cons=len(fixval)
75-
self.fun=fun
76-
self.fixval=fixval
76+
self.num_cons = len(fixval)
77+
self.fun = fun
78+
self.fixval = fixval
7779
if fun_mask is None:
7880
self.fun_mask = np.full((self.num_cons, 3), True, dtype=bool)
7981
else:
8082
self.fun_mask = fun_mask
81-
def adjust_control_points(self,src_pts):
83+
84+
def adjust_control_points(self, src_pts):
8285
'''
8386
Adjust the FFD control points such that fun(ffd(src_pts))=fixval
8487
8588
:param np.ndarray src_pts: the points whose deformation we want to be
8689
constrained.
8790
:rtype: None.
8891
'''
89-
hyper_param=self.fun_mask.copy().astype(float)
90-
hyper_param=hyper_param/np.sum(hyper_param,axis=1)
92+
hyper_param = self.fun_mask.copy().astype(float)
93+
hyper_param = hyper_param / np.sum(hyper_param, axis=1)
9194
mask_bak = self.ffd_mask.copy()
92-
fixval_bak=self.fixval.copy()
95+
fixval_bak = self.fixval.copy()
9396
diffvolume = self.fixval - self.fun(self.ffd(src_pts))
9497
for i in range(3):
95-
self.ffd_mask = np.full((*self.n_control_points, 3), False, dtype=bool)
98+
self.ffd_mask = np.full((*self.n_control_points, 3),
99+
False,
100+
dtype=bool)
96101
self.ffd_mask[:, :, :, i] = mask_bak[:, :, :, i].copy()
97-
self.fixval = self.fun(self.ffd(src_pts)) + hyper_param[:,i] * (
98-
diffvolume
99-
)
102+
self.fixval = self.fun(
103+
self.ffd(src_pts)) + hyper_param[:, i] * (diffvolume)
100104
saved_parameters = self._save_parameters()
101105
indices = np.arange(np.prod(self.n_control_points) *
102106
3)[self.ffd_mask.reshape(-1)]
103107
A, b = self._compute_linear_map(src_pts, saved_parameters.copy(),
104-
indices)
105-
A=A[self.fun_mask[:,i].reshape(-1),:]
106-
b=b[self.fun_mask[:,i].reshape(-1)]
108+
indices)
109+
A = A[self.fun_mask[:, i].reshape(-1), :]
110+
b = b[self.fun_mask[:, i].reshape(-1)]
107111
d = A @ saved_parameters[indices] + b
108-
fixval=self.fixval[self.fun_mask[:,i].reshape(-1)]
112+
fixval = self.fixval[self.fun_mask[:, i].reshape(-1)]
109113
deltax = np.linalg.multi_dot([
110114
A.T,
111-
np.linalg.inv(np.linalg.multi_dot([A, A.T])),
112-
(fixval - d)
115+
np.linalg.inv(np.linalg.multi_dot([A, A.T])), (fixval - d)
113116
])
114117
saved_parameters[indices] = saved_parameters[indices] + deltax
115118
self._load_parameters(saved_parameters)
116119
self.ffd_mask = mask_bak.copy()
117-
self.fixval=fixval_bak.copy()
120+
self.fixval = fixval_bak.copy()
118121

119122
def ffd(self, src_pts):
120123
'''
@@ -182,7 +185,7 @@ def _compute_linear_map(self, src_pts, saved_parameters, indices):
182185
A = sol[0].T[:, :-1] #coefficient
183186
b = sol[0].T[:, -1] #intercept
184187
return A, b
185-
188+
186189

187190
class BFFD(CFFD):
188191
'''
@@ -213,33 +216,27 @@ class BFFD(CFFD):
213216
:Example:
214217
215218
>>> from pygem import BFFD
216-
>>> import numpy as np
217219
>>> b = np.random.rand(3)
218220
>>> bffd = BFFD(b, [2, 2, 2])
219-
>>> bffd.read_parameters('tests/test_datasets/parameters_test_ffd_sphere.prm')
220-
>>> original_mesh_points = np.load('tests/test_datasets/meshpoints_sphere_orig.npy')
221+
>>> bffd.read_parameters('tests/test_datasets/parameters_test_cffd')
222+
>>> original_mesh_points = np.random.rand(100, 3)
221223
>>> bffd.adjust_control_points(original_mesh_points[:-4])
222224
>>> assert np.isclose(np.linalg.norm(bffd.fun(bffd.ffd(original_mesh_points[:-4])) - b), np.array([0.]))
223-
>>> new_mesh_points = bffd.ffd(original_mesh_points)
225+
new_mesh_points = bffd.ffd(original_mesh_points)
224226
'''
225227

226-
def __init__(self,
227-
fixval=None,
228-
n_control_points=None,
229-
ffd_mask=None):
230-
super().__init__(fixval,None,n_control_points,ffd_mask,None)
228+
def __init__(self, fixval=None, n_control_points=None, ffd_mask=None):
229+
super().__init__(fixval, None, n_control_points, ffd_mask, None)
231230

232231
def linfun(x):
233232
return np.mean(x.reshape(-1, 3), axis=0)
234233

235234
self.fun = linfun
236235
self.fixval = fixval
237-
self.fun_mask = np.array([[True, False, False],
238-
[False, True, False],
236+
self.fun_mask = np.array([[True, False, False], [False, True, False],
239237
[False, False, True]])
240238

241239

242-
243240
class VFFD(CFFD):
244241
'''
245242
Class that handles the Volumetric Free Form Deformation on the mesh points.
@@ -275,35 +272,33 @@ class VFFD(CFFD):
275272
>>> from pygem import VFFD
276273
>>> import numpy as np
277274
>>> import meshio
278-
>>> mesh = meshio.read('tests/test_datasets/test_sphere.stl')
275+
>>> mesh = meshio.read('tests/test_datasets/test_sphere_cffd.stl')
279276
>>> original_mesh_points = mesh.points
280277
>>> triangles = mesh.cells_dict["triangle"]
281-
>>> b = np.random.rand()
282-
>>> vffd = VFFD(triangles, b,[2, 2, 2])
283-
>>> vffd.read_parameters('tests/test_datasets/parameters_test_ffd_sphere.prm')
278+
>>> b = np.random.rand(1)
279+
>>> vffd = VFFD(triangles, b, [2, 2, 2])
280+
>>> vffd.read_parameters('tests/test_datasets/parameters_test_cffd.prm')
284281
>>> vffd.adjust_control_points(original_mesh_points)
285282
>>> new_mesh_points = vffd(original_mesh_points)
286-
>>> assert np.isclose(np.linalg.norm(vffd.fun(new_mesh_points) - b),np.array([0.]), atol=1e-07)
283+
>>> assert np.isclose(np.linalg.norm(vffd.fun(new_mesh_points) - b), np.array([0.]), atol=1e-07)
287284
288285
'''
286+
def __init__(self, triangles, fixval, n_control_points=None, ffd_mask=None):
287+
super().__init__(fixval, None, n_control_points, ffd_mask, None)
289288

290-
def __init__(self,
291-
triangles,
292-
fixval,
293-
n_control_points=None,
294-
ffd_mask=None):
295-
super().__init__(fixval,None,n_control_points,ffd_mask,None)
289+
self.triangles = triangles
296290

297-
self.triangles=triangles
298291
def volume_inn(x):
299-
return _volume(x,self.triangles)
292+
return _volume(x, self.triangles)
300293

301294
self.fun = volume_inn
302-
self.fixval=fixval
303-
self.fun_mask=np.array([[True, True, True]])
304-
305-
def _volume(x,triangles):
295+
self.fixval = fixval
296+
self.fun_mask = np.array([[True, True, True]])
297+
298+
299+
def _volume(x, triangles):
306300
x = x.reshape(-1, 3)
307301
mesh = x[triangles]
308302
return np.array([np.sum(np.linalg.det(mesh))])
309303

304+

tests/test_cffd.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,48 @@
22
import os
33
from unittest import TestCase
44
import numpy as np
5-
from pygem import CFFD,BFFD,VFFD
5+
from pygem import CFFD, BFFD, VFFD
66
from pygem.cffd import _volume
77

8+
89
class TestCFFD(TestCase):
910

1011
def test_nothing_happens_cffd(self):
1112
np.random.seed(0)
12-
original_mesh_points = np.random.rand(100,3)
13+
original_mesh_points = np.random.rand(100, 3)
1314
A = np.random.rand(3, original_mesh_points.reshape(-1).shape[0])
1415

1516
def fun(x):
1617
x = x.reshape(-1)
1718
return A @ x
1819

1920
b = fun(original_mesh_points)
20-
cffd = CFFD(b,fun)
21+
cffd = CFFD(b, fun)
2122
cffd.adjust_control_points(original_mesh_points)
2223
new_mesh_points = cffd.ffd(original_mesh_points)
23-
assert np.linalg.norm(original_mesh_points-new_mesh_points)/np.linalg.norm(original_mesh_points)<1e-02
24+
assert np.linalg.norm(original_mesh_points - new_mesh_points
25+
) / np.linalg.norm(original_mesh_points) < 1e-02
2426

2527
def test_constraint_cffd(self):
2628
np.random.seed(0)
27-
original_mesh_points = np.random.rand(100,3)
29+
original_mesh_points = np.random.rand(100, 3)
2830
A = np.random.rand(3, original_mesh_points.reshape(-1).shape[0])
31+
2932
def fun(x):
3033
x = x.reshape(-1)
3134
return A @ x
3235

33-
b = fun(original_mesh_points)+0.02*np.random.rand(3)
34-
cffd = CFFD(b,fun)
36+
b = fun(original_mesh_points) + 0.02 * np.random.rand(3)
37+
cffd = CFFD(b, fun)
3538
cffd.read_parameters('tests/test_datasets/parameters_test_cffd.prm')
3639
cffd.adjust_control_points(original_mesh_points)
3740
new_mesh_points = cffd.ffd(original_mesh_points)
38-
assert np.linalg.norm(b-fun(new_mesh_points))/np.linalg.norm(b)<1e-02
41+
assert np.linalg.norm(b -
42+
fun(new_mesh_points)) / np.linalg.norm(b) < 1e-02
3943

4044
def test_nothing_happens_vffd(self):
4145
np.random.seed(0)
42-
points = 0.5*np.array([
46+
points = 0.5 * np.array([
4347
[0.0, 1.0, 0.0],
4448
[0.0, 0.0, 1.0],
4549
[0.0, 1.0, 1.0],
@@ -64,16 +68,16 @@ def test_nothing_happens_vffd(self):
6468
[6, 2, 1],
6569
[6, 1, 4],
6670
])
67-
b = _volume(points,triangles)
68-
cffd = VFFD(triangles,b)
71+
b = _volume(points, triangles)
72+
cffd = VFFD(triangles, b)
6973
cffd.adjust_control_points(points)
7074
new_mesh_points = cffd.ffd(points)
71-
new_fix=cffd.fun(new_mesh_points)
75+
new_fix = cffd.fun(new_mesh_points)
7276
assert np.allclose(np.linalg.norm(points - new_mesh_points), 0.0)
7377

7478
def test_constraint_vffd(self):
7579
np.random.seed(0)
76-
points = 0.5*np.array([
80+
points = 0.5 * np.array([
7781
[0.0, 1.0, 0.0],
7882
[0.0, 0.0, 1.0],
7983
[0.0, 1.0, 1.0],
@@ -98,32 +102,34 @@ def test_constraint_vffd(self):
98102
[6, 2, 1],
99103
[6, 1, 4],
100104
])
101-
b = _volume(points,triangles)+0.02*np.random.rand()
102-
cffd = VFFD(triangles,b)
105+
b = _volume(points, triangles) + 0.02 * np.random.rand()
106+
cffd = VFFD(triangles, b)
103107
cffd.read_parameters('tests/test_datasets/parameters_test_cffd.prm')
104108
cffd.adjust_control_points(points)
105109
new_mesh_points = cffd.ffd(points)
106-
new_fix=cffd.fun(new_mesh_points)
107-
assert np.linalg.norm(new_fix - b)/np.linalg.norm(b)<1e-02
108-
110+
new_fix = cffd.fun(new_mesh_points)
111+
assert np.linalg.norm(new_fix - b) / np.linalg.norm(b) < 1e-02
112+
109113
def test_nothing_happens_bffd(self):
110114
np.random.seed(0)
111-
original_mesh_points = np.random.rand(100,3)
115+
original_mesh_points = np.random.rand(100, 3)
112116
A = np.random.rand(3, original_mesh_points.reshape(-1).shape[0])
113117

114-
b = np.mean(original_mesh_points,axis=0)
118+
b = np.mean(original_mesh_points, axis=0)
115119
cffd = BFFD(b)
116120
cffd.adjust_control_points(original_mesh_points)
117121
new_mesh_points = cffd.ffd(original_mesh_points)
118-
assert np.linalg.norm(original_mesh_points-new_mesh_points)/np.linalg.norm(original_mesh_points)<1e-02
122+
assert np.linalg.norm(original_mesh_points - new_mesh_points
123+
) / np.linalg.norm(original_mesh_points) < 1e-02
119124

120125
def test_constraint_bffd(self):
121126
np.random.seed(0)
122-
original_mesh_points = np.random.rand(100,3)
127+
original_mesh_points = np.random.rand(100, 3)
123128
A = np.random.rand(3, original_mesh_points.reshape(-1).shape[0])
124-
b = np.mean(original_mesh_points,axis=0)+0.02*np.random.rand(3)
129+
b = np.mean(original_mesh_points, axis=0) + 0.02 * np.random.rand(3)
125130
cffd = BFFD(b)
126131
cffd.read_parameters('tests/test_datasets/parameters_test_cffd.prm')
127132
cffd.adjust_control_points(original_mesh_points)
128133
new_mesh_points = cffd.ffd(original_mesh_points)
129-
assert np.linalg.norm(b-np.mean(new_mesh_points,axis=0))/np.linalg.norm(b)<1e-02
134+
assert np.linalg.norm(
135+
b - np.mean(new_mesh_points, axis=0)) / np.linalg.norm(b) < 1e-02

0 commit comments

Comments
 (0)