1 | """ |
---|
2 | Unit tests for fitting module |
---|
3 | """ |
---|
4 | import unittest |
---|
5 | from sans.guitools.plottables import Theory1D |
---|
6 | from sans.guitools.plottables import Data1D |
---|
7 | from sans.fit.AbstractFitEngine import Data, Model |
---|
8 | import math |
---|
9 | class testFitModule(unittest.TestCase): |
---|
10 | """ test fitting """ |
---|
11 | def test1(self): |
---|
12 | """ Fit 1 data (cyl_testdata.txt)and 1 model(CylinderModel) """ |
---|
13 | #load data |
---|
14 | from sans.fit.Loader import Load |
---|
15 | load = Load() |
---|
16 | load.set_filename("cyl_testdata.txt") |
---|
17 | load.set_values() |
---|
18 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
19 | load.load_data(data1) |
---|
20 | #Importing the Fit module |
---|
21 | from sans.fit.Fitting import Fit |
---|
22 | fitter = Fit('scipy') |
---|
23 | # Receives the type of model for the fitting |
---|
24 | from sans.models.CylinderModel import CylinderModel |
---|
25 | model1 = CylinderModel() |
---|
26 | model1.setParam('contrast',1) |
---|
27 | data = Data(sans_data=data1 ) |
---|
28 | model = Model(model1) |
---|
29 | |
---|
30 | pars1 =['length','radius','scale'] |
---|
31 | fitter.set_data(data,1) |
---|
32 | model.model.setParam('scale',10) |
---|
33 | fitter.set_model(model,1,pars1) |
---|
34 | result1 = fitter.fit() |
---|
35 | print "test scipy result:",result1.stderr,result1.pvec,result1.fitness |
---|
36 | self.assert_(result1) |
---|
37 | self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) |
---|
38 | self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) |
---|
39 | self.assertAlmostEquals( result1.pvec[0],0 ) |
---|
40 | self.assertAlmostEquals( result1.pvec[1],3*math.pow(10,-6) ) |
---|
41 | self.assertAlmostEquals( result1.pvec[2] , 1 ) |
---|
42 | self.assertAlmostEquals( result1.pvec[3] , 1 ) |
---|
43 | self.assertAlmostEquals( result1.pvec[4] , 400 ) |
---|
44 | self.assertAlmostEquals( result1.pvec[5] , 20 ) |
---|
45 | self.assertAlmostEquals ( result1.pvec[6] , 1 ) |
---|
46 | self.assertTrue( result1.fitness/43 < 2 ) |
---|
47 | |
---|
48 | |
---|