[20d30e9] | 1 | """ |
---|
| 2 | Unit tests for fitting module |
---|
[eb575b0] | 3 | @author: G. Alina |
---|
[20d30e9] | 4 | """ |
---|
| 5 | import unittest |
---|
| 6 | |
---|
| 7 | from sans.guiframe.dataFitting import Data1D |
---|
[1ea3488] | 8 | from sans.fit.AbstractFitEngine import Model,FitData1D |
---|
[20d30e9] | 9 | import math |
---|
| 10 | from sans.fit.Fitting import Fit |
---|
| 11 | from DataLoader.loader import Loader |
---|
| 12 | |
---|
| 13 | class testFitModule(unittest.TestCase): |
---|
| 14 | """ test fitting """ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | def test_scipy(self): |
---|
| 18 | """ Simple cylinder model fit (scipy) """ |
---|
| 19 | |
---|
[2eaaf1a] | 20 | out=Loader().load("cyl_400_20.txt") |
---|
| 21 | data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.dy) |
---|
| 22 | data = FitData1D(data1) |
---|
[20d30e9] | 23 | |
---|
| 24 | # Receives the type of model for the fitting |
---|
| 25 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
| 26 | from sans.models.CylinderModel import CylinderModel |
---|
| 27 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
| 28 | model1 = MultiplicationModel(CylinderModel(),SquareWellStructure()) |
---|
[2eaaf1a] | 29 | model1.setParam('background', 0.0) |
---|
| 30 | model1.setParam('contrast', 3e-006) |
---|
| 31 | model1.setParam('length', 600) |
---|
| 32 | model1.setParam('radius', 20) |
---|
| 33 | model1.setParam('scale', 10) |
---|
| 34 | model1.setParam('volfraction', 0.04) |
---|
| 35 | model1.setParam('welldepth', 1.5) |
---|
| 36 | model1.setParam('wellwidth', 1.2) |
---|
| 37 | |
---|
[20d30e9] | 38 | model = Model(model1) |
---|
[2eaaf1a] | 39 | |
---|
[20d30e9] | 40 | pars1 =['length','radius','scale'] |
---|
[2eaaf1a] | 41 | fitter = Fit('scipy') |
---|
[20d30e9] | 42 | fitter.set_data(data,1) |
---|
| 43 | fitter.set_model(model,1,pars1) |
---|
| 44 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
| 45 | result1 = fitter.fit() |
---|
[2eaaf1a] | 46 | |
---|
[20d30e9] | 47 | self.assert_(result1) |
---|
[2eaaf1a] | 48 | self.assertTrue(len(result1.pvec)>=0 ) |
---|
| 49 | self.assertTrue(len(result1.stderr)>= 0) |
---|
| 50 | |
---|
| 51 | self.assertTrue( math.fabs(result1.pvec[0]-605)/3.0 <= result1.stderr[0] ) |
---|
| 52 | self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 <= result1.stderr[1] ) |
---|
| 53 | self.assertTrue( math.fabs(result1.pvec[2]-1)/3.0 <= result1.stderr[2] ) |
---|
[20d30e9] | 54 | |
---|
[2eaaf1a] | 55 | self.assertTrue( result1.fitness/len(data.x) < 1.0 ) |
---|
[20d30e9] | 56 | |
---|
| 57 | |
---|