[4a0536a] | 1 | """ |
---|
| 2 | Unit tests for fitting module |
---|
| 3 | """ |
---|
[93c2ef2] | 4 | import unittest |
---|
[4a0536a] | 5 | from sans.guitools.plottables import Theory1D |
---|
| 6 | from sans.guitools.plottables import Data1D |
---|
[4dd63eb] | 7 | |
---|
[93c2ef2] | 8 | import math |
---|
[4a0536a] | 9 | class testFitModule(unittest.TestCase): |
---|
| 10 | """ test fitting """ |
---|
| 11 | def testLoader(self): |
---|
| 12 | """ |
---|
| 13 | test module Load |
---|
| 14 | """ |
---|
[7705306] | 15 | from sans.fit.Loader import Load |
---|
[4a0536a] | 16 | load= Load() |
---|
| 17 | |
---|
| 18 | load.set_filename("testdata_line.txt") |
---|
| 19 | self.assertEqual(load.get_filename(),"testdata_line.txt") |
---|
| 20 | load.set_values() |
---|
| 21 | x=[] |
---|
| 22 | y=[] |
---|
| 23 | dx=[] |
---|
| 24 | dy=[] |
---|
| 25 | |
---|
| 26 | x,y,dx,dy = load.get_values() |
---|
| 27 | |
---|
| 28 | # test that values have been loaded |
---|
| 29 | self.assertNotEqual(x, None) |
---|
| 30 | self.assertNotEqual(y, []) |
---|
| 31 | self.assertNotEqual(dy, None) |
---|
| 32 | self.assertEqual(len(x),len(y)) |
---|
| 33 | self.assertEqual(len(dy),len(y)) |
---|
| 34 | |
---|
| 35 | # test data the two plottables contained values loaded |
---|
| 36 | data1 = Theory1D(x=[], y=[], dy=None) |
---|
| 37 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
| 38 | data1.name = "data1" |
---|
| 39 | data2.name = "data2" |
---|
| 40 | load.load_data(data1) |
---|
| 41 | load.load_data(data2) |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | for i in range(len(x)): |
---|
| 45 | self.assertEqual(data2.x[i],x[i]) |
---|
| 46 | self.assertEqual(data1.y[i],y[i]) |
---|
| 47 | self.assertEqual(data2.y[i],y[i]) |
---|
| 48 | self.assertEqual(data1.dx[i],dx[i]) |
---|
| 49 | self.assertEqual(data2.dy[i],dy[i]) |
---|
| 50 | self.assertEqual(data1.x[i],data2.x[i]) |
---|
| 51 | self.assertEqual(data2.y[i],data2.y[i]) |
---|
| 52 | |
---|
| 53 | |
---|
[f24b8f4] | 54 | def testfit_1Data_1Model(self): |
---|
[792db7d5] | 55 | """ test fitting for one data and one model park vs scipy""" |
---|
[f24b8f4] | 56 | #load data |
---|
[7705306] | 57 | from sans.fit.Loader import Load |
---|
[4a0536a] | 58 | load= Load() |
---|
| 59 | load.set_filename("testdata_line.txt") |
---|
| 60 | load.set_values() |
---|
| 61 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
| 62 | load.load_data(data1) |
---|
[93c2ef2] | 63 | |
---|
[f24b8f4] | 64 | #Importing the Fit module |
---|
[7705306] | 65 | from sans.fit.Fitting import Fit |
---|
[4dd63eb] | 66 | fitter= Fit('scipy') |
---|
| 67 | |
---|
[4a0536a] | 68 | # Receives the type of model for the fitting |
---|
| 69 | from sans.guitools.LineModel import LineModel |
---|
[792db7d5] | 70 | model1 = LineModel() |
---|
| 71 | model2 = LineModel() |
---|
[93c2ef2] | 72 | |
---|
[3701620] | 73 | #Do the fit SCIPY |
---|
[985c88b] | 74 | model1.setParam( 'A', 2) |
---|
| 75 | model1.setParam( 'B', 1) |
---|
[4dd63eb] | 76 | fitter.set_data(data1,1) |
---|
[985c88b] | 77 | fitter.set_model(model1,"M1",1,['A','B']) |
---|
[f24b8f4] | 78 | |
---|
[4dd63eb] | 79 | chisqr1, out1, cov1=fitter.fit() |
---|
[3701620] | 80 | """ testing SCIPy results""" |
---|
| 81 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
| 82 | self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
| 83 | self.assert_(chisqr1/len(data1.x) < 2) |
---|
| 84 | # PARK |
---|
[4dd63eb] | 85 | fitter= Fit('park') |
---|
[f24b8f4] | 86 | |
---|
[3701620] | 87 | #Do the fit |
---|
[4dd63eb] | 88 | fitter.set_data(data1,1) |
---|
[985c88b] | 89 | model2.setParam( 'A', 2) |
---|
| 90 | model2.setParam( 'B', 1) |
---|
| 91 | fitter.set_model(model2,"M1",1,['A','B']) |
---|
[3701620] | 92 | |
---|
[985c88b] | 93 | chisqr2, out2, cov2,result=fitter.fit(None,None) |
---|
[cf3b781] | 94 | |
---|
| 95 | self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) |
---|
| 96 | self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) |
---|
| 97 | self.assert_(chisqr2/len(data1.x) < 2) |
---|
[792db7d5] | 98 | print "scipy",chisqr1, out1, cov1 |
---|
| 99 | print "park",chisqr2, out2, cov2 |
---|
| 100 | self.assertAlmostEquals(out1[1], out2[1],0) |
---|
| 101 | self.assertAlmostEquals(out1[0], out2[0],0) |
---|
| 102 | self.assertAlmostEquals(cov1[0][0], cov2[0][0],1) |
---|
| 103 | self.assertAlmostEquals(cov1[1][1], cov2[1][1],1) |
---|
| 104 | self.assertAlmostEquals(chisqr1, chisqr2) |
---|
[f24b8f4] | 105 | |
---|
[792db7d5] | 106 | |
---|