1212testdata_dir = os .path .join (tests_dir , 'testdata' )
1313
1414from diffpy .pdfmorph .morphs .morphshape import MorphSphere , MorphSpheroid
15+ from diffpy .pdfmorph .morphs .morphishape import MorphISphere , MorphISpheroid
1516
1617
1718class 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
4041class 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
0 commit comments