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

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 ace0e33 was ad4053a, checked in by Jessica Tumarkin <jtumarki@…>, 13 years ago
  • Property mode set to 100644
File size: 14.6 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    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       
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       
281        v = inv.get_volume_fraction(contrast=2.6e-6)
282        s = inv.get_surface(contrast=2.6e-6, porod_const=2)
283        # Test results
284        self.assertAlmostEquals(qstar, 1.361677e-3, 4)
285        self.assertAlmostEquals(v, 0.115352622, 2)
286        self.assertAlmostEquals(s , 941.7452, 3 )
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
302        self.assertAlmostEquals(qstar, 1.361677e-3, 4)
303        self.assertAlmostEquals(v, 0.115352622, 2)
304        self.assertAlmostEquals(s , 941.7452, 3 )
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)
320        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
321       
322        # Test results
323        self.assertAlmostEquals(qstar, 0.00138756,2)
324        self.assertAlmostEquals(v, 0.117226896,2)
325        self.assertAlmostEquals(s ,941.7452, 3)
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
334        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
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')
339       
340        # Get the volume fraction and surface
341        # WHY SHOULD THIS FAIL?
342        #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6)
343       
344        # Check that an exception is raised when the 'surface' is not defined
345        # WHY SHOULD THIS FAIL?
346        #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2)
347
348        # Test results
349        self.assertAlmostEquals(qstar, 0.0045773,2)
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
358        inv.set_extrapolation(range='low', npts=10, function='guinier')
359        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
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')
365       
366        # Get the volume fraction and surface
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)
370       
371        # Test results
372        self.assertAlmostEquals(qstar, 0.00460319,3)
373     
Note: See TracBrowser for help on using the repository browser.