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