source: sasview/sansinvariant/test/utest_use_cases.py @ 2636188

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 2636188 was ad4053a, checked in by Jessica Tumarkin <jtumarki@…>, 13 years ago
  • Property mode set to 100644
File size: 14.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
[bdd162f]30        p, dp = fit.fit(power=None)
[c8c4fa1]31
32        # Test results
[bdd162f]33        self.assertAlmostEquals(p[0], 2.3983,3)
34        self.assertAlmostEquals(p[1], 0.87833,3)
[c8c4fa1]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
[bdd162f]46        p, dp = fit.fit(power=-4)
[c8c4fa1]47
48        # Test results
[bdd162f]49        self.assertAlmostEquals(p[0], 4)
50        self.assertAlmostEquals(p[1], -4.0676,3)
[c8c4fa1]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
[bdd162f]68        p, dp = fit.fit(power=None)
[6e74ee4]69
70        # Test results
[bdd162f]71        self.assertAlmostEquals(p[0], 2.4727,3)
72        self.assertAlmostEquals(p[1], 0.6,3)
[6e74ee4]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
[bdd162f]84        p, dp = fit.fit(power=-4)
[6e74ee4]85
86        # Test results
[bdd162f]87        self.assertAlmostEquals(p[0], 4)
88        self.assertAlmostEquals(p[1], -7.8,3)
[6e74ee4]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     
[90e5ca1]239    def test_use_case_6(self):
240        """
241            Invariant with high-Q extrapolation
242        """
243        # Create invariant object. Background and scale left as defaults.
244        inv = invariant.InvariantCalculator(data=self.data)
245       
246        # Set the extrapolation parameters for the high-Q range
247        inv.set_extrapolation(range='low', npts=10, function='power_law', power=4)
248       
249        # The version of the call without error
250        # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high'
251        qstar = inv.get_qstar(extrapolation='low')
252       
253        # The version of the call with error
254        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low')
255
256        # Get the volume fraction and surface
257        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
258        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
259       
260        # Test results
261        self.assertAlmostEquals(qstar, 7.49e-5,2)
262        self.assertAlmostEquals(v, 0.005952674, 3)
263        self.assertAlmostEquals(s , 941.7452, 3)
264       
[ef9ed58]265class TestInvPinholeSmear(unittest.TestCase):
266    """
267        Test pinhole smeared data for invariant computation
268    """
269    def setUp(self):
270        # data with smear info
271        list = Loader().load("latex_smeared.xml")
272        self.data_q_smear = list[0]
273   
274    def test_use_case_1(self):
275        """
276            Invariant without extrapolation
277        """
278        inv = invariant.InvariantCalculator(data=self.data_q_smear)
279        qstar = inv.get_qstar()
280       
[4e80ae0]281        v = inv.get_volume_fraction(contrast=2.6e-6)
282        s = inv.get_surface(contrast=2.6e-6, porod_const=2)
[ef9ed58]283        # Test results
[4e80ae0]284        self.assertAlmostEquals(qstar, 1.361677e-3, 4)
285        self.assertAlmostEquals(v, 0.115352622, 2)
[277aab4e]286        self.assertAlmostEquals(s , 941.7452, 3 )
[ef9ed58]287       
288    def test_use_case_2(self):
289        """
290            Invariant without extrapolation. Invariant, volume fraction and surface
291            are given with errors.
292        """
293        # Create invariant object. Background and scale left as defaults.
294        inv = invariant.InvariantCalculator(data=self.data_q_smear)
295       
296        # Get the invariant with errors
297        qstar, qstar_err = inv.get_qstar_with_error()
298        # Get the volume fraction and surface
299        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
300        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
301        # Test results
[4e80ae0]302        self.assertAlmostEquals(qstar, 1.361677e-3, 4)
303        self.assertAlmostEquals(v, 0.115352622, 2)
[277aab4e]304        self.assertAlmostEquals(s , 941.7452, 3 )
[ef9ed58]305       
306    def test_use_case_3(self):
307        """
308            Invariant with low-Q extrapolation
309        """
310        # Create invariant object. Background and scale left as defaults.
311        inv = invariant.InvariantCalculator(data=self.data_q_smear)
312        # Set the extrapolation parameters for the low-Q range
313        inv.set_extrapolation(range='low', npts=20, function='guinier')
314        # The version of the call without error
315        qstar = inv.get_qstar(extrapolation='low')
316        # The version of the call with error
317        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low')
318        # Get the volume fraction and surface
319        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
[0957dbdb]320        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
[ef9ed58]321       
322        # Test results
[9b6497bb]323        self.assertAlmostEquals(qstar, 0.00138756,2)
[0957dbdb]324        self.assertAlmostEquals(v, 0.117226896,2)
[277aab4e]325        self.assertAlmostEquals(s ,941.7452, 3)
[ef9ed58]326     
327    def test_use_case_4(self):
328        """
329            Invariant with high-Q extrapolation
330        """
331        # Create invariant object. Background and scale left as defaults.
332        inv = invariant.InvariantCalculator(data=self.data_q_smear)
333        # Set the extrapolation parameters for the high-Q range
[437a9f0]334        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
[ef9ed58]335        # The version of the call without error
336        qstar = inv.get_qstar(extrapolation='high')
337        # The version of the call with error
338        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high')
[6311713]339       
[ef9ed58]340        # Get the volume fraction and surface
[bdd162f]341        # WHY SHOULD THIS FAIL?
342        #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6)
[ef9ed58]343       
[b345c45]344        # Check that an exception is raised when the 'surface' is not defined
[bdd162f]345        # WHY SHOULD THIS FAIL?
346        #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2)
[b345c45]347
[ef9ed58]348        # Test results
[6311713]349        self.assertAlmostEquals(qstar, 0.0045773,2)
[ef9ed58]350       
351    def test_use_case_5(self):
352        """
353            Invariant with both high- and low-Q extrapolation
354        """
355        # Create invariant object. Background and scale left as defaults.
356        inv = invariant.InvariantCalculator(data=self.data_q_smear)
357        # Set the extrapolation parameters for the low- and high-Q ranges
[437a9f0]358        inv.set_extrapolation(range='low', npts=10, function='guinier')
359        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
[ef9ed58]360        # The version of the call without error
361        # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high'
362        qstar = inv.get_qstar(extrapolation='both')
363        # The version of the call with error
364        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both')
[6311713]365       
[ef9ed58]366        # Get the volume fraction and surface
[bdd162f]367        # WHY SHOULD THIS FAIL?
368        #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6)
369        #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2)
[b6666d4]370       
[ef9ed58]371        # Test results
[6311713]372        self.assertAlmostEquals(qstar, 0.00460319,3)
[68a2c31]373     
Note: See TracBrowser for help on using the repository browser.