Beispiel #1
0
    def test_multiple_percentiles(self):
        perc = [50, 100]
        mat = np.ones((4, 3))
        nan_mat = np.nan * mat
        # For checking consistency in higher dimensional case
        large_mat = np.ones((3, 4, 5))
        large_mat[:, 0:2:4, :] = 0
        large_mat[:, :, 3:] *= 2
        for axis in [None, 0, 1]:
            for keepdim in [False, True]:
                with suppress_warnings() as sup:
                    sup.filter(RuntimeWarning, "All-NaN slice encountered")
                    val = np.percentile(mat, perc, axis=axis, keepdims=keepdim)
                    nan_val = np.nanpercentile(nan_mat, perc, axis=axis,
                                               keepdims=keepdim)
                    assert_equal(nan_val.shape, val.shape)

                    val = np.percentile(large_mat, perc, axis=axis,
                                        keepdims=keepdim)
                    nan_val = np.nanpercentile(large_mat, perc, axis=axis,
                                               keepdims=keepdim)
                    assert_equal(nan_val, val)

        megamat = np.ones((3, 4, 5, 6))
        assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6))
Beispiel #2
0
 def test_result_values(self):
     tgt = [np.percentile(d, 28) for d in _rdat]
     res = np.nanpercentile(_ndat, 28, axis=1)
     assert_almost_equal(res, tgt)
     # Transpose the array to fit the output convention of numpy1.percentile
     tgt = np.transpose([np.percentile(d, (28, 98)) for d in _rdat])
     res = np.nanpercentile(_ndat, (28, 98), axis=1)
     assert_almost_equal(res, tgt)
Beispiel #3
0
    def test_regression(self):
        ar = np.arange(24).reshape(2, 3, 4).astype(float)
        ar[0][1] = np.nan

        assert_equal(np.nanquantile(ar, q=0.5), np.nanpercentile(ar, q=50))
        assert_equal(np.nanquantile(ar, q=0.5, axis=0),
                     np.nanpercentile(ar, q=50, axis=0))
        assert_equal(np.nanquantile(ar, q=0.5, axis=1),
                     np.nanpercentile(ar, q=50, axis=1))
        assert_equal(np.nanquantile(ar, q=[0.5], axis=1),
                     np.nanpercentile(ar, q=[50], axis=1))
        assert_equal(np.nanquantile(ar, q=[0.25, 0.5, 0.75], axis=1),
                     np.nanpercentile(ar, q=[25, 50, 75], axis=1))
Beispiel #4
0
 def test_empty(self):
     mat = np.zeros((0, 3))
     for axis in [0, None]:
         with warnings.catch_warnings(record=True) as w:
             warnings.simplefilter('always')
             assert_(np.isnan(np.nanpercentile(mat, 40, axis=axis)).all())
             assert_(len(w) == 1)
             assert_(issubclass(w[0].category, RuntimeWarning))
     for axis in [1]:
         with warnings.catch_warnings(record=True) as w:
             warnings.simplefilter('always')
             assert_equal(np.nanpercentile(mat, 40, axis=axis), np.zeros([]))
             assert_(len(w) == 0)
Beispiel #5
0
 def test_out(self):
     mat = np.random.rand(3, 3)
     nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
     resout = np.zeros(3)
     tgt = np.percentile(mat, 42, axis=1)
     res = np.nanpercentile(nan_mat, 42, axis=1, out=resout)
     assert_almost_equal(res, resout)
     assert_almost_equal(res, tgt)
     # 0-d output:
     resout = np.zeros(())
     tgt = np.percentile(mat, 42, axis=None)
     res = np.nanpercentile(nan_mat, 42, axis=None, out=resout)
     assert_almost_equal(res, resout)
     assert_almost_equal(res, tgt)
     res = np.nanpercentile(nan_mat, 42, axis=(0, 1), out=resout)
     assert_almost_equal(res, resout)
     assert_almost_equal(res, tgt)
Beispiel #6
0
    def test_keepdims(self):
        mat = np.eye(3)
        for axis in [None, 0, 1]:
            tgt = np.percentile(mat, 70, axis=axis, out=None,
                                overwrite_input=False)
            res = np.nanpercentile(mat, 70, axis=axis, out=None,
                                   overwrite_input=False)
            assert_(res.ndim == tgt.ndim)

        d = np.ones((3, 5, 7, 11))
        # Randomly set some elements to NaN:
        w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
        w = w.astype(np.intp)
        d[tuple(w)] = np.nan
        with suppress_warnings() as sup:
            sup.filter(RuntimeWarning)
            res = np.nanpercentile(d, 90, axis=None, keepdims=True)
            assert_equal(res.shape, (1, 1, 1, 1))
            res = np.nanpercentile(d, 90, axis=(0, 1), keepdims=True)
            assert_equal(res.shape, (1, 1, 7, 11))
            res = np.nanpercentile(d, 90, axis=(0, 3), keepdims=True)
            assert_equal(res.shape, (1, 5, 7, 1))
            res = np.nanpercentile(d, 90, axis=(1,), keepdims=True)
            assert_equal(res.shape, (3, 1, 7, 11))
            res = np.nanpercentile(d, 90, axis=(0, 1, 2, 3), keepdims=True)
            assert_equal(res.shape, (1, 1, 1, 1))
            res = np.nanpercentile(d, 90, axis=(0, 1, 3), keepdims=True)
            assert_equal(res.shape, (1, 1, 7, 1))
Beispiel #7
0
 def test_allnans(self):
     mat = np.array([np.nan]*9).reshape(3, 3)
     for axis in [None, 0, 1]:
         with warnings.catch_warnings(record=True) as w:
             warnings.simplefilter('always')
             assert_(np.isnan(np.nanpercentile(mat, 60, axis=axis)).all())
             if axis is None:
                 assert_(len(w) == 1)
             else:
                 assert_(len(w) == 3)
             assert_(issubclass(w[0].category, RuntimeWarning))
             # Check scalar
             assert_(np.isnan(np.nanpercentile(np.nan, 60)))
             if axis is None:
                 assert_(len(w) == 2)
             else:
                 assert_(len(w) == 4)
             assert_(issubclass(w[0].category, RuntimeWarning))
Beispiel #8
0
 def test_scalar(self):
     assert_equal(np.nanpercentile(0., 100), 0.)
     a = np.arange(6)
     r = np.nanpercentile(a, 50, axis=0)
     assert_equal(r, 2.5)
     assert_(np.isscalar(r))
Beispiel #9
0
 def test_mutation(self):
     # Check that passed array is not modified.
     ndat = _ndat.copy()
     np.nanpercentile(ndat, 30)
     assert_equal(ndat, _ndat)