source: sasview/test/park_integration/test/test_fit_smeared.py @ 6c00702

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 6c00702 was 6c00702, checked in by pkienzle, 10 years ago

correct fitting tests so that they run (though not yet automatically)

  • Property mode set to 100644
File size: 6.2 KB
Line 
1"""
2    Unit tests for fitting module
3    @author M. Doucet
4"""
5import unittest
6import math
7import numpy
8from sans.fit.AbstractFitEngine import Model
9from sans.fit.Fitting import Fit
10from sans.dataloader.loader import Loader
11from sans.models.qsmearing import smear_selection
12from sans.models.CylinderModel import CylinderModel
13from sans.models.SphereModel import SphereModel
14
15class testFitModule(unittest.TestCase):
16    """ test fitting """
17   
18    def test_scipy(self):
19        """ Simple cylinder model fit (scipy)  """
20       
21        out=Loader().load("cyl_400_20.txt")
22        # This data file has not error, add them
23        out.dy = out.y
24       
25        fitter = Fit('scipy')
26        fitter.set_data(out,1)
27       
28        # Receives the type of model for the fitting
29        model1  = CylinderModel()
30        model1.setParam('sldCyl', 3.0e-6)
31        model1.setParam('sldSolv', 0.0)
32        model = Model(model1)
33        model.set(scale=1e-10)
34        pars1 =['length','radius','scale']
35        fitter.set_model(model,1,pars1)
36       
37        # What the hell is this line for?
38        fitter.select_problem_for_fit(id=1,value=1)
39        result1, = fitter.fit()
40       
41        self.assert_(result1)
42        self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 )
43        self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0)
44       
45        self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] )
46        self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0  < result1.stderr[1] )
47        self.assertTrue( math.fabs(result1.pvec[2]-9.0e-12)/3.0   < result1.stderr[2] )
48        self.assertTrue( result1.fitness < 1.0 )
49       
50    def test_scipy_dispersion(self):
51        """
52            Cylinder fit with dispersion
53        """
54        # Load data
55        # This data is for a cylinder with
56        #   length=400, radius=20, radius disp=5, scale=1e-10
57        out=Loader().load("cyl_400_20_disp5r.txt")
58        out.dy = numpy.zeros(len(out.y))
59        for i in range(len(out.y)):
60            out.dy[i] = math.sqrt(out.y[i])
61       
62        # Set up the fit
63        fitter = Fit('scipy')
64        # Receives the type of model for the fitting
65        model1  = CylinderModel()
66        model1.setParam('sldCyl', 3.0e-6)
67        model1.setParam('sldSolv', 0.0)
68
69        # Dispersion parameters
70        model1.dispersion['radius']['width'] = 0.001
71        model1.dispersion['radius']['npts'] = 50       
72       
73        model = Model(model1)
74       
75        pars1 =['length','radius','scale','radius.width']
76        fitter.set_data(out,1)
77        model.set(scale=1e-10)
78        fitter.set_model(model,1,pars1)
79        fitter.select_problem_for_fit(id=1,value=1)
80        result1, = fitter.fit()
81       
82        self.assert_(result1)
83        self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 )
84        self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0)
85       
86        self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] )
87        self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0  < result1.stderr[1] )
88        self.assertTrue( math.fabs(result1.pvec[2]-1.0e-10)/3.0   < result1.stderr[2] )
89        self.assertTrue( math.fabs(result1.pvec[3]-5.0)/3.0   < result1.stderr[3] )
90        self.assertTrue( result1.fitness < 1.0 )
91       
92       
93class smear_testdata(unittest.TestCase):
94    """
95        Test fitting with the smearing operations
96        The output of the fits should be compated to fits
97        done with IGOR for the same models and data sets.
98    """
99    def setUp(self):
100        print "TEST DONE WITHOUT PROPER OUTPUT CHECK:"
101        print "   ---> TEST NEEDS TO BE COMPLETED"
102        data = Loader().load("latex_smeared.xml")
103        self.data_res = data[0]
104        self.data_slit = data[1]
105       
106        self.sphere = SphereModel()
107        self.sphere.setParam('radius', 5000.0)
108        self.sphere.setParam('scale', 1.0e-13)
109        self.sphere.setParam('radius.npts', 30)
110        self.sphere.setParam('radius.width',500)
111       
112    def test_reso(self):
113
114        # Let the data module find out what smearing the
115        # data needs
116        smear = smear_selection(self.data_res)
117        self.assertEqual(smear.__class__.__name__, 'QSmearer')
118
119        # Fit
120        fitter = Fit('scipy')
121       
122        # Data: right now this is the only way to set the smearer object
123        # We should improve that and have a way to get access to the
124        # data for a given fit.
125        fitter.set_data(self.data_res,1)
126        fitter._engine.fit_arrange_dict[1].data_list[0].smearer = smear
127        print "smear ",smear
128        # Model: maybe there's a better way to do this.
129        # Ideally we should have to create a new model from our sans model.
130        fitter.set_model(Model(self.sphere),1, ['radius','scale'])
131       
132        # Why do we have to do this...?
133        fitter.select_problem_for_fit(id=1,value=1)
134       
135        # Perform the fit (might take a while)
136        result1, = fitter.fit()
137       
138        # Replace this with proper test once we know what the
139        # result should be
140        print result1.pvec
141        print result1.stderr
142       
143    def test_slit(self):
144        smear = smear_selection(self.data_slit)
145        self.assertEqual(smear.__class__.__name__, 'SlitSmearer')
146
147        # Fit
148        fitter = Fit('scipy')
149       
150        # Data: right now this is the only way to set the smearer object
151        # We should improve that and have a way to get access to the
152        # data for a given fit.
153        fitter.set_data(self.data_slit,1)
154        fitter._engine.fit_arrange_dict[1].data_list[0].smearer = smear
155        fitter._engine.fit_arrange_dict[1].data_list[0].qmax = 0.003
156       
157        # Model
158        fitter.set_model(Model(self.sphere),1, ['radius','scale'])
159        fitter.select_problem_for_fit(id=1,value=1)
160       
161        result1, = fitter.fit()
162       
163        # Replace this with proper test once we know what the
164        # result should be
165        print result1.pvec
166        print result1.stderr
167       
168       
169       
170if __name__ == '__main__':
171    unittest.main()
Note: See TracBrowser for help on using the repository browser.