Пример #1
0
    def setProfile(self, profile, xname = None, yname = None, dyname = None):
        """Assign the Profile for this FitContribution.

        profile --  A Profile that specifies the calculation points and that
                    will store the calculated signal.
        xname   --  The name of the independent variable from the Profile. If
                    this is None (default), then the name specified by the
                    Profile for this parameter will be used.  This variable is
                    usable within string equations with the specified name.
        yname   --  The name of the observed Profile.  If this is None
                    (default), then the name specified by the Profile for this
                    parameter will be used.  This variable is usable within
                    string equations with the specified name.
        dyname  --  The name of the uncertainty in the observed Profile. If
                    this is None (default), then the name specified by the
                    Profile for this parameter will be used.  This variable is
                    usable within string equations with the specified name.

        """
        # Enforce type of the profile argument
        if not isinstance(profile, Profile):
            emsg = "Argument must be an instance of the Profile class."
            raise TypeError(emsg)

        # Set the Profile and add its parameters to this organizer.
        self.profile = profile

        if xname is None:
            xname = self.profile.xpar.name
        if yname is None:
            yname = self.profile.ypar.name
        if dyname is None:
            dyname = self.profile.dypar.name

        self._xname = xname
        self._yname = yname
        self._dyname = dyname

        xpar = ParameterProxy(xname, self.profile.xpar)
        ypar = ParameterProxy(yname, self.profile.ypar)
        dypar = ParameterProxy(dyname, self.profile.dypar)
        self.addParameter(xpar, check = False)
        self.addParameter(ypar, check = False)
        self.addParameter(dypar, check = False)

        # If we have ProfileGenerators, set their Profiles.
        for gen in self._generators.values():
            gen.setProfile(profile)

        # If we have _eq, but not _reseq, set the residual
        if self._eq is not None and self._reseq is None:
            self.setResidualEquation('chiv')

        return
Пример #2
0
    def addVar(self,
               par,
               value=None,
               name=None,
               fixed=False,
               tag=None,
               tags=[]):
        """Add a variable to be refined.

        par     --  A Parameter that will be varied during a fit.
        value   --  An initial value for the variable. If this is None
                    (default), then the current value of par will be used.
        name    --  A name for this variable. If name is None (default), then
                    the name of the parameter will be used.
        fixed   --  Fix the variable so that it does not vary (default False).
        tag     --  A tag for the variable. This can be used to retrieve, fix
                    or free variables by tag (default None). Note that a
                    variable is automatically tagged with its name and "all".
        tags    --  A list of tags (default []). Both tag and tags can be
                    applied.

        Returns the ParameterProxy (variable) for the passed Parameter.

        Raises ValueError if the name of the variable is already taken by
        another managed object.
        Raises ValueError if par is constant.
        Raises ValueError if par is constrained.
        """
        name = name or par.name

        if par.const:
            raise ValueError("The parameter '%s' is constant" % par)

        if par.constrained:
            raise ValueError("The parameter '%s' is constrained" % par)

        var = ParameterProxy(name, par)
        if value is not None:
            var.setValue(value)

        self._addParameter(var)

        if fixed:
            self.fix(var)

        # Tag with passed tags and by name
        self._tagmanager.tag(var, var.name)
        self._tagmanager.tag(var, "all")
        self._tagmanager.tag(var, *tags)
        if tag is not None:
            self._tagmanager.tag(var, tag)
        return var
Пример #3
0
    def addVar(self, par, value = None, name = None, fixed = False, tag = None,
            tags = []):
        """Add a variable to be refined.

        par     --  A Parameter that will be varied during a fit.
        value   --  An initial value for the variable. If this is None
                    (default), then the current value of par will be used.
        name    --  A name for this variable. If name is None (default), then
                    the name of the parameter will be used.
        fixed   --  Fix the variable so that it does not vary (default False).
        tag     --  A tag for the variable. This can be used to retrieve, fix
                    or free variables by tag (default None). Note that a
                    variable is automatically tagged with its name and "all".
        tags    --  A list of tags (default []). Both tag and tags can be
                    applied.

        Returns the ParameterProxy (variable) for the passed Parameter.

        Raises ValueError if the name of the variable is already taken by
        another managed object.
        Raises ValueError if par is constant.
        Raises ValueError if par is constrained.
        """
        name = name or par.name

        if par.const:
            raise ValueError("The parameter '%s' is constant"%par)

        if par.constrained:
            raise ValueError("The parameter '%s' is constrained"%par)

        var = ParameterProxy(name, par)
        if value is not None:
            var.setValue(value)

        self._addParameter(var)

        if fixed:
            self.fix(var)

        # Tag with passed tags and by name
        self._tagmanager.tag(var, var.name)
        self._tagmanager.tag(var, "all")
        self._tagmanager.tag(var, *tags)
        if tag is not None:
            self._tagmanager.tag(var, tag)
        return var
Пример #4
0
    def __addPar(self, parname, par):
        """Constrain a parameter via proxy with a specified name

        par     --  Parameter to constrain
        idx     --  Index to identify scatterer from which par comes

        """
        newpar = ParameterProxy(parname, par)
        self.addParameter(newpar)
        return newpar
Пример #5
0
    def __init__(self, name, atom):
        """Initialize

        atom    --  A diffpy.Structure.Atom instance

        """
        ParameterSet.__init__(self, name)
        self.atom = atom
        a = atom
        # x, y, z, occupancy
        self.addParameter(
            ParameterAdapter("x", a, _xyzgetter(0), _xyzsetter(0)))
        self.addParameter(
            ParameterAdapter("y", a, _xyzgetter(1), _xyzsetter(1)))
        self.addParameter(
            ParameterAdapter("z", a, _xyzgetter(2), _xyzsetter(2)))
        occupancy = ParameterAdapter("occupancy", a, attr="occupancy")
        self.addParameter(occupancy)
        self.addParameter(ParameterProxy("occ", occupancy))
        # U
        self.addParameter(ParameterAdapter("U11", a, attr="U11"))
        self.addParameter(ParameterAdapter("U22", a, attr="U22"))
        self.addParameter(ParameterAdapter("U33", a, attr="U33"))
        U12 = ParameterAdapter("U12", a, attr="U12")
        U21 = ParameterProxy("U21", U12)
        U13 = ParameterAdapter("U13", a, attr="U13")
        U31 = ParameterProxy("U31", U13)
        U23 = ParameterAdapter("U23", a, attr="U23")
        U32 = ParameterProxy("U32", U23)
        self.addParameter(U12)
        self.addParameter(U21)
        self.addParameter(U13)
        self.addParameter(U31)
        self.addParameter(U23)
        self.addParameter(U32)
        self.addParameter(ParameterAdapter("Uiso", a, attr="Uisoequiv"))
        # B
        self.addParameter(ParameterAdapter("B11", a, attr="B11"))
        self.addParameter(ParameterAdapter("B22", a, attr="B22"))
        self.addParameter(ParameterAdapter("B33", a, attr="B33"))
        B12 = ParameterAdapter("B12", a, attr="B12")
        B21 = ParameterProxy("B21", B12)
        B13 = ParameterAdapter("B13", a, attr="B13")
        B31 = ParameterProxy("B31", B13)
        B23 = ParameterAdapter("B23", a, attr="B23")
        B32 = ParameterProxy("B32", B23)
        self.addParameter(B12)
        self.addParameter(B21)
        self.addParameter(B13)
        self.addParameter(B31)
        self.addParameter(B23)
        self.addParameter(B32)
        self.addParameter(ParameterAdapter("Biso", a, attr="Bisoequiv"))

        # Other setup
        self.__repr__ = a.__repr__
        return
Пример #6
0
    def testProxy(self):
        """Test the ParameterProxy class."""
        l = Parameter("l", 3.14)

        # Try Accessor adaptation
        la = ParameterProxy("l2", l)

        self.assertEqual("l2", la.name)
        self.assertEqual(l.getValue(), la.getValue())

        # Change the parameter
        l.value = 2.3
        self.assertEqual(l.getValue(), la.getValue())
        self.assertEqual(l.value, la.value)

        # Change the proxy
        la.value = 3.2
        self.assertEqual(l.getValue(), la.getValue())
        self.assertEqual(l.value, la.value)

        return
Пример #7
0
    def testProxy(self):
        """Test the ParameterProxy class."""
        l = Parameter("l", 3.14)

        # Try Accessor adaptation
        la = ParameterProxy("l2", l)

        self.assertEqual("l2", la.name)
        self.assertEqual(l.getValue(), la.getValue())

        # Change the parameter
        l.value = 2.3
        self.assertEqual(l.getValue(), la.getValue())
        self.assertEqual(l.value, la.value)

        # Change the proxy
        la.value = 3.2
        self.assertEqual(l.getValue(), la.getValue())
        self.assertEqual(l.value, la.value)

        return