source: sasview/Invariant/test/utest_use_cases.py @ 2389bd4

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 2389bd4 was 6e74ee4, checked in by Jae Cho <jhjcho@…>, 15 years ago

added fitfunctor.fit tests without errorbar data

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