[30c6721] | 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 | |
---|
| 8 | import math |
---|
| 9 | class testFitModule(unittest.TestCase): |
---|
| 10 | """ test fitting """ |
---|
| 11 | def testfit_1Data_1Model(self): |
---|
| 12 | """ test fitting for one data and one model park vs scipy""" |
---|
| 13 | #load data |
---|
| 14 | from sans.fit.Loader import Load |
---|
| 15 | load= Load() |
---|
[cfe97ea] | 16 | load.set_filename("cyl_testdata.txt") |
---|
[30c6721] | 17 | load.set_values() |
---|
| 18 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
| 19 | load.load_data(data1) |
---|
[d53bc33] | 20 | |
---|
| 21 | |
---|
| 22 | #Importing the Fit module |
---|
| 23 | from sans.fit.Fitting import Fit |
---|
| 24 | fitter= Fit('scipy') |
---|
| 25 | |
---|
| 26 | # Receives the type of model for the fitting |
---|
| 27 | from sans.models.CylinderModel import CylinderModel |
---|
| 28 | model1 = CylinderModel() |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | #Do the fit SCIPY |
---|
| 32 | fitter.set_data(data1,1) |
---|
| 33 | import math |
---|
| 34 | pars1={'background':0,'contrast':3*math.pow(10, -6),\ |
---|
| 35 | 'cyl_phi':1,'cyl_theta':1,'length':400,'radius':20,'scale':1} |
---|
| 36 | fitter.set_model(model1,"M1",1,pars1) |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | chisqr1, out1, cov1=fitter.fit() |
---|
| 40 | print "scipy1",chisqr1, out1, cov1 |
---|
| 41 | pars2={'background':1.0,'contrast':400,\ |
---|
| 42 | 'cyl_phi':20,'cyl_theta':0.0,'length':1.0,\ |
---|
| 43 | 'radius':3*math.pow(10, -6),'scale':1.0} |
---|
| 44 | fitter.set_model(model1,"M1",1,pars2) |
---|
| 45 | chisqr2, out2, cov2=fitter.fit() |
---|
| 46 | print "scipy2",chisqr2, out2, cov2 |
---|
| 47 | |
---|
| 48 | pars3={'background':5.85693826,'contrast': 5.86071451,\ |
---|
| 49 | 'cyl_phi':1.04547760*math.pow(10,-5),'cyl_theta':1.0,'length':0.0,\ |
---|
| 50 | 'radius':1.39397013*math.pow(10, 3),'scale':20} |
---|
| 51 | fitter.set_model(model1,"M1",1,pars3) |
---|
| 52 | chisqr3, out3, cov3=fitter.fit() |
---|
| 53 | print "scipy3",chisqr3, out3, cov3 |
---|
| 54 | self.assert_(chisqr1) |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | def testfit_11Data_1Model(self): |
---|
| 59 | """ test fitting for one data and one model park vs scipy""" |
---|
| 60 | #load data |
---|
| 61 | from sans.fit.Loader import Load |
---|
| 62 | load= Load() |
---|
| 63 | load.set_filename("cyl_testdata.txt") |
---|
| 64 | load.set_values() |
---|
| 65 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
| 66 | load.load_data(data1) |
---|
[30c6721] | 67 | |
---|
| 68 | load.set_filename("testdata_line1.txt") |
---|
| 69 | load.set_values() |
---|
| 70 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
| 71 | load.load_data(data2) |
---|
| 72 | |
---|
| 73 | #Importing the Fit module |
---|
| 74 | from sans.fit.Fitting import Fit |
---|
| 75 | fitter= Fit('park') |
---|
| 76 | |
---|
| 77 | # Receives the type of model for the fitting |
---|
| 78 | from sans.models.CylinderModel import CylinderModel |
---|
| 79 | model1 = CylinderModel() |
---|
[cfe97ea] | 80 | #model2 = CylinderModel() |
---|
[30c6721] | 81 | |
---|
| 82 | #Do the fit SCIPY |
---|
| 83 | fitter.set_data(data1,1) |
---|
[cfe97ea] | 84 | import math |
---|
| 85 | pars1={'background':0,'contrast':3*math.pow(10, -6),\ |
---|
| 86 | 'cyl_phi':1,'cyl_theta':1,'length':400,'radius':20,'scale':1} |
---|
| 87 | fitter.set_model(model1,"M1",1,pars1) |
---|
[30c6721] | 88 | |
---|
[cfe97ea] | 89 | #fitter.set_data(data2,2) |
---|
| 90 | #fitter.set_model(model1,"M1",2,pars1) |
---|
[30c6721] | 91 | |
---|
| 92 | chisqr1, out1, cov1=fitter.fit() |
---|
| 93 | print "park",chisqr1, out1, cov1 |
---|
| 94 | self.assert_(chisqr1) |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|