Skip to content

Commit 7478228

Browse files
authored
Merge pull request #78 from mtezzele/write
write method in the RBFParameters class and tests
2 parents af438ea + 9dd6ea9 commit 7478228

File tree

7 files changed

+189
-4
lines changed

7 files changed

+189
-4
lines changed

pygem/params.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,11 @@ def __init__(self):
324324
self.deformed_control_points = None
325325

326326

327-
def read_parameters(self, filename='parameters.prm'):
327+
def read_parameters(self, filename='parameters_rbf.prm'):
328328
"""
329329
Reads in the parameters file and fill the self structure.
330330
331-
:param string filename: parameters file to be read in.
331+
:param string filename: parameters file to be read in. Default value is parameters_rbf.prm.
332332
"""
333333
if not isinstance(filename, basestring):
334334
raise TypeError('filename must be a string')
@@ -344,7 +344,7 @@ def read_parameters(self, filename='parameters.prm'):
344344
0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1., 1.]).reshape((8, 3))
345345
self.deformed_control_points = np.array([0., 0., 0., 0., 0., 1., 0., 1., 0., 1., 0., 0., \
346346
0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1., 1.]).reshape((8, 3))
347-
#self.write_parameters(filename)
347+
self.write_parameters(filename)
348348
return
349349

350350
config = ConfigParser.RawConfigParser()
@@ -374,6 +374,56 @@ def read_parameters(self, filename='parameters.prm'):
374374
self.deformed_control_points[i] = np.array([float(values[0]), float(values[1]), float(values[2])])
375375

376376

377+
def write_parameters(self, filename='parameters_rbf.prm'):
378+
"""
379+
This method writes a parameters file (.prm) called `filename` and fills it with all
380+
the parameters class members. Default value is parameters_rbf.prm.
381+
382+
:param string filename: parameters file to be written out.
383+
"""
384+
if not isinstance(filename, basestring):
385+
raise TypeError("filename must be a string")
386+
387+
with open(filename, 'w') as output_file:
388+
output_file.write('\n[Radial Basis Functions]\n')
389+
output_file.write('# This section describes the radial basis functions shape.\n')
390+
391+
output_file.write('\n# basis funtion is the name of the basis functions to use in the transformation. ' + \
392+
'The functions\n')
393+
output_file.write('# implemented so far are: gaussian_spline, multi_quadratic_biharmonic_spline,\n')
394+
output_file.write('# inv_multi_quadratic_biharmonic_spline, thin_plate_spline, beckert_wendland_c2_basis.\n')
395+
output_file.write('# For a comprehensive list with details see the class RBF.\n')
396+
output_file.write('basis function: ' + str(self.basis) + '\n')
397+
398+
output_file.write('\n# radius is the scaling parameter r that affects the shape of the basis functions. ' + \
399+
'See the documentation\n')
400+
output_file.write('# of the class RBF for details.\n')
401+
output_file.write('radius: ' + str(self.radius) + '\n')
402+
403+
output_file.write('\n\n[Control points]\n')
404+
output_file.write('# This section describes the RBF control points.\n')
405+
406+
output_file.write('\n# original control points collects the coordinates of the interpolation ' + \
407+
'control points before the deformation.\n')
408+
output_file.write('original control points:')
409+
offset = 1
410+
for i in range(0, self.n_control_points):
411+
output_file.write(offset * ' ' + str(self.original_control_points[i][0]) + ' ' + \
412+
str(self.original_control_points[i][1]) + ' ' + \
413+
str(self.original_control_points[i][2]) + '\n')
414+
offset = 25
415+
416+
output_file.write('\n# deformed control points collects the coordinates of the interpolation ' + \
417+
'control points after the deformation.\n')
418+
output_file.write('deformed control points:')
419+
offset = 1
420+
for i in range(0, self.n_control_points):
421+
output_file.write(offset * ' ' + str(self.deformed_control_points[i][0]) + ' ' + \
422+
str(self.deformed_control_points[i][1]) + ' ' + \
423+
str(self.deformed_control_points[i][2]) + '\n')
424+
offset = 25
425+
426+
377427
def print_info(self):
378428
"""
379429
This method prints all the RBF parameters on the screen. Its purpose is for debugging.

tests/test_datasets/parameters_rbf_bugged_01.prm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ basis function: gaussian_spline
1212
# of the class RBF for details.
1313
radius: 0.5
1414

15+
1516
[Control points]
1617
# This section describes the RBF control points.
1718

tests/test_datasets/parameters_rbf_bugged_02.prm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ basis function: gaussian_splines
1212
# of the class RBF for details.
1313
radius: 0.5
1414

15+
1516
[Control points]
1617
# This section describes the RBF control points.
1718

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
[Radial Basis Functions]
3+
# This section describes the radial basis functions shape.
4+
5+
# basis funtion is the name of the basis functions to use in the transformation. The functions
6+
# implemented so far are: gaussian_spline, multi_quadratic_biharmonic_spline,
7+
# inv_multi_quadratic_biharmonic_spline, thin_plate_spline, beckert_wendland_c2_basis.
8+
# For a comprehensive list with details see the class RBF.
9+
basis function: gaussian_spline
10+
11+
# radius is the scaling parameter r that affects the shape of the basis functions. See the documentation
12+
# of the class RBF for details.
13+
radius: 0.5
14+
15+
16+
[Control points]
17+
# This section describes the RBF control points.
18+
19+
# original control points collects the coordinates of the interpolation control points before the deformation.
20+
original control points: -.1 0.0 0.0
21+
0.0 0.0 1.0
22+
0.0 1.0 0.0
23+
1.0 0.0 0.0
24+
0.0 1 1.0
25+
1.0 0.0 1.0
26+
1.0 1.0 0
27+
1.0 1.0 1.0
28+
29+
# deformed control points collects the coordinates of the interpolation control points after the deformation.
30+
deformed control points: 0.1 0.2 .3
31+
0.0 0.0 1.0
32+
0.0 1.0 0.0
33+
1.0 0.0 0.0
34+
0.0 .8 1.0
35+
1.0 0.0 1.0
36+
1.0 1.0 0.0
37+
1.2 1.2 1.2
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
[Radial Basis Functions]
3+
# This section describes the radial basis functions shape.
4+
5+
# basis funtion is the name of the basis functions to use in the transformation. The functions
6+
# implemented so far are: gaussian_spline, multi_quadratic_biharmonic_spline,
7+
# inv_multi_quadratic_biharmonic_spline, thin_plate_spline, beckert_wendland_c2_basis.
8+
# For a comprehensive list with details see the class RBF.
9+
basis function: gaussian_spline
10+
11+
# radius is the scaling parameter r that affects the shape of the basis functions. See the documentation
12+
# of the class RBF for details.
13+
radius: 0.5
14+
15+
16+
[Control points]
17+
# This section describes the RBF control points.
18+
19+
# original control points collects the coordinates of the interpolation control points before the deformation.
20+
original control points: -0.1 0.0 0.0
21+
0.0 0.0 1.0
22+
0.0 1.0 0.0
23+
1.0 0.0 0.0
24+
0.0 1.0 1.0
25+
1.0 0.0 1.0
26+
1.0 1.0 0.0
27+
1.0 1.0 1.0
28+
29+
# deformed control points collects the coordinates of the interpolation control points after the deformation.
30+
deformed control points: 0.1 0.2 0.3
31+
0.0 0.0 1.0
32+
0.0 1.0 0.0
33+
1.0 0.0 0.0
34+
0.0 0.8 1.0
35+
1.0 0.0 1.0
36+
1.0 1.0 0.0
37+
1.2 1.2 1.2

tests/test_datasets/parameters_rbf_default.prm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ basis function: gaussian_spline
1212
# of the class RBF for details.
1313
radius: 0.5
1414

15+
1516
[Control points]
1617
# This section describes the RBF control points.
1718

tests/test_rbfparams.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88

99

10-
class TestFFDParameters(TestCase):
10+
class TestRBFParameters(TestCase):
1111

1212

1313
def test_class_members_default_basis(self):
@@ -81,6 +81,64 @@ def test_read_parameters_failing_number_deformed_control_points(self):
8181
params.read_parameters('tests/test_datasets/parameters_rbf_bugged_01.prm')
8282

8383

84+
def test_read_parameters_filename_default(self):
85+
params = rbfp.RBFParameters()
86+
params.read_parameters()
87+
outfilename = 'parameters_rbf.prm'
88+
outfilename_expected = 'tests/test_datasets/parameters_rbf_default.prm'
89+
90+
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
91+
os.remove(outfilename)
92+
93+
94+
def test_write_parameters_failing_filename_type(self):
95+
params = rbfp.RBFParameters()
96+
with self.assertRaises(TypeError):
97+
params.write_parameters(5)
98+
99+
100+
def test_write_parameters_filename_default_existance(self):
101+
params = rbfp.RBFParameters()
102+
params.basis = 'inv_multi_quadratic_biharmonic_spline'
103+
params.radius = 0.1
104+
params.n_control_points = 3
105+
params.original_control_points = np.array([0., 0., 0., 0., 0., 1., 0., 1., 0.]).reshape((3, 3))
106+
params.deformed_control_points = np.array([0., 0., 0., 0., 0., 1., 0., 1., 0.]).reshape((3, 3))
107+
params.write_parameters()
108+
outfilename = 'parameters_rbf.prm'
109+
assert os.path.isfile(outfilename)
110+
os.remove(outfilename)
111+
112+
113+
def test_write_parameters_filename_default(self):
114+
params = rbfp.RBFParameters()
115+
params.basis = 'gaussian_spline'
116+
params.radius = 0.5
117+
params.n_control_points = 8
118+
params.original_control_points = np.array([0., 0., 0., 0., 0., 1., 0., 1., 0., 1., 0., 0., \
119+
0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1., 1.]).reshape((8, 3))
120+
params.deformed_control_points = np.array([0., 0., 0., 0., 0., 1., 0., 1., 0., 1., 0., 0., \
121+
0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1., 1.]).reshape((8, 3))
122+
params.write_parameters()
123+
outfilename = 'parameters_rbf.prm'
124+
outfilename_expected = 'tests/test_datasets/parameters_rbf_default.prm'
125+
126+
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
127+
os.remove(outfilename)
128+
129+
130+
def test_write_parameters(self):
131+
params = rbfp.RBFParameters()
132+
params.read_parameters('tests/test_datasets/parameters_rbf_cube.prm')
133+
134+
outfilename = 'tests/test_datasets/parameters_rbf_cube_out.prm'
135+
outfilename_expected = 'tests/test_datasets/parameters_rbf_cube_out_true.prm'
136+
params.write_parameters(outfilename)
137+
138+
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
139+
os.remove(outfilename)
140+
141+
84142
def test_print_info(self):
85143
params = rbfp.RBFParameters()
86144
params.print_info()

0 commit comments

Comments
 (0)