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)
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))
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))
def test_sld(self): p = SLD(5 + 1j, name="pop") assert_equal(float(p.real), 5) assert_equal(float(p.imag), 1) # test that we can cast to complex assert_equal(complex(p), 5 + 1j) p = SLD(5) assert_equal(float(p.real), 5) q = Parameter(5) r = Parameter(1) p = SLD([q, r]) assert_equal(float(p.real), 5) assert_equal(float(p.imag), 1) # use SLD to make a Slab thickness = Parameter(100) roughness = Parameter(3.0) vfsolv = Parameter(0.2) s = p(thickness, roughness) assert_equal(s.thick.value, thickness.value) assert_equal(s.rough.value, roughness.value) assert_equal(s.vfsolv.value, 0) s = p(thickness, roughness, vfsolv) assert_equal(s.thick.value, thickness.value) assert_equal(s.rough.value, roughness.value) assert_equal(s.vfsolv.value, vfsolv.value) # check that we can construct SLDs from a constrained par deut_par = Parameter(6.36) h2o_solvent = SLD(-0.56) ms_val = 0.6 * deut_par + 0.4 * h2o_solvent.real mixed_solvent = SLD(ms_val) assert isinstance(mixed_solvent.real, _BinaryOp) sld = complex(mixed_solvent) assert_allclose(sld.real, 0.6 * 6.36 + 0.4 * -0.56) deut_par.value = 5.0 sld = complex(mixed_solvent) assert_allclose(sld.real, 0.6 * 5.0 + 0.4 * -0.56)