1 | """ |
---|
2 | Implementation of the use-case from a usage perspective. |
---|
3 | TODO: check the return values to perform actual unit testing. |
---|
4 | """ |
---|
5 | import unittest |
---|
6 | import numpy |
---|
7 | from DataLoader.loader import Loader |
---|
8 | from sans.invariant import invariant |
---|
9 | |
---|
10 | class Data1D: |
---|
11 | print "I am not of type Dataloader.Data1D" |
---|
12 | |
---|
13 | class TestInvPolySphere(unittest.TestCase): |
---|
14 | """ |
---|
15 | Test unsmeared data for invariant computation |
---|
16 | """ |
---|
17 | def setUp(self): |
---|
18 | self.data = Loader().load("PolySpheres.txt") |
---|
19 | |
---|
20 | def test_wrong_data(self): |
---|
21 | """ test receiving Data1D not of type loader""" |
---|
22 | |
---|
23 | wrong_data= Data1D() |
---|
24 | try: |
---|
25 | self.assertRaises(ValueError,invariant.InvariantCalculator(wrong_data)) |
---|
26 | except ValueError, msg: |
---|
27 | print "test pass "+ str(msg) |
---|
28 | else: raise ValueError, "fail to raise exception when expected" |
---|
29 | |
---|
30 | |
---|
31 | def test_use_case_1(self): |
---|
32 | """ |
---|
33 | Invariant without extrapolation |
---|
34 | """ |
---|
35 | # Create invariant object. Background and scale left as defaults. |
---|
36 | inv = invariant.InvariantCalculator(data=self.data) |
---|
37 | |
---|
38 | # We have to be able to tell the InvariantCalculator whether we want the |
---|
39 | # extrapolation or not. By default, when the user doesn't specify, we |
---|
40 | # should compute Q* without extrapolation. That's what should be done in __init__. |
---|
41 | |
---|
42 | # We call get_qstar() with no argument, which signifies that we do NOT |
---|
43 | # want extrapolation. |
---|
44 | qstar = inv.get_qstar() |
---|
45 | |
---|
46 | # The volume fraction and surface use Q*. That means that the following |
---|
47 | # methods should check that Q* has been computed. If not, it should |
---|
48 | # compute it by calling get_qstare(), leaving the parameters as default. |
---|
49 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
50 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
51 | |
---|
52 | # Test results |
---|
53 | self.assertAlmostEquals(qstar, 7.48959e-5,2) |
---|
54 | self.assertAlmostEquals(v, 0.005644689) |
---|
55 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
56 | |
---|
57 | def test_use_case_2(self): |
---|
58 | """ |
---|
59 | Invariant without extrapolation. Invariant, volume fraction and surface |
---|
60 | are given with errors. |
---|
61 | """ |
---|
62 | # Create invariant object. Background and scale left as defaults. |
---|
63 | inv = invariant.InvariantCalculator(data=self.data) |
---|
64 | |
---|
65 | # Get the invariant with errors |
---|
66 | qstar, qstar_err = inv.get_qstar_with_error() |
---|
67 | |
---|
68 | # The volume fraction and surface use Q*. That means that the following |
---|
69 | # methods should check that Q* has been computed. If not, it should |
---|
70 | # compute it by calling get_qstare(), leaving the parameters as default. |
---|
71 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
72 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
73 | # Test results |
---|
74 | self.assertAlmostEquals(qstar, 7.48959e-5,2) |
---|
75 | self.assertAlmostEquals(v, 0.005644689) |
---|
76 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
77 | |
---|
78 | |
---|
79 | def test_use_case_3(self): |
---|
80 | """ |
---|
81 | Invariant with low-Q extrapolation |
---|
82 | """ |
---|
83 | # Create invariant object. Background and scale left as defaults. |
---|
84 | inv = invariant.InvariantCalculator(data=self.data) |
---|
85 | |
---|
86 | # Set the extrapolation parameters for the low-Q range |
---|
87 | |
---|
88 | # The npts parameter should have a good default. |
---|
89 | # The range parameter should be 'high' or 'low' |
---|
90 | # The function parameter should default to None. If it is None, |
---|
91 | # the method should pick a good default (Guinier at low-Q and 1/q^4 at high-Q). |
---|
92 | # The method should also check for consistency of the extrapolation and function |
---|
93 | # parameters. For instance, you might not want to allow 'high' and 'guinier'. |
---|
94 | # The power parameter (not shown below) should default to 4. |
---|
95 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
96 | |
---|
97 | # The version of the call without error |
---|
98 | # At this point, we could still compute Q* without extrapolation by calling |
---|
99 | # get_qstar with arguments, or with extrapolation=None. |
---|
100 | qstar = inv.get_qstar(extrapolation='low') |
---|
101 | |
---|
102 | # The version of the call with error |
---|
103 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') |
---|
104 | |
---|
105 | # Get the volume fraction and surface |
---|
106 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
107 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
108 | |
---|
109 | # Test results |
---|
110 | self.assertAlmostEquals(qstar, 7.49e-5) |
---|
111 | self.assertAlmostEquals(v, 0.005648401) |
---|
112 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
113 | |
---|
114 | def test_use_case_4(self): |
---|
115 | """ |
---|
116 | Invariant with high-Q extrapolation |
---|
117 | """ |
---|
118 | # Create invariant object. Background and scale left as defaults. |
---|
119 | inv = invariant.InvariantCalculator(data=self.data) |
---|
120 | |
---|
121 | # Set the extrapolation parameters for the high-Q range |
---|
122 | inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) |
---|
123 | |
---|
124 | # The version of the call without error |
---|
125 | # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' |
---|
126 | qstar = inv.get_qstar(extrapolation='high') |
---|
127 | |
---|
128 | # The version of the call with error |
---|
129 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') |
---|
130 | |
---|
131 | # Get the volume fraction and surface |
---|
132 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
133 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
134 | |
---|
135 | # Test results |
---|
136 | self.assertAlmostEquals(qstar, 7.49e-5,2) |
---|
137 | self.assertAlmostEquals(v, 0.005952674) |
---|
138 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
139 | |
---|
140 | def test_use_case_5(self): |
---|
141 | """ |
---|
142 | Invariant with both high- and low-Q extrapolation |
---|
143 | """ |
---|
144 | # Create invariant object. Background and scale left as defaults. |
---|
145 | inv = invariant.InvariantCalculator(data=self.data) |
---|
146 | |
---|
147 | # Set the extrapolation parameters for the low- and high-Q ranges |
---|
148 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
149 | inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) |
---|
150 | |
---|
151 | # The version of the call without error |
---|
152 | # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' |
---|
153 | qstar = inv.get_qstar(extrapolation='both') |
---|
154 | |
---|
155 | # The version of the call with error |
---|
156 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') |
---|
157 | |
---|
158 | # Get the volume fraction and surface |
---|
159 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
160 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
161 | |
---|
162 | # Test results |
---|
163 | self.assertAlmostEquals(qstar, 7.88981e-5,2) |
---|
164 | self.assertAlmostEquals(v, 0.005952674) |
---|
165 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
166 | |
---|
167 | class TestInvSlitSmear(unittest.TestCase): |
---|
168 | """ |
---|
169 | Test slit smeared data for invariant computation |
---|
170 | """ |
---|
171 | def setUp(self): |
---|
172 | # Data with slit smear |
---|
173 | list = Loader().load("latex_smeared.xml") |
---|
174 | self.data_slit_smear = list[1] |
---|
175 | |
---|
176 | def test_use_case_1(self): |
---|
177 | """ |
---|
178 | Invariant without extrapolation |
---|
179 | """ |
---|
180 | inv = invariant.InvariantCalculator(data=self.data_slit_smear) |
---|
181 | # get invariant |
---|
182 | qstar = inv.get_qstar() |
---|
183 | # Get the volume fraction and surface |
---|
184 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
185 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
186 | # Test results |
---|
187 | self.assertAlmostEquals(qstar, 4.1539e-4) |
---|
188 | self.assertAlmostEquals(v, 0.032164596) |
---|
189 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
190 | |
---|
191 | def test_use_case_2(self): |
---|
192 | """ |
---|
193 | Invariant without extrapolation. Invariant, volume fraction and surface |
---|
194 | are given with errors. |
---|
195 | """ |
---|
196 | # Create invariant object. Background and scale left as defaults. |
---|
197 | inv = invariant.InvariantCalculator(data=self.data_slit_smear) |
---|
198 | # Get the invariant with errors |
---|
199 | qstar, qstar_err = inv.get_qstar_with_error() |
---|
200 | # Get the volume fraction and surface |
---|
201 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
202 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
203 | # Test results |
---|
204 | self.assertAlmostEquals(qstar, 4.1539e-4) |
---|
205 | self.assertAlmostEquals(v, 0.032164596) |
---|
206 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
207 | |
---|
208 | def test_use_case_3(self): |
---|
209 | """ |
---|
210 | Invariant with low-Q extrapolation |
---|
211 | """ |
---|
212 | # Create invariant object. Background and scale left as defaults. |
---|
213 | inv = invariant.InvariantCalculator(data=self.data_slit_smear) |
---|
214 | # Set the extrapolation parameters for the low-Q range |
---|
215 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
216 | # The version of the call without error |
---|
217 | # At this point, we could still compute Q* without extrapolation by calling |
---|
218 | # get_qstar with arguments, or with extrapolation=None. |
---|
219 | qstar = inv.get_qstar(extrapolation='low') |
---|
220 | # The version of the call with error |
---|
221 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') |
---|
222 | # Get the volume fraction and surface |
---|
223 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
224 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
225 | |
---|
226 | # Test results |
---|
227 | self.assertAlmostEquals(qstar,4.1534e-4,3) |
---|
228 | self.assertAlmostEquals(v, 0.032164596) |
---|
229 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
230 | |
---|
231 | def test_use_case_4(self): |
---|
232 | """ |
---|
233 | Invariant with high-Q extrapolation |
---|
234 | """ |
---|
235 | # Create invariant object. Background and scale left as defaults. |
---|
236 | inv = invariant.InvariantCalculator(data=self.data_slit_smear) |
---|
237 | |
---|
238 | # Set the extrapolation parameters for the high-Q range |
---|
239 | inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) |
---|
240 | |
---|
241 | # The version of the call without error |
---|
242 | # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' |
---|
243 | qstar = inv.get_qstar(extrapolation='high') |
---|
244 | |
---|
245 | # The version of the call with error |
---|
246 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') |
---|
247 | |
---|
248 | # Get the volume fraction and surface |
---|
249 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
250 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
251 | |
---|
252 | # Test results |
---|
253 | self.assertAlmostEquals(qstar, 4.1539e-4) |
---|
254 | self.assertAlmostEquals(v, 0.032164596) |
---|
255 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
256 | |
---|
257 | def test_use_case_5(self): |
---|
258 | """ |
---|
259 | Invariant with both high- and low-Q extrapolation |
---|
260 | """ |
---|
261 | # Create invariant object. Background and scale left as defaults. |
---|
262 | inv = invariant.InvariantCalculator(data=self.data_slit_smear) |
---|
263 | |
---|
264 | # Set the extrapolation parameters for the low- and high-Q ranges |
---|
265 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
266 | inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) |
---|
267 | |
---|
268 | # The version of the call without error |
---|
269 | # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' |
---|
270 | qstar = inv.get_qstar(extrapolation='both') |
---|
271 | |
---|
272 | # The version of the call with error |
---|
273 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') |
---|
274 | |
---|
275 | # Get the volume fraction and surface |
---|
276 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
277 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
278 | |
---|
279 | # Test results |
---|
280 | self.assertAlmostEquals(qstar, 4.1534e-4,3) |
---|
281 | self.assertAlmostEquals(v, 0.032164596) |
---|
282 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
283 | |
---|
284 | class TestInvPinholeSmear(unittest.TestCase): |
---|
285 | """ |
---|
286 | Test pinhole smeared data for invariant computation |
---|
287 | """ |
---|
288 | def setUp(self): |
---|
289 | # data with smear info |
---|
290 | list = Loader().load("latex_smeared.xml") |
---|
291 | self.data_q_smear = list[0] |
---|
292 | |
---|
293 | def test_use_case_1(self): |
---|
294 | """ |
---|
295 | Invariant without extrapolation |
---|
296 | """ |
---|
297 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
298 | qstar = inv.get_qstar() |
---|
299 | |
---|
300 | volume_fraction = inv.get_volume_fraction(contrast=2.6e-6) |
---|
301 | surface = inv.get_surface(contrast=2.6e-6, porod_const=2) |
---|
302 | # Test results |
---|
303 | self.assertAlmostEquals(qstar, 1.361677e-3) |
---|
304 | self.assertAlmostEquals(v, 0.115352622) |
---|
305 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
306 | |
---|
307 | def test_use_case_2(self): |
---|
308 | """ |
---|
309 | Invariant without extrapolation. Invariant, volume fraction and surface |
---|
310 | are given with errors. |
---|
311 | """ |
---|
312 | # Create invariant object. Background and scale left as defaults. |
---|
313 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
314 | |
---|
315 | # Get the invariant with errors |
---|
316 | qstar, qstar_err = inv.get_qstar_with_error() |
---|
317 | # Get the volume fraction and surface |
---|
318 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
319 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
320 | # Test results |
---|
321 | self.assertAlmostEquals(qstar, 1.361677e-3) |
---|
322 | self.assertAlmostEquals(v, 0.115352622) |
---|
323 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
324 | |
---|
325 | def test_use_case_3(self): |
---|
326 | """ |
---|
327 | Invariant with low-Q extrapolation |
---|
328 | """ |
---|
329 | # Create invariant object. Background and scale left as defaults. |
---|
330 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
331 | # Set the extrapolation parameters for the low-Q range |
---|
332 | inv.set_extrapolation(range='low', npts=20, function='guinier') |
---|
333 | # The version of the call without error |
---|
334 | qstar = inv.get_qstar(extrapolation='low') |
---|
335 | # The version of the call with error |
---|
336 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') |
---|
337 | # Get the volume fraction and surface |
---|
338 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
339 | s, ds = inv.get_surface_with_error(contrast=1.0, porod_const=0.08) |
---|
340 | |
---|
341 | # Test results |
---|
342 | self.assertAlmostEquals(qstar, 0.002037677) |
---|
343 | self.assertAlmostEquals(v, 0.115352622) |
---|
344 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
345 | |
---|
346 | def test_use_case_4(self): |
---|
347 | """ |
---|
348 | Invariant with high-Q extrapolation |
---|
349 | """ |
---|
350 | # Create invariant object. Background and scale left as defaults. |
---|
351 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
352 | # Set the extrapolation parameters for the high-Q range |
---|
353 | inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) |
---|
354 | # The version of the call without error |
---|
355 | qstar = inv.get_qstar(extrapolation='high') |
---|
356 | # The version of the call with error |
---|
357 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') |
---|
358 | # Get the volume fraction and surface |
---|
359 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
360 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
361 | |
---|
362 | # Test results |
---|
363 | self.assertAlmostEquals(qstar, 1.481677e-3) |
---|
364 | self.assertAlmostEquals(v, 0.127225804) |
---|
365 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|
366 | |
---|
367 | def test_use_case_5(self): |
---|
368 | """ |
---|
369 | Invariant with both high- and low-Q extrapolation |
---|
370 | """ |
---|
371 | # Create invariant object. Background and scale left as defaults. |
---|
372 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
373 | # Set the extrapolation parameters for the low- and high-Q ranges |
---|
374 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
375 | inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) |
---|
376 | # The version of the call without error |
---|
377 | # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' |
---|
378 | qstar = inv.get_qstar(extrapolation='both') |
---|
379 | # The version of the call with error |
---|
380 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') |
---|
381 | # Get the volume fraction and surface |
---|
382 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
383 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
384 | |
---|
385 | # Test results |
---|
386 | self.assertAlmostEquals(qstar, 0.0021621) |
---|
387 | self.assertAlmostEquals(v, 0.202846825) |
---|
388 | self.assertAlmostEquals(s, 9.42e+2,2) |
---|