Example #1
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)
Example #2
0
    def test_cylinder_fit(self):
        """ Simple cylinder model fit """

        out = Loader().load("cyl_400_20.txt")

        fitter = Fit('bumps')
        # Receives the type of model for the fitting
        from sas.models.CylinderModel import CylinderModel
        model = CylinderModel()
        model.setParam('sldCyl', 1)
        model.setParam('sldSolv', 0)
        model.setParam('scale', 1e-10)

        pars1 = ['length', 'radius', 'scale']
        fitter.set_data(out, 1)
        fitter.set_model(model, 1, pars1, constraints=())
        fitter.select_problem_for_fit(id=1, value=1)
        result1, = fitter.fit()
        #print result1
        #print result1.__dict__

        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)

        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)
Example #3
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)
Example #4
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')

        # Fit
        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_res, 1)
        fitter._engine.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)
Example #5
0
 def _reset_helper(self, path=None, npts=NPTS):
     """
     Set value to fitter and prepare inputs for map function
     """
     for i in range(npts):
         data = Loader().load(path)
         fitter = Fit()
         #create model
         model = CylinderModel()
         model.setParam('scale', 1.0)
         model.setParam('radius', 20.0)
         model.setParam('length', 400.0)
         model.setParam('sldCyl', 4e-006)
         model.setParam('sldSolv', 1e-006)
         model.setParam('background', 0.0)
         for param in model.dispersion.keys():
             model.set_dispersion(param, self.polydisp['gaussian']())
         model.setParam('cyl_phi.width', 10)
         model.setParam('cyl_phi.npts', 3)
         model.setParam('cyl_theta.nsigmas', 10)
         # for 2 data cyl_theta = 60.0 [deg] cyl_phi= 60.0 [deg]
         fitter.set_model(model, i, self.param_to_fit, 
                          self.list_of_constraints)
         #smear data
         current_smearer = smear_selection(data, model)
         import cPickle
         p = cPickle.dumps(current_smearer)
         sm = cPickle.loads(p)
         fitter.set_data(data=data, id=i,
                          smearer=current_smearer, qmin=self.qmin, qmax=self.qmax)
         fitter.select_problem_for_fit(id=i, value=1)
         self.list_of_fitter.append(copy.deepcopy(fitter))
         self.list_of_function.append('fit')
         self.list_of_mapper.append(classMapper)
Example #6
0
 def test_dispersion(self):
     """
         Cylinder fit with dispersion
     """
     alg = 'lm'
     from bumps import fitters
     fitters.FIT_DEFAULT = alg
     #fitters.FIT_OPTIONS[alg].options.update(opts)
     fitters.FIT_OPTIONS[alg].options.update(monitors=[])
     self._dispersion(fitter=Fit('bumps'))
Example #7
0
    def _fit(self, name="bumps"):
        """ return fit result """
        fitter = Fit(name)
        fitter.set_data(self.data,1)
        fitter.set_model(self.model,1,self.pars1)
        fitter.select_problem_for_fit(id=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)

        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.0)/3.0   < result1.stderr[2] )
        self.assertTrue( result1.fitness < 1.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)
Example #9
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('bumps')
        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)
Example #10
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')
Example #11
0
 def test_constrained_bumps(self):
     """ Simultaneous cylinder model fit  """
     self._run_fit(Fit('bumps'))
Example #12
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"
Example #13
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)
Example #14
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)