source: sasview/park_integration/test/test_fit_smeared.py @ 3cd5806

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 3cd5806 was eb575b0, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add author name to unittest

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