class TestConstant(unittest.TestCase): """Unit tests for Cos(x) function""" def setUp(self): from sas.models.Constant import Constant self.const = Constant() self.const.setParam('value', 56.1) def test1D(self): self.assertEqual(self.const.run(1.13), 56.1) def test2D(self): self.assertEqual(self.const.run([1.13, 0.56]), 56.1) self.assertEqual(self.const.runXY([1.13, 0.56]), 56.1) def testFunction(self): # version 0.5.0: No longer supported return from sas.models.Sin import Sin s = Sin() A = self.const A.setParam('Value', 1.5) B = self.const.clone() B.setParam('value', 2.0) C = self.const.clone() C.setParam('value', 3.0) f = A + B * s * s + C answer = 1.5 + 2.0 * math.sin(1.1)**2 + 3.0 self.assertEqual(f.run(1.1), answer)
class TestConstant(unittest.TestCase): """Unit tests for Cos(x) function""" def setUp(self): from sas.models.Constant import Constant self.const = Constant() self.const.setParam('value',56.1) def test1D(self): self.assertEqual(self.const.run(1.13), 56.1) def test2D(self): self.assertEqual(self.const.run([1.13,0.56]), 56.1) self.assertEqual(self.const.runXY([1.13,0.56]), 56.1) def testFunction(self): # version 0.5.0: No longer supported return from sas.models.Sin import Sin s = Sin() A = self.const A.setParam('Value',1.5) B = self.const.clone() B.setParam('value',2.0) C = self.const.clone() C.setParam('value',3.0) f = A+B*s*s+C answer = 1.5+2.0*math.sin(1.1)**2+3.0 self.assertEqual(f.run(1.1), answer)
def test_constraints(self): """ fit 2 data and 2 model with 1 constrainst""" #load data l = Loader() data1= l.load("testdata_line.txt") data1.name = data1.filename data2= l.load("testdata_cst.txt") data2.name = data2.filename # Receives the type of model for the fitting model11 = LineModel() model11.name= "line" model11.setParam("A", 1.0) model11.setParam("B",1.0) model22 = Constant() model22.name= "cst" model22.setParam("value", 1.0) model1 = Model(model11,data1) model2 = Model(model22,data2) model1.set(A=4) model1.set(B=3) # Constraint the constant value to be equal to parameter B (the real value is 2.5) #model2.set(value='line.B') pars1= ['A','B'] pars2= ['value'] #Importing the Fit module fitter = Fit() fitter.set_data(data1,1) fitter.set_model(model1,1,pars1) fitter.set_data(data2,2,smearer=None) fitter.set_model(model2,2,pars2,constraints=[("value","line.B")]) fitter.select_problem_for_fit(id=1,value=1) fitter.select_problem_for_fit(id=2,value=1) R1,R2 = fitter.fit(handler=FitHandler()) self.assertTrue( math.fabs(R1.pvec[0]-4.0)/3. <= R1.stderr[0]) self.assertTrue( math.fabs(R1.pvec[1]-2.5)/3. <= R1.stderr[1]) self.assertTrue( R1.fitness/(len(data1.x)+len(data2.x)) < 2)
def test_constraints(self): """ fit 2 data and 2 model with 1 constrainst""" #load data l = Loader() data1 = l.load("testdata_line.txt") data1.name = data1.filename data2 = l.load("testdata_cst.txt") data2.name = data2.filename # Receives the type of model for the fitting model11 = LineModel() model11.name = "line" model11.setParam("A", 1.0) model11.setParam("B", 1.0) model22 = Constant() model22.name = "cst" model22.setParam("value", 1.0) model1 = Model(model11, data1) model2 = Model(model22, data2) model1.set(A=4) model1.set(B=3) # Constraint the constant value to be equal to parameter B (the real value is 2.5) #model2.set(value='line.B') pars1 = ['A', 'B'] pars2 = ['value'] #Importing the Fit module fitter = Fit('bumps') fitter.set_data(data1, 1) fitter.set_model(model1, 1, pars1) fitter.set_data(data2, 2, smearer=None) fitter.set_model(model2, 2, pars2, constraints=[("value", "line.B")]) fitter.select_problem_for_fit(id=1, value=1) fitter.select_problem_for_fit(id=2, value=1) R1, R2 = fitter.fit(handler=FitHandler()) self.assertTrue(math.fabs(R1.pvec[0] - 4.0) / 3. <= R1.stderr[0]) self.assertTrue(math.fabs(R1.pvec[1] - 2.5) / 3. <= R1.stderr[1]) self.assertTrue(R1.fitness / (len(data1.x) + len(data2.x)) < 2)