""" Unit tests for fitting module """ import unittest from sans.guitools.plottables import Theory1D from sans.guitools.plottables import Data1D from sans.fit.AbstractFitEngine import Data, Model import math from sans.fit.Fitting import Fit from DataLoader.loader import Loader class testFitModule(unittest.TestCase): """ test fitting """ def test_park(self): """ Simple cylinder model fit (park) """ out=Loader().load("cyl_400_20.txt") data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) fitter = Fit('park') # Receives the type of model for the fitting from sans.models.CylinderModel import CylinderModel model1 = CylinderModel() model1.setParam('contrast', 1) data = Data(sans_data=data1) model = Model(model1) pars1 =['length','radius','scale'] fitter.set_data(data,1) model.set( scale=1e-10 ) fitter.set_model(model,1,pars1) fitter.select_problem_for_fit(Uid=1,value=1) result1 = fitter.fit() self.assert_(result1) self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) #print result1.pvec[0]-400.0, result1.pvec[0] #print math.fabs(result1.pvec[0]-400.0)/3.0 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) self.assertTrue( math.fabs(result1.pvec[2]-9.0e-12)/3.0 < result1.stderr[2] ) self.assertTrue( result1.fitness < 1.0 )