Exemple #1
0
def test_apply_inversion_not_applied(reparam):
    """Assert the apply inversion works correctly"""
    reparam.parameters = ['x']
    reparam.prime_parameters = ['x_prime']
    reparam.offsets = {'x': 1.0}
    reparam._edges = {'x': False}
    reparam.bounds = {'x': [0, 5]}
    reparam.rescale_bounds = {'x': [0, 1]}

    x_val = np.array([[1], [2]])
    x_prime = numpy_array_to_live_points(x_val, ['x_prime'])
    x = numpy_array_to_live_points(np.array([3, 4]), ['x'])
    log_j = np.zeros(2)

    with patch('nessai.reparameterisations.rescale_minus_one_to_one',
               side_effect=lambda x, *args, **kwargs:
               (x, np.array([5, 6]))) as f:
        x_out, x_prime_out, log_j_out = RescaleToBounds._apply_inversion(
            reparam,
            x,
            x_prime,
            log_j,
            'x',
            'x_prime',
            False,
        )

    assert f.call_args_list[0][1] == {'xmin': 0, 'xmax': 5}
    # Should be output of rescaling minus offset
    np.testing.assert_array_equal(x_prime_out['x_prime'], np.array([0, 1]))
    # x_prime should be the same
    assert x_out is x
    # Jacobian should just include jacobian from rescaling
    np.testing.assert_array_equal(log_j_out, np.array([5, 6]))
Exemple #2
0
def test_apply_inversion_detect_edge(reparam):
    """Assert detect edge is called with the correct arguments"""
    reparam.parameters = ['x']
    reparam.prime_parameters = ['x_prime']
    reparam.offsets = {'x': 1.0}
    reparam._edges = {'x': None}
    reparam.detect_edges_kwargs = {'allowed_bounds': ['lower']}
    reparam.bounds = {'x': [0, 5]}
    reparam.rescale_bounds = {'x': [0, 1]}

    x = numpy_array_to_live_points(np.array([1, 2]), ['x'])
    x_prime = numpy_array_to_live_points(np.array([3, 4]), ['x_prime'])
    log_j = np.zeros(2)

    with patch('nessai.reparameterisations.detect_edge',
               return_value=False) as mock_fn:

        _ = RescaleToBounds._apply_inversion(reparam,
                                             x,
                                             x_prime,
                                             log_j,
                                             'x',
                                             'x_prime',
                                             False,
                                             test=True)

    reparam.update_prime_prior_bounds.assert_called_once()
    mock_fn.assert_called_once_with(
        x_prime['x_prime'],
        test=True,
        allowed_bounds=['lower'],
    )

    assert reparam._edges == {'x': False}
Exemple #3
0
def test_apply_inversion_duplicate(reparam, inv_type, compute_radius):
    """Assert apply inversion with duplicate works as intended.

    This test also covers compute_radius=True
    """
    reparam.parameters = ['x', 'y']
    reparam.prime_parameters = ['x_prime', 'y']
    reparam.offsets = {'x': 1.0}
    reparam._edges = {'x': 'lower'}
    reparam.bounds = {'x': [0, 5]}
    reparam.rescale_bounds = {'x': [0, 1]}
    reparam.boundary_inversion = {'x': inv_type}

    x_val = np.array([[1.2, 1.0], [1.7, 2.0]])
    x_prime = numpy_array_to_live_points(x_val, ['x_prime', 'y'])
    x = numpy_array_to_live_points(np.array([[1, 2], [3, 4]]), ['x', 'y'])
    log_j = np.zeros(2)

    with patch('numpy.random.choice') as rnd, \
         patch('nessai.reparameterisations.rescale_zero_to_one',
               side_effect=lambda x, *args: (x, np.array([5, 6]))) as f:
        x_out, x_prime_out, log_j_out = RescaleToBounds._apply_inversion(
            reparam,
            x,
            x_prime,
            log_j,
            'x',
            'x_prime',
            compute_radius,
        )

    rnd.assert_not_called()
    assert f.call_args_list[0][0][1] == 0.0
    assert f.call_args_list[0][0][2] == 5.0
    # Output should be x_val minus offset
    # Then duplicated
    np.testing.assert_array_almost_equal(
        x_prime_out['x_prime'],
        np.array([0.2, 0.7, -0.2, -0.7]),
        decimal=10,
    )
    np.testing.assert_array_almost_equal(
        x_prime_out['y'],
        np.array([1.0, 2.0, 1.0, 2.0]),
        decimal=10,
    )
    # x should be the same but duplicated
    np.testing.assert_array_equal(x_out, np.concatenate([x, x]))
    # Jacobian should just include jacobian from rescaling but duplicated
    np.testing.assert_array_equal(log_j_out, np.array([5, 6, 5, 6]))
Exemple #4
0
def test_apply_inversion_split(reparam):
    """Assert apply inversion with split works as intended.

    Also tests "upper" setting.
    """
    reparam.parameters = ['x']
    reparam.prime_parameters = ['x_prime']
    reparam.offsets = {'x': 1.0}
    reparam._edges = {'x': 'upper'}
    reparam.bounds = {'x': [0, 5]}
    reparam.rescale_bounds = {'x': [0, 1]}
    reparam.boundary_inversion = {'x': 'split'}

    x_val = np.array([[1.2], [1.7]])
    x_prime = numpy_array_to_live_points(x_val, ['x_prime'])
    x = numpy_array_to_live_points(np.array([3, 4]), ['x'])
    log_j = np.zeros(2)

    with patch('numpy.random.choice', return_value=np.array([1])) as rnd, \
         patch('nessai.reparameterisations.rescale_zero_to_one',
               side_effect=lambda x, *args: (x, np.array([5, 6]))) as f:
        x_out, x_prime_out, log_j_out = RescaleToBounds._apply_inversion(
            reparam,
            x,
            x_prime,
            log_j,
            'x',
            'x_prime',
            False,
        )

    rnd.assert_called_once_with(2, 1, replace=False)
    assert f.call_args_list[0][0][1] == 0.0
    assert f.call_args_list[0][0][2] == 5.0
    # Output should be x_val minus offset
    # Then 1 - that for 'upper'
    # Then *= -1 with the output of rnd
    np.testing.assert_array_almost_equal(
        x_prime_out['x_prime'],
        np.array([0.8, -0.3]),
        decimal=10,
    )
    # x should be the same
    assert x_out is x
    # Jacobian should just include jacobian from rescaling
    np.testing.assert_array_equal(log_j_out, np.array([5, 6]))