Skip to content

Commit 1f1a150

Browse files
fixed tutorials, use of code formatter
1 parent 5d3dfee commit 1f1a150

File tree

7 files changed

+114
-105
lines changed

7 files changed

+114
-105
lines changed

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

Lines changed: 15 additions & 21 deletions
Large diffs are not rendered by default.

pygem/bffd.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ class BFFD(CFFD):
5959
>>> assert np.isclose(np.linalg.norm(bffd.fun(bffd.ffd(original_mesh_points[:-4]))-b),np.array([0.]))
6060
>>> new_mesh_points = bffd.ffd(original_mesh_points)
6161
'''
62-
def __init__(self, n_control_points=None, fixval=None, weight_matrix=None, mask=None):
63-
super().__init__(n_control_points,None,fixval,weight_matrix,mask)
62+
def __init__(self,
63+
n_control_points=None,
64+
fixval=None,
65+
weight_matrix=None,
66+
mask=None):
67+
super().__init__(n_control_points, None, fixval, weight_matrix, mask)
6468

6569
def linfun(x):
6670
return np.mean(x.reshape(-1, 3), axis=0)
6771

68-
self.fun = linfun
72+
self.fun = linfun

pygem/cffd.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,34 @@ class CFFD(FFD):
5959
>>> assert np.isclose(np.linalg.norm(fun(cffd.ffd(original_mesh_points[:-4]))-b),np.array([0.]),atol=1e-06)
6060
>>> new_mesh_points = cffd.ffd(original_mesh_points)
6161
"""
62-
def __init__(self, n_control_points=None, fun=None, fixval=None, weight_matrix=None, mask=None ):
62+
def __init__(self,
63+
n_control_points=None,
64+
fun=None,
65+
fixval=None,
66+
weight_matrix=None,
67+
mask=None):
6368
super().__init__(n_control_points)
6469

6570
if mask is None:
66-
self.mask=np.full((*self.n_control_points,3), True, dtype=bool)
71+
self.mask = np.full((*self.n_control_points, 3), True, dtype=bool)
6772
else:
68-
self.mask=mask
73+
self.mask = mask
6974

7075
if fixval is None:
71-
self.fixval=np.array([1.])
76+
self.fixval = np.array([1.])
7277
else:
73-
self.fixval=fixval
74-
78+
self.fixval = fixval
79+
7580
if fun is None:
76-
self.fun=lambda x: self.fixval
77-
81+
self.fun = lambda x: self.fixval
82+
7883
else:
79-
self.fun=fun
84+
self.fun = fun
8085

8186
if weight_matrix is None:
82-
self.weight_matrix=np.eye(np.sum(self.mask.astype(int)))
87+
self.weight_matrix = np.eye(np.sum(self.mask.astype(int)))
8388

84-
def adjust_control_points(self,src_pts):
89+
def adjust_control_points(self, src_pts):
8590
'''
8691
Adjust the FFD control points such that F(ffd(src_pts))=c
8792
@@ -91,11 +96,17 @@ def adjust_control_points(self,src_pts):
9196
'''
9297

9398
saved_parameters = self._save_parameters()
94-
indices=np.arange(np.prod(self.n_control_points)*3)[self.mask.reshape(-1)]
95-
A, b = self._compute_linear_map(src_pts, saved_parameters.copy(),indices)
99+
indices = np.arange(np.prod(self.n_control_points) *
100+
3)[self.mask.reshape(-1)]
101+
A, b = self._compute_linear_map(src_pts, saved_parameters.copy(),
102+
indices)
96103
d = A @ saved_parameters[indices] + b
97-
invM=np.linalg.inv(self.weight_matrix)
98-
deltax = np.linalg.multi_dot([invM , A.T , np.linalg.inv(np.linalg.multi_dot([A, invM, A.T])) , (self.fixval - d)])
104+
invM = np.linalg.inv(self.weight_matrix)
105+
deltax = np.linalg.multi_dot([
106+
invM, A.T,
107+
np.linalg.inv(np.linalg.multi_dot([A, invM, A.T])),
108+
(self.fixval - d)
109+
])
99110
saved_parameters[indices] = saved_parameters[indices] + deltax
100111
self._load_parameters(saved_parameters)
101112

@@ -134,14 +145,15 @@ def _load_parameters(self, tmp):
134145
self.array_mu_y = tmp[:, :, :, 1]
135146
self.array_mu_z = tmp[:, :, :, 2]
136147

137-
def read_parameters(self,filename='parameters.prm'):
148+
def read_parameters(self, filename='parameters.prm'):
138149
super().read_parameters(filename)
139-
self.mask=np.full((*self.n_control_points,3), True, dtype=bool)
140-
self.weight_matrix=np.eye(np.sum(self.mask.astype(int)))
150+
self.mask = np.full((*self.n_control_points, 3), True, dtype=bool)
151+
self.weight_matrix = np.eye(np.sum(self.mask.astype(int)))
152+
141153

142154
# I see that a similar function already exists in pygem.utils, but it does not work for inputs and outputs of different dimensions
143155

144-
def _compute_linear_map(self, src_pts, saved_parameters,indices):
156+
def _compute_linear_map(self, src_pts, saved_parameters, indices):
145157
'''
146158
Computes the coefficient and the intercept of the linear map from the control points to the output.
147159
@@ -171,4 +183,4 @@ def _compute_linear_map(self, src_pts, saved_parameters,indices):
171183
rcond=None) #computation of the linear map
172184
A = sol[0].T[:, :-1] #coefficient
173185
b = sol[0].T[:, -1] #intercept
174-
return A, b
186+
return A, b

pygem/vffd.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ class VFFD(CFFD):
6565
>>> assert np.isclose(np.linalg.norm(vffd.fun(new_mesh_points)-b),np.array([0.]),atol=1e-07)
6666
6767
'''
68-
def __init__(self, triangles, n_control_points=None, fixval=None, weight_matrix=None, mask=None ):
69-
super().__init__(n_control_points,None,fixval,weight_matrix,mask)
68+
def __init__(self,
69+
triangles,
70+
n_control_points=None,
71+
fixval=None,
72+
weight_matrix=None,
73+
mask=None):
74+
super().__init__(n_control_points, None, fixval, weight_matrix, mask)
7075
self.triangles = triangles
7176
self.vweight = [1 / 3, 1 / 3, 1 / 3]
7277

@@ -79,12 +84,12 @@ def volume(x):
7984

8085
def adjust_control_points(self, src_pts):
8186
self.vweight = np.abs(self.vweight) / np.sum(np.abs(self.vweight))
82-
mask_bak=self.mask.copy()
87+
mask_bak = self.mask.copy()
8388
diffvolume = self.fixval - self.fun(self.ffd(src_pts))
8489
for i in range(3):
85-
self.mask=np.full((*self.n_control_points,3), False, dtype=bool)
86-
self.mask[:,:,:,i]=mask_bak[:,:,:,i].copy()
90+
self.mask = np.full((*self.n_control_points, 3), False, dtype=bool)
91+
self.mask[:, :, :, i] = mask_bak[:, :, :, i].copy()
8792
self.weight_matrix = np.eye(np.sum(self.mask.astype(int)))
8893
self.fixval = self.fun(
8994
self.ffd(src_pts)) + self.vweight[i] * (diffvolume)
90-
super().adjust_control_points(src_pts)
95+
super().adjust_control_points(src_pts)

tests/test_cffd.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def test_nothing_happens_0(self):
1616
def fun(x):
1717
x = x.reshape(-1)
1818
return A @ x
19+
1920
new_mesh_points = cffd(original_mesh_points)
2021
assert np.array_equal(original_mesh_points, new_mesh_points)
2122

22-
2323
def test_nothing_happens(self):
2424
np.random.seed(0)
2525
cffd = CFFD()
@@ -57,7 +57,8 @@ def fun(x):
5757
cffd.adjust_control_points(original_mesh_points)
5858
new_mesh_points = cffd.ffd(original_mesh_points)
5959
assert np.isclose(np.linalg.norm(fun(new_mesh_points) - b),
60-
np.array([0.0]),atol=1e-7)
60+
np.array([0.0]),
61+
atol=1e-7)
6162

6263
def test_interpolation(self):
6364
cffd = CFFD()
@@ -73,8 +74,10 @@ def fun(x):
7374
cffd.fixval = b
7475
cffd.fun = fun
7576
save_par = cffd._save_parameters()
76-
indices=np.arange(np.prod(cffd.n_control_points)*3)[cffd.mask.reshape(-1)]
77-
C, d = cffd._compute_linear_map(original_mesh_points, save_par.copy(),indices)
77+
indices = np.arange(np.prod(cffd.n_control_points) *
78+
3)[cffd.mask.reshape(-1)]
79+
C, d = cffd._compute_linear_map(original_mesh_points, save_par.copy(),
80+
indices)
7881
for i in range(2 * len(indices)):
7982
tmp = np.random.rand(len(indices))
8083
save_par[indices] = tmp

tutorials/tutorial7/tutorial-7-cffd.ipynb

Lines changed: 30 additions & 36 deletions
Large diffs are not rendered by default.

tutorials/tutorial7/tutorial-7-cffd.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,29 @@ def custom_linear_constraint(x):
1919
print("The custom linear function on the non deformed points is", custom_linear_constraint(x))
2020
print("The custom linear function on the classic FFD deformed points is", custom_linear_constraint(x_def))
2121
from pygem.cffd import CFFD
22-
ffd=CFFD([3,3,1])
22+
ffd=CFFD([3,3,1],custom_linear_constraint,np.array([1.]))
2323
np.random.seed(0)
2424
ffd.array_mu_x=ffd.array_mu_x+0.5*np.random.rand(*ffd.array_mu_x.shape)
2525
ffd.array_mu_y=ffd.array_mu_x+0.5*np.random.rand(*ffd.array_mu_x.shape)
26-
ffd.linconstraint=custom_linear_constraint
27-
ffd.valconstraint=np.array([1.])
28-
ffd.indices=np.arange(3*3*3*1).reshape(3,3,1,3)
29-
ffd.indices=ffd.indices[:,:,:,:-1] #removing z indices
30-
ffd.indices=ffd.indices.reshape(-1).tolist()
31-
ffd.M=np.eye(len(ffd.indices))#no weighting
26+
ffd.mask[:,:,:,-1]=False #removing z masks
27+
ffd.weight_matrix=np.eye(np.sum(ffd.mask))#no weighting
28+
ffd.adjust_control_points(x)
3229
x_def=ffd(x)
3330
plt.plot(x_def[:,0],x_def[:,1],'o')
3431
print("The custom linear function on the constrained FFD deformed points is", custom_linear_constraint(x_def))
3532
from pygem.bffd import BFFD
3633
def mesh_points(num_pts = 2000):
3734
indices = np.arange(0, num_pts, dtype=float) + 0.5
35+
3836
phi = np.arccos(1 - 2*indices/num_pts)
3937
theta = np.pi * (1 + 5**0.5) * indices
38+
4039
return np.array([np.cos(theta) * np.sin(phi), np.sin(theta) * np.sin(phi), np.cos(phi)]).T
4140
mesh = mesh_points()
42-
ffd = BFFD([2, 2, 2])
43-
ffd.valconstraint=np.array([0.,0.,0.])
41+
ffd = BFFD([2, 2, 2],np.array([0.,0.,0.]))
4442
ffd.array_mu_x[1, 1, 1] = 2
4543
ffd.array_mu_z[1, 1, 1] = 0
46-
ffd.indices=np.arange(2*2*2*3).tolist()
47-
ffd.M=np.eye(len(ffd.indices))
44+
ffd.adjust_control_points(mesh)
4845
mesh_def=ffd(mesh)
4946
print(np.mean(mesh_def,axis=0))
5047
ax = plt.figure(figsize=(8,8)).add_subplot(111, projection='3d')
@@ -65,19 +62,19 @@ def mesh_points(num_pts = 2000):
6562
ax.plot_trisurf(points[:,0], points[:,1], points[:,2], triangles=faces, cmap=plt.cm.Spectral)
6663
from pygem.vffd import VFFD
6764
vffd=VFFD(faces,[2,2,2])
68-
initvolume=vffd.linconstraint(points)
69-
vffd.valconstraint=np.array([initvolume])
70-
vffd.indices=np.arange(2*2*2*3).tolist()
71-
vffd.M=np.eye(len(vffd.indices))
65+
initvolume=vffd.fun(points)
66+
vffd.fixval=np.array([initvolume])
7267
vffd.vweight=np.array([0,1,0])
7368
np.random.seed(0)
7469
vffd.array_mu_x=vffd.array_mu_x+0.5*np.random.rand(2,2,2)
7570
vffd.array_mu_y=vffd.array_mu_y+0.5*np.random.rand(2,2,2)
7671
vffd.array_mu_z=vffd.array_mu_z+0.5*np.random.rand(2,2,2)
72+
vffd.adjust_control_points(points)
7773
mesh_def=vffd(points)
7874
mesh_def=mesh_def.reshape(points.shape)
7975
print("Percentage difference from the original mesh is ", np.linalg.norm(mesh_def-points)/np.linalg.norm(points)*100)
8076
fig = plt.figure(figsize=plt.figaspect(0.5))
8177
ax = fig.add_subplot(1, 2, 1, projection='3d')
8278
ax.plot_trisurf(mesh_def[:,0], mesh_def[:,1], mesh_def[:,2], triangles=faces, cmap=plt.cm.Spectral)
8379

80+

0 commit comments

Comments
 (0)