def test_poisson_conf_array_rootn0_zero(): n = np.zeros((3, 4, 5)) assert_allclose(funcs.poisson_conf_interval(n, interval='root-n-0'), funcs.poisson_conf_interval(n[0, 0, 0], interval='root-n-0')[:, None, None, None] * np.ones_like(n)) # noqa: E501 assert not np.any(np.isnan( funcs.poisson_conf_interval(n, interval='root-n-0')))
def test_poisson_conf_list_rootn0_zero(): n = [0, 0, 0] assert_allclose(funcs.poisson_conf_interval(n, interval='root-n-0'), [[0, 0, 0], [1, 1, 1]]) assert not np.any(np.isnan( funcs.poisson_conf_interval(n, interval='root-n-0')))
def test_poisson_conf_kbn_value_errors(): with pytest.raises(ValueError, match='number between 0 and 1'): funcs.poisson_conf_interval(5, 'kraft-burrows-nousek', background=2.5, confidence_level=99) with pytest.raises(ValueError, match='Set confidence_level for method'): funcs.poisson_conf_interval(5, 'kraft-burrows-nousek', background=2.5) with pytest.raises(ValueError, match='Background must be'): funcs.poisson_conf_interval(5, 'kraft-burrows-nousek', background=-2.5, confidence_level=.99) with pytest.raises(TypeError, match='Number of counts must be integer'): funcs.poisson_conf_interval(5., 'kraft-burrows-nousek', background=2.5, confidence_level=.99) with pytest.raises(TypeError, match='Number of counts must be integer'): funcs.poisson_conf_interval([5., 6.], 'kraft-burrows-nousek', background=[2.5, 2.], confidence_level=[.99, .9])
def test_poisson_conf_list_rootn0_zero(): n = [0, 0, 0] assert_allclose(funcs.poisson_conf_interval(n, interval='root-n-0'), [[0, 0, 0], [1, 1, 1]]) assert not np.any( np.isnan(funcs.poisson_conf_interval(n, interval='root-n-0')))
def test_poisson_limit_nodependencies(): with pytest.raises(ImportError): with pytest.warns(AstropyDeprecationWarning): funcs.poisson_conf_interval(20., interval='kraft-burrows-nousek', background=10., conflevel=.95)
def test_poisson_conf_array_rootn0_zero(): n = np.zeros((3, 4, 5)) assert_allclose(funcs.poisson_conf_interval(n, interval='root-n-0'), funcs.poisson_conf_interval(n[0, 0, 0], interval='root-n-0')[:, None, None, None] * np.ones_like(n)) assert not np.any(np.isnan( funcs.poisson_conf_interval(n, interval='root-n-0')))
def test_poisson_conf_array_frequentist_confidence_zero(): n = np.zeros((3, 4, 5)) assert_allclose( funcs.poisson_conf_interval(n, interval='frequentist-confidence'), funcs.poisson_conf_interval(n[0, 0, 0], interval='frequentist-confidence')[:, None, None, None] * np.ones_like(n)) assert not np.any(np.isnan( funcs.poisson_conf_interval(n, interval='root-n-0')))
def test_poisson_conf_array_rootn0(): n = 7 * np.ones((3, 4, 5)) assert_allclose(funcs.poisson_conf_interval(n, interval='root-n-0'), funcs.poisson_conf_interval(n[0, 0, 0], interval='root-n-0')[:, None, None, None] * np.ones_like(n)) n[1, 2, 3] = 0 assert not np.any(np.isnan( funcs.poisson_conf_interval(n, interval='root-n-0')))
def test_poisson_conf_array_fc(): n = 7 * np.ones((3, 4, 5)) assert_allclose( funcs.poisson_conf_interval(n, interval='frequentist-confidence'), funcs.poisson_conf_interval(n[0, 0, 0], interval='frequentist-confidence')[:, None, None, None] * np.ones_like(n)) n[1, 2, 3] = 0 assert not np.any(np.isnan( funcs.poisson_conf_interval(n, interval='frequentist-confidence')))
def test_poisson_conf_array_fc(): n = 7 * np.ones((3, 4, 5)) assert_allclose( funcs.poisson_conf_interval(n, interval='frequentist-confidence'), funcs.poisson_conf_interval(n[0, 0, 0], interval='frequentist-confidence')[:, None, None, None] * np.ones_like(n)) # noqa: E501 n[1, 2, 3] = 0 assert not np.any(np.isnan( funcs.poisson_conf_interval(n, interval='frequentist-confidence')))
def test_mpmath_poisson_limit(): assert_allclose(funcs._mpmath_kraft_burrows_nousek(1., .1, .99), (0.00, 6.54), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(1., .5, .95), (0.00, 4.36), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(5., 0., .99), (1.17, 13.32), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(5., 2.5, .99), (0, 10.67), rtol=1e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(np.int32(6), 2., .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(np.int64(6), 2., .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., np.float32(2.), .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., np.float64(2.), .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., 2., np.float32(.9)), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., 2., np.float64(.9)), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(5., 2.5, .99), (0, 10.67), rtol=1e-3) assert_allclose(funcs.poisson_conf_interval( n=160, background=154.543, confidence_level=.95, interval='kraft-burrows-nousek')[:, 0], (0, 30.30454909)) # For this one we do not have the "true" answer from the publication, # but we want to make sure that it at least runs without error # see https://github.com/astropy/astropy/issues/9596 _ = funcs._mpmath_kraft_burrows_nousek(1000., 900., .9)
def test_scipy_poisson_limit(): '''Test that the lower-level routine gives the snae number. Test numbers are from table1 1, 3 in Kraft, Burrows and Nousek in `ApJ 374, 344 (1991) <https://ui.adsabs.harvard.edu/abs/1991ApJ...374..344K>`_ ''' assert_allclose(funcs._scipy_kraft_burrows_nousek(5, 2.5, .99), (0, 10.67), rtol=1e-3) assert_allclose(funcs._scipy_kraft_burrows_nousek(np.int32(5.), 2.5, .99), (0, 10.67), rtol=1e-3) assert_allclose(funcs._scipy_kraft_burrows_nousek(np.int64(5.), 2.5, .99), (0, 10.67), rtol=1e-3) assert_allclose(funcs._scipy_kraft_burrows_nousek(5, np.float32(2.5), .99), (0, 10.67), rtol=1e-3) assert_allclose(funcs._scipy_kraft_burrows_nousek(5, np.float64(2.5), .99), (0, 10.67), rtol=1e-3) assert_allclose(funcs._scipy_kraft_burrows_nousek(5, 2.5, np.float32(.99)), (0, 10.67), rtol=1e-3) assert_allclose(funcs._scipy_kraft_burrows_nousek(5, 2.5, np.float64(.99)), (0, 10.67), rtol=1e-3) conf = funcs.poisson_conf_interval([5, 6], 'kraft-burrows-nousek', background=[2.5, 2.], confidence_level=[.99, .9]) assert_allclose(conf[:, 0], (0, 10.67), rtol=1e-3) assert_allclose(conf[:, 1], (0.81, 8.99), rtol=5e-3)
def test_mpmath_poisson_limit(): assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., 2., .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(5., 2.5, .99), (0, 10.67), rtol=1e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(np.int32(6), 2., .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(np.int64(6), 2., .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., np.float32(2.), .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., np.float64(2.), .9), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., 2., np.float32(.9)), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(6., 2., np.float64(.9)), (0.81, 8.99), rtol=5e-3) assert_allclose(funcs._mpmath_kraft_burrows_nousek(5., 2.5, .99), (0, 10.67), rtol=1e-3) assert_allclose( funcs.poisson_conf_interval(n=160, background=154.543, confidence_level=.95, interval='kraft-burrows-nousek')[:, 0], (0, 30.30454909))
def test_poisson_conf_value_errors(): with pytest.raises(ValueError, match='Only sigma=1 supported'): funcs.poisson_conf_interval([5, 6], 'root-n', sigma=2) with pytest.raises(ValueError, match='background not supported'): funcs.poisson_conf_interval([5, 6], 'pearson', background=[2.5, 2.]) with pytest.raises(ValueError, match='confidence_level not supported'): funcs.poisson_conf_interval([5, 6], 'sherpagehrels', confidence_level=[2.5, 2.]) with pytest.raises(ValueError, match='Invalid method'): funcs.poisson_conf_interval(1, 'foo')
def test_scipy_poisson_limit(): '''Test that the lower-level routine gives the snae number. Test numbers are from table1 1, 3 in Kraft, Burrows and Nousek in `ApJ 374, 344 (1991) <http://adsabs.harvard.edu/abs/1991ApJ...374..344K>`_ ''' assert_allclose(funcs._scipy_kraft_burrows_nousek(5., 2.5, .99), (0, 10.67), rtol=1e-3) conf = funcs.poisson_conf_interval([5., 6.], 'kraft-burrows-nousek', background=[2.5, 2.], conflevel=[.99, .9]) assert_allclose(conf[:, 0], (0, 10.67), rtol=1e-3) assert_allclose(conf[:, 1], (0.81, 8.99), rtol=5e-3)
def test_poisson_conf_value_errors(): with pytest.raises(ValueError) as e: funcs.poisson_conf_interval([5, 6], 'root-n', sigma=2) assert 'Only sigma=1 supported' in str(e.value) with pytest.raises(ValueError) as e: funcs.poisson_conf_interval([5, 6], 'pearson', background=[2.5, 2.]) assert 'background not supported' in str(e.value) with pytest.raises(ValueError) as e: funcs.poisson_conf_interval([5, 6], 'sherpagehrels', conflevel=[2.5, 2.]) assert 'conflevel not supported' in str(e.value) with pytest.raises(ValueError) as e: funcs.poisson_conf_interval(1, 'foo') assert 'Invalid method' in str(e.value)
def test_poisson_conf_frequentist_confidence_gehrels_2sigma(): """Test intervals against those published in Gehrels 1986 Note: I think there's a typo (transposition of digits) in Gehrels 1986, specifically for the two-sigma lower limit for 3 events; they claim 0.569 but this function returns 0.59623... """ nlh = np.array([(0, 2, 0, 3.783), (1, 2, 2.30e-2, 5.683), (2, 2, 0.230, 7.348), (3, 2, 0.596, 8.902), (4, 2, 1.058, 10.39), (5, 2, 1.583, 11.82), (6, 2, 2.153, 13.22), (7, 2, 2.758, 14.59), (8, 2, 3.391, 15.94), (9, 2, 4.046, 17.27), (10, 2, 4.719, 18.58)]) assert_allclose(funcs.poisson_conf_interval( nlh[:, 0], sigma=2, interval='frequentist-confidence').T, nlh[:, 2:], rtol=0.01)
def test_poisson_conf_frequentist_confidence_gehrels(): """Test intervals against those published in Gehrels 1986""" nlh = np.array([(0, 0, 1.841), (1, 0.173, 3.300), (2, 0.708, 4.638), (3, 1.367, 5.918), (4, 2.086, 7.163), (5, 2.840, 8.382), (6, 3.620, 9.584), (7, 4.419, 10.77), (8, 5.232, 11.95), (9, 6.057, 13.11), (10, 6.891, 14.27), ]) assert_allclose( funcs.poisson_conf_interval(nlh[:, 0], interval='frequentist-confidence'), nlh[:, 1:].T, rtol=0.001, atol=0.001)
def test_poisson_conf_frequentist_confidence_gehrels_3sigma(): """Test intervals against those published in Gehrels 1986""" nlh = np.array([(0, 3, 0, 6.608), (1, 3, 1.35e-3, 8.900), (2, 3, 5.29e-2, 10.87), (3, 3, 0.212, 12.68), (4, 3, 0.465, 14.39), (5, 3, 0.792, 16.03), (6, 3, 1.175, 17.62), (7, 3, 1.603, 19.17), (8, 3, 2.068, 20.69), (9, 3, 2.563, 22.18), (10, 3, 3.084, 23.64), ]) assert_allclose( funcs.poisson_conf_interval(nlh[:, 0], sigma=3, interval='frequentist-confidence').T, nlh[:, 2:], rtol=0.01, verbose=True)
def test_poisson_conf_kbn_value_errors(): with pytest.raises(ValueError) as e: funcs.poisson_conf_interval(5., 'kraft-burrows-nousek', background=2.5, confidence_level=99) assert 'number between 0 and 1' in str(e.value) with pytest.raises(ValueError) as e: funcs.poisson_conf_interval(5., 'kraft-burrows-nousek', background=2.5) assert 'Set confidence_level for method' in str(e.value) with pytest.raises(ValueError) as e: funcs.poisson_conf_interval(5., 'kraft-burrows-nousek', background=-2.5, confidence_level=.99) assert 'Background must be' in str(e.value)
def test_poisson_conf_kbn_value_errors(): with pytest.raises(ValueError) as e: funcs.poisson_conf_interval(5., 'kraft-burrows-nousek', background=2.5, conflevel=99) assert 'number between 0 and 1' in str(e.value) with pytest.raises(ValueError) as e: funcs.poisson_conf_interval(5., 'kraft-burrows-nousek', background=2.5) assert 'Set conflevel for method' in str(e.value) with pytest.raises(ValueError) as e: funcs.poisson_conf_interval(5., 'kraft-burrows-nousek', background=-2.5, conflevel=.99) assert 'Background must be' in str(e.value)
def test_poisson_conf_frequentist_confidence_gehrels_2sigma(): """Test intervals against those published in Gehrels 1986 Note: I think there's a typo (transposition of digits) in Gehrels 1986, specifically for the two-sigma lower limit for 3 events; they claim 0.569 but this function returns 0.59623... """ nlh = np.array([(0, 2, 0, 3.783), (1, 2, 2.30e-2, 5.683), (2, 2, 0.230, 7.348), (3, 2, 0.596, 8.902), (4, 2, 1.058, 10.39), (5, 2, 1.583, 11.82), (6, 2, 2.153, 13.22), (7, 2, 2.758, 14.59), (8, 2, 3.391, 15.94), (9, 2, 4.046, 17.27), (10, 2, 4.719, 18.58)]) assert_allclose( funcs.poisson_conf_interval(nlh[:, 0], sigma=2, interval='frequentist-confidence').T, nlh[:, 2:], rtol=0.01)
def test_poisson_conf_gehrels86(n): assert_allclose( funcs.poisson_conf_interval(n, interval='sherpagehrels')[1], funcs.poisson_conf_interval(n, interval='frequentist-confidence')[1], rtol=0.02)
def test_poisson_conf_large(interval): n = 100 assert_allclose(funcs.poisson_conf_interval(n, interval='root-n'), funcs.poisson_conf_interval(n, interval=interval), rtol=2e-2)
def test_poisson_conf_interval_rootn(): assert_allclose(funcs.poisson_conf_interval(16, interval='root-n'), (12, 20))
def test_poisson_limit_nodependencies(): with pytest.raises(ImportError): funcs.poisson_conf_interval(20., interval='kraft-burrows-nousek', background=10., conflevel=.95)
def test_poisson_limit_nodependencies(): with pytest.raises(ImportError): funcs.poisson_conf_interval(20, interval='kraft-burrows-nousek', background=10., confidence_level=.95)