[ca6d914] | 1 | """ |
---|
| 2 | Unit tests for fitting module |
---|
| 3 | """ |
---|
| 4 | import unittest |
---|
[5fcfca4] | 5 | #from sans.guitools.plottables import Theory1D |
---|
| 6 | #from sans.guitools.plottables import Data1D |
---|
| 7 | from danse.common.plottools.plottables import Data1D,Theory1D |
---|
[7d0c1a8] | 8 | from sans.fit.AbstractFitEngine import Data, Model,FitData1D |
---|
[ca6d914] | 9 | import math |
---|
| 10 | class testFitModule(unittest.TestCase): |
---|
[b461b6d7] | 11 | def smalltest(self): |
---|
| 12 | |
---|
| 13 | import numpy |
---|
| 14 | x=[1,22,3] |
---|
| 15 | y=[5,6,7,8] |
---|
| 16 | array= numpy.zeros(len(x), len(y)) |
---|
| 17 | |
---|
[ca6d914] | 18 | """ test fitting """ |
---|
| 19 | def test1(self): |
---|
[8bbab51] | 20 | """ Fit 1 data (testdata_line.txt)and 1 model(lineModel) """ |
---|
[ca6d914] | 21 | #load data |
---|
[1374174f] | 22 | from DataLoader.loader import Loader |
---|
| 23 | data1 = Loader().load("testdata_line.txt") |
---|
[ca6d914] | 24 | #Importing the Fit module |
---|
| 25 | from sans.fit.Fitting import Fit |
---|
| 26 | fitter = Fit('scipy') |
---|
| 27 | # Receives the type of model for the fitting |
---|
[52ac10b] | 28 | from sans.models.LineModel import LineModel |
---|
[ca6d914] | 29 | model1 = LineModel() |
---|
| 30 | model1.name = "M1" |
---|
[7d0c1a8] | 31 | #data = Data(sans_data=data1 ) |
---|
[52ac10b] | 32 | #data1.smearer=None |
---|
[7d0c1a8] | 33 | data = FitData1D(data1 ) |
---|
[ca6d914] | 34 | model = Model(model1) |
---|
| 35 | #fit with scipy test |
---|
| 36 | |
---|
| 37 | pars1= ['param1','param2'] |
---|
| 38 | fitter.set_data(data,1) |
---|
| 39 | try:fitter.set_model(model,1,pars1) |
---|
| 40 | except ValueError,msg: |
---|
| 41 | assert str(msg)=="wrong paramter %s used to set model %s. Choose\ |
---|
| 42 | parameter name within %s"%('param1', model.model.name,str(model.model.getParamList())) |
---|
| 43 | else: raise AssertError,"No error raised for scipy fitting with wrong parameters name to fit" |
---|
| 44 | pars1= ['A','B'] |
---|
| 45 | fitter.set_model(model,1,pars1) |
---|
[8bbab51] | 46 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
[ca6d914] | 47 | result1 = fitter.fit() |
---|
| 48 | self.assert_(result1) |
---|
| 49 | |
---|
| 50 | self.assertTrue( ( math.fabs(result1.pvec[0]-4)/3 == result1.stderr[0] ) or |
---|
| 51 | ( math.fabs(result1.pvec[0]-4)/3 < result1.stderr[0]) ) |
---|
| 52 | |
---|
| 53 | self.assertTrue( ( math.fabs(result1.pvec[1]-2.5)/3 == result1.stderr[1]) or |
---|
| 54 | ( math.fabs(result1.pvec[1]-2.5)/3 < result1.stderr[1] ) ) |
---|
| 55 | self.assertTrue( result1.fitness/49 < 2 ) |
---|
| 56 | |
---|
| 57 | #fit with park test |
---|
| 58 | fitter = Fit('park') |
---|
| 59 | fitter.set_data(data,1) |
---|
| 60 | fitter.set_model(model,1,pars1) |
---|
[8bbab51] | 61 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
[ca6d914] | 62 | result2 = fitter.fit() |
---|
| 63 | |
---|
| 64 | self.assert_(result2) |
---|
| 65 | |
---|
| 66 | self.assertTrue( ( math.fabs(result2.pvec[0]-4)/3 == result2.stderr[0] ) or |
---|
| 67 | ( math.fabs(result2.pvec[0]-4)/3 < result2.stderr[0]) ) |
---|
| 68 | |
---|
| 69 | self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3 == result2.stderr[1] ) or |
---|
| 70 | ( math.fabs(result2.pvec[1]-2.5)/3 < result2.stderr[1]) ) |
---|
| 71 | self.assertTrue(result2.fitness/49 < 2) |
---|
| 72 | # compare fit result result for scipy and park |
---|
| 73 | self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] ) |
---|
| 74 | self.assertAlmostEquals( result1.pvec[1],result2.pvec[1] ) |
---|
| 75 | self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) |
---|
| 76 | self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) |
---|
| 77 | self.assertAlmostEquals( result1.fitness,result2.fitness ) |
---|
| 78 | |
---|
| 79 | def test2(self): |
---|
| 80 | """ fit 2 data and 2 model with no constrainst""" |
---|
| 81 | #load data |
---|
| 82 | from DataLoader.loader import Loader |
---|
| 83 | l = Loader() |
---|
| 84 | out=l.load("testdata_line.txt") |
---|
| 85 | data11 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
| 86 | out=l.load("testdata_line1.txt") |
---|
| 87 | data22 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
| 88 | #Importing the Fit module |
---|
| 89 | from sans.fit.Fitting import Fit |
---|
| 90 | fitter = Fit('scipy') |
---|
| 91 | # Receives the type of model for the fitting |
---|
[52ac10b] | 92 | from sans.models.LineModel import LineModel |
---|
[ca6d914] | 93 | model11 = LineModel() |
---|
| 94 | model11.name= "M1" |
---|
| 95 | model22 = LineModel() |
---|
| 96 | model11.name= "M2" |
---|
[af4af5a] | 97 | data11.smearer=None |
---|
| 98 | data22.smearer=None |
---|
[7d0c1a8] | 99 | data1 = FitData1D(data11 ) |
---|
| 100 | data2 = FitData1D(data22 ) |
---|
| 101 | |
---|
[ca6d914] | 102 | model1 = Model(model11) |
---|
| 103 | model2 = Model(model22) |
---|
| 104 | #fit with scipy test |
---|
| 105 | pars1= ['A','B'] |
---|
| 106 | fitter.set_data(data1,1) |
---|
| 107 | fitter.set_model(model1,1,pars1) |
---|
[8bbab51] | 108 | fitter.select_problem_for_fit(Uid=1,value=0) |
---|
[ca6d914] | 109 | fitter.set_data(data2,2) |
---|
| 110 | fitter.set_model(model2,2,pars1) |
---|
[8bbab51] | 111 | fitter.select_problem_for_fit(Uid=2,value=0) |
---|
| 112 | |
---|
| 113 | try: result1 = fitter.fit() |
---|
| 114 | except RuntimeError,msg: |
---|
| 115 | assert str(msg)=="No Assembly scheduled for Scipy fitting." |
---|
| 116 | else: raise AssertError,"No error raised for scipy fitting with no model" |
---|
| 117 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
| 118 | fitter.select_problem_for_fit(Uid=2,value=1) |
---|
[ca6d914] | 119 | try: result1 = fitter.fit() |
---|
| 120 | except RuntimeError,msg: |
---|
| 121 | assert str(msg)=="Scipy can't fit more than a single fit problem at a time." |
---|
| 122 | else: raise AssertError,"No error raised for scipy fitting with more than 2 models" |
---|
| 123 | |
---|
| 124 | #fit with park test |
---|
| 125 | fitter = Fit('park') |
---|
| 126 | fitter.set_data(data1,1) |
---|
| 127 | fitter.set_model(model1,1,pars1) |
---|
| 128 | fitter.set_data(data2,2) |
---|
| 129 | fitter.set_model(model2,2,pars1) |
---|
[8bbab51] | 130 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
| 131 | fitter.select_problem_for_fit(Uid=2,value=1) |
---|
[ca6d914] | 132 | result2 = fitter.fit() |
---|
| 133 | |
---|
| 134 | self.assert_(result2) |
---|
| 135 | self.assertTrue( ( math.fabs(result2.pvec[0]-4)/3 == result2.stderr[0] ) or |
---|
| 136 | ( math.fabs(result2.pvec[0]-4)/3 < result2.stderr[0]) ) |
---|
| 137 | |
---|
| 138 | self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3 == result2.stderr[1] ) or |
---|
| 139 | ( math.fabs(result2.pvec[1]-2.5)/3 < result2.stderr[1]) ) |
---|
| 140 | self.assertTrue(result2.fitness/49 < 2) |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | def test3(self): |
---|
| 144 | """ fit 2 data and 2 model with 1 constrainst""" |
---|
| 145 | #load data |
---|
| 146 | from DataLoader.loader import Loader |
---|
| 147 | l = Loader() |
---|
| 148 | out=l.load("testdata_line.txt") |
---|
| 149 | data11 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
| 150 | out=l.load("testdata_cst.txt") |
---|
| 151 | data22 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
| 152 | #Importing the Fit module |
---|
| 153 | from sans.fit.Fitting import Fit |
---|
| 154 | fitter = Fit('park') |
---|
| 155 | # Receives the type of model for the fitting |
---|
[52ac10b] | 156 | from sans.models.LineModel import LineModel |
---|
[ca6d914] | 157 | from sans.models.Constant import Constant |
---|
| 158 | model11 = LineModel() |
---|
| 159 | model11.name= "line" |
---|
[8bbab51] | 160 | |
---|
[ca6d914] | 161 | model22 = Constant() |
---|
| 162 | model22.name= "cst" |
---|
[6aa47df] | 163 | # Constrain the constant value to be equal to parameter B (the real value is 2.5) |
---|
[8bbab51] | 164 | |
---|
[7d0c1a8] | 165 | #data1 = Data(sans_data=data11 ) |
---|
| 166 | #data2 = Data(sans_data=data22 ) |
---|
[af4af5a] | 167 | data11.smearer= None |
---|
| 168 | data22.smearer= None |
---|
[7d0c1a8] | 169 | data1 = FitData1D(data11 ) |
---|
| 170 | data2 = FitData1D(data22 ) |
---|
[ca6d914] | 171 | model1 = Model(model11) |
---|
| 172 | model2 = Model(model22) |
---|
[8bbab51] | 173 | model1.set(A=4) |
---|
| 174 | model1.set(B=3) |
---|
| 175 | model2.set(value='line.B') |
---|
[ca6d914] | 176 | #fit with scipy test |
---|
| 177 | pars1= ['A','B'] |
---|
| 178 | pars2= ['value'] |
---|
| 179 | |
---|
| 180 | fitter.set_data(data1,1) |
---|
| 181 | fitter.set_model(model1,1,pars1) |
---|
[8296ff5] | 182 | fitter.set_data(data2,2,smearer=None) |
---|
[ca6d914] | 183 | fitter.set_model(model2,2,pars2) |
---|
[8bbab51] | 184 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
| 185 | fitter.select_problem_for_fit(Uid=2,value=1) |
---|
[ca6d914] | 186 | |
---|
| 187 | result2 = fitter.fit() |
---|
| 188 | self.assert_(result2) |
---|
[3931a24e] | 189 | self.assertTrue( ( math.fabs(result2.pvec[0]-4.0)/3. < result2.stderr[0]) ) |
---|
[ca6d914] | 190 | |
---|
[3931a24e] | 191 | self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3. < result2.stderr[1]) ) |
---|
[ca6d914] | 192 | self.assertTrue(result2.fitness/49 < 2) |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | def test4(self): |
---|
| 196 | """ fit 2 data concatenates with limited range of x and one model """ |
---|
| 197 | #load data |
---|
| 198 | from DataLoader.loader import Loader |
---|
| 199 | l = Loader() |
---|
| 200 | out=l.load("testdata_line.txt") |
---|
| 201 | data11 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
| 202 | out=l.load("testdata_line1.txt") |
---|
| 203 | data22 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
| 204 | #Importing the Fit module |
---|
| 205 | from sans.fit.Fitting import Fit |
---|
| 206 | fitter = Fit('scipy') |
---|
| 207 | # Receives the type of model for the fitting |
---|
[52ac10b] | 208 | from sans.models.LineModel import LineModel |
---|
[ca6d914] | 209 | model1 = LineModel() |
---|
| 210 | model1.name= "M1" |
---|
[916a15f] | 211 | |
---|
[af4af5a] | 212 | data11.smearer=None |
---|
| 213 | data22.smearer=None |
---|
[7d0c1a8] | 214 | data1 = FitData1D(data11 ) |
---|
| 215 | data2 = FitData1D(data22 ) |
---|
[ca6d914] | 216 | model = Model(model1) |
---|
| 217 | |
---|
| 218 | #fit with scipy test |
---|
| 219 | pars1= ['A','B'] |
---|
| 220 | fitter.set_data(data1,1,qmin=0, qmax=7) |
---|
| 221 | fitter.set_model(model,1,pars1) |
---|
| 222 | fitter.set_data(data2,1,qmin=1,qmax=10) |
---|
[8bbab51] | 223 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
[ca6d914] | 224 | |
---|
| 225 | result1 = fitter.fit() |
---|
| 226 | self.assert_(result1) |
---|
[916a15f] | 227 | |
---|
[ca6d914] | 228 | self.assertTrue( ( math.fabs(result1.pvec[0]-4)/3 == result1.stderr[0] ) or |
---|
| 229 | ( math.fabs(result1.pvec[0]-4)/3 < result1.stderr[0]) ) |
---|
| 230 | |
---|
| 231 | self.assertTrue( ( math.fabs(result1.pvec[1]-2.5)/3 == result1.stderr[1]) or |
---|
| 232 | ( math.fabs(result1.pvec[1]-2.5)/3 < result1.stderr[1] ) ) |
---|
| 233 | self.assertTrue( result1.fitness/49 < 2 ) |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | #fit with park test |
---|
| 237 | fitter = Fit('park') |
---|
| 238 | fitter.set_data(data1,1,qmin=0, qmax=7) |
---|
| 239 | fitter.set_model(model,1,pars1) |
---|
| 240 | fitter.set_data(data2,1,qmin=1,qmax=10) |
---|
[8bbab51] | 241 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
[ca6d914] | 242 | result2 = fitter.fit() |
---|
| 243 | |
---|
| 244 | self.assert_(result2) |
---|
| 245 | self.assertTrue( ( math.fabs(result2.pvec[0]-4)/3 == result2.stderr[0] ) or |
---|
| 246 | ( math.fabs(result2.pvec[0]-4)/3 < result2.stderr[0]) ) |
---|
| 247 | |
---|
| 248 | self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3 == result2.stderr[1] ) or |
---|
| 249 | ( math.fabs(result2.pvec[1]-2.5)/3 < result2.stderr[1]) ) |
---|
| 250 | self.assertTrue(result2.fitness/49 < 2) |
---|
| 251 | # compare fit result result for scipy and park |
---|
| 252 | self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] ) |
---|
| 253 | self.assertAlmostEquals( result1.pvec[1],result2.pvec[1] ) |
---|
| 254 | self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) |
---|
| 255 | self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) |
---|
| 256 | self.assertAlmostEquals( result1.fitness,result2.fitness ) |
---|
| 257 | |
---|