Beispiel #1
0
    def test_constraint_analyser(self):
        a = Parameter(1)
        b = Parameter(2, constraint=a)
        c = Parameter(2.)

        d = Parameter(3, constraint=np.cos(b + np.sin(a) + 2 * (a + b + c)))
        val = d.value

        tree = constraint_tree(d.constraint)
        new_constraint = build_constraint_from_tree(tree)
        assert_allclose(new_constraint.value, val)

        a.value = 10
        assert_allclose(new_constraint.value, d.value)

        # inject constraint into parameter
        e = Parameter(1)
        e.constraint = new_constraint
        a.value = 11
        assert_allclose(e.value, d.value)

        # check that it's possible to build a constraint tree from a single
        # param
        tree = constraint_tree(b.constraint)
        new_constraint = build_constraint_from_tree(tree)
        e = Parameter(1)
        e.constraint = new_constraint
        a.value = 0.1234
        assert_allclose(e.value, a.value)

        # check that it's possible to build a constraint tree from a single
        # param
        e = Parameter(1)
        e.constraint = 2
        assert_allclose(e.value, 2)
Beispiel #2
0
 def test_remove_constraint(self):
     x = Parameter(5.)
     y = Parameter(1.)
     y.constraint = x * 2.
     y.constraint = None
     assert_(y.vary is False)
     assert_equal(y.value, 10)
     assert_(y._constraint is None)
Beispiel #3
0
    def test_parameter(self):
        # simple test of constraint
        x = Parameter(5.)
        y = Parameter(1.)
        y.constraint = x * 2.
        assert_equal(y.value, x.value * 2.)

        # if you've constrained a parameter it shouldn't be varying
        assert_(y.vary is False)

        # you can't set a constraint on a parameter with an expression that
        # already involves the parameter
        from pytest import raises
        with raises(ValueError):
            x.constraint = y

        # try a negative value
        x.value = -1.
        assert_equal(y.value, -2.)

        # nested constraints
        z = Parameter(1.)
        z.constraint = x + y
        assert_equal(z.value, -3)
        # check that nested circular constraints aren't allowed
        with raises(ValueError):
            x.constraint = z

        # absolute value constraint
        y.constraint = abs(x)
        assert_equal(y.value, 1)

        # sin constraint
        y.constraint = np.sin(x) + 2.
        assert_equal(y.value, 2. + np.sin(x.value))
Beispiel #4
0
    def test_parameter(self):
        # simple test of constraint
        x = Parameter(5.0)
        y = Parameter(1.0)

        y.constraint = x
        assert x in y.dependencies()

        y.constraint = x * 2.0
        assert_equal(y.value, x.value * 2.0)

        # parameter should be in y's dependencies
        assert x in y._deps
        assert x in y.dependencies()

        # if you've constrained a parameter it shouldn't be varying
        assert_(y.vary is False)

        # you can't set a constraint on a parameter with an expression that
        # already involves the parameter
        from pytest import raises

        with raises(ValueError):
            x.constraint = y

        # try a negative value
        x.value = -1.0
        assert_equal(y.value, -2.0)

        # nested constraints
        z = Parameter(1.0)
        z.constraint = x + y
        assert_equal(z.value, -3)
        # check that nested circular constraints aren't allowed
        with raises(ValueError):
            x.constraint = z

        # z = x + y --> z = x + 2*x
        # therefore y shouldn't be in z's dependencies, but x should be.
        assert x in z.dependencies()
        assert y not in z.dependencies()

        # absolute value constraint
        y.constraint = abs(x)
        assert_equal(y.value, 1)

        # sin constraint
        y.constraint = np.sin(x) + 2.0
        assert_equal(y.value, 2.0 + np.sin(x.value))

        x.value = 10
        assert_equal(y.value, 2.0 + np.sin(10))
Beispiel #5
0
    def test_pickle(self):
        # a parameter and a constrained parameter should be pickleable
        bounds = PDF(norm(1., 2.))
        x = Parameter(1, bounds=bounds)
        pkl = pickle.dumps(x)
        unpkl = pickle.loads(pkl)

        # test pickling on a constrained parameter system
        a = Parameter(1.)
        b = Parameter(2.)
        b.constraint = np.sin(a)

        assert_(hasattr(a, 'sin'))
        c = [a, b]
        pkl = pickle.dumps(c)
        unpkl = pickle.loads(pkl)
        d, e = unpkl
        d.value = 2.
        assert_equal(e.value, np.sin(2.))
        # should still have all math functions
        assert_(hasattr(d, 'sin'))