source: sasview/test/sasinvariant/test/utest_use_cases.py @ b09095a

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since b09095a was b09095a, checked in by krzywon, 7 years ago

Refactor ASCII reader to use FileReader? class.

  • Property mode set to 100644
File size: 14.0 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
7from sas.sascalc.dataloader.loader import  Loader
8from sas.sascalc.invariant import invariant
9
10
11class Data1D:
12    pass
13
14
15class TestLineFit(unittest.TestCase):
16    """
17        Test Line fit
18    """
19    def setUp(self):
20        self.data_list = Loader().load("linefittest.txt")
21        self.data = self.data_list[0]
22
23    def test_fit_line_data(self):
24        """
25            Fit_Test_1: test linear fit, ax +b, without fixed
26        """
27
28        # Create invariant object. Background and scale left as defaults.
29        fit = invariant.Extrapolator(data=self.data)
30       
31        # Without holding
32        p, dp = fit.fit(power=None)
33
34        # Test results
35        self.assertAlmostEquals(p[0], 2.3983,3)
36        self.assertAlmostEquals(p[1], 0.87833,3)
37
38    def test_fit_line_data_fixed(self):
39        """
40            Fit_Test_2: test linear fit, ax +b, with 'a' fixed
41        """
42
43        # Create invariant object. Background and scale left as defaults.
44        fit = invariant.Extrapolator(data=self.data)
45
46        # With holding a = -power =4
47        p, dp = fit.fit(power=-4)
48
49        # Test results
50        self.assertAlmostEquals(p[0], 4)
51        self.assertAlmostEquals(p[1], -4.0676,3)
52
53
54class TestLineFitNoweight(unittest.TestCase):
55    """
56        Test Line fit without weight(dy data)
57    """
58    def setUp(self):
59        self.data_list = Loader().load("linefittest_no_weight.txt")
60        self.data = self.data_list[0]
61
62    def skip_test_fit_line_data_no_weight(self):
63        """
64            Fit_Test_1: test linear fit, ax +b, without fixed
65        """
66
67        # Create invariant object. Background and scale left as defaults.
68        fit = invariant.Extrapolator(data=self.data)
69
70        # Without holding
71        p, dp = fit.fit(power=None)
72
73        # Test results
74        self.assertAlmostEquals(p[0], 2.4727,3)
75        self.assertAlmostEquals(p[1], 0.6,3)
76
77    def test_fit_line_data_fixed_no_weight(self):
78        """
79            Fit_Test_2: test linear fit, ax +b, with 'a' fixed
80        """
81
82        # Create invariant object. Background and scale left as defaults.
83        fit = invariant.Extrapolator(data=self.data)
84
85        #With holding a = -power =4
86        p, dp = fit.fit(power=-4)
87
88        # Test results
89        self.assertAlmostEquals(p[0], 4)
90        self.assertAlmostEquals(p[1], -7.8,3)
91
92
93class TestInvPolySphere(unittest.TestCase):
94    """
95        Test unsmeared data for invariant computation
96    """
97    def setUp(self):
98        self.data_list = Loader().load("PolySpheres.txt")
99        self.data = self.data_list[0]
100
101    def test_wrong_data(self):
102        """ test receiving Data1D not of type loader"""
103        self.assertRaises(ValueError,invariant.InvariantCalculator, Data1D())
104
105    def test_use_case_1(self):
106        """
107            Invariant without extrapolation
108        """
109        # Create invariant object. Background and scale left as defaults.
110        inv = invariant.InvariantCalculator(data=self.data)
111
112        # We have to be able to tell the InvariantCalculator whether we want the
113        # extrapolation or not. By default, when the user doesn't specify, we
114        # should compute Q* without extrapolation. That's what should be done
115        # in __init__.
116
117        # We call get_qstar() with no argument, which signifies that we do NOT
118        # want extrapolation.
119        qstar = inv.get_qstar()
120
121        # The volume fraction and surface use Q*. That means that the following
122        # methods should check that Q* has been computed. If not, it should
123        # compute it by calling get_qstare(), leaving the parameters as default.
124        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
125        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
126
127        # Test results
128        self.assertAlmostEquals(qstar, 7.48959e-5,2)
129        self.assertAlmostEquals(v, 0.005644689, 4)
130        self.assertAlmostEquals(s , 941.7452, 3)
131
132    def test_use_case_2(self):
133        """
134        Invariant without extrapolation. Invariant, volume fraction and surface
135        are given with errors.
136        """
137        # Create invariant object. Background and scale left as defaults.
138        inv = invariant.InvariantCalculator(data=self.data)
139
140        # Get the invariant with errors
141        qstar, qstar_err = inv.get_qstar_with_error()
142
143        # The volume fraction and surface use Q*. That means that the following
144        # methods should check that Q* has been computed. If not, it should
145        # compute it by calling get_qstare(), leaving the parameters as default.
146        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
147        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
148        # Test results
149        self.assertAlmostEquals(qstar, 7.48959e-5,2)
150        self.assertAlmostEquals(v, 0.005644689, 1)
151        self.assertAlmostEquals(s , 941.7452, 3)
152
153    def test_use_case_3(self):
154        """
155            Invariant with low-Q extrapolation
156        """
157        # Create invariant object. Background and scale left as defaults.
158        inv = invariant.InvariantCalculator(data=self.data)
159
160        # Set the extrapolation parameters for the low-Q range
161
162        # The npts parameter should have a good default.
163        # The range parameter should be 'high' or 'low'
164        # The function parameter should default to None. If it is None,
165        #    the method should pick a good default
166        #    (Guinier at low-Q and 1/q^4 at high-Q).
167        #    The method should also check for consistency of the extrapolation
168        #    and function parameters. For instance, you might not want to allow
169        #    '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
175        # calling 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 , 941.7452, 3)
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',
199                              power=4)
200       
201        # The version of the call without error
202        # The function parameter defaults to None, then is picked to be
203        # 'power_law' for extrapolation='high'
204        qstar = inv.get_qstar(extrapolation='high')
205
206        # The version of the call with error
207        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high')
208
209        # Get the volume fraction and surface
210        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
211        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
212
213        # Test results
214        self.assertAlmostEquals(qstar, 7.49e-5,2)
215        self.assertAlmostEquals(v, 0.005952674, 3)
216        self.assertAlmostEquals(s , 941.7452, 3)
217
218    def test_use_case_5(self):
219        """
220            Invariant with both high- and low-Q extrapolation
221        """
222        # Create invariant object. Background and scale left as defaults.
223        inv = invariant.InvariantCalculator(data=self.data)
224
225        # Set the extrapolation parameters for the low- and high-Q ranges
226        inv.set_extrapolation(range='low', npts=10, function='guinier')
227        inv.set_extrapolation(range='high', npts=10, function='power_law',
228                              power=4)
229
230        # The version of the call without error
231        # The function parameter defaults to None, then is picked to be
232        # 'power_law' for extrapolation='high'
233        qstar = inv.get_qstar(extrapolation='both')
234       
235        # The version of the call with error
236        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both')
237
238        # Get the volume fraction and surface
239        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
240        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
241
242        # Test results
243        self.assertAlmostEquals(qstar, 7.88981e-5,2)
244        self.assertAlmostEquals(v, 0.005952674, 3)
245        self.assertAlmostEquals(s , 941.7452, 3)
246
247    def test_use_case_6(self):
248        """
249            Invariant with high-Q extrapolation
250        """
251        # Create invariant object. Background and scale left as defaults.
252        inv = invariant.InvariantCalculator(data=self.data)
253
254        # Set the extrapolation parameters for the high-Q range
255        inv.set_extrapolation(range='low', npts=10, function='power_law', power=4)
256
257        # The version of the call without error
258        # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high'
259        qstar = inv.get_qstar(extrapolation='low')
260
261        # The version of the call with error
262        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low')
263
264        # Get the volume fraction and surface
265        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
266        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
267
268        # Test results
269        self.assertAlmostEquals(qstar, 7.49e-5,2)
270        self.assertAlmostEquals(v, 0.005952674, 3)
271        self.assertAlmostEquals(s , 941.7452, 3)
272
273
274class TestInvPinholeSmear(unittest.TestCase):
275    """
276        Test pinhole smeared data for invariant computation
277    """
278    def setUp(self):
279        # data with smear info
280        list = Loader().load("latex_smeared.xml")
281        self.data_q_smear = list[0]
282
283    def test_use_case_1(self):
284        """
285            Invariant without extrapolation
286        """
287        inv = invariant.InvariantCalculator(data=self.data_q_smear)
288        qstar = inv.get_qstar()
289
290        v = inv.get_volume_fraction(contrast=2.6e-6)
291        s = inv.get_surface(contrast=2.6e-6, porod_const=2)
292        # Test results
293        self.assertAlmostEquals(qstar, 1.361677e-3, 4)
294        self.assertAlmostEquals(v, 0.115352622, 2)
295        self.assertAlmostEquals(s , 941.7452, 3 )
296
297    def test_use_case_2(self):
298        """
299            Invariant without extrapolation. Invariant, volume fraction and surface
300            are given with errors.
301        """
302        # Create invariant object. Background and scale left as defaults.
303        inv = invariant.InvariantCalculator(data=self.data_q_smear)
304
305        # Get the invariant with errors
306        qstar, qstar_err = inv.get_qstar_with_error()
307        # Get the volume fraction and surface
308        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
309        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
310        # Test results
311        self.assertAlmostEquals(qstar, 1.361677e-3, 4)
312        self.assertAlmostEquals(v, 0.115352622, 2)
313        self.assertAlmostEquals(s , 941.7452, 3 )
314
315    def test_use_case_3(self):
316        """
317            Invariant with low-Q extrapolation
318        """
319        # Create invariant object. Background and scale left as defaults.
320        inv = invariant.InvariantCalculator(data=self.data_q_smear)
321        # Set the extrapolation parameters for the low-Q range
322        inv.set_extrapolation(range='low', npts=20, function='guinier')
323        # The version of the call without error
324        qstar = inv.get_qstar(extrapolation='low')
325        # The version of the call with error
326        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low')
327        # Get the volume fraction and surface
328        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6)
329        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2)
330
331        # Test results
332        self.assertAlmostEquals(qstar, 0.00138756,2)
333        self.assertAlmostEquals(v, 0.117226896,2)
334        self.assertAlmostEquals(s ,941.7452, 3)
335
336    def test_use_case_4(self):
337        """
338            Invariant with high-Q extrapolation
339        """
340        # Create invariant object. Background and scale left as defaults.
341        inv = invariant.InvariantCalculator(data=self.data_q_smear)
342        # Set the extrapolation parameters for the high-Q range
343        inv.set_extrapolation(range='high', npts=10, function='power_law', power=4)
344        # The version of the call without error
345        qstar = inv.get_qstar(extrapolation='high')
346        # The version of the call with error
347        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high')
348
349        # Test results
350        self.assertAlmostEquals(qstar, 0.0045773,2)
351
352    def test_use_case_5(self):
353        """
354            Invariant with both high- and low-Q extrapolation
355        """
356        # Create invariant object. Background and scale left as defaults.
357        inv = invariant.InvariantCalculator(data=self.data_q_smear)
358        # Set the extrapolation parameters for the low- and high-Q ranges
359        inv.set_extrapolation(range='low', npts=10, function='guinier')
360        inv.set_extrapolation(range='high', npts=10, function='power_law',
361                              power=4)
362        # The version of the call without error
363        # The function parameter defaults to None, then is picked to be
364        # 'power_law' for extrapolation='high'
365        qstar = inv.get_qstar(extrapolation='both')
366        # The version of the call with error
367        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both')
368
369        # Test results
370        self.assertAlmostEquals(qstar, 0.00460319,3)
371     
372 
373if __name__ == '__main__':
374    unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
375   
Note: See TracBrowser for help on using the repository browser.