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