source: sasview/Invariant/test/utest_use_cases.py @ 889d5ab4

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 889d5ab4 was aafa962, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

invariant: minor code refactor

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