def test_concatenate(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    ba1: BlockArray = nps.arange(5)
    ba2: BlockArray = nps.arange(6)
    ba = nps.concatenate((ba1, ba2))
    np_arr = np.concatenate((np.arange(5), np.arange(6)))
    assert np.allclose(ba.get(), np_arr)
示例#2
0
def test_array_copy(nps_app_inst):
    import nums.numpy as nps
    assert nps_app_inst is not None

    ba = nps.arange(10)
    ba2 = nps.array(ba, copy=True)
    assert ba is not ba2
示例#3
0
def test_shuffle(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    shape = (12, 34, 56)
    block_shape = (2, 5, 7)
    arr: BlockArray = nps.arange(np.product(shape)).reshape(shape, block_shape=block_shape)
    np_arr = arr.get()

    for axis in range(3):
        for axis_frac in (1.0, 0.5):
            rs = nps.random.RandomState(1337)
            idx: BlockArray = rs.permutation(int(shape[axis]*axis_frac))
            np_idx = idx.get()
            if axis == 0:
                arr_shuffle = arr[idx]
                np_arr_shuffle = np_arr[np_idx]
            else:
                arr_shuffle = arr._advanced_single_array_subscript((np_idx,), axis=axis)
                np_ss = [slice(None, None) for _ in range(3)]
                np_ss[axis] = np_idx
                np_ss = tuple(np_ss)
                np_arr_shuffle = np_arr[np_ss]
            assert np.all(np_arr_shuffle == arr_shuffle.get())
示例#4
0
def test_reshape(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None
    ba = nps.arange(2 * 3 * 4).reshape((2, 3, 4), block_shape=(2, 3, 4))
    assert nps.allclose(ba.reshape((6, 4), block_shape=(6, 4)),
                        nps.reshape(ba, shape=(6, 4)))
def test_arange(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    ba: BlockArray = nps.arange(5)
    np_arr = np.arange(5)
    assert np.allclose(ba.get(), np_arr)
示例#6
0
def test_arange(nps_app_inst):
    import nums.numpy as nps
    assert nps_app_inst is not None

    start_indices = [3.1, 3, 3.1]
    stop_indices = [5.1, 5.1, 5]

    for start, stop in zip(start_indices, stop_indices):
        a = nps.arange(start, stop).get()
        b = np.arange(start, stop)
        assert np.allclose(a, b)
def test_split(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    ba: BlockArray = nps.arange(10)
    np_arr = np.arange(10)
    ba_list = nps.split(ba, 2)
    np_arr_list = np.split(np_arr, 2)
    for i in range(len(np_arr_list)):
        assert np.allclose(ba_list[i].get(), np_arr_list[i])
示例#8
0
def test_reshape_int(nps_app_inst):
    import nums.numpy as nps
    assert nps_app_inst is not None

    shape = (3, 5, 10)
    arr = nps.arange(np.product(shape))
    np_arr = arr.get()
    assert np.allclose(np_arr.reshape(shape), arr.reshape(shape).get())
    assert np.allclose(
        np_arr.reshape(shape).reshape(-1),
        arr.reshape(shape).reshape(-1).get())
    assert np.allclose(
        np_arr.reshape(shape).reshape(np.product(shape)),
        arr.reshape(shape).reshape(np.product(shape)).get())
示例#9
0
def test_shuffle_subscript_ops(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    shape = (123, 45)
    block_shape = (10, 20)
    arr: BlockArray = nps.arange(np.product(shape)).reshape(shape, block_shape=block_shape)
    np_arr = arr.get()
    rs = nps.random.RandomState(1337)
    idx: BlockArray = rs.permutation(shape[1])
    np_idx = idx.get()
    arr_shuffle = arr[:, idx]
    np_arr_shuffle = np_arr[:, np_idx]
    assert np.all(np_arr_shuffle == arr_shuffle.get())
示例#10
0
def test_subscript_edge_cases(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None
    rs = nps.random.RandomState(1337)

    testset = [
        [(123, ), (10, ), rs.randint(123, size=7)],
        [(123, 45), (10, 20), rs.randint(123, size=13)],
    ]
    for shape, block_shape, idx in testset:
        arr: BlockArray = nps.arange(np.product(shape)).reshape(
            shape, block_shape=block_shape)
        np_arr = arr.get()
        np_idx = idx.get()
        result = arr[idx]
        np_result = np_arr[np_idx]
        assert np.all(np_result == result.get())

    # NumPy integer idx.
    arr: BlockArray = nps.arange(1)
    np_arr = arr.get()
    idx = nps.array(0)
    np_idx = idx.get()
    result = arr[idx]
    np_result = np_arr[np_idx]
    assert np.all(np_result == result.get())

    # NumPy array of length 1.
    arr: BlockArray = nps.arange(10)
    np_arr = arr.get()
    idx = nps.array([2])
    np_idx = idx.get()
    result = arr[idx]
    np_result = np_arr[np_idx]
    assert np.all(np_result == result.get())
示例#11
0
def test_atleast_1d(nps_app_inst):
    import nums.numpy as nps
    assert nps_app_inst is not None

    x = 1.0
    assert np.allclose(nps.atleast_1d(x).get(), np.atleast_1d(x))

    x_np = np.arange(9).reshape(3, 3)
    x_nps = nps.arange(9).reshape(3, 3)
    assert np.allclose(nps.atleast_1d(x_np).get(), np.atleast_1d(x_np))
    assert np.allclose(nps.atleast_1d(x_nps).get(), np.atleast_1d(x_np))
    assert np.atleast_1d(x_np) is x_np
    assert nps.atleast_1d(x_nps) is x_nps

    try_multiple_nd(np.atleast_1d, nps.atleast_1d)
示例#12
0
def test_blockarray_perm(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    shape = (12, 34)
    block_shape = (5, 10)
    arr: BlockArray = nps.arange(np.product(shape)).reshape(shape, block_shape=block_shape)
    np_arr = arr.get()
    rs = nps.random.RandomState(1337)
    np_arr_shuffle: BlockArray = rs.permutation(arr).get()
    for i in range(shape[0]):
        num_found = 0
        for j in range(shape[0]):
            if np.allclose(np_arr[i], np_arr_shuffle[j]):
                num_found += 1
        assert num_found == 1
示例#13
0
def test_select_assign(nps_app_inst):
    def select(idx, np_idx, arr, np_arr, idx_axes=None, idx_vals=None):
        ss = [slice(None, None) for _ in range(3)]
        ss[axis] = idx
        np_ss = [slice(None, None) for _ in range(3)]
        np_ss[axis] = np_idx
        if idx_axes:
            # If idx_axes is set, then we should not expect and exception.
            ss[idx_axes[0]], ss[idx_axes[1]] = idx_vals[0], idx_vals[1]
            np_ss[idx_axes[0]], np_ss[idx_axes[1]] = idx_vals[0], idx_vals[1]
        arr_shuffle = arr[tuple(ss)]
        np_arr_shuffle = np_arr[tuple(np_ss)]
        assert np.all(np_arr_shuffle == arr_shuffle.get())

    def assign(idx,
               np_idx,
               arr,
               np_arr,
               axis,
               mode,
               idx_axes=None,
               idx_vals=None):
        arr = arr.copy()
        np_arr = np_arr.copy()
        if mode == "scalar":
            np_value = np.random.randint(-np.product(shape), -1, size=1).item()
        elif mode == "single-dim":
            np_value = np.random.randint(-np.product(shape),
                                         -1,
                                         size=len(np_idx))
        elif mode == "multi-dim":
            value_shape = tuple(
                list(np_arr.shape[:axis]) + [len(np_idx)] +
                list(np_arr.shape[axis + 1:]))
            np_value = np.random.randint(-np.product(shape),
                                         -1,
                                         size=value_shape)
        else:
            raise Exception()
        value = nps.array(np_value)

        ss = [slice(None, None) for _ in range(3)]
        ss[axis] = idx
        np_ss = [slice(None, None) for _ in range(3)]
        np_ss[axis] = np_idx
        if mode == "single-dim" and axis != 2:
            if idx_axes:
                # If idx_axes is set, then we should not expect and exception.
                ss[idx_axes[0]], ss[idx_axes[1]] = idx_vals[0], idx_vals[1]
                np_ss[idx_axes[0]], np_ss[
                    idx_axes[1]] = idx_vals[0], idx_vals[1]
                arr[tuple(ss)] = value
                np_arr[tuple(np_ss)] = np_value
                assert np.all(np_arr == arr.get())
            else:
                with pytest.raises(ValueError):
                    np_arr[tuple(np_ss)] = np_value
                with pytest.raises(ValueError):
                    arr[tuple(ss)] = value
        else:
            if mode == "scalar":
                # Run indexed subscripts on scalar values.
                if idx_axes:
                    ss[idx_axes[0]], ss[idx_axes[1]] = idx_vals[0], idx_vals[1]
                    np_ss[idx_axes[0]], np_ss[
                        idx_axes[1]] = idx_vals[0], idx_vals[1]
            arr[tuple(ss)] = value
            np_arr[tuple(np_ss)] = np_value
            assert np.all(np_arr == arr.get())

    import nums.numpy as nps

    assert nps_app_inst is not None

    shape = (12, 34, 56)
    block_shape = (2, 5, 7)
    arr: BlockArray = nps.arange(np.product(shape)).reshape(
        shape, block_shape=block_shape)
    np_arr = arr.get()

    for axis in range(3):
        idx_axes = [(1, 2), (0, 2), None][axis]
        idx_values = [(13, 40), (3, 55), None][axis]
        for axis_frac in (1.0, 0.5):
            rs = nps.random.RandomState(1337)
            idx = rs.permutation(int(shape[axis] * axis_frac))
            np_idx = idx.get()
            select(idx, np_idx, arr, np_arr)
            select(idx,
                   np_idx,
                   arr,
                   np_arr,
                   idx_axes=idx_axes,
                   idx_vals=idx_values)
            for mode in ["scalar", "single-dim", "multi-dim"]:
                assign(idx, np_idx, arr, np_arr, axis, mode)
                assign(
                    idx,
                    np_idx,
                    arr,
                    np_arr,
                    axis,
                    mode,
                    idx_axes=idx_axes,
                    idx_vals=idx_values,
                )
            # Also test boolean mask.
            np_mask = np.zeros(shape[axis], dtype=bool)
            np_mask[np_idx] = True
            mask = nps.array(np_mask)
            assert np.allclose(mask.get(), np_mask)
            select(mask,
                   np_mask,
                   arr,
                   np_arr,
                   idx_axes=idx_axes,
                   idx_vals=idx_values)
            for mode in ["scalar", "single-dim", "multi-dim"]:
                assign(idx, np_idx, arr, np_arr, axis, mode)
                assign(
                    idx,
                    np_idx,
                    arr,
                    np_arr,
                    axis,
                    mode,
                    idx_axes=idx_axes,
                    idx_vals=idx_values,
                )