source: sasview/Invariant/test/utest_data_handling.py @ 3244cbe1

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 3244cbe1 was bdd162f, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

Removed smearing, added errors to all outputs, streamlined the sequence of behind-the-scenes computation calls.

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