Skip to content

Commit 6fdf12b

Browse files
authored
Merge pull request #80 from Sparks29032/testdata_metadata
Added test for MorphISpheroid, some metadata in test files
2 parents ad4df08 + f8a8b82 commit 6fdf12b

File tree

6 files changed

+80
-15
lines changed

6 files changed

+80
-15
lines changed

diffpy/pdfmorph/morphs/morphishape.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class MorphISpheroid -- apply inverse spheroidal shape function
2121

2222
from diffpy.pdfmorph.morphs.morph import *
2323
from diffpy.pdfmorph.morphs.morphshape import _sphericalCF, _spheroidalCF
24+
import numpy
2425

2526

2627
class MorphISphere(Morph):
@@ -44,7 +45,8 @@ def morph(self, x_morph, y_morph, x_target, y_target):
4445
"""Apply a scale factor."""
4546
Morph.morph(self, x_morph, y_morph, x_target, y_target)
4647
f = _sphericalCF(x_morph, 2 * self.iradius)
47-
self.y_morph_out /= f
48+
with numpy.errstate(divide='ignore', invalid='ignore'):
49+
self.y_morph_out /= f
4850
self.y_morph_out[f == 0] = 0
4951
return self.xyallout
5052

@@ -74,7 +76,8 @@ def morph(self, x_morph, y_morph, x_target, y_target):
7476
"""Apply a scale factor."""
7577
Morph.morph(self, x_morph, y_morph, x_target, y_target)
7678
f = _spheroidalCF(x_morph, self.iradius, self.ipradius)
77-
self.y_morph_out /= f
79+
with numpy.errstate(divide='ignore', invalid='ignore'):
80+
self.y_morph_out /= f
7881
self.y_morph_out[f == 0] = 0
7982
return self.xyallout
8083

diffpy/pdfmorph/tests/test_morphshape.py

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
testdata_dir = os.path.join(tests_dir, 'testdata')
1313

1414
from diffpy.pdfmorph.morphs.morphshape import MorphSphere, MorphSpheroid
15+
from diffpy.pdfmorph.morphs.morphishape import MorphISphere, MorphISpheroid
1516

1617

1718
class TestMorphSphere(unittest.TestCase):
1819
def setUp(self):
1920
morph_file = os.path.join(testdata_dir, "ni_qmax25.cgr")
2021
self.x_morph, self.y_morph = numpy.loadtxt(morph_file, unpack=True)
21-
target_file = os.path.join(testdata_dir, "ni_qmax25_psize30.cgr")
22+
target_file = os.path.join(testdata_dir, "ni_qmax25_psize35.cgr")
2223
self.x_target, self.y_target = numpy.loadtxt(target_file, unpack=True)
2324
return
2425

@@ -38,24 +39,48 @@ def test_morph(self):
3839

3940

4041
class TestMorphSpheroid(unittest.TestCase):
42+
# Common configs for testing MorphSpheroid and MorphISpheroid
43+
# FIXME: add test data for prolate spheroids
44+
config_sphere = {"radius": 17.5, "pradius": 17.5}
45+
config_oblate = {"radius": 17.5, "pradius": 5.0}
46+
spheroid_configs = [config_sphere, config_oblate]
47+
iconfig_sphere = {"iradius": 17.5, "ipradius": 17.5}
48+
iconfig_oblate = {"iradius": 17.5, "ipradius": 5.0}
49+
ispheroid_configs = [iconfig_sphere, iconfig_oblate]
50+
51+
# Files used for testing
52+
flag_inverse = 0 # Indicates whether we are testing MorphSpheroid or MorphISpheroid
53+
testfiles = [
54+
["ni_qmax25.cgr", "ni_qmax25_psize35.cgr"], # Sphere
55+
["ni_qmax25.cgr", "ni_qmax25_e17.5_p5.0.cgr"], # Oblate spheroid
56+
]
57+
testfile = [] # Initialize testfile array
58+
4159
def setUp(self):
42-
morph_file = os.path.join(testdata_dir, "ni_qmax25.cgr")
60+
if len(self.testfile) == 0:
61+
# Ignore first init
62+
return
63+
morph_file = os.path.join(testdata_dir, self.testfile[0 - self.flag_inverse])
4364
self.x_morph, self.y_morph = numpy.loadtxt(morph_file, unpack=True)
44-
target_file = os.path.join(testdata_dir, "ni_qmax25_e17.5_p5.0.cgr")
65+
target_file = os.path.join(testdata_dir, self.testfile[1 - self.flag_inverse])
4566
self.x_target, self.y_target = numpy.loadtxt(target_file, unpack=True)
4667
return
4768

4869
def test_morph(self):
49-
"""check MorphSphere.morph()"""
50-
config_oblate = {
51-
"radius": 17.5,
52-
"pradius": 5.0,
53-
}
54-
# FIXME: add test data for prolate spheroids and spheres
55-
configs = [config_oblate]
56-
57-
for config in configs:
58-
self.shape_test_helper(config)
70+
"""check MorphSpheroid.morph() and MorphISpheroid.morph()"""
71+
72+
for idx in range(len(self.testfiles)):
73+
self.testfile = self.testfiles[idx]
74+
75+
# Test MorphSpheroid.morph()
76+
self.flag_inverse = 0
77+
self.setUp()
78+
self.shape_test_helper(self.spheroid_configs[idx])
79+
80+
# Test MorphISpheroid.morph()
81+
self.flag_inverse = 1
82+
self.setUp()
83+
self.ishape_test_helper(self.ispheroid_configs[idx])
5984
return
6085

6186
def shape_test_helper(self, config):
@@ -67,6 +92,23 @@ def shape_test_helper(self, config):
6792
self.assertTrue(numpy.allclose(y_morph, y_target))
6893
return
6994

95+
def ishape_test_helper(self, config):
96+
morph = MorphISpheroid(config)
97+
98+
x_morph, y_morph, x_target, y_target = morph(self.x_morph, self.y_morph, self.x_target, self.y_target)
99+
100+
self.assertTrue(numpy.allclose(self.y_target, y_target))
101+
102+
psize = 2 * max(config["iradius"], config["ipradius"])
103+
for idx in range(len(x_morph)):
104+
if x_morph[idx] < psize: # Within the particle
105+
self.assertTrue(numpy.isclose(y_morph[idx], y_target[idx]))
106+
elif x_morph[idx] == psize:
107+
pass # FIXME: determine behavior at boundary
108+
else: # Outside the particle morph should be zero
109+
self.assertTrue(y_morph[idx] == 0)
110+
return
111+
70112

71113
# End of class TestMorphSpheroid
72114

diffpy/pdfmorph/tests/testdata/ni_qmax25.cgr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# qmax=25
2+
3+
##### start data
4+
#L r(A) G(r)
15
0.000000000000000000e+00 0.000000000000000000e+00
26
1.000000000000000021e-02 -1.243355089687663353e-02
37
2.000000000000000042e-02 -2.480463559665002649e-02

diffpy/pdfmorph/tests/testdata/ni_qmax25_e17.5_p5.0.cgr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# qmax = 25
2+
# erad = 17.5
3+
# prad = 5.0
4+
5+
##### start data
6+
#L r(A) G(r)
17
0.000000000000000000e+00 0.000000000000000000e+00
28
1.000000000000000021e-02 -1.242269673220141278e-02
39
2.000000000000000042e-02 -2.476132802191605836e-02

diffpy/pdfmorph/tests/testdata/ni_qmax25_psize30.cgr renamed to diffpy/pdfmorph/tests/testdata/ni_qmax25_psize35.cgr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# qmax = 25
2+
# psize = 35
3+
4+
##### start data
5+
#L r(A) G(r)
16
0.000000000000000000e+00 0.000000000000000000e+00
27
1.000000000000000021e-02 -1.242822223235153997e-02
38
2.000000000000000042e-02 -2.478337448273846069e-02

diffpy/pdfmorph/tests/testdata/ni_qmax25_qdamp0.01.cgr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# qmax = 25
2+
# qdamp = 0.01
3+
4+
##### start data
5+
#L r(A) G(r)
16
0.000000000000000000e+00 0.000000000000000000e+00
27
1.000000000000000021e-02 -1.243355083470887995e-02
38
2.000000000000000042e-02 -2.480463510055731791e-02

0 commit comments

Comments
 (0)