Esempio n. 1
0
    def testAccessors(self):
        """Test accessors."""
        m1 = self.m
        p1 = Parameter("p1", 1)
        m1._addObject(p1, m1._parameters)

        m2 = RecipeContainer("m2")
        p2 = Parameter("p2", 2)
        m2._addObject(p2, m2._parameters)

        m1._addObject(m2, m1._containers)

        self.assertTrue(m1.m2 is m2)
        self.assertTrue(m1.p1 is p1)
        self.assertTrue(m2.p2 is p2)
        self.assertTrue(m1.m2.p2 is p2)

        self.assertTrue(m1[0] is p1)
        self.assertTrue(m1[0:] == [
            p1,
        ])
        self.assertTrue(m2[0] is p2)

        self.assertEqual(1, len(m1))
        self.assertEqual(1, len(m2))
        return
Esempio n. 2
0
    def testGetRestraints(self):
        """Test the _getRestraints method."""
        m2 = RecipeOrganizer("m2")
        self.m._organizers = {}
        self.m._manage(self.m._organizers)
        self.m._addObject(m2, self.m._organizers)

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)
        p3 = Parameter("p3", 3)
        p4 = Parameter("p4", 4)

        self.m._addParameter(p1)
        self.m._addParameter(p2)

        m2._addParameter(p3)
        m2._addParameter(p4)

        r1 = self.m.restrain("p1 + p2")
        r2 = m2.restrain("2*p3 + p4")

        res = self.m._getRestraints()
        self.assertTrue(r1 in res)
        self.assertTrue(r2 in res)
        self.assertEqual(2, len(res))
        return
Esempio n. 3
0
    def testGetConstraints(self):
        """Test the _getConstraints method."""
        m2 = RecipeOrganizer("m2")
        self.m._organizers = {}
        self.m._manage(self.m._organizers)
        self.m._addObject(m2, self.m._organizers)

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)
        p3 = Parameter("p3", 3)
        p4 = Parameter("p4", 4)

        self.m._addParameter(p1)
        self.m._addParameter(p2)

        m2._addParameter(p3)
        m2._addParameter(p4)

        self.m.constrain(p1, "p2")
        m2.constrain(p3, "p4")

        cons = self.m._getConstraints()
        self.assertTrue(p1 in cons)
        self.assertTrue(p3 in cons)
        self.assertEqual(2, len(cons))
        return
Esempio n. 4
0
    def testRestrain(self):
        """Test the restrain method."""

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)
        p3 = Parameter("p3", 3)
        self.m._eqfactory.registerArgument("p1", p1)
        self.m._eqfactory.registerArgument("p2", p2)

        self.assertEqual(0, len(self.m._restraints))
        r = self.m.restrain("p1+p2", ub=10)
        self.assertEqual(1, len(self.m._restraints))
        p2.setValue(10)
        self.assertEqual(1, r.penalty())
        self.m.unrestrain(r)
        self.assertEqual(0, len(self.m._restraints))

        r = self.m.restrain(p1, ub=10)
        self.assertEqual(1, len(self.m._restraints))
        p1.setValue(11)
        self.assertEqual(1, r.penalty())

        # Check errors on unregistered parameters
        self.assertRaises(ValueError, self.m.restrain, "2*p3")
        self.assertRaises(ValueError, self.m.restrain, "2*p2", ns={"p2": p3})
        return
Esempio n. 5
0
def bound_range(variable: Parameter, bound: tp.Union[tp.Tuple, tp.Dict], ratio: bool = False) -> Parameter:
    """Bound variable by range."""
    value = variable.getValue()
    if isinstance(bound, dict):
        if ratio:
            for k, r in bound.items():
                bound[k] = value * r
        variable.boundRange(**bound)
    else:
        if ratio:
            bound = tuple((r * value for r in bound))
        variable.boundRange(*bound)
    return variable
Esempio n. 6
0
    def __init__(self, name, model, parname=None):
        """Create the Parameter.

        name    --  Name of the Parameter
        model   --  The BaseModel to which the underlying parameter belongs
        parname --  Name of parameter used by the model. If this is None
                    (default), then name is used.

        """
        self._parname = parname or name
        val = model.getParam(self._parname)
        self._model = model
        Parameter.__init__(self, name, val)
        return
Esempio n. 7
0
    def __init__(self, name, model, parname = None):
        """Create the Parameter.

        name    --  Name of the Parameter
        model   --  The BaseModel to which the underlying parameter belongs
        parname --  Name of parameter used by the model. If this is None
                    (default), then name is used.

        """
        self._parname = parname or name
        val = model.getParam(self._parname)
        self._model = model
        Parameter.__init__(self, name, val)
        return
Esempio n. 8
0
    def testAddParameterSet(self):
        """Test the addParameterSet method."""
        parset2 = ParameterSet("parset2")
        p1 = Parameter("parset2", 1)

        self.parset.addParameterSet(parset2)
        self.assertTrue(self.parset.parset2 is parset2)

        self.assertRaises(ValueError, self.parset.addParameterSet, p1)

        p1.name = "p1"
        parset2.addParameter(p1)

        self.assertTrue(self.parset.parset2.p1 is p1)

        return
Esempio n. 9
0
    def testAddParameterSet(self):
        """Test the addParameterSet method."""
        parset2 = ParameterSet("parset2")
        p1 = Parameter("parset2", 1)

        self.parset.addParameterSet(parset2)
        self.assertTrue(self.parset.parset2 is parset2)

        self.assertRaises(ValueError, self.parset.addParameterSet, p1)

        p1.name = "p1"
        parset2.addParameter(p1)

        self.assertTrue(self.parset.parset2.p1 is p1)

        return
Esempio n. 10
0
    def __init__(self):
        """Initialize the attributes."""
        Observable.__init__(self)
        self._xobs = None
        self._yobs = None
        self._dyobs = None
        self.xpar = Parameter("x")
        self.ypar = Parameter("y")
        self.dypar = Parameter("dy")
        self.ycpar = Parameter("ycalc")
        self.meta = {}

        # Observable
        self.xpar.addObserver(self._flush)
        self.ypar.addObserver(self._flush)
        self.dypar.addObserver(self._flush)
        return
Esempio n. 11
0
    def testEquationFromString(self):
        """Test the equationFromString method."""

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)
        p3 = Parameter("p3", 3)
        p4 = Parameter("p4", 4)

        factory = EquationFactory()

        factory.registerArgument("p1", p1)
        factory.registerArgument("p2", p2)

        # Check usage where all parameters are registered with the factory
        eq = equationFromString("p1+p2", factory)

        self.assertEqual(2, len(eq.args))
        self.assertTrue(p1 in eq.args)
        self.assertTrue(p2 in eq.args)
        self.assertEqual(3, eq())

        # Try to use a parameter that is not registered
        self.assertRaises(ValueError, equationFromString, "p1+p2+p3", factory)

        # Pass that argument in the ns dictionary
        eq = equationFromString("p1+p2+p3", factory, {"p3": p3})
        self.assertEqual(3, len(eq.args))
        self.assertTrue(p1 in eq.args)
        self.assertTrue(p2 in eq.args)
        self.assertTrue(p3 in eq.args)
        self.assertEqual(6, eq())

        # Make sure that there are no remnants of p3 in the factory
        self.assertTrue("p3" not in factory.builders)

        # Pass and use an unregistered parameter
        self.assertRaises(ValueError, equationFromString, "p1+p2+p3+p4",
                          factory, {"p3": p3})

        # Try to overload a registered parameter
        self.assertRaises(ValueError, equationFromString, "p1+p2", factory,
                          {"p2": p4})

        return
Esempio n. 12
0
    def _newParameter(self, name, value, check=True):
        """Add a new Parameter to the container.

        This creates a new Parameter and adds it to the container using the
        _addParameter method.

        Returns the Parameter.
        """
        p = Parameter(name, value)
        self._addParameter(p, check)
        return p
Esempio n. 13
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
    def testRestrain(self):
        """Test the restrain method."""

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)
        p3 = Parameter("p3", 3)
        self.m._eqfactory.registerArgument("p1", p1)
        self.m._eqfactory.registerArgument("p2", p2)

        self.assertEquals(0, len(self.m._restraints))
        r = self.m.restrain("p1+p2", ub = 10)
        self.assertEquals(1, len(self.m._restraints))
        p2.setValue(10)
        self.assertEquals(1, r.penalty())
        self.m.unrestrain(r)
        self.assertEquals(0, len(self.m._restraints))

        r = self.m.restrain(p1, ub = 10)
        self.assertEquals(1, len(self.m._restraints))
        p1.setValue(11)
        self.assertEquals(1, r.penalty())

        # Check errors on unregistered parameters
        self.assertRaises(ValueError, self.m.restrain, "2*p3")
        self.assertRaises(ValueError, self.m.restrain, "2*p2", ns = {"p2":p3})
        return
Esempio n. 15
0
    def testLocateManagedObject(self):
        """Test the locateManagedObject method."""
        m1 = self.m
        p1 = Parameter("p1", 1)
        m1._addObject(p1, m1._parameters)

        m2 = RecipeContainer("m2")
        p2 = Parameter("p2", 2)
        m2._addObject(p2, m2._parameters)

        m1._addObject(m2, m1._containers)

        p3 = Parameter("p3", 3)

        # Locate m2 in m1 (m1.m2)
        loc = m1._locateManagedObject(m2)
        self.assertEqual(loc, [m1, m2])

        # Locate p1 (m1.p1)
        loc = m1._locateManagedObject(p1)
        self.assertEqual(loc, [m1, p1])

        # Locate p2 in m2 (m2.p2)
        loc = m2._locateManagedObject(p2)
        self.assertEqual(loc, [m2, p2])

        # Locate p2 in m1 (m1.m2.p2)
        loc = m1._locateManagedObject(p2)
        self.assertEqual(loc, [m1, m2, p2])

        # Locate p3 in m1 (not there)
        loc = m1._locateManagedObject(p3)
        self.assertEqual(loc, [])

        # Locate p3 in m2 (not there)
        loc = m2._locateManagedObject(p3)
        self.assertEqual(loc, [])

        return
Esempio n. 16
0
    def testAddParameter(self):
        """Test the addParameter method."""

        m = self.m

        p1 = Parameter("p1", 1)
        p2 = Parameter("p1", 2)

        # Check normal insert
        m._addParameter(p1)
        self.assertTrue(m.p1 is p1)
        self.assertTrue(p1.name in m._eqfactory.builders)

        # Try to insert another parameter with the same name
        self.assertRaises(ValueError, m._addParameter, p2)

        # Now allow this
        m._addParameter(p2, check=False)
        self.assertTrue(m.p1 is p2)
        self.assertTrue(p1.name in m._eqfactory.builders)

        # Try to insert a Parameter when a RecipeContainer with the same name
        # is already inside.
        c = RecipeContainer("test")
        m._addObject(c, m._containers)

        p3 = Parameter("test", 0)
        self.assertRaises(ValueError, m._addParameter, p3)

        p4 = Parameter("xyz", 0)
        m._addParameter(p4)

        # Check order
        self.assertEqual(m._parameters.keys(), ["p1", "xyz"])
        self.assertEqual(m._parameters.values(), [p2, p4])

        return
Esempio n. 17
0
    def testNewParameter(self):
        """Test the addParameter method."""

        m = self.m

        p1 = Parameter("p1", 1)
        m._addParameter(p1)

        # Test duplication of Parameters
        self.assertRaises(ValueError, m._newParameter, "p1", 0)

        # Add a new Parameter
        p2 = m._newParameter("p2", 0)
        self.assertTrue(p2 is m.p2)
        return
Esempio n. 18
0
    def testRemoveParameter(self):
        """Test removeParameter method."""

        m = self.m

        p1 = Parameter("p1", 1)
        p2 = Parameter("p1", 2)

        m._addParameter(p1)

        # Check for bad remove
        self.assertRaises(ValueError, m._removeParameter, p2)

        # Remove p1
        m._removeParameter(p1)
        self.assertTrue(p1.name not in m._eqfactory.builders)

        # Try to remove it again
        self.assertRaises(ValueError, m._removeParameter, p1)

        # Try to remove a RecipeContainer
        c = RecipeContainer("test")
        self.assertRaises(ValueError, m._removeParameter, c)
        return
 def test___call__(self):
     """check WeakBoundMethod.__call__()
     """
     f = self.f
     self.assertEqual(7, f.evaluate())
     self.assertEqual(7, f._eq._value)
     # verify f has the same effect as f._eq._flush
     self.w(())
     self.assertTrue(None is f._eq._value)
     # check WeakBoundMethod behavior with no fallback
     x = Parameter('x', value=3)
     wgetx = weak_ref(x.getValue)
     self.assertEqual(3, wgetx())
     del x
     self.assertRaises(ReferenceError, wgetx)
     return
Esempio n. 20
0
    def testVars(self):
        """Test to see if variables are added and removed properly."""
        recipe = self.recipe
        con = self.fitcontribution

        recipe.addVar(con.A, 2)
        recipe.addVar(con.k, 1)
        recipe.addVar(con.c, 0)
        recipe.newVar("B", 0)

        names = recipe.getNames()
        self.assertEquals(names, ["A", "k", "c", "B"])
        values = recipe.getValues()
        self.assertTrue((values == [2, 1, 0, 0]).all())

        # Constrain a parameter to the B-variable to give it a value
        p = Parameter("Bpar", -1)
        recipe.constrain(recipe.B, p)
        values = recipe.getValues()
        self.assertTrue((values == [2, 1, 0]).all())
        recipe.delVar(recipe.B)

        recipe.fix(recipe.k)

        names = recipe.getNames()
        self.assertEquals(names, ["A", "c"])
        values = recipe.getValues()
        self.assertTrue((values == [2, 0]).all())

        recipe.fix("all")
        names = recipe.getNames()
        self.assertEquals(names, [])
        values = recipe.getValues()
        self.assertTrue((values == []).all())

        recipe.free("all")
        names = recipe.getNames()
        self.assertEquals(3, len(names))
        self.assertTrue("A" in names)
        self.assertTrue("k" in names)
        self.assertTrue("c" in names)
        values = recipe.getValues()
        self.assertEquals(3, len(values))
        self.assertTrue(0 in values)
        self.assertTrue(1 in values)
        self.assertTrue(2 in values)
        return
Esempio n. 21
0
    def __init__(self):
        """Initialize the attributes."""
        Observable.__init__(self)
        self._xobs = None
        self._yobs = None
        self._dyobs = None
        self.xpar = Parameter("x")
        self.ypar = Parameter("y")
        self.dypar = Parameter("dy")
        self.ycpar = Parameter("ycalc")
        self.meta = {}

        # Observable
        self.xpar.addObserver(self._flush)
        self.ypar.addObserver(self._flush)
        self.dypar.addObserver(self._flush)
        return
Esempio n. 22
0
def bound_window(
    variable: Parameter, bound: tp.Union[float, tp.Tuple, tp.Dict], ratio: bool = False
) -> Parameter:
    """Bound variable by window."""
    value = variable.getValue()
    if isinstance(bound, dict):
        if ratio:
            for k, r in bound.items():
                bound[k] = value * r
        variable.boundWindow(**bound)
    elif isinstance(bound, float):
        if ratio:
            bound = bound * value
        variable.boundWindow(bound)
    else:
        if ratio:
            bound = tuple((r * value for r in bound))
        variable.boundWindow(*bound)
    return variable
Esempio n. 23
0
    def testConstrain(self):
        """Test the constrain method."""

        p1 = self.m._newParameter("p1", 1)
        p2 = self.m._newParameter("p2", 2)
        p3 = Parameter("p3", 3)

        self.assertFalse(p1.constrained)
        self.assertEqual(0, len(self.m._constraints))
        self.m.constrain(p1, "2*p2")

        self.assertTrue(p1.constrained)
        self.assertTrue(p1 in self.m._constraints)
        self.assertEqual(1, len(self.m._constraints))
        self.assertTrue(self.m.isConstrained(p1))

        p2.setValue(10)
        self.m._constraints[p1].update()
        self.assertEqual(20, p1.getValue())

        # Check errors on unregistered parameters
        self.assertRaises(ValueError, self.m.constrain, p1, "2*p3")
        self.assertRaises(ValueError, self.m.constrain, p1, "2*p2", {"p2": p3})

        # Remove the constraint
        self.m.unconstrain(p1)
        self.assertFalse(p1.constrained)
        self.assertEqual(0, len(self.m._constraints))
        self.assertFalse(self.m.isConstrained(p1))

        # Try an straight constraint
        self.m.constrain(p1, p2)
        p2.setValue(7)
        self.m._constraints[p1].update()
        self.assertEqual(7, p1.getValue())
        return
Esempio n. 24
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
Esempio n. 25
0
    def testConstraint(self):
        """Test the Constraint class."""

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)

        factory = EquationFactory()

        factory.registerArgument("p1", p1)
        factory.registerArgument("p2", p2)

        c = Constraint()
        # Constrain p1 = 2*p2
        eq = equationFromString("2*p2", factory)
        c.constrain(p1, eq)

        self.assertTrue(p1.constrained)
        self.assertFalse(p2.constrained)

        eq2 = equationFromString("2*p2+1", factory)
        c2 = Constraint()
        self.assertRaises(ValueError, c2.constrain, p1, eq2)
        p2.setConst()
        eq3 = equationFromString("p1", factory)
        self.assertRaises(ValueError, c2.constrain, p2, eq3)

        p2.setValue(2.5)
        c.update()
        self.assertEquals(5.0, p1.getValue())

        p2.setValue(8.1)
        self.assertEquals(5.0, p1.getValue())
        c.update()
        self.assertEquals(16.2, p1.getValue())
        return
Esempio n. 26
0
    def testSetValue(self):
        """Test initialization."""
        l = Parameter("l")

        l.setValue(3.14)
        self.assertAlmostEqual(3.14, l.getValue())

        # Try array
        import numpy
        x = numpy.arange(0, 10, 0.1)
        l.setValue(x)
        self.assertTrue(l.getValue() is x)
        self.assertTrue(l.value is x)

        # Change the array
        y = numpy.arange(0, 10, 0.5)
        l.value = y
        self.assertTrue(l.getValue() is y)
        self.assertTrue(l.value is y)

        # Back to scalar
        l.setValue(1.01)
        self.assertAlmostEqual(1.01, l.getValue())
        self.assertAlmostEqual(1.01, l.value)
        return
Esempio n. 27
0
class Profile(Observable, Validatable):
    """Observed and calculated profile container.

    Profile is an Observable. The xpar, ypar and dypar attributes are observed
    by the Profile, which can in turn be observed by some other object.

    Attributes

    _xobs   --  A numpy array of the observed independent variable (default
                None)
    xobs    --  Read-only property of _xobs.
    _yobs   --  A numpy array of the observed signal (default None)
    yobs    --  Read-only property of _yobs.
    _dyobs  --  A numpy array of the uncertainty of the observed signal (default
                None, optional).
    dyobs   --  Read-only property of _dyobs.
    x       --  A numpy array of the calculated independent variable (default
                None, property for xpar accessors).
    y       --  The profile over the calculation range (default None, property
                for ypar accessors).
    dy      --  The uncertainty in the profile over the calculation range
                (default None, property for dypar accessors).
    ycalc   --  A numpy array of the calculated signal (default None).
    xpar    --  A Parameter that stores x (named "x").
    ypar    --  A Parameter that stores y (named "y").
    dypar   --  A Parameter that stores dy (named "dy").
    ycpar   --  A Parameter that stores ycalc (named "ycalc"). This is
                not observed by the profile, but it is present so it can be
                constrained to.
    meta    --  A dictionary of metadata. This is only set if provided by a
                parser.

    """

    def __init__(self):
        """Initialize the attributes."""
        Observable.__init__(self)
        self._xobs = None
        self._yobs = None
        self._dyobs = None
        self.xpar = Parameter("x")
        self.ypar = Parameter("y")
        self.dypar = Parameter("dy")
        self.ycpar = Parameter("ycalc")
        self.meta = {}

        # Observable
        self.xpar.addObserver(self._flush)
        self.ypar.addObserver(self._flush)
        self.dypar.addObserver(self._flush)
        return

    # We want x, y, ycalc and dy to stay in-sync with xpar, ypar and dypar
    x = property( lambda self : self.xpar.getValue(),
                  lambda self, val : self.xpar.setValue(val) )
    y = property( lambda self : self.ypar.getValue(),
                  lambda self, val : self.ypar.setValue(val) )
    dy = property( lambda self : self.dypar.getValue(),
                   lambda self, val : self.dypar.setValue(val) )
    ycalc = property( lambda self : self.ycpar.getValue(),
                  lambda self, val : self.ycpar.setValue(val) )

    # We want xobs, yobs and dyobs to be read-only
    xobs = property( lambda self: self._xobs )
    yobs = property( lambda self: self._yobs )
    dyobs = property( lambda self: self._dyobs )

    def loadParsedData(self, parser):
        """Load parsed data from a ProfileParser.

        This sets the xobs, yobs, dyobs arrays as well as the metadata.

        """
        x, y, junk, dy = parser.getData()
        self.meta = dict(parser.getMetaData())
        self.setObservedProfile(x, y, dy)
        return

    def setObservedProfile(self, xobs, yobs, dyobs = None):
        """Set the observed profile.

        Arguments
        xobs    --  Numpy array of the independent variable
        yobs    --  Numpy array of the observed signal.
        dyobs   --  Numpy array of the uncertainty in the observed signal. If
                    dyobs is None (default), it will be set to 1 at each
                    observed xobs.

        Raises ValueError if len(yobs) != len(xobs)
        Raises ValueError if dyobs != None and len(dyobs) != len(xobs)

        """
        if len(yobs) != len(xobs):
            raise ValueError("xobs and yobs are different lengths")
        if dyobs is not None and len(dyobs) != len(xobs):
            raise ValueError("xobs and dyobs are different lengths")

        self._xobs = numpy.asarray(xobs, dtype=float)
        self._yobs = numpy.asarray(yobs, dtype=float)

        if dyobs is None:
            self._dyobs = numpy.ones_like(xobs)
        else:
            self._dyobs = numpy.asarray(dyobs, dtype=float)

        # Set the default calculation points
        if self.x is None:
            self.setCalculationPoints(self._xobs)
        else:
            self.setCalculationPoints(self.x)

        return

    def setCalculationRange(self, xmin = None, xmax = None, dx = None):
        """Set the calculation range

        Arguments
        xmin    --  The minimum value of the independent variable.
                    If xmin is None (default), the minimum observed value will
                    be used. This is clipped to the minimum observed x.
        xmax    --  The maximum value of the independent variable.
                    If xmax is None (default), the maximum observed value will
                    be used. This is clipped to the maximum observed x.
        dx      --  The sample spacing in the independent variable. If dx is
                    None (default), then the spacing in the observed points
                    will be preserved.

        Note that xmin is always inclusive (unless clipped). xmax is inclusive
        if it is within the bounds of the observed data.

        raises AttributeError if there is no observed profile
        raises ValueError if xmin > xmax
        raises ValueError if dx > xmax-xmin
        raises ValueError if dx <= 0

        """
        clip = dx is None

        if self.xobs is None:
            raise AttributeError("No observed profile")

        if xmin is None and xmax is None and dx is None:
            self.x = self.xobs
            self.y = self.yobs
            self.dy = self.dyobs
            return

        if xmin is None:
            xmin = self.xobs[0]
        else:
            xmin = float(xmin)

        if xmax is None:
            xmax = self.xobs[-1]
        else:
            xmax = float(xmax)

        if dx is None:
            dx = (self.xobs[-1] - self.xobs[0]) / len(self.xobs)
        else:
            dx = float(dx)

        if xmin > xmax:
            raise ValueError("xmax must be greater than xmin")
        if dx > xmax - xmin:
            raise ValueError("dx must be less than xmax-xmin")
        if dx <= 0:
            raise ValueError("dx must be positive")

        if clip:
            x = self.xobs
            indices = numpy.logical_and( xmin - epsilon <= x , x <= xmax +
                    epsilon )
            self.x = self.xobs[indices]
            self.y = self.yobs[indices]
            self.dy = self.dyobs[indices]
        else:
            self.setCalculationPoints(numpy.arange(xmin, xmax+0.5*dx, dx))

        return

    def setCalculationPoints(self, x):
        """Set the calculation points.

        Arguments
        x   --  A non-empty numpy array containing the calculation points. If
                xobs exists, the bounds of x will be limited to its bounds.

        This will create y and dy on the specified grid if xobs, yobs and
        dyobs exist.

        """
        x = numpy.asarray(x)
        if self.xobs is not None:
            x = x[ x >= self.xobs[0] - epsilon ]
            x = x[ x <= self.xobs[-1] + epsilon ]
        self.x = x
        if self.yobs is not None:
            self.y = rebinArray(self.yobs, self.xobs, self.x)
        if self.dyobs is not None:
            # work around for interpolation issue making some of these non-1
            if (self.dyobs == 1).all():
                self.dy = numpy.ones_like(self.x)
            else:
            # FIXME - This does not follow error propogation rules and it
            # introduces (more) correlation between the data points.
                self.dy = rebinArray(self.dyobs, self.xobs, self.x)

        return

    def loadtxt(self, *args, **kw):
        """Use numpy.loadtxt to load data.

        Arguments are passed to numpy.loadtxt.
        unpack = True is enforced.
        The first two arrays returned by numpy.loadtxt are assumed to be x and
        y.  If there is a third array, it is assumed to by dy. Any other arrays
        are ignored. These are passed to setObservedProfile.

        Raises ValueError if the call to numpy.loadtxt returns fewer than 2
        arrays.

        Returns the x, y and dy arrays loaded from the file

        """
        if len(args) == 8 and not args[-1]:
            args = list(args)
            args[-1] = True
        else:
            kw["unpack"] = True
        cols = numpy.loadtxt(*args, **kw)

        x = y = dy = None
        # Due to using 'unpack', a single column will come out as a single
        # array, thus the second check.
        if len(cols) < 2 or not isinstance(cols[0], numpy.ndarray):
            raise ValueError("numpy.loadtxt returned fewer than 2 arrays")
        x = cols[0]
        y = cols[1]
        if len(cols) > 2:
            dy = cols[2]

        self.setObservedProfile(x, y, dy)
        return x, y, dy

    def savetxt(self, fname, fmt='%.18e', delimiter=' '):
        """Call numpy.savetxt with x, ycalc, y, dy

        Arguments are passed to numpy.savetxt.

        """
        x = self.x
        ycalc = self.ycalc
        if ycalc is None:
            raise AttributeError("ycalc is None")
        y = self.y
        dy = self.dy

        # Add the header
        if not hasattr(fname, 'write'):
            fname = file(fname, 'w')
        if fname.closed:
            raise ValueError("I/O operation on closed file")
        header = "# x           ycalc           y           dy\n"
        fname.write(header)
        numpy.savetxt(fname, zip(x, ycalc, y, dy), fmt, delimiter)
        return

    def _flush(self, other):
        """Invalidate cached state.

        This will force any observer to invalidate its state.

        """
        self.ycalc = None
        self.notify(other)
        return

    def _validate(self):
        """Validate my state.

        This validates that x, y, dy, xobx, yobs and dyobs are not None.
        This validates that x, y, and dy are the same length.

        Raises SrFitError if validation fails.

        """
        datanotset = any(v is None for v in
                [self.x, self.y, self.dy, self.xobs, self.yobs, self.dyobs])
        if datanotset:
            raise SrFitError("Missing data")
        if len(self.x) != len(self.y) or len(self.x) != len(self.dy):
            raise SrFitError("Data are different lengths")
        return
Esempio n. 28
0
    def testWrapper(self):
        """Test the adapter.

        This adapts a Parameter to the Parameter interface. :)
        """
        l = Parameter("l", 3.14)

        # Try Accessor adaptation
        la = ParameterAdapter("l",
                              l,
                              getter=Parameter.getValue,
                              setter=Parameter.setValue)

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

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

        # Change the adapter
        la.setValue(3.2)
        self.assertEqual(l.getValue(), la.getValue())

        # Try Attribute adaptation
        la = ParameterAdapter("l", l, attr="value")

        self.assertEqual(l.name, la.name)
        self.assertEqual("value", la.attr)
        self.assertEqual(l.getValue(), la.getValue())

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

        # Change the adapter
        la.setValue(3.2)
        self.assertEqual(l.getValue(), la.getValue())

        return
Esempio n. 29
0
    def testSetValue(self):
        """Test initialization."""
        l = Parameter("l")

        l.setValue(3.14)
        self.assertAlmostEqual(3.14, l.getValue())

        # Try array
        import numpy
        x = numpy.arange(0, 10, 0.1)
        l.setValue(x)
        self.assertTrue( l.getValue() is x )
        self.assertTrue( l.value is x )

        # Change the array
        y = numpy.arange(0, 10, 0.5)
        l.value = y
        self.assertTrue( l.getValue() is y )
        self.assertTrue( l.value is y )

        # Back to scalar
        l.setValue(1.01)
        self.assertAlmostEqual(1.01, l.getValue())
        self.assertAlmostEqual(1.01, l.value)
        return
Esempio n. 30
0
    def testRestraint(self):
        """Test the Restraint class."""

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)

        factory = EquationFactory()

        factory.registerArgument("p1", p1)
        factory.registerArgument("p2", p2)

        # Restrain 1 <  p1 + p2 < 5
        eq = equationFromString("p1 + p2", factory)
        r = Restraint(eq, 1, 5)

        # This should have no penalty
        p1.setValue(1)
        p2.setValue(1)
        self.assertEquals(0, r.penalty())

        # Make p1 + p2 = 0
        # This should have a penalty of 1*(1 - 0)**2 = 1
        p1.setValue(-1)
        p2.setValue(1)
        self.assertEquals(1, r.penalty())

        # Make p1 + p2 = 8
        # This should have a penalty of 1*(8 - 5)**2 = 9
        p1.setValue(4)
        p2.setValue(4)
        self.assertEquals(9, r.penalty())

        # Set the chi^2 to get a dynamic penalty
        r.scaled = True
        self.assertEquals(13.5, r.penalty(1.5))

        # Make a really large number to check the upper bound
        import numpy
        r.ub = numpy.inf
        p1.setValue(1e100)
        self.assertEquals(0, r.penalty())

        return
Esempio n. 31
0
class Profile(Observable, Validatable):
    """Observed and calculated profile container.

    Profile is an Observable. The xpar, ypar and dypar attributes are observed
    by the Profile, which can in turn be observed by some other object.

    Attributes

    _xobs   --  A numpy array of the observed independent variable (default
                None)
    xobs    --  Read-only property of _xobs.
    _yobs   --  A numpy array of the observed signal (default None)
    yobs    --  Read-only property of _yobs.
    _dyobs  --  A numpy array of the uncertainty of the observed signal (default
                None, optional).
    dyobs   --  Read-only property of _dyobs.
    x       --  A numpy array of the calculated independent variable (default
                None, property for xpar accessors).
    y       --  The profile over the calculation range (default None, property
                for ypar accessors).
    dy      --  The uncertainty in the profile over the calculation range
                (default None, property for dypar accessors).
    ycalc   --  A numpy array of the calculated signal (default None).
    xpar    --  A Parameter that stores x (named "x").
    ypar    --  A Parameter that stores y (named "y").
    dypar   --  A Parameter that stores dy (named "dy").
    ycpar   --  A Parameter that stores ycalc (named "ycalc"). This is
                not observed by the profile, but it is present so it can be
                constrained to.
    meta    --  A dictionary of metadata. This is only set if provided by a
                parser.

    """

    def __init__(self):
        """Initialize the attributes."""
        Observable.__init__(self)
        self._xobs = None
        self._yobs = None
        self._dyobs = None
        self.xpar = Parameter("x")
        self.ypar = Parameter("y")
        self.dypar = Parameter("dy")
        self.ycpar = Parameter("ycalc")
        self.meta = {}

        # Observable
        self.xpar.addObserver(self._flush)
        self.ypar.addObserver(self._flush)
        self.dypar.addObserver(self._flush)
        return

    # We want x, y, ycalc and dy to stay in-sync with xpar, ypar and dypar
    x = property( lambda self : self.xpar.getValue(),
                  lambda self, val : self.xpar.setValue(val) )
    y = property( lambda self : self.ypar.getValue(),
                  lambda self, val : self.ypar.setValue(val) )
    dy = property( lambda self : self.dypar.getValue(),
                   lambda self, val : self.dypar.setValue(val) )
    ycalc = property( lambda self : self.ycpar.getValue(),
                  lambda self, val : self.ycpar.setValue(val) )

    # We want xobs, yobs and dyobs to be read-only
    xobs = property( lambda self: self._xobs )
    yobs = property( lambda self: self._yobs )
    dyobs = property( lambda self: self._dyobs )

    def loadParsedData(self, parser):
        """Load parsed data from a ProfileParser.

        This sets the xobs, yobs, dyobs arrays as well as the metadata.

        """
        x, y, junk, dy = parser.getData()
        self.meta = dict(parser.getMetaData())
        self.setObservedProfile(x, y, dy)
        return

    def setObservedProfile(self, xobs, yobs, dyobs = None):
        """Set the observed profile.

        Arguments
        xobs    --  Numpy array of the independent variable
        yobs    --  Numpy array of the observed signal.
        dyobs   --  Numpy array of the uncertainty in the observed signal. If
                    dyobs is None (default), it will be set to 1 at each
                    observed xobs.

        Raises ValueError if len(yobs) != len(xobs)
        Raises ValueError if dyobs != None and len(dyobs) != len(xobs)

        """
        if len(yobs) != len(xobs):
            raise ValueError("xobs and yobs are different lengths")
        if dyobs is not None and len(dyobs) != len(xobs):
            raise ValueError("xobs and dyobs are different lengths")

        self._xobs = numpy.asarray(xobs, dtype=float)
        self._yobs = numpy.asarray(yobs, dtype=float)

        if dyobs is None:
            self._dyobs = numpy.ones_like(xobs)
        else:
            self._dyobs = numpy.asarray(dyobs, dtype=float)

        # Set the default calculation points
        if self.x is None:
            self.setCalculationPoints(self._xobs)
        else:
            self.setCalculationPoints(self.x)

        return

    def setCalculationRange(self, xmin=None, xmax=None, dx=None):
        """Set epsilon-inclusive calculation range.

        Adhere to the observed ``xobs`` points when ``dx`` is the same
        as in the data.  ``xmin`` and ``xmax`` are clipped at the bounds
        of the observed data.

        Parameters
        ----------

        xmin : float or "obs", optional
            The minimum value of the independent variable.  Keep the
            current minimum when not specified.  If specified as "obs"
            reset to the minimum observed value.
        xmax : float or "obs", optional
            The maximum value of the independent variable.  Keep the
            current maximum when not specified.  If specified as "obs"
            reset to the maximum observed value.
        dx : float or "obs", optional
            The sample spacing in the independent variable.  When different
            from the data, resample the ``x`` as anchored at ``xmin``.

        Note that xmin is always inclusive (unless clipped). xmax is inclusive
        if it is within the bounds of the observed data.

        Raises
        ------
        AttributeError
            If there is no observed data.
        ValueError
            When xmin > xmax or if dx <= 0.  Also if dx > xmax - xmin.
        """
        if self.xobs is None:
            raise AttributeError("No observed profile")
        # local helper function
        def _isobs(a):
            if not isinstance(a, six.string_types):
                return False
            if a != 'obs':
                raise ValueError('Must be either float or "obs".')
            return True
        # resolve new low and high bounds for x
        lo = (self.x[0] if xmin is None else
              self.xobs[0] if _isobs(xmin) else float(xmin))
        lo = max(lo, self.xobs[0])
        hi = (self.x[-1] if xmax is None else
              self.xobs[-1] if _isobs(xmax) else float(xmax))
        hi = min(hi, self.xobs[-1])
        # determine if we need to clip the original grid
        clip = True
        step = None
        ncur = len(self.x)
        stepcur = (1 if ncur < 2
                   else (self.x[-1] - self.x[0]) / (ncur - 1.0))
        nobs = len(self.xobs)
        stepobs = (1 if nobs < 2
                   else (self.xobs[-1] - self.xobs[0]) / (nobs - 1.0))
        if dx is None:
            # check if xobs overlaps with x
            i0 = numpy.fabs(self.xobs - self.x[0]).argmin()
            n0 = min(len(self.x), len(self.xobs) - i0)
            if not numpy.allclose(self.xobs[i0 : i0 + n0], self.x[:n0]):
                clip = False
                step = stepcur if ncur > 1 else stepobs
        elif _isobs(dx):
            assert clip and step is None
        elif numpy.allclose(stepobs, dx):
            assert clip and step is None
        else:
            clip = False
            step = float(dx)
        # verify that we either clip or have the step defined.
        assert clip or step is not None
        # hi, lo, step, clip all resolved here.
        # validate arguments
        if lo > hi:
            raise ValueError("xmax must be greater than xmin.")
        if not clip:
            if step > hi - lo:
                raise ValueError("dx must be less than (xmax - xmin).")
            if step <= 0:
                raise ValueError("dx must be positive.")
        # determine epsilon extensions to the lower and upper bounds.
        epslo = abs(lo) * epsilon + epsilon
        epshi = abs(hi) * epsilon + epsilon
        # process the new grid.
        if clip:
            indices = (lo - epslo <= self.xobs) & (self.xobs <= hi + epshi)
            self.x = self.xobs[indices]
            self.y = self.yobs[indices]
            self.dy = self.dyobs[indices]
        else:
            x1 = numpy.arange(lo, hi + epshi, step)
            self.setCalculationPoints(x1)
        return


    def setCalculationPoints(self, x):
        """Set the calculation points.

        Arguments
        x   --  A non-empty numpy array containing the calculation points. If
                xobs exists, the bounds of x will be limited to its bounds.

        This will create y and dy on the specified grid if xobs, yobs and
        dyobs exist.

        """
        x = numpy.asarray(x)
        if self.xobs is not None:
            x = x[ x >= self.xobs[0] - epsilon ]
            x = x[ x <= self.xobs[-1] + epsilon ]
        self.x = x
        if self.yobs is not None:
            self.y = rebinArray(self.yobs, self.xobs, self.x)
        if self.dyobs is not None:
            # work around for interpolation issue making some of these non-1
            if (self.dyobs == 1).all():
                self.dy = numpy.ones_like(self.x)
            else:
            # FIXME - This does not follow error propogation rules and it
            # introduces (more) correlation between the data points.
                self.dy = rebinArray(self.dyobs, self.xobs, self.x)

        return

    def loadtxt(self, *args, **kw):
        """Use numpy.loadtxt to load data.

        Arguments are passed to numpy.loadtxt.
        unpack = True is enforced.
        The first two arrays returned by numpy.loadtxt are assumed to be x and
        y.  If there is a third array, it is assumed to by dy. Any other arrays
        are ignored. These are passed to setObservedProfile.

        Raises ValueError if the call to numpy.loadtxt returns fewer than 2
        arrays.

        Returns the x, y and dy arrays loaded from the file

        """
        if len(args) == 8 and not args[-1]:
            args = list(args)
            args[-1] = True
        else:
            kw["unpack"] = True
        cols = numpy.loadtxt(*args, **kw)

        x = y = dy = None
        # Due to using 'unpack', a single column will come out as a single
        # array, thus the second check.
        if len(cols) < 2 or not isinstance(cols[0], numpy.ndarray):
            raise ValueError("numpy.loadtxt returned fewer than 2 arrays")
        x = cols[0]
        y = cols[1]
        if len(cols) > 2:
            dy = cols[2]

        self.setObservedProfile(x, y, dy)
        return x, y, dy


    def savetxt(self, fname, **kwargs):
        """Call `numpy.savetxt` with x, ycalc, y, dy.

        Parameters
        ----------
        fname : filename or file handle
            This is passed to `numpy.savetxt`.
        **kwargs
            The keyword arguments that are passed to `numpy.savetxt`.
            We preset file header "x  ycalc  y  dy".  Use ``header=''``
            to save data without any header.

        Raises
        ------
        SrFitError
            When `self.ycalc` has not been set.

        See also
        --------
        numpy.savetxt
        """
        x = self.x
        ycalc = self.ycalc
        if ycalc is None:
            raise SrFitError("ycalc is None")
        y = self.y
        dy = self.dy
        kwargs.setdefault('header', 'x  ycalc  y  dy')
        data = numpy.transpose([x, ycalc, y, dy])
        numpy.savetxt(fname, data, **kwargs)
        return


    def _flush(self, other):
        """Invalidate cached state.

        This will force any observer to invalidate its state.

        """
        self.ycalc = None
        self.notify(other)
        return

    def _validate(self):
        """Validate my state.

        This validates that x, y, dy, xobx, yobs and dyobs are not None.
        This validates that x, y, and dy are the same length.

        Raises SrFitError if validation fails.

        """
        datanotset = any(v is None for v in
                [self.x, self.y, self.dy, self.xobs, self.yobs, self.dyobs])
        if datanotset:
            raise SrFitError("Missing data")
        if len(self.x) != len(self.y) or len(self.x) != len(self.dy):
            raise SrFitError("Data are different lengths")
        return
Esempio n. 32
0
    def testRestraint(self):
        """Test the Restraint class."""

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)

        factory = EquationFactory()

        factory.registerArgument("p1", p1)
        factory.registerArgument("p2", p2)

        # Restrain 1 <  p1 + p2 < 5
        eq = equationFromString("p1 + p2", factory)
        r = Restraint(eq, 1, 5)

        # This should have no penalty
        p1.setValue(1)
        p2.setValue(1)
        self.assertEqual(0, r.penalty())

        # Make p1 + p2 = 0
        # This should have a penalty of 1*(1 - 0)**2 = 1
        p1.setValue(-1)
        p2.setValue(1)
        self.assertEqual(1, r.penalty())

        # Make p1 + p2 = 8
        # This should have a penalty of 1*(8 - 5)**2 = 9
        p1.setValue(4)
        p2.setValue(4)
        self.assertEqual(9, r.penalty())

        # Set the chi^2 to get a dynamic penalty
        r.scaled = True
        self.assertEqual(13.5, r.penalty(1.5))

        # Make a really large number to check the upper bound
        import numpy
        r.ub = numpy.inf
        p1.setValue(1e100)
        self.assertEqual(0, r.penalty())

        return
Esempio n. 33
0
    def testResidual(self):
        """Test the residual, which requires all other methods."""
        fc = self.fitcontribution
        profile = self.profile
        gen = self.gen

        # Add the calculator and profile
        fc.setProfile(profile)
        self.assertTrue(fc.profile is profile)
        fc.addProfileGenerator(gen, "I")
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)
        self.assertEquals(1, len(fc._generators))
        self.assertTrue(gen.name in fc._generators)

        # Let's create some data
        xobs = arange(0, 10, 0.5)
        yobs = xobs
        profile.setObservedProfile(xobs, yobs)

        # Check our fitting equation.
        self.assertTrue(array_equal(fc._eq(), gen(xobs)))

        # Now calculate the residual
        chiv = fc.residual()
        self.assertAlmostEqual(0, dot(chiv, chiv))

        # Now change the equation
        fc.setEquation("2*I")
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)
        chiv = fc.residual()
        self.assertAlmostEqual(dot(yobs, yobs), dot(chiv, chiv))

        # Try to add a parameter
        c = Parameter("c", 2)
        fc._addParameter(c)
        fc.setEquation("c*I")
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)
        chiv = fc.residual()
        self.assertAlmostEqual(dot(yobs, yobs), dot(chiv, chiv))

        # Try something more complex
        c.setValue(3)
        fc.setEquation("c**2*sin(I)")
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)
        from numpy import sin

        xobs = arange(0, 10, 0.5)
        from numpy import sin

        yobs = 9 * sin(xobs)
        profile.setObservedProfile(xobs, yobs)
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)

        chiv = fc.residual()
        self.assertAlmostEqual(0, dot(chiv, chiv))

        # Choose a new residual.
        fc.setEquation("2*I")
        fc.setResidualEquation("resv")
        chiv = fc.residual()
        self.assertAlmostEqual(sum((2 * xobs - yobs) ** 2) / sum(yobs ** 2), dot(chiv, chiv))

        # Make a custom residual.
        fc.setResidualEquation("abs(eq-y)**0.5")
        chiv = fc.residual()
        self.assertEqual(sum(abs(2 * xobs - yobs)), dot(chiv, chiv))

        return
Esempio n. 34
0
    def testResidual(self):
        """Test the residual, which requires all other methods."""
        fc = self.fitcontribution
        profile = self.profile
        gen = self.gen

        # Add the calculator and profile
        fc.setProfile(profile)
        self.assertTrue(fc.profile is profile)
        fc.addProfileGenerator(gen, "I")
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)
        self.assertEquals(1, len(fc._generators))
        self.assertTrue(gen.name in fc._generators)

        # Let's create some data
        xobs = arange(0, 10, 0.5)
        yobs = xobs
        profile.setObservedProfile(xobs, yobs)

        # Check our fitting equation.
        self.assertTrue(array_equal(fc._eq(), gen(xobs)))

        # Now calculate the residual
        chiv = fc.residual()
        self.assertAlmostEqual(0, dot(chiv, chiv))

        # Now change the equation
        fc.setEquation("2*I")
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)
        chiv = fc.residual()
        self.assertAlmostEqual(dot(yobs, yobs), dot(chiv, chiv))

        # Try to add a parameter
        c = Parameter("c", 2)
        fc._addParameter(c)
        fc.setEquation("c*I")
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)
        chiv = fc.residual()
        self.assertAlmostEqual(dot(yobs, yobs), dot(chiv, chiv))

        # Try something more complex
        c.setValue(3)
        fc.setEquation("c**2*sin(I)")
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)
        from numpy import sin
        xobs = arange(0, 10, 0.5)
        from numpy import sin
        yobs = 9*sin(xobs)
        profile.setObservedProfile(xobs, yobs)
        self.assertTrue(fc._eq._value is None)
        self.assertTrue(fc._reseq._value is None)

        chiv = fc.residual()
        self.assertAlmostEqual(0, dot(chiv, chiv))

        # Choose a new residual.
        fc.setEquation("2*I")
        fc.setResidualEquation("resv")
        chiv = fc.residual()
        self.assertAlmostEqual(sum((2*xobs-yobs)**2)/sum(yobs**2),
                dot(chiv, chiv))

        # Make a custom residual.
        fc.setResidualEquation("abs(eq-y)**0.5")
        chiv = fc.residual()
        self.assertEqual(sum(abs(2*xobs-yobs)), dot(chiv, chiv))

        return
Esempio n. 35
0
class Profile(Observable, Validatable):
    """Observed and calculated profile container.

    Profile is an Observable. The xpar, ypar and dypar attributes are observed
    by the Profile, which can in turn be observed by some other object.

    Attributes

    _xobs   --  A numpy array of the observed independent variable (default
                None)
    xobs    --  Read-only property of _xobs.
    _yobs   --  A numpy array of the observed signal (default None)
    yobs    --  Read-only property of _yobs.
    _dyobs  --  A numpy array of the uncertainty of the observed signal (default
                None, optional).
    dyobs   --  Read-only property of _dyobs.
    x       --  A numpy array of the calculated independent variable (default
                None, property for xpar accessors).
    y       --  The profile over the calculation range (default None, property
                for ypar accessors).
    dy      --  The uncertainty in the profile over the calculation range
                (default None, property for dypar accessors).
    ycalc   --  A numpy array of the calculated signal (default None).
    xpar    --  A Parameter that stores x (named "x").
    ypar    --  A Parameter that stores y (named "y").
    dypar   --  A Parameter that stores dy (named "dy").
    ycpar   --  A Parameter that stores ycalc (named "ycalc"). This is
                not observed by the profile, but it is present so it can be
                constrained to.
    meta    --  A dictionary of metadata. This is only set if provided by a
                parser.

    """
    def __init__(self):
        """Initialize the attributes."""
        Observable.__init__(self)
        self._xobs = None
        self._yobs = None
        self._dyobs = None
        self.xpar = Parameter("x")
        self.ypar = Parameter("y")
        self.dypar = Parameter("dy")
        self.ycpar = Parameter("ycalc")
        self.meta = {}

        # Observable
        self.xpar.addObserver(self._flush)
        self.ypar.addObserver(self._flush)
        self.dypar.addObserver(self._flush)
        return

    # We want x, y, ycalc and dy to stay in-sync with xpar, ypar and dypar
    x = property(lambda self: self.xpar.getValue(),
                 lambda self, val: self.xpar.setValue(val))
    y = property(lambda self: self.ypar.getValue(),
                 lambda self, val: self.ypar.setValue(val))
    dy = property(lambda self: self.dypar.getValue(),
                  lambda self, val: self.dypar.setValue(val))
    ycalc = property(lambda self: self.ycpar.getValue(),
                     lambda self, val: self.ycpar.setValue(val))

    # We want xobs, yobs and dyobs to be read-only
    xobs = property(lambda self: self._xobs)
    yobs = property(lambda self: self._yobs)
    dyobs = property(lambda self: self._dyobs)

    def loadParsedData(self, parser):
        """Load parsed data from a ProfileParser.

        This sets the xobs, yobs, dyobs arrays as well as the metadata.

        """
        x, y, junk, dy = parser.getData()
        self.meta = dict(parser.getMetaData())
        self.setObservedProfile(x, y, dy)
        return

    def setObservedProfile(self, xobs, yobs, dyobs=None):
        """Set the observed profile.

        Arguments
        xobs    --  Numpy array of the independent variable
        yobs    --  Numpy array of the observed signal.
        dyobs   --  Numpy array of the uncertainty in the observed signal. If
                    dyobs is None (default), it will be set to 1 at each
                    observed xobs.

        Raises ValueError if len(yobs) != len(xobs)
        Raises ValueError if dyobs != None and len(dyobs) != len(xobs)

        """
        if len(yobs) != len(xobs):
            raise ValueError("xobs and yobs are different lengths")
        if dyobs is not None and len(dyobs) != len(xobs):
            raise ValueError("xobs and dyobs are different lengths")

        self._xobs = numpy.asarray(xobs, dtype=float)
        self._yobs = numpy.asarray(yobs, dtype=float)

        if dyobs is None:
            self._dyobs = numpy.ones_like(xobs)
        else:
            self._dyobs = numpy.asarray(dyobs, dtype=float)

        # Set the default calculation points
        if self.x is None:
            self.setCalculationPoints(self._xobs)
        else:
            self.setCalculationPoints(self.x)

        return

    def setCalculationRange(self, xmin=None, xmax=None, dx=None):
        """Set epsilon-inclusive calculation range.

        Adhere to the observed ``xobs`` points when ``dx`` is the same
        as in the data.  ``xmin`` and ``xmax`` are clipped at the bounds
        of the observed data.

        Parameters
        ----------

        xmin : float or "obs", optional
            The minimum value of the independent variable.  Keep the
            current minimum when not specified.  If specified as "obs"
            reset to the minimum observed value.
        xmax : float or "obs", optional
            The maximum value of the independent variable.  Keep the
            current maximum when not specified.  If specified as "obs"
            reset to the maximum observed value.
        dx : float or "obs", optional
            The sample spacing in the independent variable.  When different
            from the data, resample the ``x`` as anchored at ``xmin``.

        Note that xmin is always inclusive (unless clipped). xmax is inclusive
        if it is within the bounds of the observed data.

        Raises
        ------
        AttributeError
            If there is no observed data.
        ValueError
            When xmin > xmax or if dx <= 0.  Also if dx > xmax - xmin.
        """
        if self.xobs is None:
            raise AttributeError("No observed profile")
        # local helper function
        def _isobs(a):
            if not isinstance(a, six.string_types):
                return False
            if a != 'obs':
                raise ValueError('Must be either float or "obs".')
            return True

        # resolve new low and high bounds for x
        lo = (self.x[0] if xmin is None else
              self.xobs[0] if _isobs(xmin) else float(xmin))
        lo = max(lo, self.xobs[0])
        hi = (self.x[-1] if xmax is None else
              self.xobs[-1] if _isobs(xmax) else float(xmax))
        hi = min(hi, self.xobs[-1])
        # determine if we need to clip the original grid
        clip = True
        step = None
        ncur = len(self.x)
        stepcur = (1 if ncur < 2 else (self.x[-1] - self.x[0]) / (ncur - 1.0))
        nobs = len(self.xobs)
        stepobs = (1 if nobs < 2 else
                   (self.xobs[-1] - self.xobs[0]) / (nobs - 1.0))
        if dx is None:
            # check if xobs overlaps with x
            i0 = numpy.fabs(self.xobs - self.x[0]).argmin()
            n0 = min(len(self.x), len(self.xobs) - i0)
            if not numpy.allclose(self.xobs[i0:i0 + n0], self.x[:n0]):
                clip = False
                step = stepcur if ncur > 1 else stepobs
        elif _isobs(dx):
            assert clip and step is None
        elif numpy.allclose(stepobs, dx):
            assert clip and step is None
        else:
            clip = False
            step = float(dx)
        # verify that we either clip or have the step defined.
        assert clip or step is not None
        # hi, lo, step, clip all resolved here.
        # validate arguments
        if lo > hi:
            raise ValueError("xmax must be greater than xmin.")
        if not clip:
            if step > hi - lo:
                raise ValueError("dx must be less than (xmax - xmin).")
            if step <= 0:
                raise ValueError("dx must be positive.")
        # determine epsilon extensions to the lower and upper bounds.
        epslo = abs(lo) * epsilon + epsilon
        epshi = abs(hi) * epsilon + epsilon
        # process the new grid.
        if clip:
            indices = (lo - epslo <= self.xobs) & (self.xobs <= hi + epshi)
            self.x = self.xobs[indices]
            self.y = self.yobs[indices]
            self.dy = self.dyobs[indices]
        else:
            x1 = numpy.arange(lo, hi + epshi, step)
            self.setCalculationPoints(x1)
        return

    def setCalculationPoints(self, x):
        """Set the calculation points.

        Arguments
        x   --  A non-empty numpy array containing the calculation points. If
                xobs exists, the bounds of x will be limited to its bounds.

        This will create y and dy on the specified grid if xobs, yobs and
        dyobs exist.

        """
        x = numpy.asarray(x)
        if self.xobs is not None:
            x = x[x >= self.xobs[0] - epsilon]
            x = x[x <= self.xobs[-1] + epsilon]
        self.x = x
        if self.yobs is not None:
            self.y = rebinArray(self.yobs, self.xobs, self.x)
        if self.dyobs is not None:
            # work around for interpolation issue making some of these non-1
            if (self.dyobs == 1).all():
                self.dy = numpy.ones_like(self.x)
            else:
                # FIXME - This does not follow error propogation rules and it
                # introduces (more) correlation between the data points.
                self.dy = rebinArray(self.dyobs, self.xobs, self.x)

        return

    def loadtxt(self, *args, **kw):
        """Use numpy.loadtxt to load data.

        Arguments are passed to numpy.loadtxt.
        unpack = True is enforced.
        The first two arrays returned by numpy.loadtxt are assumed to be x and
        y.  If there is a third array, it is assumed to by dy. Any other arrays
        are ignored. These are passed to setObservedProfile.

        Raises ValueError if the call to numpy.loadtxt returns fewer than 2
        arrays.

        Returns the x, y and dy arrays loaded from the file

        """
        if len(args) == 8 and not args[-1]:
            args = list(args)
            args[-1] = True
        else:
            kw["unpack"] = True
        cols = numpy.loadtxt(*args, **kw)

        x = y = dy = None
        # Due to using 'unpack', a single column will come out as a single
        # array, thus the second check.
        if len(cols) < 2 or not isinstance(cols[0], numpy.ndarray):
            raise ValueError("numpy.loadtxt returned fewer than 2 arrays")
        x = cols[0]
        y = cols[1]
        if len(cols) > 2:
            dy = cols[2]

        self.setObservedProfile(x, y, dy)
        return x, y, dy

    def savetxt(self, fname, **kwargs):
        """Call `numpy.savetxt` with x, ycalc, y, dy.

        Parameters
        ----------
        fname : filename or file handle
            This is passed to `numpy.savetxt`.
        **kwargs
            The keyword arguments that are passed to `numpy.savetxt`.
            We preset file header "x  ycalc  y  dy".  Use ``header=''``
            to save data without any header.

        Raises
        ------
        SrFitError
            When `self.ycalc` has not been set.

        See also
        --------
        numpy.savetxt
        """
        x = self.x
        ycalc = self.ycalc
        if ycalc is None:
            raise SrFitError("ycalc is None")
        y = self.y
        dy = self.dy
        kwargs.setdefault('header', 'x  ycalc  y  dy')
        data = numpy.transpose([x, ycalc, y, dy])
        numpy.savetxt(fname, data, **kwargs)
        return

    def _flush(self, other):
        """Invalidate cached state.

        This will force any observer to invalidate its state.

        """
        self.ycalc = None
        self.notify(other)
        return

    def _validate(self):
        """Validate my state.

        This validates that x, y, dy, xobx, yobs and dyobs are not None.
        This validates that x, y, and dy are the same length.

        Raises SrFitError if validation fails.

        """
        datanotset = any(
            v is None for v in
            [self.x, self.y, self.dy, self.xobs, self.yobs, self.dyobs])
        if datanotset:
            raise SrFitError("Missing data")
        if len(self.x) != len(self.y) or len(self.x) != len(self.dy):
            raise SrFitError("Data are different lengths")
        return
Esempio n. 36
0
    def testConstraint(self):
        """Test the Constraint class."""

        p1 = Parameter("p1", 1)
        p2 = Parameter("p2", 2)

        factory = EquationFactory()

        factory.registerArgument("p1", p1)
        factory.registerArgument("p2", p2)

        c = Constraint()
        # Constrain p1 = 2*p2
        eq = equationFromString("2*p2", factory)
        c.constrain(p1, eq)

        self.assertTrue(p1.constrained)
        self.assertFalse(p2.constrained)

        eq2 = equationFromString("2*p2+1", factory)
        c2 = Constraint()
        self.assertRaises(ValueError, c2.constrain, p1, eq2)
        p2.setConst()
        eq3 = equationFromString("p1", factory)
        self.assertRaises(ValueError, c2.constrain, p2, eq3)

        p2.setValue(2.5)
        c.update()
        self.assertEquals(5.0, p1.getValue())

        p2.setValue(8.1)
        self.assertEquals(5.0, p1.getValue())
        c.update()
        self.assertEquals(16.2, p1.getValue())
        return
Esempio n. 37
0
    def testWrapper(self):
        """Test the adapter.

        This adapts a Parameter to the Parameter interface. :)
        """
        l = Parameter("l", 3.14)

        # Try Accessor adaptation
        la = ParameterAdapter("l", l, getter = Parameter.getValue, setter =
                Parameter.setValue)

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

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

        # Change the adapter
        la.setValue(3.2)
        self.assertEqual(l.getValue(), la.getValue())

        # Try Attribute adaptation
        la = ParameterAdapter("l", l, attr = "value")

        self.assertEqual(l.name, la.name)
        self.assertEqual("value", la.attr)
        self.assertEqual(l.getValue(), la.getValue())

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

        # Change the adapter
        la.setValue(3.2)
        self.assertEqual(l.getValue(), la.getValue())

        return