Exemple #1
0
    def test_nans_infs(self):
        with np.errstate(all='ignore'):
            # Check some of the ufuncs
            assert_equal(np.isnan(self.all_f16), np.isnan(self.all_f32))
            assert_equal(np.isinf(self.all_f16), np.isinf(self.all_f32))
            assert_equal(np.isfinite(self.all_f16), np.isfinite(self.all_f32))
            assert_equal(np.signbit(self.all_f16), np.signbit(self.all_f32))
            assert_equal(np.spacing(float16(65504)), np.inf)

            # Check comparisons of all values with NaN
            nan = float16(np.nan)

            assert_(not (self.all_f16 == nan).any())
            assert_(not (nan == self.all_f16).any())

            assert_((self.all_f16 != nan).all())
            assert_((nan != self.all_f16).all())

            assert_(not (self.all_f16 < nan).any())
            assert_(not (nan < self.all_f16).any())

            assert_(not (self.all_f16 <= nan).any())
            assert_(not (nan <= self.all_f16).any())

            assert_(not (self.all_f16 > nan).any())
            assert_(not (nan > self.all_f16).any())

            assert_(not (self.all_f16 >= nan).any())
            assert_(not (nan >= self.all_f16).any())
Exemple #2
0
    def test_float_modulus_corner_cases(self):
        # Check remainder magnitude.
        for dt in np.typecodes['Float']:
            b = np.array(1.0, dtype=dt)
            a = np.nextafter(np.array(0.0, dtype=dt), -b)
            rem = operator.mod(a, b)
            assert_(rem <= b, 'dt: %s' % dt)
            rem = operator.mod(-a, -b)
            assert_(rem >= -b, 'dt: %s' % dt)

        # Check nans, inf
        with suppress_warnings() as sup:
            sup.filter(RuntimeWarning,
                       "invalid value encountered in remainder")
            for dt in np.typecodes['Float']:
                fone = np.array(1.0, dtype=dt)
                fzer = np.array(0.0, dtype=dt)
                finf = np.array(np.inf, dtype=dt)
                fnan = np.array(np.nan, dtype=dt)
                rem = operator.mod(fone, fzer)
                assert_(np.isnan(rem), 'dt: %s' % dt)
                # MSVC 2008 returns NaN here, so disable the check.
                #rem = operator.mod(fone, finf)
                #assert_(rem == fone, 'dt: %s' % dt)
                rem = operator.mod(fone, fnan)
                assert_(np.isnan(rem), 'dt: %s' % dt)
                rem = operator.mod(finf, fone)
                assert_(np.isnan(rem), 'dt: %s' % dt)
    def assert_array_compare(self, comparison, x, y, err_msg='', header='',
                         fill_value=True):
        """
        Assert that a comparison of two masked arrays is satisfied elementwise.

        """
        xf = self.filled(x)
        yf = self.filled(y)
        m = self.mask_or(self.getmask(x), self.getmask(y))

        x = self.filled(self.masked_array(xf, mask=m), fill_value)
        y = self.filled(self.masked_array(yf, mask=m), fill_value)
        if (x.dtype.char != "O"):
            x = x.astype(float_)
            if isinstance(x, np.ndarray) and x.size > 1:
                x[np.isnan(x)] = 0
            elif np.isnan(x):
                x = 0
        if (y.dtype.char != "O"):
            y = y.astype(float_)
            if isinstance(y, np.ndarray) and y.size > 1:
                y[np.isnan(y)] = 0
            elif np.isnan(y):
                y = 0
        try:
            cond = (x.shape == () or y.shape == ()) or x.shape == y.shape
            if not cond:
                msg = build_err_msg([x, y],
                                    err_msg
                                    + '\n(shapes %s, %s mismatch)' % (x.shape,
                                                                      y.shape),
                                    header=header,
                                    names=('x', 'y'))
                assert cond, msg
            val = comparison(x, y)
            if m is not self.nomask and fill_value:
                val = self.masked_array(val, mask=m)
            if isinstance(val, bool):
                cond = val
                reduced = [0]
            else:
                reduced = val.ravel()
                cond = reduced.all()
                reduced = reduced.tolist()
            if not cond:
                match = 100-100.0*reduced.count(1)/len(reduced)
                msg = build_err_msg([x, y],
                                    err_msg
                                    + '\n(mismatch %s%%)' % (match,),
                                    header=header,
                                    names=('x', 'y'))
                assert cond, msg
        except ValueError:
            msg = build_err_msg([x, y], err_msg, header=header, names=('x', 'y'))
            raise ValueError(msg)
Exemple #4
0
 def test_allnans(self):
     mat = np.array([np.nan]*9).reshape(3, 3)
     for f in self.nanfuncs:
         for axis in [None, 0, 1]:
             with warnings.catch_warnings(record=True) as w:
                 warnings.simplefilter('always')
                 assert_(np.isnan(f(mat, axis=axis)).all())
                 assert_(len(w) == 1)
                 assert_(issubclass(w[0].category, RuntimeWarning))
                 # Check scalar
                 assert_(np.isnan(f(np.nan)))
                 assert_(len(w) == 2)
                 assert_(issubclass(w[0].category, RuntimeWarning))
 def _check_ninf_nan(dummy):
     msgform = "csqrt(-inf, nan) is (%f, %f), expected (nan, +-inf)"
     z = np.sqrt(np.array(complex(-np.inf, np.nan)))
     #Fixme: ugly workaround for isinf bug.
     with np.errstate(invalid='ignore'):
         if not (np.isnan(z.real) and np.isinf(z.imag)):
             raise AssertionError(msgform % (z.real, z.imag))
Exemple #6
0
 def test_zero_division(self):
     with np.errstate(all="ignore"):
         for t in [np.complex64, np.complex128]:
             a = t(0.0)
             b = t(1.0)
             assert_(np.isinf(b / a))
             b = t(complex(np.inf, np.inf))
             assert_(np.isinf(b / a))
             b = t(complex(np.inf, np.nan))
             assert_(np.isinf(b / a))
             b = t(complex(np.nan, np.inf))
             assert_(np.isinf(b / a))
             b = t(complex(np.nan, np.nan))
             assert_(np.isnan(b / a))
             b = t(0.)
             assert_(np.isnan(b / a))
Exemple #7
0
    def test_allnans(self):
        mat = np.array([np.nan]*9).reshape(3, 3)
        for axis in [None, 0, 1]:
            with suppress_warnings() as sup:
                sup.record(RuntimeWarning)

                assert_(np.isnan(np.nanmedian(mat, axis=axis)).all())
                if axis is None:
                    assert_(len(sup.log) == 1)
                else:
                    assert_(len(sup.log) == 3)
                # Check scalar
                assert_(np.isnan(np.nanmedian(np.nan)))
                if axis is None:
                    assert_(len(sup.log) == 2)
                else:
                    assert_(len(sup.log) == 4)
Exemple #8
0
 def test_retstep(self):
     y = linspace(0, 1, 2, retstep=True)
     assert_(isinstance(y, tuple) and len(y) == 2)
     for num in (0, 1):
         for ept in (False, True):
             y = linspace(0, 1, num, endpoint=ept, retstep=True)
             assert_(
                 isinstance(y, tuple) and len(y) == 2 and len(y[0]) == num
                 and isnan(y[1]), 'num={0}, endpoint={1}'.format(num, ept))
Exemple #9
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))
Exemple #10
0
    def test_subclass(self):
        class MyNDArray(np.ndarray):
            pass

        # Check that it works and that type and
        # shape are preserved
        mine = np.eye(3).view(MyNDArray)
        for f in self.nanfuncs:
            res = f(mine, axis=0)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == (3,))
            res = f(mine, axis=1)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == (3,))
            res = f(mine)
            assert_(res.shape == ())

        # check that rows of nan are dealt with for subclasses (#4628)
        mine[1] = np.nan
        for f in self.nanfuncs:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                res = f(mine, axis=0)
                assert_(isinstance(res, MyNDArray))
                assert_(not np.any(np.isnan(res)))
                assert_(len(w) == 0)

            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                res = f(mine, axis=1)
                assert_(isinstance(res, MyNDArray))
                assert_(np.isnan(res[1]) and not np.isnan(res[0])
                        and not np.isnan(res[2]))
                assert_(len(w) == 1, 'no warning raised')
                assert_(issubclass(w[0].category, RuntimeWarning))

            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                res = f(mine)
                assert_(res.shape == ())
                assert_(res != np.nan)
                assert_(len(w) == 0)
Exemple #11
0
 def test_result_values(self):
     for f, fcmp in zip(self.nanfuncs, [np.greater, np.less]):
         for row in _ndat:
             with suppress_warnings() as sup:
                 sup.filter(RuntimeWarning, "invalid value encountered in")
                 ind = f(row)
                 val = row[ind]
                 # comparing with NaN is tricky as the result
                 # is always false except for NaN != NaN
                 assert_(not np.isnan(val))
                 assert_(not fcmp(val, row).any())
                 assert_(not np.equal(val, row[:ind]).any())
Exemple #12
0
    def test_mirr(self):
        val = [-4500, -800, 800, 800, 600, 600, 800, 800, 700, 3000]
        assert_almost_equal(np.mirr(val, 0.08, 0.055), 0.0666, 4)

        val = [-120000, 39000, 30000, 21000, 37000, 46000]
        assert_almost_equal(np.mirr(val, 0.10, 0.12), 0.126094, 6)

        val = [100, 200, -50, 300, -200]
        assert_almost_equal(np.mirr(val, 0.05, 0.06), 0.3428, 4)

        val = [39000, 30000, 21000, 37000, 46000]
        assert_(np.isnan(np.mirr(val, 0.10, 0.12)))
Exemple #13
0
def test_nanfunctions_matrices():
    # Check that it works and that type and
    # shape are preserved
    # 2018-04-29: moved here from core.tests.test_nanfunctions
    mat = np.matrix(np.eye(3))
    for f in [np.nanmin, np.nanmax]:
        res = f(mat, axis=0)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (1, 3))
        res = f(mat, axis=1)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (3, 1))
        res = f(mat)
        assert_(np.isscalar(res))
    # check that rows of nan are dealt with for subclasses (#4628)
    mat[1] = np.nan
    for f in [np.nanmin, np.nanmax]:
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            res = f(mat, axis=0)
            assert_(isinstance(res, np.matrix))
            assert_(not np.any(np.isnan(res)))
            assert_(len(w) == 0)

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            res = f(mat, axis=1)
            assert_(isinstance(res, np.matrix))
            assert_(np.isnan(res[1, 0]) and not np.isnan(res[0, 0])
                    and not np.isnan(res[2, 0]))
            assert_(len(w) == 1, 'no warning raised')
            assert_(issubclass(w[0].category, RuntimeWarning))

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            res = f(mat)
            assert_(np.isscalar(res))
            assert_(res != np.nan)
            assert_(len(w) == 0)
Exemple #14
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)
Exemple #15
0
    def test_small_large(self):
        # test the small and large code paths, current cutoff 400 elements
        for s in [5, 20, 51, 200, 1000]:
            d = np.random.randn(4, s)
            # Randomly set some elements to NaN:
            w = np.random.randint(0, d.size, size=d.size // 5)
            d.ravel()[w] = np.nan
            d[:,0] = 1.  # ensure at least one good value
            # use normal median without nans to compare
            tgt = []
            for x in d:
                nonan = np.compress(~np.isnan(x), x)
                tgt.append(np.median(nonan, overwrite_input=True))

            assert_array_equal(np.nanmedian(d, axis=-1), tgt)
Exemple #16
0
 def test_ddof_too_big(self):
     nanfuncs = [np.nanvar, np.nanstd]
     stdfuncs = [np.var, np.std]
     dsize = [len(d) for d in _rdat]
     for nf, rf in zip(nanfuncs, stdfuncs):
         for ddof in range(5):
             with suppress_warnings() as sup:
                 sup.record(RuntimeWarning)
                 sup.filter(np.ComplexWarning)
                 tgt = [ddof >= d for d in dsize]
                 res = nf(_ndat, axis=1, ddof=ddof)
                 assert_equal(np.isnan(res), tgt)
                 if any(tgt):
                     assert_(len(sup.log) == 1)
                 else:
                     assert_(len(sup.log) == 0)
Exemple #17
0
    def test_mirr_decimal(self):
        val = [Decimal('-4500'), Decimal('-800'), Decimal('800'), Decimal('800'),
               Decimal('600'), Decimal('600'), Decimal('800'), Decimal('800'),
               Decimal('700'), Decimal('3000')]
        assert_equal(np.mirr(val, Decimal('0.08'), Decimal('0.055')),
                     Decimal('0.066597175031553548874239618'))

        val = [Decimal('-120000'), Decimal('39000'), Decimal('30000'),
               Decimal('21000'), Decimal('37000'), Decimal('46000')]
        assert_equal(np.mirr(val, Decimal('0.10'), Decimal('0.12')), Decimal('0.126094130365905145828421880'))

        val = [Decimal('100'), Decimal('200'), Decimal('-50'),
               Decimal('300'), Decimal('-200')]
        assert_equal(np.mirr(val, Decimal('0.05'), Decimal('0.06')), Decimal('0.342823387842176663647819868'))

        val = [Decimal('39000'), Decimal('30000'), Decimal('21000'), Decimal('37000'), Decimal('46000')]
        assert_(np.isnan(np.mirr(val, Decimal('0.10'), Decimal('0.12'))))
Exemple #18
0
def _median_nancheck(data, result, axis, out):
    """
    Utility function to check median result from data for NaN values at the end
    and return NaN in that case. Input result can also be a MaskedArray.

    Parameters
    ----------
    data : array
        Input data to median function
    result : Array or MaskedArray
        Result of median function
    axis : {int, sequence of int, None}, optional
        Axis or axes along which the median was computed.
    out : ndarray, optional
        Output array in which to place the result.
    Returns
    -------
    median : scalar or ndarray
        Median or NaN in axes which contained NaN in the input.
    """
    if data.size == 0:
        return result
    data = np.moveaxis(data, axis, -1)
    n = np.isnan(data[..., -1])
    # masked NaN values are ok
    if np.ma.isMaskedArray(n):
        n = n.filled(False)
    if result.ndim == 0:
        if n == True:
            warnings.warn("Invalid value encountered in median",
                          RuntimeWarning,
                          stacklevel=3)
            if out is not None:
                out[...] = data.dtype.type(np.nan)
                result = out
            else:
                result = data.dtype.type(np.nan)
    elif np.count_nonzero(n.ravel()) > 0:
        warnings.warn("Invalid value encountered in median for" +
                      " %d results" % np.count_nonzero(n.ravel()),
                      RuntimeWarning,
                      stacklevel=3)
        result[n] = np.nan
    return result
Exemple #19
0
 def test_goodvalues(self):
     z = np.array((-1., 0., 1.))
     res = np.isnan(z) == 0
     assert_all(np.all(res, axis=0))
 def _check_inf_inf(dummy):
     msgform = "cexp(inf, inf) is (%f, %f), expected (+-inf, nan)"
     with np.errstate(invalid='ignore'):
         z = f(np.array(complex(np.inf, np.inf)))
         if not np.isinf(z.real) or not np.isnan(z.imag):
             raise AssertionError(msgform % (z.real, z.imag))
Exemple #21
0
    def test_half_ufuncs(self):
        """Test the various ufuncs"""

        a = np.array([0, 1, 2, 4, 2], dtype=float16)
        b = np.array([-2, 5, 1, 4, 3], dtype=float16)
        c = np.array([0, -1, -np.inf, np.nan, 6], dtype=float16)

        assert_equal(np.add(a, b), [-2, 6, 3, 8, 5])
        assert_equal(np.subtract(a, b), [2, -4, 1, 0, -1])
        assert_equal(np.multiply(a, b), [0, 5, 2, 16, 6])
        assert_equal(np.divide(a, b), [0, 0.199951171875, 2, 1, 0.66650390625])

        assert_equal(np.equal(a, b), [False, False, False, True, False])
        assert_equal(np.not_equal(a, b), [True, True, True, False, True])
        assert_equal(np.less(a, b), [False, True, False, False, True])
        assert_equal(np.less_equal(a, b), [False, True, False, True, True])
        assert_equal(np.greater(a, b), [True, False, True, False, False])
        assert_equal(np.greater_equal(a, b), [True, False, True, True, False])
        assert_equal(np.logical_and(a, b), [False, True, True, True, True])
        assert_equal(np.logical_or(a, b), [True, True, True, True, True])
        assert_equal(np.logical_xor(a, b), [True, False, False, False, False])
        assert_equal(np.logical_not(a), [True, False, False, False, False])

        assert_equal(np.isnan(c), [False, False, False, True, False])
        assert_equal(np.isinf(c), [False, False, True, False, False])
        assert_equal(np.isfinite(c), [True, True, False, False, True])
        assert_equal(np.signbit(b), [True, False, False, False, False])

        assert_equal(np.copysign(b, a), [2, 5, 1, 4, 3])

        assert_equal(np.maximum(a, b), [0, 5, 2, 4, 3])
        x = np.maximum(b, c)
        assert_(np.isnan(x[3]))
        x[3] = 0
        assert_equal(x, [0, 5, 1, 0, 6])
        assert_equal(np.minimum(a, b), [-2, 1, 1, 4, 2])
        x = np.minimum(b, c)
        assert_(np.isnan(x[3]))
        x[3] = 0
        assert_equal(x, [-2, -1, -np.inf, 0, 3])
        assert_equal(np.fmax(a, b), [0, 5, 2, 4, 3])
        assert_equal(np.fmax(b, c), [0, 5, 1, 4, 6])
        assert_equal(np.fmin(a, b), [-2, 1, 1, 4, 2])
        assert_equal(np.fmin(b, c), [-2, -1, -np.inf, 4, 3])

        assert_equal(np.floor_divide(a, b), [0, 0, 2, 1, 0])
        assert_equal(np.remainder(a, b), [0, 1, 0, 0, 2])
        assert_equal(np.divmod(a, b), ([0, 0, 2, 1, 0], [0, 1, 0, 0, 2]))
        assert_equal(np.square(b), [4, 25, 1, 16, 9])
        assert_equal(np.reciprocal(b),
                     [-0.5, 0.199951171875, 1, 0.25, 0.333251953125])
        assert_equal(np.ones_like(b), [1, 1, 1, 1, 1])
        assert_equal(np.conjugate(b), b)
        assert_equal(np.absolute(b), [2, 5, 1, 4, 3])
        assert_equal(np.negative(b), [2, -5, -1, -4, -3])
        assert_equal(np.positive(b), b)
        assert_equal(np.sign(b), [-1, 1, 1, 1, 1])
        assert_equal(np.modf(b), ([0, 0, 0, 0, 0], b))
        assert_equal(np.frexp(b),
                     ([-0.5, 0.625, 0.5, 0.5, 0.75], [2, 3, 1, 3, 2]))
        assert_equal(np.ldexp(b, [0, 1, 2, 4, 2]), [-2, 10, 4, 64, 12])
Exemple #22
0
 def test_neginf(self):
     with np.errstate(divide='ignore'):
         assert_all(np.isnan(np.array((-1., )) / 0.) == 0)
Exemple #23
0
 def test_ind(self):
     with np.errstate(divide='ignore', invalid='ignore'):
         assert_all(np.isnan(np.array((0., )) / 0.) == 1)
Exemple #24
0
 def test_beta_small_parameters(self):
     # Test that beta with small a and b parameters does not produce
     # NaNs due to roundoff errors causing 0 / 0, gh-5851
     np.random.seed(1234567890)
     x = np.random.beta(0.0001, 0.0001, size=100)
     assert_(not np.any(np.isnan(x)), 'Nans in np.random.beta')
Exemple #25
0
 def test_complex1(self):
     with np.errstate(divide='ignore', invalid='ignore'):
         assert_all(np.isnan(np.array(0 + 0j) / 0.) == 1)
Exemple #26
0
 def test_complex(self):
     assert_all(np.isnan(1 + 1j) == 0)
Exemple #27
0
 def test_integer(self):
     assert_all(np.isnan(1) == 0)