def test_spline_c_idx(): p = get_particle( "a", model="spline_c_idx", min_m=1.0, max_m=3.0, interp_N=20, with_bound=True, ) with variable_scope() as vm: p.init_params() for i in range(20): vm.set(f"a_point_{i}r", 1.0) vm.set(f"a_point_{i}i", 0.0) amp = p(np.array([1.0, 3.0, 2.0])) assert np.allclose(amp, 1.0) p = get_particle( "a", model="spline_c_idx", polar=False, points=[1.0, 2.0, 3.0, 4.0, 5.0], ) with variable_scope() as vm: p.init_params() for i in range(3): vm.set(f"a_point_{i}r", i) vm.set(f"a_point_{i}i", i) amp = p(np.array([1.0, 3.0, 2.0, 4.0])) assert np.allclose(amp, [0.0, 1 + 1j, 0.0, 2 + 2j])
def test_variable2(): set_config("polar", False) with variable_scope() as vm: b = Variable("R_total", cplx=True) assert vm.complex_vars["R_total"] == False set_config("polar", True) with variable_scope() as vm: b = Variable("R_total", cplx=True) assert vm.complex_vars["R_total"] == True
def test_refresh_vars(): with variable_scope() as vm: Variable("a", cplx=True) Variable("b", value=1.0) Variable("c", value=1.0, range_=[0, 3]) Variable("b", value=1.0) Variable("d", value=1.0, fix=True) vm.refresh_vars()
def test_hist_idx(): p = get_particle("a", model="hist_idx", min_m=1.0, max_m=3.0, interp_N=20) with variable_scope() as vm: p.init_params() vm.set("a_point_0r", 1.0) vm.set("a_point_0i", 0.0) vm.set("a_point_1r", 1.0) vm.set("a_point_1i", 0.0) amp = p(np.array([1.0, 1.001, 2.0, 3.0, 4.0])) assert np.allclose(amp[0:2], 1.0)
def test_minimize(): with variable_scope() as vm: m = Variable("R_m", value=2.1) vm.set_bound({"R_m": [-2, 3]}) def f(): return m() * m() ret = vm.minimize(f) print(ret) assert np.allclose(m().numpy(), 0.0)
def test_rename(): with variable_scope() as vm: Variable("a", value=1.0) vm.rename_var("a", "b") assert vm.get("b") == 1.0 assert "a" not in vm.variables a = Variable("d", cplx=True) # BUG: cannot use "a" a.set_value([2, 3]) vm.rename_var("d", "c", True) assert vm.get("cr") == 2 assert vm.get("ci") == 3
def test_minimize(): with variable_scope() as vm: m = Variable("R_m", value=2.1) vm.set_bound({"R_m": (None, 3)}) def f(): return m() * m() from scipy.optimize import minimize as mini ret = vm.minimize( f, method=lambda g, x: mini(g, x, jac=True, method="L-BFGS-B")) print(ret) assert abs(m().numpy()) < 1e-6
def test_variable(): with variable_scope() as vm: m = Variable("R_m", value=2.1) # trainable is True by default g_ls = Variable("A2BR_H", shape=[3], cplx=True) fcr = Variable("R_total", cplx=True) m1 = Variable("R1_m", value=2.3) g_ls1 = Variable("A2BR1_H", shape=[3], cplx=True) fcr1 = Variable("R1_total", cplx=True) print(g_ls.value) print(g_ls()) m.fixed(2.4) g_ls.sameas(g_ls1) fcr.r_shareto(fcr1)
def test_polar(): with variable_scope() as vm: a = Variable("a", cplx=True) b = a().numpy() vm.rp2xy_all() c = a().numpy() vm.xy2rp_all() d = a().numpy() vm.std_polar_all() e = vm.get("ai") vm.trans_params(True) f = a().numpy() assert np.allclose(b, c) assert np.allclose(c, d) assert np.allclose(d, f) assert -3.2 < e and e < 3.2
def test_minimize2(): data = np.linspace(-1.5, 1.5, 10000) w = tf.cos(data) + 1 np.random.seed(2) cut = np.random.random(data.shape) * np.max(w) * 1.1 < w data = data[cut] phsp = np.linspace(-1.5, 1.5, 10000) with variable_scope() as vm: a = Variable("R_m", cplx=True) vm.set_bound({"R_mr": (-2, None)}) def f(x): fx = tf.cos(x) + tf.abs(a()) return fx ret = vm.minimize(nll_funciton(f, data, phsp)) print(ret) assert abs(float(np.abs(a())) - 1.0) < 0.2