Skip to content

Commit 2e263f5

Browse files
committed
Migrate Python 2 syntax to Python 3 and fix Pylint warnings in pygem/
1 parent d799725 commit 2e263f5

29 files changed

+855
-818
lines changed

pygem/__init__.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
"""
2-
PyGeM init
3-
"""
4-
5-
__all__ = [
6-
"deformation",
7-
"ffd",
8-
"rbf",
9-
"idw",
10-
"rbf_factory",
11-
"custom_deformation",
12-
"cffd" "bffd" "vffd",
13-
]
1+
"""PyGeM init."""
142

153
from .deformation import Deformation
164
from .ffd import FFD
@@ -19,6 +7,47 @@
197
from .idw import IDW
208
from .rbf_factory import RBFFactory
219
from .custom_deformation import CustomDeformation
22-
from .meta import *
2310
from .bffd import BFFD
2411
from .vffd import VFFD
12+
13+
from .meta import (
14+
__project__,
15+
__title__,
16+
__author__,
17+
__copyright__,
18+
__license__,
19+
__version__,
20+
__mail__,
21+
__maintainer__,
22+
__status__,
23+
)
24+
25+
__all__ = [
26+
"Deformation",
27+
"FFD",
28+
"CFFD",
29+
"RBF",
30+
"IDW",
31+
"RBFFactory",
32+
"CustomDeformation",
33+
"BFFD",
34+
"VFFD",
35+
"deformation",
36+
"ffd",
37+
"rbf",
38+
"idw",
39+
"rbf_factory",
40+
"custom_deformation",
41+
"cffd",
42+
"bffd",
43+
"vffd",
44+
"__project__",
45+
"__title__",
46+
"__author__",
47+
"__copyright__",
48+
"__license__",
49+
"__version__",
50+
"__mail__",
51+
"__maintainer__",
52+
"__status__",
53+
]

pygem/bffd.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from pygem.cffd import CFFD
21
import numpy as np
32

3+
from pygem.cffd import CFFD
4+
45

56
class BFFD(CFFD):
6-
"""
7-
Class that handles the Barycenter Free Form Deformation on the mesh points.
7+
"""Class that handles the Barycenter Free Form Deformation on the mesh
8+
points.
89
910
:param list n_control_points: number of control points in the x, y, and z
1011
direction. Default is [2, 2, 2].
@@ -21,11 +22,15 @@ class BFFD(CFFD):
2122
y, normalized with the box length y.
2223
:cvar numpy.ndarray array_mu_z: collects the displacements (weights) along
2324
z, normalized with the box length z.
24-
:cvar callable fun: it defines the F of the constraint F(x)=c. Default is the constant 1 function.
25-
:cvar numpy.ndarray fixval: it defines the c of the constraint F(x)=c. Default is 1.
25+
:cvar callable fun: it defines the F of the constraint F(x)=c.
26+
Default is the constant 1 function.
27+
:cvar numpy.ndarray fixval: it defines the c of the constraint
28+
F(x)=c. Default is 1.
2629
:cvar numpy.ndarray mask: a boolean tensor that tells to the class
27-
which control points can be moved, and in what direction, to enforce the constraint.
28-
The tensor has shape (n_x,n_y,n_z,3), where the last dimension indicates movement
30+
which control points can be moved, and in what direction,
31+
to enforce the constraint.
32+
The tensor has shape (n_x,n_y,n_z,3), where the last
33+
dimension indicates movement
2934
on x,y,z respectively. Default is all true.
3035
3136
:Example:
@@ -34,9 +39,11 @@ class BFFD(CFFD):
3439
>>> b = np.random.rand(3)
3540
>>> bffd = BFFD(b, [2, 2, 2])
3641
>>> bffd.read_parameters('tests/test_datasets/parameters_test_cffd')
37-
>>> original_mesh_points = np.load("tests/test_datasets/test_sphere_cffd.npy")
42+
>>> original_mesh_points =
43+
np.load("tests/test_datasets/test_sphere_cffd.npy")
3844
>>> bffd.adjust_control_points(original_mesh_points[:-4])
39-
>>> assert np.isclose(np.linalg.norm(bffd.fun(bffd.ffd(original_mesh_points[:-4])) - b), np.array([0.]))
45+
>>> assert np.isclose(np.linalg.norm(bffd.fun(bffd.ffd(
46+
... original_mesh_points[:-4])) - b), np.array([0.]))
4047
>>> new_mesh_points = bffd.ffd(original_mesh_points)
4148
"""
4249

pygem/cad/__init__.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
"""
2-
PyGeM CAD init
3-
"""
1+
"""PyGeM CAD init."""
42

53
try:
6-
import OCC
4+
pass
75
except ModuleNotFoundError as e:
86
print("\nOCC not found, but required to deal with CAD files")
97
print("Install it using:")
108
print("\tconda install -c conda-forge pythonocc-core=7.4.0")
119
print("or visit https://github.com/tpaviot/pythonocc-core for more info\n")
1210
raise e
13-
14-
15-
from .ffd import FFD
16-
from .rbf import RBF
17-
from .idw import IDW
18-
from .custom_deformation import CustomDeformation
19-
from .cad_deformation import CADDeformation

pygem/cad/cad_deformation.py

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,50 @@
1-
"""
2-
Module for generic deformation for CAD file.
3-
"""
1+
"""Module for generic deformation for CAD file."""
42

53
import os
6-
import numpy as np
74
from itertools import product
8-
from OCC.Core.TopoDS import (
9-
TopoDS_Shape,
10-
TopoDS_Compound,
11-
TopoDS_Face,
12-
TopoDS_Edge,
13-
TopoDS_Wire,
14-
topods
15-
)
16-
from OCC.Core.BRep import BRep_Builder
17-
from OCC.Core.TopExp import TopExp_Explorer
18-
from OCC.Core.TopAbs import TopAbs_EDGE, TopAbs_FACE, TopAbs_WIRE
19-
from OCC.Core.TopTools import TopTools_ListOfShape
5+
6+
import numpy as np
7+
from OCC.Core.BRep import BRep_Builder, BRep_Tool, BRep_Tool_Curve
208
from OCC.Core.BRepBuilderAPI import (
9+
BRepBuilderAPI_MakeEdge,
2110
BRepBuilderAPI_MakeFace,
2211
BRepBuilderAPI_MakeWire,
23-
BRepBuilderAPI_MakeEdge,
2412
BRepBuilderAPI_NurbsConvert,
2513
)
26-
from OCC.Core.BRep import BRep_Tool, BRep_Tool_Curve
14+
from OCC.Core.BRepTools import breptools_OuterWire
2715
from OCC.Core.Geom import Geom_BSplineCurve, Geom_BSplineSurface
2816
from OCC.Core.GeomConvert import (
29-
geomconvert_SurfaceToBSplineSurface,
30-
geomconvert_CurveToBSplineCurve,
3117
GeomConvert_CompCurveToBSplineCurve,
18+
geomconvert_CurveToBSplineCurve,
19+
geomconvert_SurfaceToBSplineSurface,
3220
)
3321
from OCC.Core.gp import gp_Pnt
34-
from OCC.Core.BRepTools import breptools_OuterWire
3522
from OCC.Core.IFSelect import IFSelect_RetDone
23+
from OCC.Core.IGESControl import (
24+
IGESControl_Controller_Init,
25+
IGESControl_Reader,
26+
IGESControl_Writer,
27+
)
3628
from OCC.Core.Interface import Interface_Static_SetCVal
3729
from OCC.Core.STEPControl import (
38-
STEPControl_Writer,
39-
STEPControl_Reader,
4030
STEPControl_AsIs,
31+
STEPControl_Reader,
32+
STEPControl_Writer,
4133
)
42-
from OCC.Core.IGESControl import (
43-
IGESControl_Writer,
44-
IGESControl_Reader,
45-
IGESControl_Controller_Init,
34+
from OCC.Core.TopAbs import TopAbs_EDGE, TopAbs_FACE, TopAbs_WIRE
35+
from OCC.Core.TopExp import TopExp_Explorer
36+
from OCC.Core.TopoDS import (
37+
TopoDS_Compound,
38+
TopoDS_Face,
39+
TopoDS_Shape,
40+
TopoDS_Wire,
41+
topods,
4642
)
43+
from OCC.Core.TopTools import TopTools_ListOfShape
4744

4845

4946
class CADDeformation:
50-
"""
51-
Base class for applting deformation to CAD geometries.
47+
"""Base class for applting deformation to CAD geometries.
5248
5349
:param int u_knots_to_add: the number of knots to add to the NURBS surfaces
5450
along `u` direction before the deformation. This parameter is useful
@@ -93,7 +89,11 @@ class CADDeformation:
9389
"""
9490

9591
def __init__(
96-
self, u_knots_to_add=0, v_knots_to_add=0, t_knots_to_add=0, tolerance=1e-4
92+
self,
93+
u_knots_to_add=0,
94+
v_knots_to_add=0,
95+
t_knots_to_add=0,
96+
tolerance=1e-4,
9797
):
9898
self.u_knots_to_add = u_knots_to_add
9999
self.v_knots_to_add = v_knots_to_add
@@ -154,14 +154,14 @@ def write_shape(filename, shape):
154154
"""
155155

156156
def write_iges(filename, shape):
157-
"""IGES writer"""
157+
"""IGES writer."""
158158
IGESControl_Controller_Init()
159159
writer = IGESControl_Writer()
160160
writer.AddShape(shape)
161161
writer.Write(filename)
162162

163163
def write_step(filename, shape):
164-
"""STEP writer"""
164+
"""STEP writer."""
165165
step_writer = STEPControl_Writer()
166166
# Changes write schema to STEP standard AP203
167167
# It is considered the most secure standard for STEP.
@@ -184,8 +184,7 @@ def write_step(filename, shape):
184184
writer(filename, shape)
185185

186186
def _bspline_surface_from_face(self, face):
187-
"""
188-
Private method that takes a TopoDS_Face and transforms it into a
187+
"""Private method that takes a TopoDS_Face and transforms it into a
189188
Bspline_Surface.
190189
191190
:param TopoDS_Face face: the TopoDS_Face to be converted
@@ -202,8 +201,7 @@ def _bspline_surface_from_face(self, face):
202201
return bspline_surface
203202

204203
def _bspline_curve_from_wire(self, wire):
205-
"""
206-
Private method that takes a TopoDS_Wire and transforms it into a
204+
"""Private method that takes a TopoDS_Wire and transforms it into a
207205
Bspline_Curve.
208206
209207
:param TopoDS_Wire wire: the TopoDS_Face to be converted
@@ -247,8 +245,7 @@ def _bspline_curve_from_wire(self, wire):
247245
return comp_curve
248246

249247
def _enrich_curve_knots(self, bsp_curve):
250-
"""
251-
Private method that adds `self.t_knots_to_add` poles to the passed
248+
"""Private method that adds `self.t_knots_to_add` poles to the passed
252249
curve.
253250
254251
:param Geom_BSplineCurve bsp_curve: the NURBS curve to enrich
@@ -263,17 +260,19 @@ def _enrich_curve_knots(self, bsp_curve):
263260
last_param = bsp_curve.LastParameter()
264261
for i in range(self.t_knots_to_add):
265262
bsp_curve.InsertKnot(
266-
first_param + i * (last_param - first_param) / self.t_knots_to_add,
263+
first_param
264+
+ i * (last_param - first_param) / self.t_knots_to_add,
267265
1,
268266
self.tolerance,
269267
)
270268

271269
def _enrich_surface_knots(self, bsp_surface):
272-
"""
273-
Private method that adds `self.u_knots_to_add` and `self.v_knots_to_add`
274-
knots to the input surface `bsp_surface`, in u and v direction respectively.
270+
"""Private method that adds `self.u_knots_to_add` and
271+
`self.v_knots_to_add` knots to the input surface `bsp_surface`, in u
272+
and v direction respectively.
275273
276-
:param Geom_BSplineCurve bsp_surface: the NURBS surface to enrich
274+
:param Geom_BSplineCurve bsp_surface: the NURBS surface to
275+
enrich
277276
"""
278277
if not isinstance(bsp_surface, Geom_BSplineSurface):
279278
raise TypeError("bsp_surface must be a Geom_BSplineSurface")
@@ -297,18 +296,20 @@ def _enrich_surface_knots(self, bsp_surface):
297296
)
298297

299298
def _pole_get_components(self, pole):
300-
"""Extract component from gp_Pnt"""
299+
"""Extract component from gp_Pnt."""
301300
return pole.X(), pole.Y(), pole.Z()
302301

303302
def _pole_set_components(self, components):
304-
"""Return a gp_Pnt with the passed components"""
305-
assert len(components) == 3
303+
"""Return a gp_Pnt with the passed components."""
304+
if len(components) != 3:
305+
raise ValueError(
306+
f"Expected 3 components for gp_Pnt, got {len(components)}"
307+
)
306308
return gp_Pnt(*components)
307309

308310
def _deform_bspline_curve(self, bsp_curve):
309-
"""
310-
Private method that deforms the control points of `bsp_curve` using the
311-
inherited method. All the changes are performed in place.
311+
"""Private method that deforms the control points of `bsp_curve` using
312+
the inherited method. All the changes are performed in place.
312313
313314
:param Geom_Bspline_Curve bsp_curve: the curve to deform
314315
"""
@@ -336,9 +337,8 @@ def _deform_bspline_curve(self, bsp_curve):
336337
bsp_curve.SetPole(pole_id + 1, new_pole)
337338

338339
def _deform_bspline_surface(self, bsp_surface):
339-
"""
340-
Private method that deforms the control points of `surface` using the
341-
inherited method. All the changes are performed in place.
340+
"""Private method that deforms the control points of `surface` using
341+
the inherited method. All the changes are performed in place.
342342
343343
:param Geom_Bspline_Surface bsp_surface: the surface to deform
344344
"""
@@ -368,12 +368,11 @@ def _deform_bspline_surface(self, bsp_surface):
368368
bsp_surface.SetPole(u + 1, v + 1, new_pole)
369369

370370
def __call__(self, obj, dst=None):
371-
"""
372-
This method performs the deformation on the CAD geometry. If `obj` is a
373-
TopoDS_Shape, the method returns a TopoDS_Shape containing the deformed
374-
geometry. If `obj` is a filename, the method deforms the geometry
375-
contained in the file and writes the deformed shape to `dst` (which has
376-
to be set).
371+
"""This method performs the deformation on the CAD geometry. If `obj`
372+
is a TopoDS_Shape, the method returns a TopoDS_Shape containing the
373+
deformed geometry. If `obj` is a filename, the method deforms the
374+
geometry contained in the file and writes the deformed shape to `dst`
375+
(which has to be set).
377376
378377
:param obj: the input geometry.
379378
:type obj: str or TopoDS_Shape
@@ -484,7 +483,9 @@ def __call__(self, obj, dst=None):
484483
# so once we finished looping on all the wires to modify them,
485484
# we first use the only outer one to trim the surface
486485
# face builder object
487-
face_maker = BRepBuilderAPI_MakeFace(bspline_surface, outer_wires[0])
486+
face_maker = BRepBuilderAPI_MakeFace(
487+
bspline_surface, outer_wires[0]
488+
)
488489

489490
# and then add all other inner wires for the holes
490491
for inner_wire in inner_wires:

0 commit comments

Comments
 (0)