1 | """ |
---|
2 | Unit tests for data manipulations |
---|
3 | @author Gervaise Alina: unittest imcoplete so far |
---|
4 | """ |
---|
5 | import unittest |
---|
6 | import numpy, math |
---|
7 | from DataLoader.loader import Loader |
---|
8 | from DataLoader.data_info import Data1D, Data2D |
---|
9 | from DataLoader.invariant import InvariantCalculator |
---|
10 | |
---|
11 | |
---|
12 | class TestInvPolySphere(unittest.TestCase): |
---|
13 | """ |
---|
14 | Test unsmeared data for invariant computation |
---|
15 | """ |
---|
16 | def setUp(self): |
---|
17 | #Data with no slit smear information |
---|
18 | data= Loader().load("PolySpheres.txt") |
---|
19 | self.I = InvariantCalculator( data= data) |
---|
20 | |
---|
21 | |
---|
22 | def testWrongData(self): |
---|
23 | """ test receiving Data1D not of type loader""" |
---|
24 | #compute invariant with smear information |
---|
25 | from danse.common.plottools.plottables import Data1D |
---|
26 | data= Data1D(x=[1,2],y=[2,3] ) |
---|
27 | try: |
---|
28 | self.assertRaises(ValueError,InvariantCalculator(data)) |
---|
29 | except ValueError, msg: |
---|
30 | print "test pass "+ str(msg) |
---|
31 | else: raise ValueError, "fail to raise exception when expected" |
---|
32 | |
---|
33 | def testInvariant(self): |
---|
34 | """ test the invariant value for data without smearing""" |
---|
35 | self.assertAlmostEquals(self.I.q_star, 7.48959e-5,2) |
---|
36 | |
---|
37 | def testVolume(self): |
---|
38 | """ test volume fraction for polysphere""" |
---|
39 | volume = self.I.get_volume_fraction(contrast=2.6e-6) |
---|
40 | self.assert_(volume >= 0 and volume <=1) |
---|
41 | self.assertAlmostEqual(volume ,0.01) |
---|
42 | |
---|
43 | def testSurface(self): |
---|
44 | """ test surface for polysphere """ |
---|
45 | surface = self.I.get_surface(contrast=2.6e-6, porod_const=20) |
---|
46 | self.assertAlmostEqual(surface,0.01) |
---|
47 | |
---|
48 | |
---|
49 | class TestInvariantSmear(unittest.TestCase): |
---|
50 | """ |
---|
51 | Test smeared data for invariant computation |
---|
52 | """ |
---|
53 | def setUp(self): |
---|
54 | # data with smear info |
---|
55 | list = Loader().load("latex_smeared.xml") |
---|
56 | self.data_q_smear = list[0] |
---|
57 | self.I_q_smear = InvariantCalculator( data= self.data_q_smear) |
---|
58 | |
---|
59 | self.data_slit_smear = list[1] |
---|
60 | self.I_slit_smear = InvariantCalculator( data= self.data_slit_smear) |
---|
61 | |
---|
62 | from sans.models.PowerLawModel import PowerLawModel |
---|
63 | self.power_law = PowerLawModel() |
---|
64 | from sans.models.GuinierModel import GuinierModel |
---|
65 | self.guinier = GuinierModel() |
---|
66 | |
---|
67 | |
---|
68 | def test_invariant_slit(self): |
---|
69 | """ test the invariant value for data with slit smear""" |
---|
70 | self.assertTrue(self.I_slit_smear.q_star>0) |
---|
71 | self.assertAlmostEquals(self.I_slit_smear.q_star, 4.1539e-4) |
---|
72 | |
---|
73 | def test_volume_slit_smear(self): |
---|
74 | """ test volume fraction for data with slit smear""" |
---|
75 | volume = self.I_slit_smear.get_volume_fraction(contrast=2.6e-6) |
---|
76 | self.assert_(volume >= 0 and volume <=1) |
---|
77 | self.assertAlmostEqual(volume ,0.01) |
---|
78 | |
---|
79 | def test_invariant_slit_low_guinier(self): |
---|
80 | """ test the invariant with slit smear for data extrapolated low q range using guinier""" |
---|
81 | q_star_min = self.I_slit_smear.get_qstar_min( data= self.data_slit_smear, |
---|
82 | model= self.guinier, npts=10) |
---|
83 | self.assertAlmostEquals(q_star_min, 4.89783e-8) |
---|
84 | self.assertAlmostEquals(self.I.q_star_min, q_star_min) |
---|
85 | |
---|
86 | def test_invariant_slit_low_power(self): |
---|
87 | """ test the invariant with slit smear for data extrapolated low q range using pow_law""" |
---|
88 | q_star_min = self.I_slit_smear.get_qstar_min( data=self.data_slit_smear, |
---|
89 | model= self.power_law, npts=10) |
---|
90 | self.assertFalse(numpy.isfinite(q_star_min)) |
---|
91 | self.assertEquals(self.I.q_star_min, q_star_min) |
---|
92 | |
---|
93 | def test_invariant_slit_high_power(self): |
---|
94 | """ test the invariant with slit smear for data extrapolated high q range using pow_law""" |
---|
95 | q_star_max = self.I_slit_smear.get_qstar_max( data= self.data_slit_smear, |
---|
96 | model= self.power_law, npts=10) |
---|
97 | self.assertFalse(numpy.isfinite(q_star_max)) |
---|
98 | self.assertEquals(self.I.q_star_max, q_star_max) |
---|
99 | |
---|
100 | def testSurface(self): |
---|
101 | """ test volume fraction for data with slit smear""" |
---|
102 | surface = self.I_q_smear.get_surface(contrast=2.6e-6, porod_const=20) |
---|
103 | self.assertAlmostEqual(surface,0.01) |
---|
104 | |
---|
105 | def test_invariant_q(self): |
---|
106 | """ test the invariant value for data with q smear""" |
---|
107 | self.assertTrue(self.I_q_smear.q_star>0) |
---|
108 | self.assertAlmostEquals(self.I_q_smear.q_star, 1.361677e-3) |
---|
109 | |
---|
110 | def test_invariant_q_low_guinier(self): |
---|
111 | """ test the invariant with q smear for data extrapolated low q range using guinier""" |
---|
112 | q_star_min = self.I_slit_smear.get_qstar_min( data= self.data_q_smear, |
---|
113 | model= self.guinier, npts=10) |
---|
114 | self.assertAlmostEquals(q_star_min,6.76e-4) |
---|
115 | self.assertAlmostEquals(self.I.q_star_min, q_star_min) |
---|
116 | |
---|
117 | def test_invariant_q_low_power(self): |
---|
118 | """ test the invariant with q smear for data extrapolated low q range using pow_law""" |
---|
119 | q_star_min = self.I_slit_smear.get_qstar_min( data= self.data_q_smear, |
---|
120 | model= self.power_law, npts=10) |
---|
121 | self.assertFalse(numpy.isfinite(q_star_min)) |
---|
122 | self.assertEquals(self.I.q_star_min, q_star_min) |
---|
123 | |
---|
124 | def test_invariant_q_high_power(self): |
---|
125 | """ test the invariant with q smear for data extrapolated high q range using pow_law""" |
---|
126 | q_star_max = self.I_slit_smear.get_qstar_max( data= self.data_q_smear, |
---|
127 | model= self.power_law, npts=10) |
---|
128 | self.assertAlmostEquals(q_star_max,1.2e-4) |
---|
129 | self.assertEquals(self.I.q_star_max, q_star_max) |
---|
130 | |
---|
131 | def test_volume_q_smear(self): |
---|
132 | """ test volume fraction for data with q smear""" |
---|
133 | volume = self.I_slit_smear.get_volume_fraction(contrast=2.6e-6) |
---|
134 | self.assert_(volume > 0 and volume <=1) |
---|
135 | self.assertAlmostEqual(volume ,0.01) |
---|
136 | |
---|
137 | def testSurface_q_smear(self): |
---|
138 | """ test surface for data with q smear""" |
---|
139 | surface = self.I_q_smear.get_surface(contrast=2.6e-6, porod_const=20) |
---|
140 | self.assertAlmostEqual(surface,0.01) |
---|
141 | |
---|
142 | |
---|
143 | class ExtrapolationTest(unittest.TestCase): |
---|
144 | |
---|
145 | def setUp(self): |
---|
146 | #Data with no slit smear information |
---|
147 | self.data= Loader().load("PolySpheres.txt") |
---|
148 | from sans.models.PowerLawModel import PowerLawModel |
---|
149 | self.power_law = PowerLawModel() |
---|
150 | from sans.models.GuinierModel import GuinierModel |
---|
151 | self.guinier = GuinierModel() |
---|
152 | self.I = InvariantCalculator( data= self.data) |
---|
153 | |
---|
154 | |
---|
155 | def testInvariant(self): |
---|
156 | """ test the invariant value for data extrapolated""" |
---|
157 | self.assertAlmostEquals(self.I.q_star, 7.48959e-5) |
---|
158 | |
---|
159 | def testInvariantLowGuinier(self): |
---|
160 | """ test the invariant value for data extrapolated low range using guinier""" |
---|
161 | q_star_min = self.I.get_qstar_min( data= self.data, model= self.guinier, npts=10) |
---|
162 | self.assertAlmostEquals(q_star_min, 4.89783e-8) |
---|
163 | self.assertAlmostEquals(self.I.q_star_min, q_star_min) |
---|
164 | |
---|
165 | def testInvariantLowPower(self): |
---|
166 | """ test the invariant value for data extrapolated low range using pow_law""" |
---|
167 | q_star_min = self.I.get_qstar_min( data= self.data, model= self.power_law, npts=10) |
---|
168 | self.assertFalse(numpy.isfinite(q_star_min)) |
---|
169 | self.assertEquals(self.I.q_star_min, q_star_min) |
---|
170 | |
---|
171 | def testInvariantHigh(self): |
---|
172 | """ test the invariant value for data extrapolated high range""" |
---|
173 | q_star_max = self.I.get_qstar_max( self.data, model=self.power_law, npts=10) |
---|
174 | self.assertAlmostEquals(q_star_max, 4.066202e-6) |
---|
175 | self.assertAlmostEquals(self.I0.q_star_max, q_star_max) |
---|
176 | |
---|
177 | def testInvariantTotal(self): |
---|
178 | """ test the total invariant value for data extrapolated""" |
---|
179 | self.assertAlmostEquals(self.I.q_star_total, 7.88981e-5) |
---|
180 | |
---|
181 | def testVolume(self): |
---|
182 | """ test volume fraction for data extrapolated""" |
---|
183 | volume = self.I.get_volume_fraction(contrast=2.6e-6) |
---|
184 | self.assert_(volume > 0 and volume <=1) |
---|
185 | self.assertAlmostEqual(volume ,0.01) |
---|
186 | |
---|
187 | def testSurface(self): |
---|
188 | """ test surface for data extrapolated""" |
---|
189 | surface = self.I.get_surface(contrast=2.6e-6, porod_const=20) |
---|
190 | self.assertAlmostEqual(surface,0.01) |
---|
191 | |
---|
192 | if __name__ == '__main__': |
---|
193 | unittest.main() |
---|