def test_invertibility(reparam, angle, radial=None): x = np.zeros([n], dtype=get_dtype(reparam.parameters)) x_prime = np.zeros([n], dtype=get_dtype(reparam.prime_parameters)) log_j = 0 x[reparam.angle] = angle if radial is not None: x[reparam.radial] = radial assert x.size == x_prime.size x_re, x_prime_re, log_j_re = reparam.reparameterise(x, x_prime, log_j) np.testing.assert_array_equal(x[reparam.angle], x_re[reparam.angle]) if radial is not None: np.testing.assert_array_equal(x[reparam.radial], x_re[reparam.radial]) x_in = np.zeros([n], dtype=get_dtype(reparam.parameters)) x_inv, x_prime_inv, log_j_inv = \ reparam.inverse_reparameterise(x_in, x_prime_re, log_j) np.testing.assert_array_almost_equal(x[reparam.angle], x_inv[reparam.angle]) if radial is not None: np.testing.assert_array_almost_equal(x[reparam.radial], x_inv[reparam.radial]) np.testing.assert_array_equal(x_prime_re, x_prime_inv) np.testing.assert_array_almost_equal(log_j_re, -log_j_inv) return True
def test_inverse_reparameterise(reparam): """Test the inverse_reparameterise function""" reparam.has_pre_rescaling = False reparam.has_post_rescaling = False reparam.parameters = ['x'] reparam.prime_parameters = ['x_prime'] reparam.offsets = {'x': 1.0} reparam.boundary_inversion = {} x_prime = numpy_array_to_live_points(np.array([(1.0, ), (2.0, )]), reparam.prime_parameters) x_in = np.zeros([ 2, ], dtype=get_dtype(reparam.parameters)) x_val = np.array([0.0, 0.5]) log_j = np.zeros(x_prime.size) reparam._inverse_rescale_to_bounds = MagicMock( return_value=(x_val, np.array([0, 0.5]))) x_out, x_prime_out, log_j_out = \ RescaleToBounds.inverse_reparameterise(reparam, x_in, x_prime, log_j) # x[p] is updated in place, can't test inputs reparam._inverse_rescale_to_bounds.assert_called_once() assert \ reparam._inverse_rescale_to_bounds.call_args_list[0][0][1] == 'x' np.testing.assert_array_equal(x_prime_out, x_prime) np.testing.assert_array_equal(x_out['x'], x_val + 1.0) np.testing.assert_array_equal(log_j_out, np.array([0.0, 0.5]))
def test_reparameterise(reparam): """Test the reparameterise function""" reparam.has_pre_rescaling = False reparam.has_post_rescaling = False reparam.parameters = ['x'] reparam.prime_parameters = ['x_prime'] reparam.offsets = {'x': 1.0} reparam.boundary_inversion = {} x = numpy_array_to_live_points(np.array([(1.0, ), (2.0, )]), reparam.parameters) x_prime_in = np.zeros([ 2, ], dtype=get_dtype(reparam.prime_parameters)) x_prime_val = np.array([0.0, 0.5]) log_j = np.zeros(x.size) reparam._rescale_to_bounds = MagicMock(return_value=(x_prime_val, np.array([0, 0.5]))) x_out, x_prime_out, log_j_out = \ RescaleToBounds.reparameterise(reparam, x, x_prime_in, log_j) np.testing.assert_array_equal( np.array([0.0, 1.0]), reparam._rescale_to_bounds.call_args_list[0][0][0]) assert reparam._rescale_to_bounds.call_args_list[0][0][1] == 'x' np.testing.assert_array_equal(x, x_out) np.testing.assert_array_equal(x_prime_out['x_prime'], x_prime_val) np.testing.assert_array_equal(log_j_out, np.array([0.0, 0.5]))
def test_invertiblity(): """Ensure the reparameterisation does not change the values""" reparam = NullReparameterisation(parameters=['x', 'y']) n = 100 x = numpy_array_to_live_points(np.random.rand(n, 2), reparam.parameters) x_prime = np.zeros([n], dtype=get_dtype(reparam.prime_parameters)) log_j = 0 x_re, x_prime_re, log_j_re = reparam.reparameterise(x, x_prime, log_j) assert_array_equal(x, x_re) assert_array_equal(x, x_prime_re) x_in = np.zeros([n], dtype=get_dtype(reparam.parameters)) x_inv, x_prime_inv, log_j_inv = \ reparam.inverse_reparameterise(x_in, x_prime_re, log_j) assert_array_equal(x, x_inv) assert_array_equal(x, x_prime_inv) assert log_j == log_j_re == log_j_inv
def test_invertibility(reparam, model=model, decimal=16): x = model.new_point(N=n) x_prime = np.zeros([n], dtype=get_dtype(reparam.prime_parameters)) log_j = np.zeros(n) assert x.size == x_prime.size x_re, x_prime_re, log_j_re = reparam.reparameterise(x, x_prime, log_j) x_in = np.zeros([x_re.size], dtype=get_dtype(reparam.parameters)) log_j = np.zeros(x_re.size) x_inv, x_prime_inv, log_j_inv = \ reparam.inverse_reparameterise(x_in, x_prime_re, log_j) m = x_re.size // n for i in range(m): start, end = (i * n), (i + 1) * n for name in x.dtype.names: np.testing.assert_array_almost_equal( x[name], x_re[name][start:end], decimal=decimal, ) np.testing.assert_array_almost_equal( x[name], x_inv[name][start:end], decimal=decimal, ) for name in x_prime.dtype.names: np.testing.assert_array_almost_equal( x_prime_re[name], x_prime_inv[name], decimal=decimal, ) np.testing.assert_array_almost_equal(log_j_re, -log_j_inv, decimal=decimal) return True
def test_invertibility(reparam, angles, radial=None): n = list(angles.values())[0].size x = np.zeros([n], dtype=get_dtype(reparam.parameters)) x_prime = np.zeros([n], dtype=get_dtype(reparam.prime_parameters)) log_j = 0 for a in reparam.angles: x[a] = angles[a] if radial is not None: x[reparam.radial] = radial assert x.size == x_prime.size x_re, x_prime_re, log_j_re = reparam.reparameterise( x, x_prime, log_j) for a in reparam.angles: np.testing.assert_array_equal(x[a], x_re[a]) if radial is not None: np.testing.assert_array_equal(x[reparam.radial], x_re[reparam.radial]) x_in = np.zeros([n], dtype=get_dtype(reparam.parameters)) x_inv, x_prime_inv, log_j_inv = \ reparam.inverse_reparameterise(x_in, x_prime_re, log_j) for a in reparam.angles: np.testing.assert_array_almost_equal(x[a], x_inv[a]) if radial is not None: np.testing.assert_array_almost_equal(x[reparam.radial], x_inv[reparam.radial]) np.testing.assert_array_equal(x_prime_re, x_prime_inv) np.testing.assert_array_almost_equal(log_j_re, -log_j_inv) return True
def test_reparameterise_boundary_inversion(reparam): """Test the reparameterise function with boundary inversion""" reparam.has_pre_rescaling = False reparam.has_post_rescaling = False reparam.parameters = ['x'] reparam.prime_parameters = ['x_prime'] reparam.offsets = {'x': 1.0} reparam.boundary_inversion = {'x': 'split'} x = numpy_array_to_live_points(np.array([(1.0, ), (2.0, )]), reparam.parameters) inversion_out = numpy_array_to_live_points( np.array([(-1.0, ), (-2.0, ), (1.0, ), (2.0, )]), reparam.prime_parameters) x_prime_in = np.zeros([ 2, ], dtype=get_dtype(reparam.prime_parameters)) log_j = np.zeros(x.size) x_ex = np.concatenate([x, x]) x_prime_ex = inversion_out log_j_ex = np.array([0, 0.5, 0, 0.5]) reparam._apply_inversion = MagicMock(return_value=(x_ex, x_prime_ex, log_j_ex)) x_out, x_prime_out, log_j_out = RescaleToBounds.reparameterise( reparam, x, x_prime_in, log_j, compute_radius=True, test='test', ) np.testing.assert_array_equal( reparam._apply_inversion.call_args_list[0][0][0], x) np.testing.assert_array_equal( reparam._apply_inversion.call_args_list[0][0][1], x_prime_in, ) np.testing.assert_array_equal( reparam._apply_inversion.call_args_list[0][0][2], log_j) assert reparam._apply_inversion.call_args_list[0][0][3] == 'x' assert reparam._apply_inversion.call_args_list[0][0][4] == 'x_prime' assert reparam._apply_inversion.call_args_list[0][0][5] is True assert reparam._apply_inversion.call_args_list[0][1] == {'test': 'test'} np.testing.assert_array_equal(x_out, x_ex) np.testing.assert_array_equal(x_prime_out, x_prime_ex) np.testing.assert_array_equal(log_j_out, log_j_ex)
def test_add_multiple_reparameterisations(model): """ Test adding multiple reparameterisations and using the reparameterisation. """ r = [ RescaleToBounds(parameters='x', prior_bounds=model.bounds['x'], prior='uniform'), RescaleToBounds(parameters='y', prior_bounds=model.bounds['y'], prior='uniform') ] reparam = CombinedReparameterisation() reparam.add_reparameterisations(r) assert reparam.parameters == ['x', 'y'] assert reparam.has_prime_prior is True n = 100 x = model.new_point(N=n) x_prime = np.zeros([n], dtype=get_dtype(reparam.prime_parameters)) log_j = 0 x_re, x_prime_re, log_j_re = reparam.reparameterise(x, x_prime, log_j) assert reparam.x_prime_log_prior(x_prime_re) is not None np.testing.assert_array_equal(x, x_re) x_in = np.zeros([n], dtype=get_dtype(reparam.parameters)) x_inv, x_prime_inv, log_j_inv = \ reparam.inverse_reparameterise(x_in, x_prime_re, log_j) np.testing.assert_array_equal(x, x_inv) np.testing.assert_array_equal(x_prime_re, x_prime_inv) np.testing.assert_array_equal(log_j_re, -log_j_inv)
def test_inverse_reparameterise_pre_post_rescaling(reparam): """Test the inverse_reparameterise function with pre and post rescaling""" reparam.has_pre_rescaling = True reparam.has_post_rescaling = True reparam.parameters = ['x'] reparam.prime_parameters = ['x_prime'] reparam.offsets = {'x': 0.0} reparam.boundary_inversion = {} x_prime = numpy_array_to_live_points(np.array([(1.0, ), (2.0, )]), reparam.prime_parameters) x_in = np.zeros([ 2, ], dtype=get_dtype(reparam.parameters)) x_val = np.array([4.0, 8.0]) log_j = np.zeros(x_prime.size) reparam._inverse_rescale_to_bounds = MagicMock( return_value=(x_val, np.array([0, 0.5]))) reparam.pre_rescaling_inv = MagicMock(return_value=(np.array([0.5, 1.0]), np.array([0.5, 0.5]))) reparam.post_rescaling_inv = MagicMock(return_value=(np.array([1.0, 1.5]), np.array([2.0, 3.0]))) x_out, x_prime_out, log_j_out = \ RescaleToBounds.inverse_reparameterise(reparam, x_in, x_prime, log_j) np.testing.assert_array_equal( np.array([0.5, 1.0]), reparam._inverse_rescale_to_bounds.call_args_list[0][0][0]) assert reparam._inverse_rescale_to_bounds.call_args_list[0][0][1] == 'x' np.testing.assert_array_equal( reparam.post_rescaling_inv.call_args_list[0][0][0], x_prime['x_prime'], ) reparam.pre_rescaling_inv.assert_called_once() # x_prime gets replaces so change check inputs to the function reparam.post_rescaling_inv.assert_called_once() np.testing.assert_array_equal(x_prime, x_prime_out) np.testing.assert_array_equal(x_out['x'], np.array([0.5, 1.0])) np.testing.assert_array_equal(log_j_out, np.array([2.5, 4.0]))
def test_inverse_reparameterise_boundary_inversion(reparam): """Test the inverse_reparameterise function with boundary inversion""" reparam.has_pre_rescaling = False reparam.has_post_rescaling = False reparam.parameters = ['x'] reparam.prime_parameters = ['x'] reparam.offsets = {'x': 1.0} reparam.boundary_inversion = {'x': 'split'} x_prime = numpy_array_to_live_points(np.array([(-1.0, ), (2.0, )]), reparam.prime_parameters) inversion_out = numpy_array_to_live_points(np.array([(1.0, ), (2.0, )]), reparam.parameters) x_in = np.zeros([ 2, ], dtype=get_dtype(reparam.parameters)) log_j = np.zeros(x_prime.size) x_ex = inversion_out x_prime_ex = x_prime log_j_ex = np.array([0, 0.5]) reparam._reverse_inversion = MagicMock(return_value=(x_ex, x_prime_ex, log_j_ex)) x_out, x_prime_out, log_j_out = RescaleToBounds.inverse_reparameterise( reparam, x_in, x_prime, log_j, ) np.testing.assert_array_equal( reparam._reverse_inversion.call_args_list[0][0][0], x_in) np.testing.assert_array_equal( reparam._reverse_inversion.call_args_list[0][0][1], x_prime) np.testing.assert_array_equal( reparam._reverse_inversion.call_args_list[0][0][2], log_j) np.testing.assert_array_equal(x_out, x_ex) np.testing.assert_array_equal(x_prime_out, x_prime_ex) np.testing.assert_array_equal(log_j_out, log_j_ex)