[9e85792] | 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.ScipyFitting import Parameter |
---|
| 8 | import math |
---|
| 9 | class testFitModule(unittest.TestCase): |
---|
| 10 | |
---|
| 11 | def test2models2dataonconstraint(self): |
---|
| 12 | """ test fitting for two set of data and one model""" |
---|
| 13 | from sans.fit.Loader import Load |
---|
| 14 | load= Load() |
---|
[792db7d5] | 15 | #Load the first data |
---|
[9e85792] | 16 | load.set_filename("testdata1.txt") |
---|
| 17 | load.set_values() |
---|
| 18 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
| 19 | load.load_data(data1) |
---|
| 20 | |
---|
[792db7d5] | 21 | #Load the second data |
---|
[9e85792] | 22 | load.set_filename("testdata2.txt") |
---|
| 23 | load.set_values() |
---|
| 24 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
| 25 | load.load_data(data2) |
---|
| 26 | |
---|
[792db7d5] | 27 | #Load the third data |
---|
| 28 | load.set_filename("testdata_line.txt") |
---|
| 29 | load.set_values() |
---|
| 30 | data3 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
| 31 | load.load_data(data3) |
---|
| 32 | |
---|
[9e85792] | 33 | #Importing the Fit module |
---|
| 34 | from sans.fit.Fitting import Fit |
---|
[4dd63eb] | 35 | fitter= Fit('park') |
---|
[9e85792] | 36 | # Receives the type of model for the fitting |
---|
| 37 | from sans.guitools.LineModel import LineModel |
---|
| 38 | model1 = LineModel() |
---|
| 39 | model2 = LineModel() |
---|
[4dd63eb] | 40 | |
---|
[9e85792] | 41 | #Do the fit |
---|
[4dd63eb] | 42 | fitter.set_model(model1,"M1",1, {'A':2.5,'B':4}) |
---|
| 43 | fitter.set_data(data1,1) |
---|
[9e85792] | 44 | |
---|
[4dd63eb] | 45 | fitter.set_model(model2,"M2",2, {'A':2,'B':3}) |
---|
| 46 | fitter.set_data(data2,2) |
---|
[792db7d5] | 47 | |
---|
[4dd63eb] | 48 | chisqr1, out1, cov1= fitter.fit() |
---|
[792db7d5] | 49 | |
---|
| 50 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
| 51 | print math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) |
---|
| 52 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
| 53 | self.assert_(math.fabs(out1[3]-2.5)/math.sqrt(cov1[3][3]) < 2) |
---|
| 54 | self.assert_(math.fabs(out1[2]-4.0)/math.sqrt(cov1[2][2]) < 2) |
---|
| 55 | print chisqr1/len(data1.x) |
---|
| 56 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
| 57 | print chisqr1/len(data2.x) |
---|
| 58 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
| 59 | |
---|
| 60 | |
---|
[4dd63eb] | 61 | fitter.set_data(data3,1) |
---|
| 62 | chisqr2, out2, cov2= fitter.fit(None,None) |
---|
[792db7d5] | 63 | self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) |
---|
| 64 | print math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) |
---|
| 65 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
| 66 | self.assert_(math.fabs(out2[3]-2.5)/math.sqrt(cov2[3][3]) < 2) |
---|
| 67 | self.assert_(math.fabs(out2[2]-4.0)/math.sqrt(cov2[2][2]) < 2) |
---|
| 68 | print chisqr2/len(data1.x) |
---|
| 69 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
| 70 | print chisqr2/len(data2.x) |
---|
| 71 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
| 72 | |
---|
[4dd63eb] | 73 | fitter.remove_Fit_Problem(2) |
---|
[792db7d5] | 74 | |
---|
[4dd63eb] | 75 | chisqr3, out3, cov3= fitter.fit() |
---|
[792db7d5] | 76 | #print "park",chisqr3, out3, cov3 |
---|
| 77 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
| 78 | print math.fabs(out1[0]-4.0) |
---|
| 79 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
| 80 | print chisqr1/len(data1.x) |
---|
| 81 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
| 82 | #self.assert_(chisqr1/len(data2.x) < 2) |
---|
| 83 | #failing at 7 place |
---|
| 84 | self.assertAlmostEquals(out3[1],out1[1]) |
---|
| 85 | self.assertAlmostEquals(out3[0],out1[0]) |
---|
| 86 | self.assertAlmostEquals(cov3[1][1],cov1[1][1]) |
---|
| 87 | self.assertAlmostEquals(cov3[0][0],cov1[0][0]) |
---|
| 88 | |
---|
| 89 | self.assertAlmostEquals(out2[1],out1[1]) |
---|
| 90 | self.assertAlmostEquals(out2[0],out1[0]) |
---|
| 91 | self.assertAlmostEquals(cov2[1][1],cov1[1][1]) |
---|
| 92 | self.assertAlmostEquals(cov2[0][0],cov1[0][0]) |
---|
| 93 | |
---|
| 94 | self.assertAlmostEquals(out2[1],out3[1]) |
---|
| 95 | self.assertAlmostEquals(out2[0],out3[0]) |
---|
| 96 | self.assertAlmostEquals(cov2[1][1],cov3[1][1]) |
---|
| 97 | self.assertAlmostEquals(cov2[0][0],cov3[0][0]) |
---|
| 98 | print chisqr1,chisqr2,chisqr3 |
---|
| 99 | #self.assertAlmostEquals(chisqr1,chisqr2) |
---|