source: sasview/sansinvariant/test/utest_data_handling.py @ 31ac4a1

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 31ac4a1 was 97700d7, checked in by Jae Cho <jhjcho@…>, 12 years ago

Egual to AlmostEqual? in 8 below decimal

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