Ejemplo n.º 1
0
    def setUp(self):
        """ initialize data"""
        
        self.data1=Loader().load("cyl_400_20.txt")
        self.data2=Loader().load("cyl_400_40.txt")
    
        # Receives the type of model for the fitting
        from sas.models.CylinderModel import CylinderModel
        cyl1  = CylinderModel()
        cyl1.name = "C1"
        self.model1 = Model(cyl1)
        self.model1.set(scale= 1.0)
        self.model1.set(radius=18)
        self.model1.set(length=200)
        self.model1.set(sldCyl=3e-006, sldSolv=0.0)
        self.model1.set(background=0.0)

        cyl2  = CylinderModel()
        cyl2.name = "C2"
        self.model2 = Model(cyl2)
        self.model2.set(scale= 1.0)
        self.model2.set(radius=37)
        self.model2.set(length=300)
        self.model2.set(sldCyl=3e-006, sldSolv=0.0)
        self.model2.set(background=0.0)
Ejemplo n.º 2
0
    def setUp(self):
        """ initialize data"""

        self.data1 = Loader().load("cyl_400_20.txt")
        self.data2 = Loader().load("cyl_400_40.txt")

        # Receives the type of model for the fitting
        from sas.models.CylinderModel import CylinderModel

        cyl1 = CylinderModel()
        cyl1.name = "C1"
        self.model1 = Model(cyl1)
        self.model1.set(scale=1.0)
        self.model1.set(radius=18)
        self.model1.set(length=200)
        self.model1.set(sldCyl=3e-006, sldSolv=0.0)
        self.model1.set(background=0.0)

        cyl2 = CylinderModel()
        cyl2.name = "C2"
        self.model2 = Model(cyl2)
        self.model2.set(scale=1.0)
        self.model2.set(radius=37)
        self.model2.set(length=300)
        self.model2.set(sldCyl=3e-006, sldSolv=0.0)
        self.model2.set(background=0.0)
Ejemplo n.º 3
0
    def test2(self):
        """ fit 2 data and 2 model with no constrainst"""
        #load data
        l = Loader()
        data1 = l.load("testdata_line.txt")
        data1.name = data1.filename

        data2 = l.load("testdata_line1.txt")
        data2.name = data2.filename

        #Importing the Fit module
        fitter = Fit('bumps')
        # Receives the type of model for the fitting
        model11 = LineModel()
        model11.name = "M1"
        model22 = LineModel()
        model11.name = "M2"

        model1 = Model(model11, data1)
        model2 = Model(model22, data2)
        pars1 = ['A', 'B']
        fitter.set_data(data1, 1)
        fitter.set_model(model1, 1, pars1)
        fitter.select_problem_for_fit(id=1, value=0)
        fitter.set_data(data2, 2)
        fitter.set_model(model2, 2, pars1)
        fitter.select_problem_for_fit(id=2, value=0)

        try:
            result1, = fitter.fit(handler=FitHandler())
        except RuntimeError, msg:
            assert str(msg) == "Nothing to fit"
Ejemplo n.º 4
0
    def fit_single(self, fitter_name, isdream=False):
        fitter = Fit(fitter_name)

        data = Loader().load("testdata_line.txt")
        data.name = data.filename
        fitter.set_data(data, 1)

        # Receives the type of model for the fitting
        model1 = LineModel()
        model1.name = "M1"
        model = Model(model1, data)

        pars1 = ['A', 'B']
        fitter.set_model(model, 1, pars1)
        fitter.select_problem_for_fit(id=1, value=1)
        result1, = fitter.fit(handler=FitHandler())

        # The target values were generated from the following statements
        p, s, fx = result1.pvec, result1.stderr, result1.fitness
        #print "p0,p1,s0,s1,fx = %g, %g, %g, %g, %g"%(p[0],p[1],s[0],s[1],fx)
        p0, p1, s0, s1, fx_ = 3.68353, 2.61004, 0.336186, 0.105244, 1.20189

        if isdream:
            # Dream is not a minimizer: just check that the fit is within
            # uncertainty
            self.assertTrue(abs(p[0] - p0) <= s0)
            self.assertTrue(abs(p[1] - p1) <= s1)
        else:
            self.assertTrue(abs(p[0] - p0) <= 1e-5)
            self.assertTrue(abs(p[1] - p1) <= 1e-5)
            self.assertTrue(abs(fx - fx_) <= 1e-5)
Ejemplo n.º 5
0
    def test_without_resolution(self):
        """ Simple cylinder model fit  """
        
        out=Loader().load("cyl_400_20.txt")
        # This data file has not error, add them
        #out.dy = out.y
        
        fitter = Fit()
        fitter.set_data(out,1)
        
        # Receives the type of model for the fitting
        model1  = CylinderModel()
        model1.setParam("scale", 1.0)
        model1.setParam("radius",18)
        model1.setParam("length", 397)
        model1.setParam("sldCyl",3e-006 )
        model1.setParam("sldSolv",0.0 )
        model1.setParam("background", 0.0)
        model = Model(model1)
        pars1 =['length','radius','scale']
        fitter.set_model(model,1,pars1)
        
        # What the hell is this line for?
        fitter.select_problem_for_fit(id=1,value=1)
        result1, = fitter.fit()
        #print "result1",result1

        self.assert_(result1)
        self.assertTrue(len(result1.pvec) > 0)
        self.assertTrue(len(result1.stderr) > 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]-1)/3.0   < result1.stderr[2] )
        self.assertTrue( result1.fitness < 1.0 )
Ejemplo n.º 6
0
    def test4(self):
        """ fit 2 data concatenates with limited range of x and  one model """
        #load data
        l = Loader()
        data1 = l.load("testdata_line.txt")
        data1.name = data1.filename
        data2 = l.load("testdata_line1.txt")
        data2.name = data2.filename

        # Receives the type of model for the fitting
        model1 = LineModel()
        model1.name = "M1"
        model1.setParam("A", 1.0)
        model1.setParam("B", 1.0)
        model = Model(model1, data1)

        pars1 = ['A', 'B']
        #Importing the Fit module

        fitter = Fit('bumps')
        fitter.set_data(data1, 1, qmin=0, qmax=7)
        fitter.set_model(model, 1, pars1)
        fitter.set_data(data2, 1, qmin=1, qmax=10)
        fitter.select_problem_for_fit(id=1, value=1)
        result2, = fitter.fit(handler=FitHandler())

        self.assert_(result2)
        self.assertTrue(
            math.fabs(result2.pvec[0] - 4) / 3 <= result2.stderr[0])
        self.assertTrue(
            math.fabs(result2.pvec[1] - 2.5) / 3 <= result2.stderr[1])
        self.assertTrue(result2.fitness / len(data1.x) < 2)
Ejemplo n.º 7
0
    def test_slit(self):
        smear = smear_selection(self.data_slit)
        #self.assertEqual(smear.__class__.__name__, 'SlitSmearer')
        #self.assertEqual(smear.__class__.__name__, 'PySmearer')

        fitter = Fit()
        
        # Data: right now this is the only way to set the smearer object
        # We should improve that and have a way to get access to the
        # data for a given fit.
        fitter.set_data(self.data_slit,1)
        fitter.fit_arrange_dict[1].data_list[0].smearer = smear
        fitter.fit_arrange_dict[1].data_list[0].qmax = 0.003
        
        # Model
        fitter.set_model(Model(self.sphere),1, ['radius','scale'])
        fitter.select_problem_for_fit(id=1,value=1)
        
        result1, = fitter.fit()

        #print "v",result1.pvec
        #print "dv",result1.stderr
        #print "chisq(v)",result1.fitness

        numpy.testing.assert_allclose(result1.pvec, [2323.466,0.22137], rtol=0.001)
Ejemplo n.º 8
0
    def test_reso(self):

        # Let the data module find out what smearing the
        # data needs
        smear = smear_selection(self.data_res)
        #self.assertEqual(smear.__class__.__name__, 'QSmearer')
        #self.assertEqual(smear.__class__.__name__, 'PySmearer')

        # Fit
        fitter = Fit()
        
        # Data: right now this is the only way to set the smearer object
        # We should improve that and have a way to get access to the
        # data for a given fit.
        fitter.set_data(self.data_res,1)
        fitter.fit_arrange_dict[1].data_list[0].smearer = smear

        # Model: maybe there's a better way to do this.
        # Ideally we should have to create a new model from our sas model.
        fitter.set_model(Model(self.sphere),1, ['radius','scale', 'background'])
        
        # Why do we have to do this...?
        fitter.select_problem_for_fit(id=1,value=1)

        # Perform the fit (might take a while)
        result1, = fitter.fit()
        
        #print "v",result1.pvec
        #print "dv",result1.stderr
        #print "chisq(v)",result1.fitness

        self.assertTrue( math.fabs(result1.pvec[0]-5000) < 20 )
        self.assertTrue( math.fabs(result1.pvec[1]-0.48) < 0.02 )
        self.assertTrue( math.fabs(result1.pvec[2]-0.060)  < 0.002 )
Ejemplo n.º 9
0
    def test_slit(self):
        smear = smear_selection(self.data_slit)
        self.assertEqual(smear.__class__.__name__, 'SlitSmearer')

        fitter = Fit('bumps')

        # Data: right now this is the only way to set the smearer object
        # We should improve that and have a way to get access to the
        # data for a given fit.
        fitter.set_data(self.data_slit, 1)
        fitter._engine.fit_arrange_dict[1].data_list[0].smearer = smear
        fitter._engine.fit_arrange_dict[1].data_list[0].qmax = 0.003

        # Model
        fitter.set_model(Model(self.sphere), 1, ['radius', 'scale'])
        fitter.select_problem_for_fit(id=1, value=1)

        result1, = fitter.fit()

        #print "v",result1.pvec
        #print "dv",result1.stderr
        #print "chisq(v)",result1.fitness

        self.assertTrue(math.fabs(result1.pvec[0] - 2340) < 20)
        self.assertTrue(math.fabs(result1.pvec[1] - 0.010) < 0.002)
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    def test_bad_pars(self):
        fitter = Fit('bumps')

        data = Loader().load("testdata_line.txt")
        data.name = data.filename
        fitter.set_data(data, 1)

        model1 = LineModel()
        model1.name = "M1"
        model = Model(model1, data)
        pars1 = ['param1', 'param2']
        try:
            fitter.set_model(model, 1, pars1)
        except ValueError, exc:
            #print "ValueError was correctly raised: "+str(msg)
            assert str(exc).startswith('parameter param1')
Ejemplo n.º 13
0
    def _dispersion(self, fitter):
        # Load data
        # This data is for a cylinder with
        #   length=400, radius=20, radius disp=5, scale=1e-10
        out = Loader().load("cyl_400_20_disp5r.txt")
        out.dy = numpy.zeros(len(out.y))
        for i in range(len(out.y)):
            out.dy[i] = math.sqrt(out.y[i])

        # Receives the type of model for the fitting
        model1 = CylinderModel()
        model1.setParam("scale", 10.0)
        model1.setParam("radius", 18)
        model1.setParam("length", 397)
        model1.setParam("sldCyl", 3e-006)
        model1.setParam("sldSolv", 0.0)
        model1.setParam("background", 0.0)

        # Dispersion parameters
        model1.dispersion['radius']['width'] = 0.25
        model1.dispersion['radius']['npts'] = 50

        model = Model(model1)

        pars1 = ['length', 'radius', 'scale', 'radius.width']
        fitter.set_data(out, 1)
        fitter.set_model(model, 1, pars1)
        fitter.select_problem_for_fit(id=1, value=1)
        #import time; T0 = time.time()
        result1, = fitter.fit()
        #print "time",time.time()-T0,fitter._engine.__class__.__name__

        self.assert_(result1)
        self.assertTrue(len(result1.pvec) > 0)
        self.assertTrue(len(result1.stderr) > 0)

        #print [z for z in zip(result1.param_list,result1.pvec,result1.stderr)]
        self.assertTrue(
            math.fabs(result1.pvec[0] - 399.8) / 3.0 < result1.stderr[0])
        self.assertTrue(
            math.fabs(result1.pvec[1] - 17.5) / 3.0 < result1.stderr[1])
        self.assertTrue(
            math.fabs(result1.pvec[2] - 11.1) / 3.0 < result1.stderr[2])
        self.assertTrue(
            math.fabs(result1.pvec[3] - 0.276) / 3.0 < result1.stderr[3])
        self.assertTrue(result1.fitness < 1.0)
Ejemplo n.º 14
0
    def test_cyl_times_square(self):
        """ Simple cylinder model fit  """

        out = Loader().load("cyl_400_20.txt")
        data = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.dy)
        # Receives the type of model for the fitting
        model1 = MultiplicationModel(CylinderModel(), SquareWellStructure())
        model1.setParam('background', 0.0)
        model1.setParam('sldCyl', 3e-006)
        model1.setParam('sldSolv', 0.0)
        model1.setParam('length', 420)
        model1.setParam('radius', 40)
        model1.setParam('scale_factor', 2)
        model1.setParam('volfraction', 0.04)
        model1.setParam('welldepth', 1.5)
        model1.setParam('wellwidth', 1.2)

        model = Model(model1)

        pars1 = ['length', 'radius', 'scale_factor']
        fitter = Fit('bumps')
        fitter.set_data(data, 1)
        fitter.set_model(model, 1, pars1)
        fitter.select_problem_for_fit(id=1, value=1)
        result1, = fitter.fit()

        self.assert_(result1)
        self.assertTrue(len(result1.pvec) >= 0)
        self.assertTrue(len(result1.stderr) >= 0)

        #print "results",list(zip(result1.pvec, result1.stderr))
        self.assertTrue(
            math.fabs(result1.pvec[0] - 612) / 3.0 <= result1.stderr[0])
        self.assertTrue(
            math.fabs(result1.pvec[1] - 20.3) / 3.0 <= result1.stderr[1])
        self.assertTrue(
            math.fabs(result1.pvec[2] - 25) / 3.0 <= result1.stderr[2])

        self.assertTrue(result1.fitness / len(data.x) < 1.0)
Ejemplo n.º 15
0
class TestSimultaneousFit(unittest.TestCase):
    """ test simultaneous fitting """
    def setUp(self):
        """ initialize data"""
        
        self.data1=Loader().load("cyl_400_20.txt")
        self.data2=Loader().load("cyl_400_40.txt")
    
        # Receives the type of model for the fitting
        from sas.models.CylinderModel import CylinderModel
        cyl1  = CylinderModel()
        cyl1.name = "C1"
        self.model1 = Model(cyl1)
        self.model1.set(scale= 1.0)
        self.model1.set(radius=18)
        self.model1.set(length=200)
        self.model1.set(sldCyl=3e-006, sldSolv=0.0)
        self.model1.set(background=0.0)

        cyl2  = CylinderModel()
        cyl2.name = "C2"
        self.model2 = Model(cyl2)
        self.model2.set(scale= 1.0)
        self.model2.set(radius=37)
        self.model2.set(length=300)
        self.model2.set(sldCyl=3e-006, sldSolv=0.0)
        self.model2.set(background=0.0)


    def test_constrained_bumps(self):
        """ Simultaneous cylinder model fit  """
        self._run_fit(Fit('bumps'))

    #@unittest.skip("")
    def _run_fit(self, fitter):
        result1, result2 = self._fit(fitter)
        self.assert_(result1)
        self.assertTrue(len(result1.pvec)>0)
        self.assertTrue(len(result1.stderr)>0)

        for n, v, dv in zip(result1.param_list, result1.pvec, result1.stderr):
            #print "%s M1.%s = %s +/- %s"%(fitter._engine.__class__.__name__,n,v,dv)
            if n == "length":
                self.assertTrue( math.fabs(v-400.0)/3.0 < dv )
            elif n=='radius':
                self.assertTrue( math.fabs(v-20.0)/3.0 < dv )
            elif n=='scale':
                self.assertTrue( math.fabs(v-1.0)/3.0 < dv )
        for n, v, dv in zip(result2.param_list, result2.pvec, result2.stderr):
            #print "%s M2.%s = %s +/- %s"%(fitter._engine.__class__.__name__,n,v,dv)
            if n=='radius':
                self.assertTrue( math.fabs(v-40.0)/3.0 < dv )
            elif n=='scale':
                self.assertTrue( math.fabs(v-1.0)/3.0 < dv )

    def _fit(self, fitter):
        """ return fit result """
        fitter.set_data(self.data1,1)
        fitter.set_model(self.model1, 1, ['length','radius','scale'])

        fitter.set_data(self.data2,2)
        fitter.set_model(self.model2, 2, ['length','radius','scale'],
                         constraints=[("length","C1.length")])
        fitter.select_problem_for_fit(id=1,value=1)
        fitter.select_problem_for_fit(id=2,value=1)
        return fitter.fit()
Ejemplo n.º 16
0
class TestSimultaneousFit(unittest.TestCase):
    """ test simultaneous fitting """

    def setUp(self):
        """ initialize data"""

        self.data1 = Loader().load("cyl_400_20.txt")
        self.data2 = Loader().load("cyl_400_40.txt")

        # Receives the type of model for the fitting
        from sas.models.CylinderModel import CylinderModel

        cyl1 = CylinderModel()
        cyl1.name = "C1"
        self.model1 = Model(cyl1)
        self.model1.set(scale=1.0)
        self.model1.set(radius=18)
        self.model1.set(length=200)
        self.model1.set(sldCyl=3e-006, sldSolv=0.0)
        self.model1.set(background=0.0)

        cyl2 = CylinderModel()
        cyl2.name = "C2"
        self.model2 = Model(cyl2)
        self.model2.set(scale=1.0)
        self.model2.set(radius=37)
        self.model2.set(length=300)
        self.model2.set(sldCyl=3e-006, sldSolv=0.0)
        self.model2.set(background=0.0)

    def test_constrained_bumps(self):
        """ Simultaneous cylinder model fit  """
        self._run_fit(Fit())

    # @unittest.skip("")
    def _run_fit(self, fitter):
        result1, result2 = self._fit(fitter)
        self.assert_(result1)
        self.assertTrue(len(result1.pvec) > 0)
        self.assertTrue(len(result1.stderr) > 0)

        for n, v, dv in zip(result1.param_list, result1.pvec, result1.stderr):
            if n == "length":
                self.assertTrue(math.fabs(v - 400.0) / 3.0 < dv)
            elif n == "radius":
                self.assertTrue(math.fabs(v - 20.0) / 3.0 < dv)
            elif n == "scale":
                self.assertTrue(math.fabs(v - 1.0) / 3.0 < dv)
        for n, v, dv in zip(result2.param_list, result2.pvec, result2.stderr):
            if n == "radius":
                self.assertTrue(math.fabs(v - 40.0) / 3.0 < dv)
            elif n == "scale":
                self.assertTrue(math.fabs(v - 1.0) / 3.0 < dv)

    def _fit(self, fitter):
        """ return fit result """
        fitter.set_data(self.data1, 1)
        fitter.set_model(self.model1, 1, ["length", "radius", "scale"])

        fitter.set_data(self.data2, 2)
        fitter.set_model(self.model2, 2, ["length", "radius", "scale"], constraints=[("length", "C1.length")])
        fitter.select_problem_for_fit(id=1, value=1)
        fitter.select_problem_for_fit(id=2, value=1)
        return fitter.fit()