def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
                         header=''):
    from numpy.core import asarray, isnan, any
    from numpy import isreal, iscomplex
    x = asarray(x)
    y = asarray(y)

    def isnumber(x):
        return x.dtype.char in '?bhilqpBHILQPfdgFDG'

    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),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)

        if (isnumber(x) and isnumber(y)) and (any(isnan(x)) or any(isnan(y))):
            # Handling nan: we first check that x and y have the nan at the
            # same locations, and then we mask the nan and do the comparison as
            # usual.
            xnanid = isnan(x)
            ynanid = isnan(y)
            try:
                assert_array_equal(xnanid, ynanid)
            except AssertionError:
                msg = build_err_msg([x, y],
                                    err_msg
                                    + '\n(x and y nan location mismatch %s, ' \
                                    '%s mismatch)' % (xnanid, ynanid),
                                    verbose=verbose, header=header,
                                    names=('x', 'y'))
            val = comparison(x[~xnanid], y[~ynanid])
        else:
            val = comparison(x,y)
        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,),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)
    except ValueError:
        msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,
                            names=('x', 'y'))
        raise ValueError(msg)
Exemple #2
0
 def compare(x, y):
     try:
         if any(gisinf(x)) or any( gisinf(y)):
             xinfid = gisinf(x)
             yinfid = gisinf(y)
             if not xinfid == yinfid:
                 return False
             # if one item, x and y is +- inf
             if x.size == y.size == 1:
                 return x == y
             x = x[~xinfid]
             y = y[~yinfid]
     except TypeError:
         pass
     z = abs(x-y)
     if not issubdtype(z.dtype, number):
         z = z.astype(float_) # handle object arrays
     return around(z, decimal) <= 10.0**(-decimal)
Exemple #3
0
 def compare(x, y):
     try:
         if any(gisinf(x)) or any(gisinf(y)):
             xinfid = gisinf(x)
             yinfid = gisinf(y)
             if not xinfid == yinfid:
                 return False
             # if one item, x and y is +- inf
             if x.size == y.size == 1:
                 return x == y
             x = x[~xinfid]
             y = y[~yinfid]
     except TypeError:
         pass
     z = abs(x - y)
     if not issubdtype(z.dtype, number):
         z = z.astype(float_)  # handle object arrays
     return around(z, decimal) <= 10.0**(-decimal)
Exemple #4
0
def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
                         header=''):
    from numpy.core import array, isnan, isinf, isna, any, all, inf
    x = array(x, copy=False, subok=True)
    y = array(y, copy=False, subok=True)

    def isnumber(x):
        return x.dtype.char in '?bhilqpBHILQPefdgFDG'

    def chk_same_position(x_id, y_id, hasval='nan'):
        """Handling nan/inf: check that x and y have the nan/inf at the same
        locations."""
        try:
            assert_array_equal(x_id, y_id)
        except AssertionError:
            msg = build_err_msg([x, y],
                                err_msg + '\nx and y %s location mismatch:' \
                                % (hasval), verbose=verbose, header=header,
                                names=('x', 'y'))
            raise AssertionError(msg)

    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),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)

        if isnumber(x) and isnumber(y):
            x_isna, y_isna = isna(x), isna(y)
            x_isnan, y_isnan = isnan(x), isnan(y)
            x_isinf, y_isinf = isinf(x), isinf(y)

            # Remove any NAs from the isnan and isinf arrays
            if x.ndim == 0:
                if x_isna:
                    x_isnan = False
                    x_isinf = False
            else:
                x_isnan[x_isna] = False
                x_isinf[x_isna] = False
            if y.ndim == 0:
                if y_isna:
                    y_isnan = False
                    y_isinf = False
            else:
                y_isnan[y_isna] = False
                y_isinf[y_isna] = False


            # Validate that the special values are in the same place
            if any(x_isnan) or any(y_isnan):
                chk_same_position(x_isnan, y_isnan, hasval='nan')
            if any(x_isinf) or any(y_isinf):
                # Check +inf and -inf separately, since they are different
                chk_same_position(x == +inf, y == +inf, hasval='+inf')
                chk_same_position(x == -inf, y == -inf, hasval='-inf')
            if any(x_isna) or any(y_isna):
                chk_same_position(x_isna, y_isna, hasval='NA')

            # Combine all the special values
            x_id, y_id = x_isnan, y_isnan
            x_id |= x_isinf
            y_id |= y_isinf
            x_id |= x_isna
            y_id |= y_isna

            # Only do the comparison if actual values are left
            if all(x_id):
                return

            if any(x_id):
                val = comparison(x[~x_id], y[~y_id])
            else:
                val = comparison(x, y)
        # field-NA isn't supported yet, so skip struct dtypes for this
        elif (not x.dtype.names and not y.dtype.names) and \
                    (any(isna(x)) or any(isna(y))):
            x_isna, y_isna = isna(x), isna(y)

            if any(x_isna) or any(y_isna):
                chk_same_position(x_isna, y_isna, hasval='NA')

            if all(x_isna):
                return
            val = comparison(x[~x_isna], y[~y_isna])
        else:
            val = comparison(x,y)

        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,),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)
    except ValueError, e:
        import traceback
        efmt = traceback.format_exc()
        header = 'error during assertion:\n\n%s\n\n%s' % (efmt, header)

        msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,
                            names=('x', 'y'))
        raise ValueError(msg)
Exemple #5
0
def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
                         header=''):
    from numpy.core import array, isnan, isinf, any
    x = array(x, copy=False, subok=True)
    y = array(y, copy=False, subok=True)

    def isnumber(x):
        return x.dtype.char in '?bhilqpBHILQPfdgFDG'

    def chk_same_position(x_id, y_id, hasval='nan'):
        """Handling nan/inf: check that x and y have the nan/inf at the same
        locations."""
        try:
            assert_array_equal(x_id, y_id)
        except AssertionError:
            msg = build_err_msg([x, y],
                                err_msg + '\nx and y %s location mismatch:' \
                                % (hasval), verbose=verbose, header=header,
                                names=('x', 'y'))
            raise AssertionError(msg)

    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),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)

        if (isnumber(x) and isnumber(y)) and (any(isnan(x)) or any(isnan(y))):
            x_id = isnan(x)
            y_id = isnan(y)
            chk_same_position(x_id, y_id, hasval='nan')
            # If only one item, it was a nan, so just return
            if x.size == y.size == 1:
                return
            val = comparison(x[~x_id], y[~y_id])
        elif (isnumber(x) and isnumber(y)) and (any(isinf(x)) or any(isinf(y))):
            x_id = isinf(x)
            y_id = isinf(y)
            chk_same_position(x_id, y_id, hasval='inf')
            # If only one item, it was a inf, so just return
            if x.size == y.size == 1:
                return
            val = comparison(x[~x_id], y[~y_id])
        else:
            val = comparison(x,y)

        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,),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)
    except ValueError, e:
        header = 'error during assertion:\n%s\n\n%s' % (e, header)
        msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,
                            names=('x', 'y'))
        raise ValueError(msg)
Exemple #6
0
def assert_array_compare(comparison, x, y, err_msg="", verbose=True, header=""):
    from numpy.core import array, isnan, isinf, any, all, inf

    x = array(x, copy=False, subok=True)
    y = array(y, copy=False, subok=True)

    def isnumber(x):
        return x.dtype.char in "?bhilqpBHILQPefdgFDG"

    def chk_same_position(x_id, y_id, hasval="nan"):
        """Handling nan/inf: check that x and y have the nan/inf at the same
        locations."""
        try:
            assert_array_equal(x_id, y_id)
        except AssertionError:
            msg = build_err_msg(
                [x, y],
                err_msg + "\nx and y %s location mismatch:" % (hasval),
                verbose=verbose,
                header=header,
                names=("x", "y"),
            )
            raise AssertionError(msg)

    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),
                verbose=verbose,
                header=header,
                names=("x", "y"),
            )
            if not cond:
                raise AssertionError(msg)

        if isnumber(x) and isnumber(y):
            x_isnan, y_isnan = isnan(x), isnan(y)
            x_isinf, y_isinf = isinf(x), isinf(y)

            # Validate that the special values are in the same place
            if any(x_isnan) or any(y_isnan):
                chk_same_position(x_isnan, y_isnan, hasval="nan")
            if any(x_isinf) or any(y_isinf):
                # Check +inf and -inf separately, since they are different
                chk_same_position(x == +inf, y == +inf, hasval="+inf")
                chk_same_position(x == -inf, y == -inf, hasval="-inf")

            # Combine all the special values
            x_id, y_id = x_isnan, y_isnan
            x_id |= x_isinf
            y_id |= y_isinf

            # Only do the comparison if actual values are left
            if all(x_id):
                return

            if any(x_id):
                val = comparison(x[~x_id], y[~y_id])
            else:
                val = comparison(x, y)
        else:
            val = comparison(x, y)

        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,), verbose=verbose, header=header, names=("x", "y")
            )
            if not cond:
                raise AssertionError(msg)
    except ValueError as e:
        import traceback

        efmt = traceback.format_exc()
        header = "error during assertion:\n\n%s\n\n%s" % (efmt, header)

        msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header, names=("x", "y"))
        raise ValueError(msg)
def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
                         header=''):
    from numpy.core import array, isnan, isinf, any
    x = array(x, copy=False, subok=True)
    y = array(y, copy=False, subok=True)

    def isnumber(x):
        return x.dtype.char in '?bhilqpBHILQPfdgFDG'

    def chk_same_position(x_id, y_id, hasval='nan'):
        """Handling nan/inf: check that x and y have the nan/inf at the same
        locations."""
        try:
            assert_array_equal(x_id, y_id)
        except AssertionError:
            msg = build_err_msg([x, y],
                                err_msg + '\nx and y %s location mismatch:' \
                                % (hasval), verbose=verbose, header=header,
                                names=('x', 'y'))
            raise AssertionError(msg)

    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),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)

        if (isnumber(x) and isnumber(y)) and (any(isnan(x)) or any(isnan(y))):
            x_id = isnan(x)
            y_id = isnan(y)
            chk_same_position(x_id, y_id, hasval='nan')
            # If only one item, it was a nan, so just return
            if x.size == y.size == 1:
                return
            val = comparison(x[~x_id], y[~y_id])
        elif (isnumber(x) and isnumber(y)) and (any(isinf(x)) or any(isinf(y))):
            x_id = isinf(x)
            y_id = isinf(y)
            chk_same_position(x_id, y_id, hasval='inf')
            # If only one item, it was a inf, so just return
            if x.size == y.size == 1:
                return
            val = comparison(x[~x_id], y[~y_id])
        else:
            val = comparison(x,y)

        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,),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)
    except ValueError, e:
        header = 'error during assertion:\n%s\n\n%s' % (e, header)
        msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,
                            names=('x', 'y'))
        raise ValueError(msg)
Exemple #8
0
def assert_array_compare(comparison, x, y, err_msg="", verbose=True, header=""):
    from numpy.core import array, isnan, any

    x = array(x, copy=False, subok=True)
    y = array(y, copy=False, subok=True)

    def isnumber(x):
        return x.dtype.char in "?bhilqpBHILQPfdgFDG"

    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),
                verbose=verbose,
                header=header,
                names=("x", "y"),
            )
            if not cond:
                raise AssertionError(msg)

        if (isnumber(x) and isnumber(y)) and (any(isnan(x)) or any(isnan(y))):
            # Handling nan: we first check that x and y have the nan at the
            # same locations, and then we mask the nan and do the comparison as
            # usual.
            xnanid = isnan(x)
            ynanid = isnan(y)
            try:
                assert_array_equal(xnanid, ynanid)
            except AssertionError:
                msg = build_err_msg(
                    [x, y],
                    err_msg + "\n(x and y nan location mismatch %s, " "%s mismatch)" % (xnanid, ynanid),
                    verbose=verbose,
                    header=header,
                    names=("x", "y"),
                )
                raise AssertionError(msg)
            # If only one item, it was a nan, so just return
            if x.size == y.size == 1:
                return
            val = comparison(x[~xnanid], y[~ynanid])
        else:
            val = comparison(x, y)
        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,), verbose=verbose, header=header, names=("x", "y")
            )
            if not cond:
                raise AssertionError(msg)
    except ValueError:
        msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header, names=("x", "y"))
        raise ValueError(msg)
Exemple #9
0
def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
                         header=''):
    from numpy.core import array, isnan, isinf, any, all, inf
    x = array(x, copy=False, subok=True)
    y = array(y, copy=False, subok=True)

    def isnumber(x):
        return x.dtype.char in '?bhilqpBHILQPefdgFDG'

    def chk_same_position(x_id, y_id, hasval='nan'):
        """Handling nan/inf: check that x and y have the nan/inf at the same
        locations."""
        try:
            assert_array_equal(x_id, y_id)
        except AssertionError:
            msg = build_err_msg([x, y],
                                err_msg + '\nx and y %s location mismatch:' \
                                % (hasval), verbose=verbose, header=header,
                                names=('x', 'y'))
            raise AssertionError(msg)

    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),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)

        if isnumber(x) and isnumber(y):
            x_isnan, y_isnan = isnan(x), isnan(y)
            x_isinf, y_isinf = isinf(x), isinf(y)

            # Validate that the special values are in the same place
            if any(x_isnan) or any(y_isnan):
                chk_same_position(x_isnan, y_isnan, hasval='nan')
            if any(x_isinf) or any(y_isinf):
                # Check +inf and -inf separately, since they are different
                chk_same_position(x == +inf, y == +inf, hasval='+inf')
                chk_same_position(x == -inf, y == -inf, hasval='-inf')

            # Combine all the special values
            x_id, y_id = x_isnan, y_isnan
            x_id |= x_isinf
            y_id |= y_isinf

            # Only do the comparison if actual values are left
            if all(x_id):
                return

            if any(x_id):
                val = comparison(x[~x_id], y[~y_id])
            else:
                val = comparison(x, y)
        else:
            val = comparison(x,y)

        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,),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            if not cond :
                raise AssertionError(msg)
    except ValueError as e:
        import traceback
        efmt = traceback.format_exc()
        header = 'error during assertion:\n\n%s\n\n%s' % (efmt, header)

        msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,
                            names=('x', 'y'))
        raise ValueError(msg)