def test_assign_multiple_coef_for_single_description(self, dtype): xin = nlcpy.random.rand(2, 3).astype(dtype) dx = nlcpy.sca.create_descriptor(xin) coef1 = nlcpy.array(1, dtype=dtype) coef2 = nlcpy.array(2, dtype=dtype) with self.assertRaises(TypeError): dx[...] * coef1 * coef2
def test_nlcpy_indices_integer_array_3(self): a = nlcpy.zeros(self.shape) index = nlcpy.array([3, -5]) original_index = index.copy() a[[1, 1], index] = nlcpy.array(1.) testing.assert_array_equal(a, nlcpy.array([[0., 0., 0.], [1., 1., 0.]])) testing.assert_array_equal(index, original_index)
def test_nlcpy_indices_boolean_array(self): a = nlcpy.zeros(self.shape) index = nlcpy.array([True, False]) original_index = index.copy() a[index] = nlcpy.array(1.) testing.assert_array_equal(a, nlcpy.array([[1., 1., 1.], [0., 0., 0.]])) testing.assert_array_almost_equal(original_index, index)
def test_me_case_1(): a = ny.array([[10, 7, 4], [3, 2, 1]]) na = ny.array(a) ans = ny.std(na, axis=0) out = ny.zeros_like(ans) dst = ny.std(na, axis=0, out=out) assert_array_almost_equal(dst.get(), out.get())
def test_unary_op_out(self, dtype): a = nlcpy.array(np.array([0, 1, 2]), dtype=dtype) b = a.get() outb = np.sin(b) # pre-make output with same type as input outa = nlcpy.array(np.array([0, 1, 2]), dtype=outb.dtype) nlcpy.sin(a, out=outa) self.assertTrue(np.allclose(outa.get(), outb))
def test_binary_op_out(self, dtype): a1 = nlcpy.array(np.array([0, 1, 2]), dtype=dtype) a2 = nlcpy.array(np.array([0, 1, 2]), dtype=dtype) outa = nlcpy.array(np.array([0, 1, 2]), dtype=dtype) nlcpy.add(a1, a2, out=outa) b1 = a1.get() b2 = a2.get() outb = np.add(b1, b2) self.assertTrue(np.allclose(outa.get(), outb))
def test_assign_multiple_coef_for_multiple_description(self, dtype): xin = nlcpy.arange(10).astype(dtype) dx = nlcpy.sca.create_descriptor(xin) coef1 = nlcpy.array(-1, dtype=dtype) coef2 = nlcpy.array(2, dtype=dtype) coef3 = nlcpy.array(3, dtype=dtype) desc = dx[0] * coef1 + dx[0] * coef2 + dx[0] * coef3 res_sca = nlcpy.sca.create_kernel(desc).execute() res_naive = xin * coef1 + xin * coef2 + xin * coef3 testing.assert_allclose(res_sca, res_naive)
def test_accumulate_too_large_left_shift(self): for dtype in 'iI': x = nlcpy.left_shift.accumulate(nlcpy.array([1, 32, 32], dtype=dtype)) if x[1] != 0 or x[2] != 0: raise ValueError for dtype in 'lL': x = nlcpy.left_shift.accumulate(nlcpy.array([1, 32, 32], dtype=dtype)) if x[1] == 0 or x[2] != 0: raise ValueError
def testt_me_case_12(): np_a = np.array([[10, 5, 2, 4, 9, 3, 2], [10, 2, 8, 3, 7, 4, 1]]) np_y = np.array([1, 2, 2, 1, 1, 1, 1]) ny_a = ny.array([[10, 5, 2, 4, 9, 3, 2], [10, 2, 8, 3, 7, 4, 1]]) ny_y = ny.array([1, 2, 2, 1, 1, 1, 1]) ans_np = np.cov(np_a, fweights=np_y) ans_ny = ny.cov(ny_a, fweights=ny_y) print("numpy={} nlcpy={}".format(ans_np, ans_ny)) assert_array_almost_equal(ans_np, ans_ny.get())
def test_binary_op(self, dtype): a1 = nlcpy.array(np.array([0, 1, 2]), dtype=dtype) a2 = nlcpy.array(np.array([0, 1, 2]), dtype=dtype) outa = a1 + a2 # nlcpy operation produced a nlcpy array self.assertTrue(isinstance(outa, nlcpy.ndarray)) b1 = a1.get() b2 = a2.get() outb = np.add(b1, b2) # numpy operation produced a numpy array self.assertTrue(isinstance(outb, np.ndarray)) self.assertTrue(np.allclose(outa.get(), outb))
def test_outer_too_large_left_shift(self): for dtype in 'iI': x = nlcpy.left_shift.outer(nlcpy.array([1], dtype=dtype), nlcpy.array([32], dtype=dtype)) if x[0] != 0: raise ValueError for dtype in 'lL': x = nlcpy.left_shift.outer(nlcpy.array([1], dtype=dtype), nlcpy.array([64], dtype=dtype)) if x[0] != 0: raise ValueError
def test_setitem(self): a_cpu = numpy.arange(numpy.prod(self.shape)).reshape(self.shape) a = nlcpy.array(a_cpu) indexes_ve = [] for s in self.indexes: if isinstance(s, numpy.ndarray): s = nlcpy.array(s) indexes_ve.append(s) indexes_ve = tuple(indexes_ve) a[indexes_ve] = -1 a_cpu[self.indexes] = -1 testing.assert_array_equal(a, a_cpu)
def testt_me_case_14(): np_a = np.array([[10, 5, 2, 4, 9, 3, 2], [10, 2, 8, 3, 7, 4, 1]]) ny_a = ny.array([[10, 5, 2, 4, 9, 3, 2], [10, 2, 8, 3, 7, 4, 1]]) np_w = np.array([0.1, 0.2, 0.2, 0.2, 0.1, 0.1, 0.1]) ny_w = ny.array([0.1, 0.2, 0.2, 0.2, 0.1, 0.1, 0.1]) ans_np = np.cov(np_a, aweights=np_w) ans_ny = ny.cov(ny_a, aweights=ny_w) print("numpy={} nlcpy={}".format(ans_np, ans_ny)) assert_array_almost_equal(ans_np, ans_ny.get())
def test_me_case_3(): np_a = np.array([[1, 2, 1, 9, 10, 3, 2, 6, 7], [2, 1, 8, 3, 7, 5, 10, 7, 2]]) ny_a = ny.array([[1, 2, 1, 9, 10, 3, 2, 6, 7], [2, 1, 8, 3, 7, 5, 10, 7, 2]]) np_y = np.array([2, 1, 1, 8, 9, 4, 3, 5, 7]) ny_y = ny.array([2, 1, 1, 8, 9, 4, 3, 5, 7]) ans_np = np.corrcoef(np_a, np_y) ans_ny = ny.corrcoef(ny_a, ny_y) assert_array_almost_equal(ans_np, ans_ny.get())
def test_me_case_3(): np_a = np.array([[1, 2, 1, 9, 10, 3, 2, 6, 7], [2, 1, 8, 3, 7, 5, 10, 7, 2]]) ny_a = ny.array([[1, 2, 1, 9, 10, 3, 2, 6, 7], [2, 1, 8, 3, 7, 5, 10, 7, 2]]) np_y = np.array([2, 1, 1, 8, 9, 4, 3, 5, 7]) ny_y = ny.array([2, 1, 1, 8, 9, 4, 3, 5, 7]) ans_np = np.cov(np_a, np_y) ans_ny = ny.cov(ny_a, ny_y) print("numpy={} nlcpy={}".format(ans_np, ans_ny)) assert_array_almost_equal(ans_np, ans_ny.get())
def test_me_case_5(): ny_a = ny.array([[10, ny.nan, 4], [3, 2, 1]]) np_a = np.array([[10, np.nan, 4], [3, 2, 1]]) ans_ny = ny.std(ny_a, axis=1) ans_np = np.std(np_a, axis=1) print("ans1={} ans2={}".format(ans_ny, ans_np))
def ca3(arg): d = arg[0] t = arg[1] a = arg[2] ss = 1 for x in d: ss *= x if t == 'int32' or t == 'int64' or t == 'uint32' or t == 'uint64': seed = [random.randint(1, ss * ss) for i in range(ss)] elif t == 'float64' or t == 'float32': seed = [random.random() for i in range(ss)] else: pass seed[0] = float('NaN') na = np.array(seed) src = np.nanvar(na, axis=a) np.save(fil, src) ok = np.load(fil) na = ny.array(seed) dst = ny.nanvar(na, axis=a) return ok, dst
def test_adv_getitem_nlcpy_indices2(self): a = nlcpy.zeros(self.shape) index = nlcpy.array([1, 0]) original_index = index.copy() b = a[(slice(None), index)] b_cpu = a.get()[(slice(None), index.get())] testing.assert_array_equal(b, b_cpu) testing.assert_array_equal(original_index, index)
def test_adv_getitem_nlcpy_indices3(self): a = nlcpy.zeros(self.shape) index = nlcpy.array([True, False]) original_index = index.copy() b = a[index] b_cpu = a.get()[index.get()] testing.assert_array_equal(b, b_cpu) testing.assert_array_equal(original_index, index)
def test_adv_getitem_nlcpy_indices5(self): a = nlcpy.zeros(self.shape) index = nlcpy.array([4, -5]) original_index = index.copy() b = a[[1, 0], index] b_cpu = a.get()[[1, 0], index.get() % self.shape[1]] testing.assert_array_equal(b, b_cpu) testing.assert_array_equal(original_index, index)
def test_me_case_2(): np_a = np.array([-2.1, -1, 4.3]) ny_a = ny.array([-2.1, -1, 4.3]) ans_np = np.corrcoef(np_a) ans_ny = ny.corrcoef(ny_a) assert_array_almost_equal(ans_np, ans_ny.get())
def test_me_case_3(): ny_a = ny.array([[10, 7, 4], [3, 2, 1]]) np_a = np.array([[10, 7, 4], [3, 2, 1]]) ny_ans = ny.std(ny_a, ddof=1) np_ans = np.std(np_a, ddof=1) assert_array_almost_equal(ny_ans.get(), np_ans)
def test_me_case_3(): ny_a = ny.array([[10, 7, 4], [3, float('NaN'), 1]]) np_a = np.array([[10, 7, 4], [3, float('NaN'), 1]]) ny_ans = ny.nanvar(ny_a, ddof=1) np_ans = np.nanvar(np_a, ddof=1) assert_array_almost_equal(ny_ans.get(), np_ans)
def test_me_case_2(): ny_a = ny.array([[10, float('NaN'), 4], [3, 2, 1]]) np_a = np.array([[10, float('NaN'), 4], [3, 2, 1]]) ny_ans = ny.nanstd(ny_a, ddof=0) np_ans = np.nanstd(np_a, ddof=0) assert_array_almost_equal(ny_ans.get(), np_ans)
def test_me_case_3(): ny_a = ny.array([[10, ny.nan, 4], [3, 2, 1]]) np_a = np.array([[10, np.nan, 4], [3, 2, 1]]) ans_ny = ny.mean(ny_a, axis=1) ans_np = np.mean(np_a, axis=1) print("ans1={} ans2={}".format(ans_ny, ans_np)) assert_array_equal(ans_np, ans_ny.get())
def test_me_case_2(): np_a = np.array([-2.1, -1, 4.3]) ny_a = ny.array([-2.1, -1, 4.3]) ans_np = np.cov(np_a) ans_ny = ny.cov(ny_a) print("numpy={} nlcpy={}".format(ans_np, ans_ny)) assert_array_almost_equal(ans_np, ans_ny.get())
def test_unary_op(self, dtype): a = nlcpy.array(np.array([0, 1, 2]), dtype=dtype) outa = nlcpy.sin(a) # nlcpy operation produced a nlcpy array self.assertTrue(isinstance(outa, nlcpy.ndarray)) b = a.get() outb = np.sin(b) # numpy operation produced a numpy array self.assertTrue(isinstance(outa, nlcpy.ndarray)) self.assertTrue(np.allclose(outa.get(), outb))
def test_me_case_5(): np_a = np.array([[1, 2, 1, 9, 10, 3, 2, 6, 7], [2, 1, 8, 3, 7, 5, 10, 7, 2]]) ny_a = ny.array([[1, 2, 1, 9, 10, 3, 2, 6, 7], [2, 1, 8, 3, 7, 5, 10, 7, 2]]) ans_np = np.corrcoef(np_a.T, rowvar=True) ans_ny = ny.corrcoef(ny_a.T, rowvar=True) assert_array_almost_equal(ans_np, ans_ny.get())
def test_getitem(self): a = nlcpy.arange(numpy.prod(self.shape)).reshape(self.shape) indexes_ve = [] for s in self.indexes: if isinstance(s, numpy.ndarray): s = nlcpy.array(s) indexes_ve.append(s) indexes_ve = tuple(indexes_ve) b = a[indexes_ve] b_cpu = a.get()[self.indexes] testing.assert_array_equal(b, b_cpu)
def test_me_case_5(): np_a = np.array([[1, 2, 1, 9, 10, 3, 2, 6, 7], [2, 1, 8, 3, 7, 5, 10, 7, 2]]) ny_a = ny.array([[1, 2, 1, 9, 10, 3, 2, 6, 7], [2, 1, 8, 3, 7, 5, 10, 7, 2]]) ans_np = np.cov(np_a.T, rowvar=True) ans_ny = ny.cov(ny_a.T, rowvar=True) print("numpy={} nlcpy={}".format(ans_np, ans_ny)) assert_array_almost_equal(ans_np, ans_ny.get())