source: sasview/park_integration/test/test_fit_smeared.py @ 44aa1ed

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 44aa1ed was 5fcfca4, checked in by Gervaise Alina <gervyh@…>, 16 years ago

smearing added

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