Skip to content

Commit c9c9e7e

Browse files
committed
Merge pull request #66 from mtezzele/qc_fixes
quantified code fixes
2 parents 61ed07b + 00d5aa5 commit c9c9e7e

File tree

5 files changed

+53
-52
lines changed

5 files changed

+53
-52
lines changed

pygem/affine.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,33 @@
88

99
def angles2matrix(rot_z=0, rot_y=0, rot_x=0):
1010
"""
11-
This method returns the rotation matrix for given rotations around z, y and x axes. The output rotation matrix is
12-
equal to the composition of the individual rotations. Rotations are counter-clockwise.
13-
The default value of the three rotations is zero.
11+
This method returns the rotation matrix for given rotations around z, y and x axes.
12+
The output rotation matrix is equal to the composition of the individual rotations.
13+
Rotations are counter-clockwise. The default value of the three rotations is zero.
1414
1515
:param float rot_z: rotation angle (in radians) around z-axis.
1616
:param float rot_y: rotation angle (in radians) around y-axis.
1717
:param float rot_x: rotation angle (in radians) around x-axis.
1818
1919
:return: rot_matrix: rotation matrix for the given angles. The matrix shape is always (3, 3).
20-
:rtype: numpy.ndarray
21-
20+
:rtype: numpy.ndarray
21+
2222
:Example:
2323
2424
>>> import pygem.affine as at
2525
>>> import numpy as np
2626
27-
>>> # Example of a rotation around x, y, z axis
27+
>>> # Example of a rotation around x, y, z axis
2828
>>> rotz = 10*np.pi/180
2929
>>> roty = 20*np.pi/180
3030
>>> rotx = 30*np.pi/180
3131
>>> rot_matrix = at.angles2matrix(rotz, roty, rotx)
32-
32+
3333
.. note::
3434
3535
- The direction of rotation is given by the right-hand rule.
3636
- When applying the rotation to a vector, the vector should be column vector
3737
to the right of the rotation matrix.
38-
3938
"""
4039
rot_matrix = []
4140
if rot_z:
@@ -65,18 +64,17 @@ def to_reduced_row_echelon_form(matrix):
6564
6665
:return matrix: the reduced matrix.
6766
:rtype: matrix
68-
69-
67+
7068
:Example:
7169
7270
>>> import pygem.affine as at
7371
7472
>>> matrix = [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]
7573
>>> rref_matrix = at.to_reduced_row_echelon_form(matrix)
76-
74+
7775
.. note::
76+
7877
`matrix` will change after calling this function.
79-
8078
"""
8179
lead = 0
8280
row_count = len(matrix)
@@ -138,8 +136,8 @@ def affine_points_fit(points_start, points_end):
138136
c = [[0.0 for a in range(dim)] for i in range(dim+1)]
139137
for j in range(dim):
140138
for k in range(dim+1):
141-
for i in range(len(points_start)):
142-
qt = list(points_start[i]) + [1]
139+
for i, pnts_i in enumerate(points_start):
140+
qt = list(pnts_i) + [1]
143141
c[k][j] += qt[k] * points_end[i][j]
144142

145143
# Fill an an empty (dim+1) x (dim+1) matrix
@@ -152,11 +150,11 @@ def affine_points_fit(points_start, points_end):
152150

153151

154152
# Augement Q with c and get the reduced row echelon form of the result
155-
M = [Q[i] + c[i] for i in range(dim+1)]
153+
affine_matrix = [Q[i] + c[i] for i in range(dim+1)]
156154

157-
if np.linalg.cond(M) < 1/sys.float_info.epsilon:
158-
rref_M = to_reduced_row_echelon_form(M)
159-
rref_M = np.array(rref_M)
155+
if np.linalg.cond(affine_matrix) < 1/sys.float_info.epsilon:
156+
rref_aff_matrix = to_reduced_row_echelon_form(affine_matrix)
157+
rref_aff_matrix = np.array(rref_aff_matrix)
160158
else:
161159
raise RuntimeError("Error: singular matrix. Points are probably coplanar.")
162160

@@ -173,9 +171,9 @@ def transform_vector(source):
173171
destination = np.zeros(dim)
174172
for i in range(dim):
175173
for j in range(dim):
176-
destination[j] += source[i] * rref_M[i][j+dim+1]
174+
destination[j] += source[i] * rref_aff_matrix[i][j+dim+1]
177175
# Add the last line of the rref
178-
destination[i] += rref_M[dim][i+dim+1]
176+
destination[i] += rref_aff_matrix[dim][i+dim+1]
179177
return destination
180178

181179
return transform_vector

pygem/filehandler.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self):
1818
self.extension = None
1919

2020

21-
def parse(self, infile):
21+
def parse(self, *args):
2222
"""
2323
Abstract method to parse a specific file.
2424
@@ -28,7 +28,7 @@ def parse(self, infile):
2828
+ self.__class__.__name__ + ".parse")
2929

3030

31-
def write(self, mesh_points, outfile):
31+
def write(self, *args):
3232
"""
3333
Abstract method to write a specific file.
3434
@@ -51,19 +51,21 @@ def _check_extension(self, filename):
5151
It is {0!s}, instead of {1!s}.'.format(file_ext, self.extension))
5252

5353

54-
def _check_filename_type(self, filename):
54+
@staticmethod
55+
def _check_filename_type(filename):
5556
"""
56-
This private method checks if `filename` is a string. If not it raises a TypeError.
57+
This private static method checks if `filename` is a string. If not it raises a TypeError.
5758
5859
:param string filename: file to check.
5960
"""
6061
if not isinstance(filename, basestring):
6162
raise TypeError('The given filename ({0!s}) must be a string'.format(filename))
6263

6364

64-
def _check_infile_instantiation(self, infile):
65+
@staticmethod
66+
def _check_infile_instantiation(infile):
6567
"""
66-
This private method checks if the input file `infile` is instantiated. If not it means
68+
This private static method checks if the input file `infile` is instantiated. If not it means
6769
that nobody called the parse method, i.e. `self.infile` is None. If the check fails
6870
it raises a RuntimeError.
6971

pygem/freeform.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class FFD(object):
6767
>>> original_mesh_points = np.load('tests/test_datasets/meshpoints_sphere_orig.npy')
6868
>>> free_form = ffd.FFD(ffd_parameters, original_mesh_points)
6969
>>> free_form.perform()
70-
>>> new_mesh_points = free_form.modified_mesh_points
71-
70+
>>> new_mesh_points = free_form.modified_mesh_points
7271
"""
7372
def __init__(self, ffd_parameters, original_mesh_points):
7473
self.parameters = ffd_parameters
@@ -152,9 +151,10 @@ def perform(self):
152151
reference_frame_mesh_points, inverse_transformation) + translation
153152

154153

155-
def _transform_points(self, original_points, transformation):
154+
@staticmethod
155+
def _transform_points(original_points, transformation):
156156
"""
157-
This method transforms the points according to the affine transformation taken from
157+
This private static method transforms the points according to the affine transformation taken from
158158
affine_points_fit method.
159159
160160
:param numpy.ndarray original_points: coordinates of the original points.

pygem/unvhandler.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,25 @@ def parse(self, filename):
3535

3636
self.infile = filename
3737

38-
input_file = open(self.infile, 'r')
39-
nline = 0
40-
while True:
41-
line = input_file.readline()
42-
nline += 1
43-
if len(line) == 0:
44-
break
45-
if line.startswith(' -1'):
46-
section_id = input_file.readline().strip()
38+
with open(self.infile, 'r') as input_file:
39+
nline = 0
40+
while True:
41+
line = input_file.readline()
4742
nline += 1
48-
if section_id == '2411':
49-
count = 0
50-
while not input_file.readline().startswith(' -1'):
51-
count += 1
52-
start_line = nline + 2
53-
last_line = start_line + count
54-
else:
55-
while not input_file.readline().startswith(' -1'):
56-
nline += 1
57-
58-
input_file.close()
43+
if not line:
44+
break
45+
if line.startswith(' -1'):
46+
section_id = input_file.readline().strip()
47+
nline += 1
48+
if section_id == '2411':
49+
count = 0
50+
while not input_file.readline().startswith(' -1'):
51+
count += 1
52+
start_line = nline + 2
53+
last_line = start_line + count
54+
else:
55+
while not input_file.readline().startswith(' -1'):
56+
nline += 1
5957

6058
n_points = count/2
6159
mesh_points = np.zeros(shape=(n_points, 3))
@@ -65,7 +63,7 @@ def parse(self, filename):
6563
with open(self.infile, 'r') as input_file:
6664
for line in input_file:
6765
nline += 1
68-
if nline % 2 == 1 and nline > start_line and nline < last_line:
66+
if nline % 2 == 1 and start_line < nline < last_line:
6967
line = line.strip()
7068
j = 0
7169
for number in line.split():
@@ -100,7 +98,7 @@ def write(self, mesh_points, filename):
10098
with open(self.infile, 'r') as input_file, open(self.outfile, 'w') as output_file:
10199
for line in input_file:
102100
nrow += 1
103-
if nrow % 2 == 1 and nrow > 20 and nrow <= (20 + n_points * 2):
101+
if nrow % 2 == 1 and 20 < nrow <= (20 + n_points * 2):
104102
for j in range(0, 3):
105103
output_file.write(' ' + str(mesh_points[i][j]))
106104
output_file.write('\n')

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from setuptools import setup
22

33
def readme():
4+
"""
5+
This function just return the content of README.md
6+
"""
47
with open('README.md') as f:
58
return f.read()
69

0 commit comments

Comments
 (0)