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

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

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

  • Property mode set to 100644
File size: 13.5 KB
Line 
1"""
2   Implementation of the use-case from a usage perspective.
3 
4"""
5#TODO: there's no test for smeared extrapolation
6import unittest
7import numpy
8from DataLoader.loader import  Loader
9from sans.invariant import invariant
10
11class Data1D:
12    pass
13   
14class TestLineFit(unittest.TestCase):
15    """
16        Test Line fit
17    """
18    def setUp(self):
19        self.data = Loader().load("linefittest.txt")
20       
21    def test_fit_line_data(self):
22        """
23            Fit_Test_1: test linear fit, ax +b, without fixed
24        """
25       
26        # Create invariant object. Background and scale left as defaults.
27        fit = invariant.Extrapolator(data=self.data)
28       
29        ##Without holding
30        p, dp = fit.fit(power=None)
31
32        # Test results
33        self.assertAlmostEquals(p[0], 2.3983,3)
34        self.assertAlmostEquals(p[1], 0.87833,3)
35
36
37    def test_fit_line_data_fixed(self):
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.
43        fit = invariant.Extrapolator(data=self.data)
44       
45        #With holding a = -power =4
46        p, dp = fit.fit(power=-4)
47
48        # Test results
49        self.assertAlmostEquals(p[0], 4)
50        self.assertAlmostEquals(p[1], -4.0676,3)
51       
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.
65        fit = invariant.Extrapolator(data=self.data)
66       
67        ##Without holding
68        p, dp = fit.fit(power=None)
69
70        # Test results
71        self.assertAlmostEquals(p[0], 2.4727,3)
72        self.assertAlmostEquals(p[1], 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.
81        fit = invariant.Extrapolator(data=self.data)
82       
83        #With holding a = -power =4
84        p, dp = fit.fit(power=-4)
85
86        # Test results
87        self.assertAlmostEquals(p[0], 4)
88        self.assertAlmostEquals(p[1], -7.8,3)
89               
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       
97    def test_wrong_data(self):
98        """ test receiving Data1D not of type loader"""
99
100
101        self.assertRaises(ValueError,invariant.InvariantCalculator, Data1D())
102       
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.
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)
126        self.assertAlmostEquals(v, 0.005644689, 4)
127        self.assertAlmostEquals(s , 941.7452, 3)
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()
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)
147        self.assertAlmostEquals(v, 0.005644689, 1)
148        self.assertAlmostEquals(s , 941.7452, 3)
149       
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).
164        #    The method should also check for consistency of the extrapolation and function
165        #    parameters. For instance, you might not want to allow 'high' and 'guinier'.
166        # The power parameter (not shown below) should default to 4.
167        inv.set_extrapolation(range='low', npts=10, function='guinier')
168       
169        # The version of the call without error
170        # At this point, we could still compute Q* without extrapolation by calling
171        # get_qstar with arguments, or with extrapolation=None.
172        qstar = inv.get_qstar(extrapolation='low')
173       
174        # The version of the call with error
175        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low')
176
177        # Get the volume fraction and surface
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)
180       
181        # Test results
182        self.assertAlmostEquals(qstar, 7.49e-5, 1)
183        self.assertAlmostEquals(v, 0.005648401, 4)
184        self.assertAlmostEquals(s , 941.7452, 3)
185           
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
194        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
195       
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')
199       
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)
209        self.assertAlmostEquals(v, 0.005952674, 3)
210        self.assertAlmostEquals(s , 941.7452, 3)
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
220        inv.set_extrapolation(range='low', npts=10, function='guinier')
221        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
222       
223        # The version of the call without error
224        # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high'
225        qstar = inv.get_qstar(extrapolation='both')
226       
227        # The version of the call with error
228        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both')
229
230        # Get the volume fraction and surface
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
235        self.assertAlmostEquals(qstar, 7.88981e-5,2)
236        self.assertAlmostEquals(v, 0.005952674, 3)
237        self.assertAlmostEquals(s , 941.7452, 3)
238     
239 
240class TestInvPinholeSmear(unittest.TestCase):
241    """
242        Test pinhole smeared data for invariant computation
243    """
244    def setUp(self):
245        # data with smear info
246        list = Loader().load("latex_smeared.xml")
247        self.data_q_smear = list[0]
248   
249    def test_use_case_1(self):
250        """
251            Invariant without extrapolation
252        """
253        inv = invariant.InvariantCalculator(data=self.data_q_smear)
254        qstar = inv.get_qstar()
255       
256        v = inv.get_volume_fraction(contrast=2.6e-6)
257        s = inv.get_surface(contrast=2.6e-6, porod_const=2)
258        # Test results
259        self.assertAlmostEquals(qstar, 1.361677e-3, 4)
260        self.assertAlmostEquals(v, 0.115352622, 2)
261        self.assertAlmostEquals(s , 941.7452, 3 )
262       
263    def test_use_case_2(self):
264        """
265            Invariant without extrapolation. Invariant, volume fraction and surface
266            are given with errors.
267        """
268        # Create invariant object. Background and scale left as defaults.
269        inv = invariant.InvariantCalculator(data=self.data_q_smear)
270       
271        # Get the invariant with errors
272        qstar, qstar_err = inv.get_qstar_with_error()
273        # Get the volume fraction and surface
274        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
275        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
276        # Test results
277        self.assertAlmostEquals(qstar, 1.361677e-3, 4)
278        self.assertAlmostEquals(v, 0.115352622, 2)
279        self.assertAlmostEquals(s , 941.7452, 3 )
280       
281    def test_use_case_3(self):
282        """
283            Invariant with low-Q extrapolation
284        """
285        # Create invariant object. Background and scale left as defaults.
286        inv = invariant.InvariantCalculator(data=self.data_q_smear)
287        # Set the extrapolation parameters for the low-Q range
288        inv.set_extrapolation(range='low', npts=20, function='guinier')
289        # The version of the call without error
290        qstar = inv.get_qstar(extrapolation='low')
291        # The version of the call with error
292        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low')
293        # Get the volume fraction and surface
294        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
295        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
296       
297        # Test results
298        self.assertAlmostEquals(qstar, 0.00138756,2)
299        self.assertAlmostEquals(v, 0.117226896,2)
300        self.assertAlmostEquals(s ,941.7452, 3)
301     
302    def test_use_case_4(self):
303        """
304            Invariant with high-Q extrapolation
305        """
306        # Create invariant object. Background and scale left as defaults.
307        inv = invariant.InvariantCalculator(data=self.data_q_smear)
308        # Set the extrapolation parameters for the high-Q range
309        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
310        # The version of the call without error
311        qstar = inv.get_qstar(extrapolation='high')
312        # The version of the call with error
313        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high')
314       
315        # Get the volume fraction and surface
316        # WHY SHOULD THIS FAIL?
317        #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6)
318       
319        # Check that an exception is raised when the 'surface' is not defined
320        # WHY SHOULD THIS FAIL?
321        #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2)
322
323        # Test results
324        self.assertAlmostEquals(qstar, 0.0045773,2)
325       
326    def test_use_case_5(self):
327        """
328            Invariant with both high- and low-Q extrapolation
329        """
330        # Create invariant object. Background and scale left as defaults.
331        inv = invariant.InvariantCalculator(data=self.data_q_smear)
332        # Set the extrapolation parameters for the low- and high-Q ranges
333        inv.set_extrapolation(range='low', npts=10, function='guinier')
334        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
335        # The version of the call without error
336        # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high'
337        qstar = inv.get_qstar(extrapolation='both')
338        # The version of the call with error
339        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both')
340       
341        # Get the volume fraction and surface
342        # WHY SHOULD THIS FAIL?
343        #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6)
344        #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2)
345       
346        # Test results
347        self.assertAlmostEquals(qstar, 0.00460319,3)
348     
Note: See TracBrowser for help on using the repository browser.