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