1 | """ |
---|
2 | This software was developed by the University of Tennessee as part of the |
---|
3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | project funded by the US National Science Foundation. |
---|
5 | |
---|
6 | See the license text in license.txt |
---|
7 | |
---|
8 | copyright 2010, University of Tennessee |
---|
9 | """ |
---|
10 | import unittest |
---|
11 | import numpy, math |
---|
12 | from sans.dataloader.loader import Loader |
---|
13 | from sans.dataloader.data_info import Data1D |
---|
14 | from sans.invariant import invariant |
---|
15 | |
---|
16 | class TestLinearFit(unittest.TestCase): |
---|
17 | """ |
---|
18 | Test Line fit |
---|
19 | """ |
---|
20 | def setUp(self): |
---|
21 | x = numpy.asarray([1.,2.,3.,4.,5.,6.,7.,8.,9.]) |
---|
22 | y = numpy.asarray([1.,2.,3.,4.,5.,6.,7.,8.,9.]) |
---|
23 | dy = y/10.0 |
---|
24 | |
---|
25 | self.data = Data1D(x=x,y=y,dy=dy) |
---|
26 | |
---|
27 | def test_fit_linear_data(self): |
---|
28 | """ |
---|
29 | Simple linear fit |
---|
30 | """ |
---|
31 | |
---|
32 | # Create invariant object. Background and scale left as defaults. |
---|
33 | fit = invariant.Extrapolator(data=self.data) |
---|
34 | #a,b = fit.fit() |
---|
35 | p, dp = fit.fit() |
---|
36 | |
---|
37 | # Test results |
---|
38 | self.assertAlmostEquals(p[0], 1.0, 5) |
---|
39 | self.assertAlmostEquals(p[1], 0.0, 5) |
---|
40 | |
---|
41 | def test_fit_linear_data_with_noise(self): |
---|
42 | """ |
---|
43 | Simple linear fit with noise |
---|
44 | """ |
---|
45 | import random, math |
---|
46 | |
---|
47 | for i in range(len(self.data.y)): |
---|
48 | self.data.y[i] = self.data.y[i]+.1*(random.random()-0.5) |
---|
49 | |
---|
50 | # Create invariant object. Background and scale left as defaults. |
---|
51 | fit = invariant.Extrapolator(data=self.data) |
---|
52 | p, dp = fit.fit() |
---|
53 | |
---|
54 | # Test results |
---|
55 | self.assertTrue(math.fabs(p[0]-1.0)<0.05) |
---|
56 | self.assertTrue(math.fabs(p[1])<0.1) |
---|
57 | |
---|
58 | def test_fit_with_fixed_parameter(self): |
---|
59 | """ |
---|
60 | Linear fit for y=ax+b where a is fixed. |
---|
61 | """ |
---|
62 | # Create invariant object. Background and scale left as defaults. |
---|
63 | fit = invariant.Extrapolator(data=self.data) |
---|
64 | p, dp = fit.fit(power=-1.0) |
---|
65 | |
---|
66 | # Test results |
---|
67 | self.assertAlmostEquals(p[0], 1.0, 5) |
---|
68 | self.assertAlmostEquals(p[1], 0.0, 5) |
---|
69 | |
---|
70 | def test_fit_linear_data_with_noise_and_fixed_par(self): |
---|
71 | """ |
---|
72 | Simple linear fit with noise |
---|
73 | """ |
---|
74 | import random, math |
---|
75 | |
---|
76 | for i in range(len(self.data.y)): |
---|
77 | self.data.y[i] = self.data.y[i]+.1*(random.random()-0.5) |
---|
78 | |
---|
79 | # Create invariant object. Background and scale left as defaults. |
---|
80 | fit = invariant.Extrapolator(data=self.data) |
---|
81 | p, dp = fit.fit(power=-1.0) |
---|
82 | |
---|
83 | # Test results |
---|
84 | self.assertTrue(math.fabs(p[0]-1.0)<0.05) |
---|
85 | self.assertTrue(math.fabs(p[1])<0.1) |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | class TestInvariantCalculator(unittest.TestCase): |
---|
90 | """ |
---|
91 | Test main functionality of the Invariant calculator |
---|
92 | """ |
---|
93 | def setUp(self): |
---|
94 | data = Loader().load("latex_smeared_slit.xml") |
---|
95 | self.data = data[0] |
---|
96 | self.data.dxl = None |
---|
97 | |
---|
98 | def test_initial_data_processing(self): |
---|
99 | """ |
---|
100 | Test whether the background and scale are handled properly |
---|
101 | when creating an InvariantCalculator object |
---|
102 | """ |
---|
103 | length = len(self.data.x) |
---|
104 | self.assertEqual(length, len(self.data.y)) |
---|
105 | inv = invariant.InvariantCalculator(self.data) |
---|
106 | |
---|
107 | self.assertEqual(length, len(inv._data.x)) |
---|
108 | self.assertEqual(inv._data.x[0], self.data.x[0]) |
---|
109 | |
---|
110 | # Now the same thing with a background value |
---|
111 | bck = 0.1 |
---|
112 | inv = invariant.InvariantCalculator(self.data, background=bck) |
---|
113 | self.assertEqual(inv._background, bck) |
---|
114 | |
---|
115 | self.assertEqual(length, len(inv._data.x)) |
---|
116 | self.assertEqual(inv._data.y[0]+bck, self.data.y[0]) |
---|
117 | |
---|
118 | # Now the same thing with a scale value |
---|
119 | scale = 0.1 |
---|
120 | inv = invariant.InvariantCalculator(self.data, scale=scale) |
---|
121 | self.assertEqual(inv._scale, scale) |
---|
122 | |
---|
123 | self.assertEqual(length, len(inv._data.x)) |
---|
124 | self.assertAlmostEqual(inv._data.y[0]/scale, self.data.y[0],7) |
---|
125 | |
---|
126 | |
---|
127 | def test_incompatible_data_class(self): |
---|
128 | """ |
---|
129 | Check that only classes that inherit from Data1D are allowed as data. |
---|
130 | """ |
---|
131 | class Incompatible(): |
---|
132 | pass |
---|
133 | self.assertRaises(ValueError, invariant.InvariantCalculator, Incompatible()) |
---|
134 | |
---|
135 | def test_error_treatment(self): |
---|
136 | x = numpy.asarray(numpy.asarray([0,1,2,3])) |
---|
137 | y = numpy.asarray(numpy.asarray([1,1,1,1])) |
---|
138 | |
---|
139 | # These are all the values of the dy array that would cause |
---|
140 | # us to set all dy values to 1.0 at __init__ time. |
---|
141 | dy_list = [ [], None, [0,0,0,0] ] |
---|
142 | |
---|
143 | for dy in dy_list: |
---|
144 | data = Data1D(x=x, y=y, dy=dy) |
---|
145 | inv = invariant.InvariantCalculator(data) |
---|
146 | self.assertEqual(len(inv._data.x), len(inv._data.dy)) |
---|
147 | self.assertEqual(len(inv._data.dy), 4) |
---|
148 | for i in range(4): |
---|
149 | self.assertEqual(inv._data.dy[i],1) |
---|
150 | |
---|
151 | def test_qstar_low_q_guinier(self): |
---|
152 | """ |
---|
153 | Test low-q extrapolation with a Guinier |
---|
154 | """ |
---|
155 | inv = invariant.InvariantCalculator(self.data) |
---|
156 | |
---|
157 | # Basic sanity check |
---|
158 | _qstar = inv.get_qstar() |
---|
159 | qstar, dqstar = inv.get_qstar_with_error() |
---|
160 | self.assertEqual(qstar, _qstar) |
---|
161 | |
---|
162 | # Low-Q Extrapolation |
---|
163 | # Check that the returned invariant is what we expect given |
---|
164 | # the result we got without extrapolation |
---|
165 | inv.set_extrapolation('low', npts=10, function='guinier') |
---|
166 | qs_extr, dqs_extr = inv.get_qstar_with_error('low') |
---|
167 | delta_qs_extr, delta_dqs_extr = inv.get_qstar_low() |
---|
168 | |
---|
169 | self.assertEqual(qs_extr, _qstar+delta_qs_extr) |
---|
170 | self.assertEqual(dqs_extr, math.sqrt(dqstar*dqstar + delta_dqs_extr*delta_dqs_extr)) |
---|
171 | |
---|
172 | # We don't expect the extrapolated invariant to be very far from the |
---|
173 | # result without extrapolation. Let's test for a result within 10%. |
---|
174 | self.assertTrue(math.fabs(qs_extr-qstar)/qstar<0.1) |
---|
175 | |
---|
176 | # Check that the two results are consistent within errors |
---|
177 | # Note that the error on the extrapolated value takes into account |
---|
178 | # a systematic error for the fact that we may not know the shape of I(q) at low Q. |
---|
179 | self.assertTrue(math.fabs(qs_extr-qstar)<dqs_extr) |
---|
180 | |
---|
181 | def test_qstar_low_q_power_law(self): |
---|
182 | """ |
---|
183 | Test low-q extrapolation with a power law |
---|
184 | """ |
---|
185 | inv = invariant.InvariantCalculator(self.data) |
---|
186 | |
---|
187 | # Basic sanity check |
---|
188 | _qstar = inv.get_qstar() |
---|
189 | qstar, dqstar = inv.get_qstar_with_error() |
---|
190 | self.assertEqual(qstar, _qstar) |
---|
191 | |
---|
192 | # Low-Q Extrapolation |
---|
193 | # Check that the returned invariant is what we expect given |
---|
194 | inv.set_extrapolation('low', npts=10, function='power_law') |
---|
195 | qs_extr, dqs_extr = inv.get_qstar_with_error('low') |
---|
196 | delta_qs_extr, delta_dqs_extr = inv.get_qstar_low() |
---|
197 | |
---|
198 | # A fit using SansView gives 0.0655 for the value of the exponent |
---|
199 | self.assertAlmostEqual(inv._low_extrapolation_function.power, 0.0655, 3) |
---|
200 | |
---|
201 | if False: |
---|
202 | npts = len(inv._data.x)-1 |
---|
203 | import matplotlib.pyplot as plt |
---|
204 | plt.loglog(inv._data.x[:npts], inv._data.y[:npts], 'o', label='Original data', markersize=10) |
---|
205 | plt.loglog(inv._data.x[:npts], inv._low_extrapolation_function.evaluate_model(inv._data.x[:npts]), 'r', label='Fitted line') |
---|
206 | plt.legend() |
---|
207 | plt.show() |
---|
208 | |
---|
209 | self.assertEqual(qs_extr, _qstar+delta_qs_extr) |
---|
210 | self.assertAlmostEqual(dqs_extr, math.sqrt(dqstar*dqstar + delta_dqs_extr*delta_dqs_extr), 15) |
---|
211 | |
---|
212 | # We don't expect the extrapolated invariant to be very far from the |
---|
213 | # result without extrapolation. Let's test for a result within 10%. |
---|
214 | self.assertTrue(math.fabs(qs_extr-qstar)/qstar<0.1) |
---|
215 | |
---|
216 | # Check that the two results are consistent within errors |
---|
217 | # Note that the error on the extrapolated value takes into account |
---|
218 | # a systematic error for the fact that we may not know the shape of I(q) at low Q. |
---|
219 | self.assertTrue(math.fabs(qs_extr-qstar)<dqs_extr) |
---|
220 | |
---|
221 | def test_qstar_high_q(self): |
---|
222 | """ |
---|
223 | Test high-q extrapolation |
---|
224 | """ |
---|
225 | inv = invariant.InvariantCalculator(self.data) |
---|
226 | |
---|
227 | # Basic sanity check |
---|
228 | _qstar = inv.get_qstar() |
---|
229 | qstar, dqstar = inv.get_qstar_with_error() |
---|
230 | self.assertEqual(qstar, _qstar) |
---|
231 | |
---|
232 | # High-Q Extrapolation |
---|
233 | # Check that the returned invariant is what we expect given |
---|
234 | # the result we got without extrapolation |
---|
235 | inv.set_extrapolation('high', npts=20, function='power_law') |
---|
236 | qs_extr, dqs_extr = inv.get_qstar_with_error('high') |
---|
237 | delta_qs_extr, delta_dqs_extr = inv.get_qstar_high() |
---|
238 | |
---|
239 | # From previous analysis using SansView, we expect an exponent of about 3 |
---|
240 | self.assertTrue(math.fabs(inv._high_extrapolation_function.power-3)<0.1) |
---|
241 | |
---|
242 | self.assertEqual(qs_extr, _qstar+delta_qs_extr) |
---|
243 | self.assertAlmostEqual(dqs_extr, math.sqrt(dqstar*dqstar + delta_dqs_extr*delta_dqs_extr), 10) |
---|
244 | |
---|
245 | # We don't expect the extrapolated invariant to be very far from the |
---|
246 | # result without extrapolation. Let's test for a result within 10%. |
---|
247 | #TODO: verify whether this test really makes sense |
---|
248 | #self.assertTrue(math.fabs(qs_extr-qstar)/qstar<0.1) |
---|
249 | |
---|
250 | # Check that the two results are consistent within errors |
---|
251 | self.assertTrue(math.fabs(qs_extr-qstar)<dqs_extr) |
---|
252 | |
---|
253 | def test_qstar_full_q(self): |
---|
254 | """ |
---|
255 | Test high-q extrapolation |
---|
256 | """ |
---|
257 | inv = invariant.InvariantCalculator(self.data) |
---|
258 | |
---|
259 | # Basic sanity check |
---|
260 | _qstar = inv.get_qstar() |
---|
261 | qstar, dqstar = inv.get_qstar_with_error() |
---|
262 | self.assertEqual(qstar, _qstar) |
---|
263 | |
---|
264 | # High-Q Extrapolation |
---|
265 | # Check that the returned invariant is what we expect given |
---|
266 | # the result we got without extrapolation |
---|
267 | inv.set_extrapolation('low', npts=10, function='guinier') |
---|
268 | inv.set_extrapolation('high', npts=20, function='power_law') |
---|
269 | qs_extr, dqs_extr = inv.get_qstar_with_error('both') |
---|
270 | delta_qs_low, delta_dqs_low = inv.get_qstar_low() |
---|
271 | delta_qs_hi, delta_dqs_hi = inv.get_qstar_high() |
---|
272 | |
---|
273 | self.assertAlmostEqual(qs_extr, _qstar+delta_qs_low+delta_qs_hi, 8) |
---|
274 | self.assertAlmostEqual(dqs_extr, math.sqrt(dqstar*dqstar + delta_dqs_low*delta_dqs_low \ |
---|
275 | + delta_dqs_hi*delta_dqs_hi), 8) |
---|
276 | |
---|
277 | # We don't expect the extrapolated invariant to be very far from the |
---|
278 | # result without extrapolation. Let's test for a result within 10%. |
---|
279 | #TODO: verify whether this test really makes sense |
---|
280 | #self.assertTrue(math.fabs(qs_extr-qstar)/qstar<0.1) |
---|
281 | |
---|
282 | # Check that the two results are consistent within errors |
---|
283 | self.assertTrue(math.fabs(qs_extr-qstar)<dqs_extr) |
---|
284 | |
---|
285 | def _check_values(to_check, reference, tolerance=0.05): |
---|
286 | self.assertTrue( math.fabs(to_check-reference)/reference < tolerance, msg="Tested value = "+str(to_check) ) |
---|
287 | |
---|
288 | # The following values should be replaced by values pulled from IGOR |
---|
289 | # Volume Fraction: |
---|
290 | v, dv = inv.get_volume_fraction_with_error(1, None) |
---|
291 | _check_values(v, 1.88737914186e-15) |
---|
292 | |
---|
293 | v_l, dv_l = inv.get_volume_fraction_with_error(1, 'low') |
---|
294 | _check_values(v_l, 1.94289029309e-15) |
---|
295 | |
---|
296 | v_h, dv_h = inv.get_volume_fraction_with_error(1, 'high') |
---|
297 | _check_values(v_h, 6.99440505514e-15) |
---|
298 | |
---|
299 | v_b, dv_b = inv.get_volume_fraction_with_error(1, 'both') |
---|
300 | _check_values(v_b, 6.99440505514e-15) |
---|
301 | |
---|
302 | # Specific Surface: |
---|
303 | s, ds = inv.get_surface_with_error(1, 1, None) |
---|
304 | _check_values(s, 3.1603095786e-09) |
---|
305 | |
---|
306 | s_l, ds_l = inv.get_surface_with_error(1, 1, 'low') |
---|
307 | _check_values(s_l, 3.1603095786e-09) |
---|
308 | |
---|
309 | s_h, ds_h = inv.get_surface_with_error(1, 1, 'high') |
---|
310 | _check_values(s_h, 3.1603095786e-09) |
---|
311 | |
---|
312 | s_b, ds_b = inv.get_surface_with_error(1, 1, 'both') |
---|
313 | _check_values(s_b, 3.1603095786e-09) |
---|
314 | |
---|
315 | |
---|
316 | def test_bad_parameter_name(self): |
---|
317 | """ |
---|
318 | The set_extrapolation method checks that the name of the extrapolation |
---|
319 | function and the name of the q-range to extrapolate (high/low) is |
---|
320 | recognized. |
---|
321 | """ |
---|
322 | inv = invariant.InvariantCalculator(self.data) |
---|
323 | self.assertRaises(ValueError, inv.set_extrapolation, 'low', npts=4, function='not_a_name') |
---|
324 | self.assertRaises(ValueError, inv.set_extrapolation, 'not_a_range', npts=4, function='guinier') |
---|
325 | self.assertRaises(ValueError, inv.set_extrapolation, 'high', npts=4, function='guinier') |
---|
326 | |
---|
327 | |
---|
328 | class TestGuinierExtrapolation(unittest.TestCase): |
---|
329 | """ |
---|
330 | Generate a Guinier distribution and verify that the extrapolation |
---|
331 | produce the correct ditribution. |
---|
332 | """ |
---|
333 | |
---|
334 | def setUp(self): |
---|
335 | """ |
---|
336 | Generate a Guinier distribution. After extrapolating, we will |
---|
337 | verify that we obtain the scale and rg parameters |
---|
338 | """ |
---|
339 | self.scale = 1.5 |
---|
340 | self.rg = 30.0 |
---|
341 | x = numpy.arange(0.0001, 0.1, 0.0001) |
---|
342 | y = numpy.asarray([self.scale * math.exp( -(q*self.rg)**2 / 3.0 ) for q in x]) |
---|
343 | dy = y*.1 |
---|
344 | self.data = Data1D(x=x, y=y, dy=dy) |
---|
345 | |
---|
346 | def test_low_q(self): |
---|
347 | """ |
---|
348 | Invariant with low-Q extrapolation |
---|
349 | """ |
---|
350 | # Create invariant object. Background and scale left as defaults. |
---|
351 | inv = invariant.InvariantCalculator(data=self.data) |
---|
352 | # Set the extrapolation parameters for the low-Q range |
---|
353 | inv.set_extrapolation(range='low', npts=20, function='guinier') |
---|
354 | |
---|
355 | self.assertEqual(inv._low_extrapolation_npts, 20) |
---|
356 | self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) |
---|
357 | |
---|
358 | # Data boundaries for fiiting |
---|
359 | qmin = inv._data.x[0] |
---|
360 | qmax = inv._data.x[inv._low_extrapolation_npts - 1] |
---|
361 | |
---|
362 | # Extrapolate the low-Q data |
---|
363 | inv._fit(model=inv._low_extrapolation_function, |
---|
364 | qmin=qmin, |
---|
365 | qmax=qmax, |
---|
366 | power=inv._low_extrapolation_power) |
---|
367 | self.assertAlmostEqual(self.scale, inv._low_extrapolation_function.scale, 6) |
---|
368 | self.assertAlmostEqual(self.rg, inv._low_extrapolation_function.radius, 6) |
---|
369 | |
---|
370 | |
---|
371 | class TestPowerLawExtrapolation(unittest.TestCase): |
---|
372 | """ |
---|
373 | Generate a power law distribution and verify that the extrapolation |
---|
374 | produce the correct ditribution. |
---|
375 | """ |
---|
376 | |
---|
377 | def setUp(self): |
---|
378 | """ |
---|
379 | Generate a power law distribution. After extrapolating, we will |
---|
380 | verify that we obtain the scale and m parameters |
---|
381 | """ |
---|
382 | self.scale = 1.5 |
---|
383 | self.m = 3.0 |
---|
384 | x = numpy.arange(0.0001, 0.1, 0.0001) |
---|
385 | y = numpy.asarray([self.scale * math.pow(q ,-1.0*self.m) for q in x]) |
---|
386 | dy = y*.1 |
---|
387 | self.data = Data1D(x=x, y=y, dy=dy) |
---|
388 | |
---|
389 | def test_low_q(self): |
---|
390 | """ |
---|
391 | Invariant with low-Q extrapolation |
---|
392 | """ |
---|
393 | # Create invariant object. Background and scale left as defaults. |
---|
394 | inv = invariant.InvariantCalculator(data=self.data) |
---|
395 | # Set the extrapolation parameters for the low-Q range |
---|
396 | inv.set_extrapolation(range='low', npts=20, function='power_law') |
---|
397 | |
---|
398 | self.assertEqual(inv._low_extrapolation_npts, 20) |
---|
399 | self.assertEqual(inv._low_extrapolation_function.__class__, invariant.PowerLaw) |
---|
400 | |
---|
401 | # Data boundaries for fitting |
---|
402 | qmin = inv._data.x[0] |
---|
403 | qmax = inv._data.x[inv._low_extrapolation_npts - 1] |
---|
404 | |
---|
405 | # Extrapolate the low-Q data |
---|
406 | inv._fit(model=inv._low_extrapolation_function, |
---|
407 | qmin=qmin, |
---|
408 | qmax=qmax, |
---|
409 | power=inv._low_extrapolation_power) |
---|
410 | |
---|
411 | self.assertAlmostEqual(self.scale, inv._low_extrapolation_function.scale, 6) |
---|
412 | self.assertAlmostEqual(self.m, inv._low_extrapolation_function.power, 6) |
---|
413 | |
---|
414 | class TestLinearization(unittest.TestCase): |
---|
415 | |
---|
416 | def test_guinier_incompatible_length(self): |
---|
417 | g = invariant.Guinier() |
---|
418 | data_in = Data1D(x=[1], y=[1,2], dy=None) |
---|
419 | self.assertRaises(AssertionError, g.linearize_data, data_in) |
---|
420 | data_in = Data1D(x=[1,1], y=[1,2], dy=[1]) |
---|
421 | self.assertRaises(AssertionError, g.linearize_data, data_in) |
---|
422 | |
---|
423 | def test_linearization(self): |
---|
424 | """ |
---|
425 | Check that the linearization process filters out points |
---|
426 | that can't be transformed |
---|
427 | """ |
---|
428 | x = numpy.asarray(numpy.asarray([0,1,2,3])) |
---|
429 | y = numpy.asarray(numpy.asarray([1,1,1,1])) |
---|
430 | g = invariant.Guinier() |
---|
431 | data_in = Data1D(x=x, y=y) |
---|
432 | data_out = g.linearize_data(data_in) |
---|
433 | x_out, y_out, dy_out = data_out.x, data_out.y, data_out.dy |
---|
434 | self.assertEqual(len(x_out), 3) |
---|
435 | self.assertEqual(len(y_out), 3) |
---|
436 | self.assertEqual(len(dy_out), 3) |
---|
437 | |
---|
438 | def test_allowed_bins(self): |
---|
439 | x = numpy.asarray(numpy.asarray([0,1,2,3])) |
---|
440 | y = numpy.asarray(numpy.asarray([1,1,1,1])) |
---|
441 | dy = numpy.asarray(numpy.asarray([1,1,1,1])) |
---|
442 | g = invariant.Guinier() |
---|
443 | data = Data1D(x=x, y=y, dy=dy) |
---|
444 | self.assertEqual(g.get_allowed_bins(data), [False, True, True, True]) |
---|
445 | |
---|
446 | data = Data1D(x=y, y=x, dy=dy) |
---|
447 | self.assertEqual(g.get_allowed_bins(data), [False, True, True, True]) |
---|
448 | |
---|
449 | data = Data1D(x=dy, y=y, dy=x) |
---|
450 | self.assertEqual(g.get_allowed_bins(data), [False, True, True, True]) |
---|
451 | |
---|
452 | class TestDataExtraLow(unittest.TestCase): |
---|
453 | """ |
---|
454 | Generate a Guinier distribution and verify that the extrapolation |
---|
455 | produce the correct ditribution. Tested if the data generated by the |
---|
456 | invariant calculator is correct |
---|
457 | """ |
---|
458 | |
---|
459 | def setUp(self): |
---|
460 | """ |
---|
461 | Generate a Guinier distribution. After extrapolating, we will |
---|
462 | verify that we obtain the scale and rg parameters |
---|
463 | """ |
---|
464 | self.scale = 1.5 |
---|
465 | self.rg = 30.0 |
---|
466 | x = numpy.arange(0.0001, 0.1, 0.0001) |
---|
467 | y = numpy.asarray([self.scale * math.exp( -(q*self.rg)**2 / 3.0 ) for q in x]) |
---|
468 | dy = y*.1 |
---|
469 | self.data = Data1D(x=x, y=y, dy=dy) |
---|
470 | |
---|
471 | def test_low_q(self): |
---|
472 | """ |
---|
473 | Invariant with low-Q extrapolation with no slit smear |
---|
474 | """ |
---|
475 | # Create invariant object. Background and scale left as defaults. |
---|
476 | inv = invariant.InvariantCalculator(data=self.data) |
---|
477 | # Set the extrapolation parameters for the low-Q range |
---|
478 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
479 | |
---|
480 | self.assertEqual(inv._low_extrapolation_npts, 10) |
---|
481 | self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) |
---|
482 | |
---|
483 | # Data boundaries for fiiting |
---|
484 | qmin = inv._data.x[0] |
---|
485 | qmax = inv._data.x[inv._low_extrapolation_npts - 1] |
---|
486 | |
---|
487 | # Extrapolate the low-Q data |
---|
488 | inv._fit(model=inv._low_extrapolation_function, |
---|
489 | qmin=qmin, |
---|
490 | qmax=qmax, |
---|
491 | power=inv._low_extrapolation_power) |
---|
492 | self.assertAlmostEqual(self.scale, inv._low_extrapolation_function.scale, 6) |
---|
493 | self.assertAlmostEqual(self.rg, inv._low_extrapolation_function.radius, 6) |
---|
494 | |
---|
495 | qstar = inv.get_qstar(extrapolation='low') |
---|
496 | test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x) |
---|
497 | for i in range(len(self.data.x)): |
---|
498 | value = math.fabs(test_y[i]-self.data.y[i])/self.data.y[i] |
---|
499 | self.assert_(value < 0.001) |
---|
500 | |
---|
501 | class TestDataExtraLowSlitGuinier(unittest.TestCase): |
---|
502 | """ |
---|
503 | for a smear data, test that the fitting go through |
---|
504 | real data for atleast the 2 first points |
---|
505 | """ |
---|
506 | |
---|
507 | def setUp(self): |
---|
508 | """ |
---|
509 | Generate a Guinier distribution. After extrapolating, we will |
---|
510 | verify that we obtain the scale and rg parameters |
---|
511 | """ |
---|
512 | self.scale = 1.5 |
---|
513 | self.rg = 30.0 |
---|
514 | x = numpy.arange(0.0001, 0.1, 0.0001) |
---|
515 | y = numpy.asarray([self.scale * math.exp( -(q*self.rg)**2 / 3.0 ) for q in x]) |
---|
516 | dy = y*.1 |
---|
517 | self.data = Data1D(x=x, y=y, dy=dy) |
---|
518 | self.npts = len(x)-10 |
---|
519 | |
---|
520 | def test_low_q(self): |
---|
521 | """ |
---|
522 | Invariant with low-Q extrapolation with slit smear |
---|
523 | """ |
---|
524 | # Create invariant object. Background and scale left as defaults. |
---|
525 | inv = invariant.InvariantCalculator(data=self.data) |
---|
526 | # Set the extrapolation parameters for the low-Q range |
---|
527 | inv.set_extrapolation(range='low', npts=self.npts, function='guinier') |
---|
528 | |
---|
529 | self.assertEqual(inv._low_extrapolation_npts, self.npts) |
---|
530 | self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) |
---|
531 | |
---|
532 | # Data boundaries for fiiting |
---|
533 | qmin = inv._data.x[0] |
---|
534 | qmax = inv._data.x[inv._low_extrapolation_npts - 1] |
---|
535 | |
---|
536 | # Extrapolate the low-Q data |
---|
537 | inv._fit(model=inv._low_extrapolation_function, |
---|
538 | qmin=qmin, |
---|
539 | qmax=qmax, |
---|
540 | power=inv._low_extrapolation_power) |
---|
541 | |
---|
542 | |
---|
543 | qstar = inv.get_qstar(extrapolation='low') |
---|
544 | |
---|
545 | test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x[:inv._low_extrapolation_npts]) |
---|
546 | self.assert_(len(test_y) == len(self.data.y[:inv._low_extrapolation_npts])) |
---|
547 | |
---|
548 | for i in range(inv._low_extrapolation_npts): |
---|
549 | value = math.fabs(test_y[i]-self.data.y[i])/self.data.y[i] |
---|
550 | self.assert_(value < 0.001) |
---|
551 | |
---|
552 | def test_low_data(self): |
---|
553 | """ |
---|
554 | Invariant with low-Q extrapolation with slit smear |
---|
555 | """ |
---|
556 | # Create invariant object. Background and scale left as defaults. |
---|
557 | inv = invariant.InvariantCalculator(data=self.data) |
---|
558 | # Set the extrapolation parameters for the low-Q range |
---|
559 | inv.set_extrapolation(range='low', npts=self.npts, function='guinier') |
---|
560 | |
---|
561 | self.assertEqual(inv._low_extrapolation_npts, self.npts) |
---|
562 | self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) |
---|
563 | |
---|
564 | # Data boundaries for fiiting |
---|
565 | qmin = inv._data.x[0] |
---|
566 | qmax = inv._data.x[inv._low_extrapolation_npts - 1] |
---|
567 | |
---|
568 | # Extrapolate the low-Q data |
---|
569 | inv._fit(model=inv._low_extrapolation_function, |
---|
570 | qmin=qmin, |
---|
571 | qmax=qmax, |
---|
572 | power=inv._low_extrapolation_power) |
---|
573 | |
---|
574 | |
---|
575 | qstar = inv.get_qstar(extrapolation='low') |
---|
576 | #Compution the y 's coming out of the invariant when computing extrapolated |
---|
577 | #low data . expect the fit engine to have been already called and the guinier |
---|
578 | # to have the radius and the scale fitted |
---|
579 | data_in_range = inv.get_extra_data_low(q_start=self.data.x[0], |
---|
580 | npts = inv._low_extrapolation_npts) |
---|
581 | test_y = data_in_range.y |
---|
582 | self.assert_(len(test_y) == len(self.data.y[:inv._low_extrapolation_npts])) |
---|
583 | for i in range(inv._low_extrapolation_npts): |
---|
584 | value = math.fabs(test_y[i]-self.data.y[i])/self.data.y[i] |
---|
585 | self.assert_(value < 0.001) |
---|
586 | |
---|
587 | |
---|
588 | class TestDataExtraHighSlitPowerLaw(unittest.TestCase): |
---|
589 | """ |
---|
590 | for a smear data, test that the fitting go through |
---|
591 | real data for atleast the 2 first points |
---|
592 | """ |
---|
593 | |
---|
594 | def setUp(self): |
---|
595 | """ |
---|
596 | Generate a Guinier distribution. After extrapolating, we will |
---|
597 | verify that we obtain the scale and rg parameters |
---|
598 | """ |
---|
599 | self.scale = 1.5 |
---|
600 | self.m = 3.0 |
---|
601 | x = numpy.arange(0.0001, 0.1, 0.0001) |
---|
602 | y = numpy.asarray([self.scale * math.pow(q ,-1.0*self.m) for q in x]) |
---|
603 | dy = y*.1 |
---|
604 | self.data = Data1D(x=x, y=y, dy=dy) |
---|
605 | self.npts = 20 |
---|
606 | |
---|
607 | def test_high_q(self): |
---|
608 | """ |
---|
609 | Invariant with high-Q extrapolation with slit smear |
---|
610 | """ |
---|
611 | # Create invariant object. Background and scale left as defaults. |
---|
612 | inv = invariant.InvariantCalculator(data=self.data) |
---|
613 | # Set the extrapolation parameters for the low-Q range |
---|
614 | inv.set_extrapolation(range='high', npts=self.npts, function='power_law') |
---|
615 | |
---|
616 | self.assertEqual(inv._high_extrapolation_npts, self.npts) |
---|
617 | self.assertEqual(inv._high_extrapolation_function.__class__, invariant.PowerLaw) |
---|
618 | |
---|
619 | # Data boundaries for fiiting |
---|
620 | xlen = len(self.data.x) |
---|
621 | start = xlen - inv._high_extrapolation_npts |
---|
622 | qmin = inv._data.x[start] |
---|
623 | qmax = inv._data.x[xlen-1] |
---|
624 | |
---|
625 | # Extrapolate the high-Q data |
---|
626 | inv._fit(model=inv._high_extrapolation_function, |
---|
627 | qmin=qmin, |
---|
628 | qmax=qmax, |
---|
629 | power=inv._high_extrapolation_power) |
---|
630 | |
---|
631 | |
---|
632 | qstar = inv.get_qstar(extrapolation='high') |
---|
633 | |
---|
634 | test_y = inv._high_extrapolation_function.evaluate_model(x=self.data.x[start: ]) |
---|
635 | self.assert_(len(test_y) == len(self.data.y[start:])) |
---|
636 | |
---|
637 | for i in range(len(self.data.x[start:])): |
---|
638 | value = math.fabs(test_y[i]-self.data.y[start+i])/self.data.y[start+i] |
---|
639 | self.assert_(value < 0.001) |
---|
640 | |
---|
641 | def test_high_data(self): |
---|
642 | """ |
---|
643 | Invariant with low-Q extrapolation with slit smear |
---|
644 | """ |
---|
645 | # Create invariant object. Background and scale left as defaults. |
---|
646 | inv = invariant.InvariantCalculator(data=self.data) |
---|
647 | # Set the extrapolation parameters for the low-Q range |
---|
648 | inv.set_extrapolation(range='high', npts=self.npts, function='power_law') |
---|
649 | |
---|
650 | self.assertEqual(inv._high_extrapolation_npts, self.npts) |
---|
651 | self.assertEqual(inv._high_extrapolation_function.__class__, invariant.PowerLaw) |
---|
652 | |
---|
653 | # Data boundaries for fiiting |
---|
654 | xlen = len(self.data.x) |
---|
655 | start = xlen - inv._high_extrapolation_npts |
---|
656 | qmin = inv._data.x[start] |
---|
657 | qmax = inv._data.x[xlen-1] |
---|
658 | |
---|
659 | # Extrapolate the high-Q data |
---|
660 | inv._fit(model=inv._high_extrapolation_function, |
---|
661 | qmin=qmin, |
---|
662 | qmax=qmax, |
---|
663 | power=inv._high_extrapolation_power) |
---|
664 | |
---|
665 | qstar = inv.get_qstar(extrapolation='high') |
---|
666 | |
---|
667 | data_in_range= inv.get_extra_data_high(q_end = max(self.data.x), |
---|
668 | npts = inv._high_extrapolation_npts) |
---|
669 | test_y = data_in_range.y |
---|
670 | self.assert_(len(test_y) == len(self.data.y[start:])) |
---|
671 | temp = self.data.y[start:] |
---|
672 | |
---|
673 | for i in range(len(self.data.x[start:])): |
---|
674 | value = math.fabs(test_y[i]- temp[i])/temp[i] |
---|
675 | self.assert_(value < 0.001) |
---|
676 | |
---|