Coverage for pesummary/tests/bounded_kde_test.py: 100.0%

39 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3from pesummary.utils.bounded_1d_kde import ReflectionBoundedKDE, bounded_1d_kde 

4from pesummary.utils.bounded_2d_kde import Bounded_2d_kde 

5from scipy.stats import gaussian_kde 

6import numpy as np 

7import pytest 

8 

9__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

10 

11 

12class TestBounded_kde(object): 

13 """Test the ReflectionBoundedKDE function 

14 """ 

15 def test_bounded_1d_kde(self): 

16 samples = np.array(np.random.uniform(10, 5, 1000)) 

17 x_low = 9.5 

18 x_high = 10.5 

19 scipy = gaussian_kde(samples) 

20 bounded = ReflectionBoundedKDE(samples, xlow=x_low, xhigh=x_high) 

21 assert scipy(9.45) != 0. 

22 assert bounded(9.45) == 0. 

23 assert scipy(10.55) != 0. 

24 assert bounded(10.55) == 0. 

25 bounded = bounded_1d_kde(samples, xlow=x_low, xhigh=x_high, method="Transform") 

26 assert bounded(10.55) == 0. 

27 assert bounded(9.45) == 0 

28 

29 def test_bounded_2d_kde(self): 

30 samples = np.array([ 

31 np.random.uniform(10, 5, 1000), 

32 np.random.uniform(5, 2, 1000) 

33 ]) 

34 x_low = 9.5 

35 x_high = 10.5 

36 y_low = 4.5 

37 y_high = 5.5 

38 scipy = gaussian_kde(samples) 

39 bounded = Bounded_2d_kde( 

40 samples, xlow=x_low, xhigh=x_high, ylow=y_low, yhigh=y_high 

41 ) 

42 assert scipy([9.45, 4.45]) != 0. 

43 assert bounded([9.45, 4.45]) == 0. 

44 assert scipy([9.45, 5.55]) != 0. 

45 assert bounded([9.45, 5.55]) == 0. 

46 

47 assert scipy([10.55, 4.45]) != 0. 

48 assert bounded([10.55, 4.45]) == 0. 

49 assert scipy([10.55, 5.55]) != 0. 

50 assert bounded([10.55, 5.55]) == 0. 

51 

52 with pytest.raises(AssertionError): 

53 np.testing.assert_almost_equal(scipy([[9.45, 10.55], [5., 5.]]), [0., 0.]) 

54 np.testing.assert_almost_equal(bounded([[9.45, 10.55], [5., 5.]]), [0., 0.])