Example #1
0
def test_composed_param():
    param1 = Parameter('param1', 1.)
    param2 = Parameter('param2', 2.)
    param3 = Parameter('param3', 3., floating=False)
    param4 = Parameter(
        'param4',
        4.)  # needed to make sure it does not only take all params as deps
    a = ztf.log(3. * param1) * tf.square(param2) - param3
    param_a = ComposedParameter('param_as', tensor=a)
    assert isinstance(param_a.get_dependents(only_floating=True), OrderedSet)
    assert param_a.get_dependents(only_floating=True) == {param1, param2}
    assert param_a.get_dependents(only_floating=False) == {
        param1, param2, param3
    }
    a_unchanged = zfit.run(a)
    assert a_unchanged == zfit.run(param_a)
    assert zfit.run(param2.assign(3.5))
    a_changed = zfit.run(a)
    assert a_changed == zfit.run(param_a)
    assert a_changed != a_unchanged

    with pytest.raises(LogicalUndefinedOperationError):
        param_a.assign(value=5.)
    with pytest.raises(LogicalUndefinedOperationError):
        param_a.load(value=5., session=zfit.run.sess)
Example #2
0
def test_param_func():
    param1 = Parameter('param1', 1.)
    param2 = Parameter('param2', 2.)
    param3 = Parameter('param3', 3., floating=False)
    param4 = Parameter('param4', 4.)
    a = ztf.log(3. * param1) * tf.square(param2) - param3
    func = SimpleFunc(func=lambda self, x: a * x, obs=obs1)

    new_func = param4 * func

    new_func_equivalent = func * param4

    result1 = zfit.run(new_func.func(x=rnd_test_values))
    result1_equivalent = zfit.run(new_func_equivalent.func(x=rnd_test_values))
    result2 = zfit.run(func.func(x=rnd_test_values) * param4)
    np.testing.assert_array_equal(result1, result2)
    np.testing.assert_array_equal(result1_equivalent, result2)