1 | """ |
---|
2 | Unit tests for fitting module using park integration |
---|
3 | fitting 2 data with 2 model and one constraint on only one parameter is not working |
---|
4 | """ |
---|
5 | import unittest |
---|
6 | from sans.guitools.plottables import Theory1D |
---|
7 | from sans.guitools.plottables import Data1D |
---|
8 | from sans.fit.AbstractFitEngine import Model,Data |
---|
9 | import math |
---|
10 | class testFitModule(unittest.TestCase): |
---|
11 | |
---|
12 | def test2models2data2constraints(self): |
---|
13 | """ test fitting for two data , 2 model , 2 constraints""" |
---|
14 | from sans.fit.Loader import Load |
---|
15 | load= Load() |
---|
16 | #Load the first data |
---|
17 | load.set_filename("testdata1.txt") |
---|
18 | load.set_values() |
---|
19 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
20 | load.load_data(data1) |
---|
21 | |
---|
22 | #Load the second data |
---|
23 | load.set_filename("testdata2.txt") |
---|
24 | load.set_values() |
---|
25 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
26 | load.load_data(data2) |
---|
27 | |
---|
28 | #Load the third data |
---|
29 | load.set_filename("testdata_line.txt") |
---|
30 | load.set_values() |
---|
31 | data3 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
32 | load.load_data(data3) |
---|
33 | |
---|
34 | #Importing the Fit module |
---|
35 | from sans.fit.Fitting import Fit |
---|
36 | fitter= Fit('park') |
---|
37 | # Receives the type of model for the fitting |
---|
38 | from sans.guitools.LineModel import LineModel |
---|
39 | model1 = LineModel() |
---|
40 | |
---|
41 | #Do the fit |
---|
42 | model1.setParam( 'A', 2.5) |
---|
43 | model1.setParam( 'B', 4) |
---|
44 | model1.name="M1" |
---|
45 | fitter.set_model(Model(model1),"M1",1, ['A','B']) |
---|
46 | fitter.set_data(Data(sans_data=data1),1) |
---|
47 | |
---|
48 | |
---|
49 | result = fitter.fit() |
---|
50 | chisqr1 = result.fitness |
---|
51 | out1 = result.pvec |
---|
52 | cov1 = result.cov |
---|
53 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
54 | print math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) |
---|
55 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
56 | self.assert_(math.fabs(out1[3]-2.5)/math.sqrt(cov1[3][3]) < 2) |
---|
57 | self.assert_(math.fabs(out1[2]-4.0)/math.sqrt(cov1[2][2]) < 2) |
---|
58 | print chisqr1/len(data1.x) |
---|
59 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
60 | print chisqr1/len(data2.x) |
---|
61 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
62 | |
---|