1 | """ |
---|
2 | Unit tests for fitting module |
---|
3 | """ |
---|
4 | import unittest |
---|
5 | |
---|
6 | from sans.guiframe.dataFitting import Data1D |
---|
7 | from sans.fit.AbstractFitEngine import Data, Model,FitData1D |
---|
8 | import math |
---|
9 | from sans.fit.Fitting import Fit |
---|
10 | from DataLoader.loader import Loader |
---|
11 | |
---|
12 | class testFitModule(unittest.TestCase): |
---|
13 | """ test fitting """ |
---|
14 | |
---|
15 | |
---|
16 | def test_scipy(self): |
---|
17 | """ Simple cylinder model fit (scipy) """ |
---|
18 | |
---|
19 | out=Loader().load("cyl_testdata1.txt") |
---|
20 | data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
21 | |
---|
22 | fitter = Fit('scipy') |
---|
23 | # Receives the type of model for the fitting |
---|
24 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
25 | from sans.models.CylinderModel import CylinderModel |
---|
26 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
27 | model1 = MultiplicationModel(CylinderModel(),SquareWellStructure()) |
---|
28 | model1.setParam('contrast', 1) |
---|
29 | #data = Data(sans_data=data1) |
---|
30 | data = FitData1D(data1) |
---|
31 | model = Model(model1) |
---|
32 | |
---|
33 | pars1 =['length','radius','scale'] |
---|
34 | fitter.set_data(data,1) |
---|
35 | model.set(scale=1e-10) |
---|
36 | fitter.set_model(model,1,pars1) |
---|
37 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
38 | result1 = fitter.fit() |
---|
39 | print "result ",result1.fitness,result1.pvec,result1.stderr |
---|
40 | self.assert_(result1) |
---|
41 | self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) |
---|
42 | self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) |
---|
43 | |
---|
44 | self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) |
---|
45 | self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) |
---|
46 | self.assertTrue( math.fabs(result1.pvec[2]-9.0e-12)/3.0 < result1.stderr[2] ) |
---|
47 | self.assertTrue( result1.fitness < 1.0 ) |
---|
48 | |
---|
49 | |
---|