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)
def test_diag(nps_app_inst): import nums.numpy as nps assert nps_app_inst is not None ba: BlockArray = nps.array([1.0, 2.0, 3.0]) np_arr = ba.get() # Make a diag matrix. ba = nps.diag(ba) np_arr = np.diag(np_arr) assert np.allclose(ba.get(), np_arr) # Take diag of diag matrix. ba = nps.diag(ba) np_arr = np.diag(np_arr) assert np.allclose(ba.get(), np_arr) # Tests all combinations of sizes and block sizes in the range 1 to 4 for i in range(1, 4): for j in range(1, 4): for k in range(1, i + 1): for l in range(1, j + 1): ba: BlockArray = nps.array(np.full((i, j), 1)) ba = ba.reshape(block_shape=(k, l)) np_arr = ba.get() ba = nps.diag(ba) np_arr = np.diag(np_arr) assert np.allclose(ba.get(), np_arr)
def test_inner_outer(nps_app_inst): assert nps_app_inst is not None A = np.random.randn(10) B = np.random.randn(10) nps_A = nps.array(A) nps_B = nps.array(B) assert np.allclose(np.inner(A, B), nps.inner(nps_A, nps_B).get()) assert np.allclose(np.outer(A, B), nps.outer(nps_A, nps_B).get())
def check_tensordot_mismatch_simple_error(_np_a, _np_b, axes): _ns_a = nps.array(_np_a) _ns_b = nps.array(_np_b) with pytest.raises(ValueError): np.tensordot(_np_a, _np_b, axes=axes) with pytest.raises(ValueError): nps.tensordot(_ns_a, _ns_b, axes=axes)
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 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())
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())
def test_matmul_tensor_error(nps_app_inst): assert nps_app_inst is not None # TODO (bcp): Replace with matmul tests for rank > 2 once implemented. def check_matmul_tensor_error(_ns_a, _ns_b): with pytest.raises(NotImplementedError): nps.matmul(_ns_a, _ns_b) ns_a = nps.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) ns_b = nps.array([[[7, 6], [5, 4]], [[3, 2], [1, 0]]]) check_matmul_tensor_error(ns_a, ns_b)
def test_trace(nps_app_inst): import nums.numpy as nps assert nps_app_inst is not None a: BlockArray = nps.array([1.0, 2.0, 3.0, 4.0]) # Construct diagonal matrix with nums and numpy. a_diag = nps.diag(a) a_diag_np = np.diag(a.get()) # Apply trace to diagonal matrices. a_diag_trace = nps.trace(a_diag).get() a_diag_np_trace = np.trace(a_diag_np) assert np.allclose(a_diag_trace, a_diag_np_trace) # Test pre-defined diagonal matrices. b: BlockArray = nps.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) b_diag_trace = nps.trace(b).get() b_diag_np_trace = np.trace(b.get()) assert np.allclose(b_diag_trace, b_diag_np_trace) # Test that trace raises on arrays with 3+ axes. mat: BlockArray = nps.zeros((2, 3, 2)) with pytest.raises(ValueError): nps.trace(mat) # Test that trace raises when called with non-zero offset. mat: BlockArray = nps.array([1.0, 2.0, 3.0, 4.0]) mat_diag = nps.diag(mat) with pytest.raises(NotImplementedError): nps.trace(mat_diag, offset=2) # Test data type of trace. mat_diag = nps.diag(nps.array([1.01, 2.02, 3.03, 4.04])) mat_diag_np = np.diag(np.array([1.01, 2.02, 3.03, 4.04])) mat_diag_trace = nps.trace(mat_diag, dtype=int).get() mat_diag_np_trace = np.trace(mat_diag_np, dtype=int) assert np.allclose(mat_diag_trace, mat_diag_np_trace) assert mat_diag_trace.dtype == int # Test trace on non-square matrices ba: BlockArray = nps.array(np.full((10, 12), 1)) ba = ba.reshape(block_shape=(3, 4)) np_arr = ba.get() ba = nps.trace(ba) np_arr = np.trace(np_arr) assert np.allclose(ba.get(), np_arr)
def check_tensordot_axes_type_error(_np_a, _np_b, axes): _ns_a = nps.array(_np_a) _ns_b = nps.array(_np_b) # TODO (bcp): Remove test once tensordot over multiple axes is implemented. if is_array_like(axes): with pytest.raises(NotImplementedError): nps.tensordot(_ns_a, _ns_b, axes=axes) else: with pytest.raises(TypeError): np.tensordot(_np_a, _np_b, axes=axes) with pytest.raises(TypeError): nps.tensordot(_ns_a, _ns_b, axes=axes)
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)))
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)
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)
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
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)
def check_broadcast_block_correctness(block, grid_shape): _np_result = np.tile(block, grid_shape) ns_a = nps.zeros(_np_result.shape).reshape(block_shape=block.shape) ns_b = nps.array(block) _ns_result = ns_a + ns_b assert np.allclose(_np_result, _ns_result.get())
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())
def test_transpose(nps_app_inst): import nums.numpy as nps assert nps_app_inst is not None npX = np.arange(10).reshape(2, 5) X = nps.array(npX) assert np.all(npX.T == X.T.get()) assert np.all(npX.T == X.transpose().get()) assert np.all(npX.T == X.transpose(defer=True).get()) assert np.all(npX.T == X.transpose(redistribute=True).get()) assert np.all(npX.T == X.transpose(defer=True, redistribute=True).get())
def test_overloads(nps_app_inst): assert nps_app_inst is not None arithmetic_ops = [ lambda a, b: a % b, lambda a, b: a + b, lambda a, b: a - b, lambda a, b: a * b, lambda a, b: a / b, lambda a, b: a // b, lambda a, b: a ** b, ] bitwise_ops = [ lambda a, b: a ^ b, lambda a, b: a | b, lambda a, b: a & b, lambda a, b: a >> b, lambda a, b: a << b, ] ineq_ops = [ lambda a, b: a < b, lambda a, b: a <= b, lambda a, b: a > b, lambda a, b: a >= b, lambda a, b: a == b, lambda a, b: a != b, ] vals = [(arithmetic_ops, 1, 2), (ineq_ops, 1, 2), (bitwise_ops, True, False)] for ops, val1, val2 in vals: for op in ops: nps_val2 = nps.array(val2) np_val2 = np.array(val2) assert op(val1, np_val2) == op(val1, nps_val2).get() assert op(np_val2, val1) == op(nps_val2, val1).get() val1 = [[1, 2], [3, 4]] val2 = [[5, 6], [7, 8]] nps_val2 = nps.array(val2) np_val2 = np.array(val2) assert np.allclose(val1 @ np_val2, (val1 @ nps_val2).get()) assert np.allclose(np_val2 @ val1, (nps_val2 @ val1).get())
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)
def test_triu(nps_app_inst): import nums.numpy as nps assert nps_app_inst is not None a: BlockArray = nps.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) # Check 1D array case with no block_shape change. a_triu = nps.triu(a).get() a_triu_np = np.triu(a.get()) assert np.allclose(a_triu, a_triu_np) # Check 1D array with block_shape change a = a.reshape(block_shape=(2, )) a_triu = nps.triu(a).get() a_triu_np = np.triu(a.get()) assert np.allclose(a_triu, a_triu_np) # Check 1D array with block_shape change a = a.reshape(block_shape=(4, )) a_triu = nps.triu(a).get() a_triu_np = np.triu(a.get()) assert np.allclose(a_triu, a_triu_np) # Check 2D array a: BlockArray = nps.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]) a_triu = nps.triu(a).get() a_triu_np = np.triu(a.get()) assert np.allclose(a_triu, a_triu_np) # Check Tall 2D array a: BlockArray = nps.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]) a_triu = nps.triu(a).get() a_triu_np = np.triu(a.get()) assert np.allclose(a_triu, a_triu_np) # Check Square a: BlockArray = nps.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) a_triu = nps.triu(a).get() a_triu_np = np.triu(a.get()) assert np.allclose(a_triu, a_triu_np) # Check 2D array with block_shape change a: BlockArray = nps.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]) a = a.reshape(block_shape=(2, 1)) a_triu = nps.triu(a).get() a_triu_np = np.triu(a.get()) assert np.allclose(a_triu, a_triu_np) # Check 2D fat array with block_shape change a: BlockArray = nps.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]) a = a.reshape(block_shape=(1, 2)) a_triu = nps.triu(a).get() a_triu_np = np.triu(a.get()) assert np.allclose(a_triu, a_triu_np)
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())
def test_all_alltrue_any(nps_app_inst): import nums.numpy as nps assert nps_app_inst is not None true_int = np.array([[1, 2, 3], [1, 2, 3]]) false_int = np.array([[1, 2, 0], [1, 2, 3]]) true_bool = np.array([[True, True, True], [True, True, True]]) false_bool = np.array([[True, False, False], [False, True, True]]) true_float = np.array([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]) false_float = np.array([[1.0, 2.0, 0.0], [1.0, 2.0, 3.0]]) checks = [ true_int, false_int, true_bool, false_bool, true_float, false_float ] for array in checks: nps_array = nps.array(array).reshape(block_shape=(2, 2)) assert nps.all(nps_array).get() == np.all(array) assert nps.alltrue(nps_array).get() == np.alltrue(array) assert nps.all(nps_array).dtype is bool assert nps.alltrue(nps_array).dtype is bool false_int = np.array([[0, 0, 0], [0, 0, 0]]) false_bool = np.array([[False, False, False], [False, False, False]]) false_float = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) checks = [ true_int, false_int, true_bool, false_bool, true_float, false_float ] for array in checks: nps_array = nps.array(array).reshape(block_shape=(2, 2)) assert nps.any(nps_array).get() == np.any(array) assert nps.any(nps_array).dtype is bool
def test_diag(nps_app_inst): import nums.numpy as nps assert nps_app_inst is not None ba: BlockArray = nps.array([1.0, 2.0, 3.0]) np_arr = ba.get() # Make a diag matrix. ba = nps.diag(ba) np_arr = np.diag(np_arr) assert np.allclose(ba.get(), np_arr) # Take diag of diag matrix. ba = nps.diag(ba) np_arr = np.diag(np_arr) assert np.allclose(ba.get(), np_arr)
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())
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)
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)
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, )
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 test_array_eq(nps_app_inst): import nums.numpy as nps assert nps_app_inst is not None int_array_1 = np.array([[1, 2, 3], [4, 5, 6]]) int_array_2 = np.array([[3, 9, 1], [8, 4, 2]]) bool_array_1 = np.array([[True, False, True], [True, False, True]]) bool_array_2 = np.array([[False, False, True], [False, False, True]]) float_array_1 = np.array([[1e10, 1e-8, 1e-8], [1e10, 1e-8, 1e-8]]) float_array_2 = np.array([[1.00001e10, 1e-9, 1e-9], [1.00001e10, 1e-9, 1e-9]]) checks = [ (int_array_1, int_array_2), (bool_array_1, bool_array_2), (float_array_1, float_array_2), ] for check in checks: nps_array_1 = nps.array(check[0]).reshape(block_shape=(2, 2)) nps_array_2 = nps.array(check[1]).reshape(block_shape=(2, 2)) assert nps.array_equal(nps_array_1, nps_array_1).get() == np.array_equal( check[0], check[0]) assert nps.array_equal(nps_array_1, nps_array_2).get() == np.array_equal( check[0], check[1]) assert nps.array_equiv(nps_array_1, nps_array_1).get() == np.array_equiv( check[0], check[0]) assert nps.array_equiv(nps_array_1, nps_array_2).get() == np.array_equiv( check[0], check[1]) assert nps.allclose(nps_array_1, nps_array_1).get() == np.allclose( check[0], check[0]) assert nps.allclose(nps_array_1, nps_array_2).get() == np.allclose( check[0], check[1]) assert nps.array_equal(nps_array_1, nps_array_2).dtype is bool assert nps.array_equiv(nps_array_1, nps_array_2).dtype is bool assert nps.allclose(nps_array_1, nps_array_2).dtype is bool # False interaction test checks_1 = [ np.array([False]), np.array([False]), np.array([0]), np.array([0]), np.array([0.0]), np.array([0.0]), ] checks_2 = [ np.array([0]), np.array([0.0]), np.array([False]), np.array([0.0]), np.array([False]), np.array([0]), ] for check_1, check_2 in zip(checks_1, checks_2): nps_check_1 = nps.array(check_1) nps_check_2 = nps.array(check_2) assert nps.array_equal(nps_check_1, nps_check_2) == np.array_equal( check_1, check_2) assert nps.array_equiv(nps_check_1, nps_check_2) == np.array_equiv( check_1, check_2) # Infinity interaction test assert nps.array_equal(nps.array([nps.inf, nps.NINF]), nps.array([nps.NINF, nps.inf])) == np.array_equal( np.array([np.inf, np.NINF]), np.array([np.NINF, np.inf]))