def check_expand_and_squeeze(_np_a, axes):
     _name = 'matmul'
     np_expand_dims = np.__getattribute__('expand_dims')
     ns_expand_dims = nps.__getattribute__('expand_dims')
     np_squeeze = np.__getattribute__('squeeze')
     ns_squeeze = nps.__getattribute__('squeeze')
     _ns_a = nps.array(_np_a)
     _np_result = np_expand_dims(_np_a, axes)
     _ns_result = ns_expand_dims(_ns_a, axes)
     assert np.allclose(_np_result, _ns_result.get())
     check_dim(_np_result, _ns_result)
     _np_result = np_squeeze(_np_a)
     _ns_result = ns_squeeze(_ns_a)
     assert np.allclose(_np_result, _ns_result.get())
     check_dim(_np_result, _ns_result)
Ejemplo n.º 2
0
def test_doctest_fallback(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    passed = []
    failed = []
    excepted = []

    plot_funcs = {
        "kaiser",
        "bartlett",
        "hanning",
        "blackman",
        "histogram2d",
        "interp",
        "sinc",
    }

    for func in settings.doctest_fallbacks:
        nps_func = nps.__getattribute__(func)
        nps_func.__doc__ = update_doc_string(np.__getattribute__(func).__doc__)

        f = io.StringIO()
        with redirect_stdout(f):
            nps_func = nps.__getattribute__(func)

            if func in plot_funcs:
                # Skip plot functions and add them to the failed list
                print("Failure")
            else:
                optionflags = doctest.NORMALIZE_WHITESPACE | doctest.FAIL_FAST
                doctest.run_docstring_examples(
                    nps_func, locals(), optionflags=optionflags
                )

        if f.getvalue() == "":
            passed.append(func)
        else:
            failed.append(func)

    print("***DOCTESTS***")
    print("PASSED:")
    print(sorted(passed))
    print("FAILED:")
    print(sorted(failed))
    print("EXCEPTED:")
    print(sorted(excepted))
Ejemplo n.º 3
0
def test_nan_reductions(nps_app_inst):
    from nums import numpy as nps
    from nums.numpy import BlockArray

    assert nps_app_inst is not None

    ba: BlockArray = nps.array([[-1, 4, np.nan, 5], [3, 2, nps.nan, 6]])
    block_shapes = [(1, 1), (1, 2), (1, 4), (2, 1), (2, 4)]
    for block_shape in block_shapes:
        ba = ba.reshape(block_shape=block_shape)
        np_arr = ba.get()
        op_params = ["nanmax", "nanmin", "nansum", "nanmean", "nanvar", "nanstd"]
        axis_params = [None, 0, 1]
        keepdims_params = [True, False]

        for op, axis, keepdims in itertools.product(
            op_params, axis_params, keepdims_params
        ):
            ns_op = nps.__getattribute__(op)
            np_op = np.__getattribute__(op)
            np_result = np_op(np_arr, axis=axis, keepdims=keepdims)
            ba_result: BlockArray = ns_op(ba, axis=axis, keepdims=keepdims)
            assert ba_result.grid.grid_shape == ba_result.blocks.shape
            assert ba_result.shape == np_result.shape
            assert np.allclose(ba_result.get(), np_result, equal_nan=True)
Ejemplo n.º 4
0
def test_stats_1d(nps_app_inst):
    from nums import numpy as nps
    from nums.numpy import BlockArray

    assert nps_app_inst is not None

    ba: BlockArray = nps.array([5, -2, 4, 8, 3, 6, 1, 7])
    block_shapes = [(1, ), (2, ), (4, ), (8, )]
    qs = [0, 50, 100]
    for block_shape in block_shapes:
        ba = ba.reshape(block_shape=block_shape)
        np_arr = ba.get()
        op_params = ["median", "percentile", "quantile"]
        axis_params = [None]
        keepdims_params = [False]

        for op, q, axis, keepdims in itertools.product(op_params, qs,
                                                       axis_params,
                                                       keepdims_params):
            ns_op = nps.__getattribute__(op)
            np_op = np.__getattribute__(op)
            if op == "median":
                np_result = np_op(np_arr, axis=axis, keepdims=keepdims)
                ba_result: BlockArray = ns_op(ba, axis=axis, keepdims=keepdims)
            elif op == "quantile":
                q = q / 100
                np_result = np_op(np_arr, q, axis=axis, keepdims=keepdims)
                ba_result: BlockArray = ns_op(ba,
                                              q,
                                              axis=axis,
                                              keepdims=keepdims)
            assert ba_result.grid.grid_shape == ba_result.blocks.shape
            assert ba_result.size == np_result.size
            assert np.allclose(ba_result.get(), np_result)
Ejemplo n.º 5
0
def test_argops(nps_app_inst):
    from nums import numpy as nps
    from nums.numpy import BlockArray

    assert nps_app_inst is not None

    bas = [
        nps.array([5, -2, 4, 8]),
        nps.array([1, 2, 3, 4]),
        nps.array([3, 2, 1, 0]),
        nps.array([-1, -2, -3, -0]),
    ]
    block_shapes = [(1, ), (2, ), (3, ), (4, )]
    for ba in bas:
        for block_shape in block_shapes:
            ba = ba.reshape(block_shape=block_shape)
            np_arr = ba.get()
            op_params = ["argmin", "argmax"]
            axis_params = [None, 0]

            for op, axis in itertools.product(op_params, axis_params):
                ns_op = nps.__getattribute__(op)
                np_op = np.__getattribute__(op)
                np_result = np_op(np_arr, axis=axis)
                ba_result: BlockArray = ns_op(ba, axis=axis)
                assert ba_result.grid.grid_shape == ba_result.blocks.shape
                assert ba_result.shape == np_result.shape
                assert np.allclose(ba_result.get(), np_result)
Ejemplo n.º 6
0
def test_ufunc(nps_app_inst):
    import numpy as np

    from nums.numpy import BlockArray
    from nums.numpy import numpy_utils
    from nums import numpy as nps

    assert nps_app_inst is not None

    uops, bops = numpy_utils.ufunc_op_signatures()
    for name, _ in sorted(uops):
        if name in ("arccosh", "arcsinh"):
            np_val = np.array([np.e])
        elif name == "invert" or name.startswith("bitwise") or name.startswith(
                "logical"):
            np_val = np.array([True, False], dtype=np.bool_)
        else:
            np_val = np.array([.1, .2, .3])
        ns_val = nps.array(np_val)
        ns_ufunc = nps.__getattribute__(name)
        np_ufunc = np.__getattribute__(name)
        np_result = np_ufunc(np_val)
        ns_result: BlockArray = ns_ufunc(ns_val)
        assert np.allclose(np_result, ns_result.get())

    def check_bop(_name, _np_a, _np_b):
        np_ufunc = np.__getattribute__(_name)
        ns_ufunc = nps.__getattribute__(_name)
        if _name in ("ldexp", ) and str(
                _np_b.dtype) not in ("int", "int32", "int64"):
            return
        _ns_a = nps.array(_np_a)
        _ns_b = nps.array(_np_b)
        _np_result = np_ufunc(_np_a, _np_b)
        _ns_result = ns_ufunc(_ns_a, _ns_b)
        assert np.allclose(_np_result, _ns_result.get())

    for name, _ in bops:
        if name.startswith("bitwise") or name.startswith("logical"):
            np_a = np.array([True, False, True, False], dtype=np.bool_)
            np_b = np.array([True, True, False, False], dtype=np.bool_)
            check_bop(name, np_a, np_b)
        elif name in ("gcd", "lcm"):
            np_a = np.array([8, 3, 7], dtype=np.int)
            np_b = np.array([4, 12, 13], dtype=np.int)
            check_bop(name, np_a, np_b)
        elif name.endswith("shift"):
            np_a = np.array([7 * 10**3, 8 * 10**3, 9 * 10**3], dtype=np.int)
            np_b = np.array([1, 2, 3], dtype=np.int)
            check_bop(name, np_a, np_b)
        else:
            pairs = [
                (np.array([.1, 5.0, .3]), np.array([.2, 6.0, .3])),
                (np.array([.1, 5.0, .3]), np.array([4, 2, 6], dtype=np.int)),
                (np.array([3, 7, 3],
                          dtype=np.int), np.array([4, 2, 6], dtype=np.int)),
            ]
            for np_a, np_b in pairs:
                check_bop(name, np_a, np_b)
Ejemplo n.º 7
0
 def check_tensordot_op(_np_a, _np_b, axes):
     _name = "tensordot"
     np_ufunc = np.__getattribute__(_name)
     ns_ufunc = nps.__getattribute__(_name)
     _ns_a = nps.array(_np_a)
     _ns_b = nps.array(_np_b)
     _np_result = np_ufunc(_np_a, _np_b, axes=axes)
     _ns_result = ns_ufunc(_ns_a, _ns_b, axes=axes)
     assert np.allclose(_np_result, _ns_result.get())
Ejemplo n.º 8
0
 def check_matmul_op(_np_a, _np_b):
     _name = "matmul"
     np_ufunc = np.__getattribute__(_name)
     ns_ufunc = nps.__getattribute__(_name)
     _ns_a = nps.array(_np_a)
     _ns_b = nps.array(_np_b)
     _np_result = np_ufunc(_np_a, _np_b)
     _ns_result = ns_ufunc(_ns_a, _ns_b)
     assert np.allclose(_np_result, _ns_result.get())
def test_basic_creation(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    ops = "empty", "zeros", "ones"
    shape = (2, 3, 4)
    for op in ops:
        ba: BlockArray = nps.__getattribute__(op)(shape=shape)
        if "zeros" in op:
            assert nps.allclose(nps.zeros(shape), ba)
        if "ones" in op:
            assert nps.allclose(nps.ones(shape), ba)

        ba2: BlockArray = nps.__getattribute__(op + "_like")(ba)
        assert ba.shape == ba2.shape
        assert ba.dtype == ba2.dtype
        assert ba.block_shape == ba2.block_shape
Ejemplo n.º 10
0
 def check_bop(_name, _np_a, _np_b):
     np_ufunc = np.__getattribute__(_name)
     ns_ufunc = nps.__getattribute__(_name)
     if _name in ("ldexp",) and str(_np_b.dtype) not in ("int", "int32", "int64"):
         return
     _ns_a = nps.array(_np_a)
     _ns_b = nps.array(_np_b)
     _np_result = np_ufunc(_np_a, _np_b)
     _ns_result = ns_ufunc(_ns_a, _ns_b)
     assert np.allclose(_np_result, _ns_result.get())
Ejemplo n.º 11
0
    def check_value_error(_ns_a, _ns_b, _a_blockshape=None, _b_blockshape=None):
        for _op in _ops:
            ns_op = nps.__getattribute__(_op)

            if _a_blockshape:
                _ns_a = _ns_a.reshape(block_shape=_a_blockshape)
            if _b_blockshape:
                _ns_b = _ns_b.reshape(block_shape=_b_blockshape)

            with pytest.raises(ValueError):
                ns_op(_ns_a, _ns_b)
Ejemplo n.º 12
0
    def check_swapaxes(_np_a, axis1, axis2):
        ns_ins = application_manager.instance()
        np_swapaxes = np.__getattribute__("swapaxes")
        ns_swapaxes = nps.__getattribute__("swapaxes")

        _ns_a = nps.array(_np_a)
        _ns_ins_a = ns_ins.array(_np_a, block_shape=block_shape)

        _np_result = np_swapaxes(_np_a, axis1, axis2)
        _ns_result = ns_swapaxes(_ns_a, axis1, axis2)
        _ns_ins_result = ns_swapaxes(_ns_ins_a, axis1, axis2)
        assert np.allclose(_np_result, _ns_result.get())
        assert np.allclose(_np_result, _ns_ins_result.get())
Ejemplo n.º 13
0
def test_manual(nps_app_inst):
    assert nps_app_inst is not None
    import nums.numpy as nps

    test_set = {}
    test_set.update(get_test_set_a())
    test_set.update(get_test_set_b2f())
    test_set.update(get_test_set_g2n())
    test_set.update(get_test_set_o2z())

    for func_name in test_set:
        nps_func = nps.__getattribute__(func_name)
        np_func = np.__getattribute__(func_name)
        for params in test_set[func_name]:
            assert np.allclose(nps_func(*params).get(), np_func(*params))
Ejemplo n.º 14
0
    def check_expand_and_squeeze(_np_a, axes):
        np_expand_dims = np.__getattribute__("expand_dims")
        ns_expand_dims = nps.__getattribute__("expand_dims")
        np_squeeze = np.__getattribute__("squeeze")
        ns_squeeze = nps.__getattribute__("squeeze")

        _ns_a = nps.array(_np_a)
        _ns_ins_a = ns_ins.array(_np_a, block_shape=block_shape)

        _np_result = np_expand_dims(_np_a, axes)
        _ns_result = ns_expand_dims(_ns_a, axes)
        _ns_ins_result = ns_expand_dims(_ns_ins_a, axes)
        assert np.allclose(_np_result, _ns_result.get())
        assert np.allclose(_np_result, _ns_ins_result.get())
        check_dim(_np_result, _ns_result)
        check_dim(_np_result, _ns_ins_result)

        _np_result = np_squeeze(_np_a)
        _ns_result = ns_squeeze(_ns_a)
        _ns_ins_result = ns_squeeze(_ns_ins_a)
        assert np.allclose(_np_result, _ns_result.get())
        assert np.allclose(_np_result, _ns_ins_result.get())
        check_dim(_np_result, _ns_result)
        check_dim(_np_result, _ns_ins_result)
Ejemplo n.º 15
0
def test_stack(nps_app_inst):
    import nums.numpy as nps
    assert nps_app_inst is not None

    for fname in ["hstack", "vstack", "dstack", "row_stack", "column_stack"]:
        nps_func = nps.__getattribute__(fname)
        np_func = np.__getattribute__(fname)
        a = nps.array((1, 2, 3))
        b = nps.array((2, 3, 4))
        np_a, np_b = a.get(), b.get()
        assert np.allclose(nps_func((a, b)).get(), np_func((np_a, np_b)))
        a = nps.array([[1], [2], [3]])
        b = nps.array([[2], [3], [4]])
        np_a, np_b = a.get(), b.get()
        assert np.allclose(nps_func((a, b)).get(), np_func((np_a, np_b)))
Ejemplo n.º 16
0
    def check_basic_broadcast_correctness(
        _np_a, _np_b, _a_blockshape=None, _b_blockshape=None
    ):
        ns_a = nps.array(_np_a)
        ns_b = nps.array(_np_b)

        if _a_blockshape:
            ns_a = ns_a.reshape(block_shape=_a_blockshape)
        if _b_blockshape:
            ns_b = ns_b.reshape(block_shape=_b_blockshape)

        for _op in _ops:
            np_op = np.__getattribute__(_op)
            ns_op = nps.__getattribute__(_op)

            _np_result = np_op(_np_a, _np_b)
            _ns_result = ns_op(ns_a, ns_b)

            assert np.allclose(_np_result, _ns_result.get())
Ejemplo n.º 17
0
    def check_bitwise_error(_name, error):
        np_ufunc = np.__getattribute__(_name)
        ns_ufunc = nps.__getattribute__(_name)

        _np_a = np.random.randn(2, 2)
        _np_b = np.random.randn(2, 2)

        _ns_a = nps.array(_np_a).touch()
        _ns_b = nps.array(_np_b).touch()
        message = ""

        # Catching expected error message to ensure same error message is raised below.
        try:
            _np_result = np_ufunc(_np_a, _np_b)
        except TypeError as err:
            message = err.args[0]

        assert message

        with pytest.raises(error, match=message):
            _ns_result = ns_ufunc(_ns_a, _ns_b)
Ejemplo n.º 18
0
def test_average(nps_app_inst):
    from nums import numpy as nps
    from nums.numpy import BlockArray

    assert nps_app_inst is not None

    bas = [
        nps.array([
            [[5, -2, 4, 8], [1, 2, 3, 4], [3, 2, 1, 0], [-1, -2, -3, 0]],
            [[6, -4, 2, 7], [4, 3, 2, 1], [1, 2, 0, 3], [0, -2, -3, -1]],
        ]),
        nps.array([
            [[5, -2, 4, 8], [1, 2, 3, 4], [3, 2, 1, 0], [-1, -2, -3, 0]],
            [[6, -4, 2, 7], [4, 3, 2, 1], [1, 2, 0, 3], [0, -2, -3, -1]],
        ]),
    ]
    ba_wts = [
        None,
        nps.array([
            [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]],
            [
                [-2, -3, -4, -5],
                [-2, -3, -4, -5],
                [-2, -3, -4, -5],
                [-2, -3, -4, -5],
            ],
        ]),
    ]
    ba_shapes = [(1, 1, 1), (1, 2, 2), (2, 1, 4), (2, 4, 2), (2, 4, 4)]
    for ba, ba_wt in zip(bas, ba_wts):
        for ba_shape in ba_shapes:
            ba = ba.reshape(block_shape=ba_shape)
            if ba_wt:
                ba_wt = ba_wt.reshape(block_shape=ba_shape)
            np_arr = ba.get()
            np_wt = ba_wt.get() if ba_wt else None
            op_params = ["average"]
            axis_params = [None, 0, 1, 2]

            for op, axis in itertools.product(op_params, axis_params):
                ns_op = nps.__getattribute__(op)
                np_op = np.__getattribute__(op)
                np_result = np_op(np_arr,
                                  axis=axis,
                                  weights=np_wt,
                                  returned=True)
                ba_result: BlockArray = ns_op(ba,
                                              axis=axis,
                                              weights=ba_wt,
                                              returned=True)

                np_avg, np_ws = np_result[0], np_result[1]
                ba_avg, ba_ws = ba_result[0], ba_result[1]
                assert np.allclose(ba_ws.get(), np_ws)
                assert ba_avg.grid.grid_shape == ba_avg.blocks.shape
                assert ba_avg.shape == np_avg.shape
                assert np.allclose(ba_avg.get(), np_avg)

    # Test zero division error
    ba = nps.array([
        [[5, -2, 4, 8], [1, 2, 3, 4], [3, 2, 1, 0], [-1, -2, -3, 0]],
        [[6, -4, 2, 7], [4, 3, 2, 1], [1, 2, 0, 3], [0, -2, -3, -1]],
    ])
    ba_wt = nps.array([
        [[1, 2, 3, 4], [1, 2, 3, -12], [1, 2, 3, 4], [1, 2, 3, 4]],  # axis 1
        [
            [-2, -3, 10, -5],  # axis 2
            [-2, -3, -4, -5],
            [-2, -2, -4, -5],  # axis 0
            [-2, -3, -4, -5],
        ],
    ])
    for ax in range(3):
        err_match = True
        try:
            np.average(ba.get(), axis=ax, weights=ba_wt.get(), returned=False)
            err_match = False
        except ZeroDivisionError:
            pass
        try:
            nps.average(ba, axis=ax, weights=ba_wt, returned=False)
            err_match = False
        except ZeroDivisionError:
            pass
        assert err_match
 def check_dim(_np_a, _ns_a):
     np_ndim = np.__getattribute__('ndim')
     ns_ndim = nps.__getattribute__('ndim')
     assert np_ndim(_np_a) == ns_ndim(_ns_a)