[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 |
---|
| 35 | fitter= Fit() |
---|
| 36 | # Receives the type of model for the fitting |
---|
| 37 | from sans.guitools.LineModel import LineModel |
---|
| 38 | model1 = LineModel() |
---|
| 39 | model2 = LineModel() |
---|
| 40 | #set engine for scipy |
---|
| 41 | fitter.fit_engine('park') |
---|
| 42 | engine = fitter.returnEngine() |
---|
| 43 | #Do the fit |
---|
| 44 | engine.set_param( model1,"M1", {'A':2.5,'B':4}) |
---|
| 45 | engine.set_model(model1,1) |
---|
| 46 | engine.set_data(data1,1) |
---|
| 47 | |
---|
[792db7d5] | 48 | engine.set_param( model2,"M2", {'A':2,'B':4}) |
---|
[9e85792] | 49 | engine.set_model(model2,2) |
---|
| 50 | engine.set_data(data2,2) |
---|
[792db7d5] | 51 | |
---|
| 52 | chisqr1, out1, cov1= engine.fit({'A':2,'B':1},None,None) |
---|
| 53 | |
---|
| 54 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
| 55 | print math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) |
---|
| 56 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
| 57 | self.assert_(math.fabs(out1[3]-2.5)/math.sqrt(cov1[3][3]) < 2) |
---|
| 58 | self.assert_(math.fabs(out1[2]-4.0)/math.sqrt(cov1[2][2]) < 2) |
---|
| 59 | print chisqr1/len(data1.x) |
---|
| 60 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
| 61 | print chisqr1/len(data2.x) |
---|
| 62 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | engine.set_data(data3,1) |
---|
| 66 | chisqr2, out2, cov2= engine.fit({'A':2,'B':1},None,None) |
---|
| 67 | self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) |
---|
| 68 | print math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) |
---|
| 69 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
| 70 | self.assert_(math.fabs(out2[3]-2.5)/math.sqrt(cov2[3][3]) < 2) |
---|
| 71 | self.assert_(math.fabs(out2[2]-4.0)/math.sqrt(cov2[2][2]) < 2) |
---|
| 72 | print chisqr2/len(data1.x) |
---|
| 73 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
| 74 | print chisqr2/len(data2.x) |
---|
| 75 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | engine.remove_Fit_Problem(2) |
---|
| 80 | chisqr3, out3, cov3= engine.fit({'A':2,'B':1},None,None) |
---|
| 81 | #print "park",chisqr3, out3, cov3 |
---|
| 82 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
| 83 | print math.fabs(out1[0]-4.0) |
---|
| 84 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
| 85 | print chisqr1/len(data1.x) |
---|
| 86 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
| 87 | #self.assert_(chisqr1/len(data2.x) < 2) |
---|
| 88 | #failing at 7 place |
---|
| 89 | self.assertAlmostEquals(out3[1],out1[1]) |
---|
| 90 | self.assertAlmostEquals(out3[0],out1[0]) |
---|
| 91 | self.assertAlmostEquals(cov3[1][1],cov1[1][1]) |
---|
| 92 | self.assertAlmostEquals(cov3[0][0],cov1[0][0]) |
---|
| 93 | |
---|
| 94 | self.assertAlmostEquals(out2[1],out1[1]) |
---|
| 95 | self.assertAlmostEquals(out2[0],out1[0]) |
---|
| 96 | self.assertAlmostEquals(cov2[1][1],cov1[1][1]) |
---|
| 97 | self.assertAlmostEquals(cov2[0][0],cov1[0][0]) |
---|
| 98 | |
---|
| 99 | self.assertAlmostEquals(out2[1],out3[1]) |
---|
| 100 | self.assertAlmostEquals(out2[0],out3[0]) |
---|
| 101 | self.assertAlmostEquals(cov2[1][1],cov3[1][1]) |
---|
| 102 | self.assertAlmostEquals(cov2[0][0],cov3[0][0]) |
---|
| 103 | print chisqr1,chisqr2,chisqr3 |
---|
| 104 | #self.assertAlmostEquals(chisqr1,chisqr2) |
---|