def test_parameter_in_bounds(model): """Test parameter in bounds method.""" x = np.array([0, 0.5, 1, 3]) model.names = ['x', 'y'] model.bounds = {'x': [0, 1], 'y': [0, 4]} val = Model.parameter_in_bounds(model, x, 'x') np.testing.assert_array_equal(val, np.array([True, True, True, False]))
def test_new_point_log_prob(model): """Test the log prob for new points. Should be zero. """ x = numpy_array_to_live_points(np.random.randn(2, 1), ['x']) log_prob = Model.new_point_log_prob(model, x) assert log_prob.size == 2 assert (log_prob == 0).all()
def test_in_bounds(model): """Test the `in_bounds` method. Tests both finite and infinite prior bounds. """ x = numpy_array_to_live_points(np.array([[0.5, 1], [2, 1]]), ['x', 'y']) model.names = ['x', 'y'] model.bounds = {'x': [0, 1], 'y': [-np.inf, np.inf]} val = Model.in_bounds(model, x) np.testing.assert_array_equal(val, np.array([True, False]))
def test_single_new_point(model): """Test the method that draw one point within the prior bounds""" model.names = ['x', 'y'] model.bounds = {'x': [-1, 1], 'y': [-2, 2]} model.lower_bounds = np.array([-1, -2]) model.upper_bounds = np.array([1, 2]) model.log_prior = MagicMock(return_value=0) model.dims = 2 x = Model._single_new_point(model) assert ((x['x'] >= -1) & (x['x'] <= 1)) assert ((x['y'] >= -2) & (x['y'] <= 2))
def test_likelihood_evaluations(model): """ Test `evaluate_log_likelihood` and ensure the counter increases. """ x = 1 model.likelihood_evaluations = 0 model.log_likelihood = MagicMock(return_value=2) log_l = Model.evaluate_log_likelihood(model, x) model.log_likelihood.assert_called_once_with(x) assert log_l == 2 assert model.likelihood_evaluations == 1
def test_multiple_new_points(model): """Test the method that draws multiple points within the prior bounds""" n = 10 model.names = ['x', 'y'] model.bounds = {'x': [-1, 1], 'y': [-2, 2]} model.lower_bounds = np.array([-1, -2]) model.upper_bounds = np.array([1, 2]) model.log_prior = MagicMock(return_value=np.zeros(10)) model.dims = 2 x = Model._multiple_new_points(model, N=n) assert x.size == n assert ((x['x'] >= -1) & (x['x'] <= 1)).all() assert ((x['y'] >= -2) & (x['y'] <= 2)).all()
def test_log_likelihood(model): """Verify the log likelihood raises a NotImplementedError""" with pytest.raises(NotImplementedError): Model.log_likelihood(model, 1)
def test_log_prior(model): """Verify the log prior raises a NotImplementedError""" with pytest.raises(NotImplementedError): Model.log_prior(model, 1)
def test_new_point_multiple(model): """Test the new point when asking for multiple points""" model._multiple_new_points = MagicMock() Model.new_point(model, N=10) model._multiple_new_points.assert_called_once_with(10)
def test_new_point_single(model): """Test the new point when asking for 1 point""" model._single_new_point = MagicMock() Model.new_point(model, N=1) model._single_new_point.assert_called_once()
def test_sample_parameter(model): """Assert an error is raised.""" with pytest.raises(NotImplementedError) as excinfo: Model.sample_parameter(model, 'x', n=2) assert 'User must implement this method!' in str(excinfo.value)