Exemplo n.º 1
0
def assert_array_greater(x, y, err_msg='', verbose=True):
    assert_array_compare(operator.__gt__,
                         x,
                         y,
                         err_msg=err_msg,
                         verbose=verbose,
                         header='Arrays are not greater-ordered')
Exemplo n.º 2
0
 def assert_allclose(actual, desired, rtol=1e-7, atol=0,
                     err_msg='', verbose=True):
     from numpy.testing.utils import assert_array_compare
     def compare(x, y):
         return np.allclose(x, y, rtol=rtol, atol=atol)
     actual, desired = np.asanyarray(actual), np.asanyarray(desired)
     header = 'Not equal to tolerance rtol=%g, atol=%g' % (rtol, atol)
     assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
                          verbose=verbose, header=header)
Exemplo n.º 3
0
def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True):
    from numpy.core import around, number, float_
    from numpy.lib import issubdtype
    from numpy.testing.utils import assert_array_compare
    def compare(x, y):
        z = abs(x-y)
        if not issubdtype(z.dtype, number):
            z = z.astype(float_) # handle object arrays
        return around(z, decimal) <= 10.0**(-decimal)
    assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose,
                         header='Arrays are not almost equal')
Exemplo n.º 4
0
def assert_array_less_equal(x, y, err_msg="", verbose=True):
    """Assert array x <= y.  Based on numpy.testing.assert_array_less."""
    assert_array_compare(
        operator.le,
        x,
        y,
        err_msg=err_msg,
        verbose=verbose,
        header="Arrays are not less-ordered",
        equal_inf=False,
    )
Exemplo n.º 5
0
def assert_array_compare(comparison,
                         x,
                         y,
                         err_msg='',
                         verbose=True,
                         header='',
                         fill_value=True):
    """Asserts that a comparison relation between two masked arrays is satisfied
    elementwise."""
    # Fill the data first
    #    xf = filled(x)
    #    yf = filled(y)
    # Allocate a common mask and refill
    m = mask_or(getmask(x), getmask(y))
    x = masked_array(x, copy=False, mask=m, keep_mask=False, subok=False)
    y = masked_array(y, copy=False, mask=m, keep_mask=False, subok=False)
    if ((x is masked) and not (y is masked)) or \
        ((y is masked) and not (x is masked)):
        msg = build_err_msg([x, y],
                            err_msg=err_msg,
                            verbose=verbose,
                            header=header,
                            names=('x', 'y'))
        raise ValueError(msg)
    # OK, now run the basic tests on filled versions
    return utils.assert_array_compare(comparison,
                                      x.filled(fill_value),
                                      y.filled(fill_value),
                                      err_msg=err_msg,
                                      verbose=verbose,
                                      header=header)
Exemplo n.º 6
0
def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True):
    from numpy.core import around, number, float_
    from numpy.lib import issubdtype
    from numpy.testing.utils import assert_array_compare

    def compare(x, y):
        z = abs(x - y)
        if not issubdtype(z.dtype, number):
            z = z.astype(float_)  # handle object arrays
        return around(z, decimal) <= 10.0**(-decimal)

    assert_array_compare(compare,
                         x,
                         y,
                         err_msg=err_msg,
                         verbose=verbose,
                         header='Arrays are not almost equal')
Exemplo n.º 7
0
    def assert_allclose(actual,
                        desired,
                        rtol=1e-7,
                        atol=0,
                        err_msg='',
                        verbose=True):
        from numpy.testing.utils import assert_array_compare

        def compare(x, y):
            return np.allclose(x, y, rtol=rtol, atol=atol)

        actual, desired = np.asanyarray(actual), np.asanyarray(desired)
        header = 'Not equal to tolerance rtol=%g, atol=%g' % (rtol, atol)
        assert_array_compare(compare,
                             actual,
                             desired,
                             err_msg=str(err_msg),
                             verbose=verbose,
                             header=header)
Exemplo n.º 8
0
def assert_array_not_equal(x, y, err_msg='', verbose=True):
    """
    Raises an AssertionError if two array_like objects are equal.

    Given two array_like objects, check that the shape is equal and all
    elements of these objects are not equal. An exception is raised at
    shape mismatch or conflicting values. In contrast to the standard usage
    in numpy, NaNs are compared like numbers, no assertion is raised if
    both objects have NaNs in the same positions (ToDo: Check 2 NaNs).

    The usual caution for verifying equality with floating point numbers is
    advised.

    Parameters
    ----------
    x : array_like
        The actual object to check.
    y : array_like
        The desired, expected object.
    err_msg : str, optional
        The error message to be printed in case of failure.
    verbose : bool, optional
        If True, the conflicting values are appended to the error message.

    Raises
    ------
    AssertionError
        If actual and desired objects are equal.

    See Also
    --------
    assert_array_equal

    """
    assert_array_compare(operator.__ne__,
                         x,
                         y,
                         err_msg=err_msg,
                         verbose=verbose,
                         header='Arrays are equal')
Exemplo n.º 9
0
def assert_array_compare(comparison, x, y, err_msg="", verbose=True, header="", fill_value=True):
    """Asserts that a comparison relation between two masked arrays is satisfied
    elementwise."""
    # Fill the data first
    #    xf = filled(x)
    #    yf = filled(y)
    # Allocate a common mask and refill
    m = mask_or(getmask(x), getmask(y))
    x = masked_array(x, copy=False, mask=m, keep_mask=False, subok=False)
    y = masked_array(y, copy=False, mask=m, keep_mask=False, subok=False)
    if ((x is masked) and not (y is masked)) or ((y is masked) and not (x is masked)):
        msg = build_err_msg([x, y], err_msg=err_msg, verbose=verbose, header=header, names=("x", "y"))
        raise ValueError(msg)
    # OK, now run the basic tests on filled versions
    return utils.assert_array_compare(
        comparison, x.filled(fill_value), y.filled(fill_value), err_msg=err_msg, verbose=verbose, header=header
    )
Exemplo n.º 10
0
    def test_IO(self):
        """ Tests basic coupling"""

        # first on the zeros initialized clements mesh

        input_values = normalize_vec(npr.random((self.N, 1)))
        self.mesh_c_z.input_couple(input_values)
        output_values = self.mesh_c_z.output_values

        # because the matrix was initialized with zeros, output should be identical to input
        np.testing.assert_array_almost_equal(input_values, output_values)

        # same goes for the partial values within MZI
        for partial_value in self.mesh_c_z.partial_values:
            np.testing.assert_array_almost_equal(input_values, partial_value)

        # then on the random initialized clements mesh

        input_values = npr.random((self.N, ))
        self.mesh_c_r.input_couple(input_values)
        output_values = self.mesh_c_r.output_values

        # should not be equal for random initialization
        assert_array_compare(operator.__ne__, input_values, output_values)

        # same goes for the partial values within MZI
        for partial_value in self.mesh_c_r.partial_values:
            assert_array_compare(operator.__ne__, input_values, output_values)

        # finally on the random initialized triangular mesh

        input_values = normalize_vec(npr.random((self.N, 1)))
        self.mesh_t.input_couple(input_values)
        output_values = self.mesh_t.output_values

        # should not be equal for random initialization
        assert_array_compare(operator.__ne__, input_values, output_values)

        # same goes for the partial values within MZI
        for partial_value in self.mesh_t.partial_values:
            assert_array_compare(operator.__ne__, input_values, output_values)