def testCreateSparseExecution(self): mat = sps.csr_matrix([[0, 0, 2], [2, 0, 0]]) t = tensor(mat, dtype='f8', chunk_size=2) res = self.executor.execute_tensor(t) self.assertIsInstance(res[0], SparseNDArray) self.assertEqual(res[0].dtype, np.float64) np.testing.assert_array_equal(res[0].toarray(), mat[..., :2].toarray()) np.testing.assert_array_equal(res[1].toarray(), mat[..., 2:].toarray()) t2 = ones_like(t, dtype='f4') res = self.executor.execute_tensor(t2) expected = sps.csr_matrix([[0, 0, 1], [1, 0, 0]]) self.assertIsInstance(res[0], SparseNDArray) self.assertEqual(res[0].dtype, np.float32) np.testing.assert_array_equal(res[0].toarray(), expected[..., :2].toarray()) np.testing.assert_array_equal(res[1].toarray(), expected[..., 2:].toarray()) t3 = tensor(np.array([[0, 0, 2], [2, 0, 0]]), chunk_size=2).tosparse() res = self.executor.execute_tensor(t3) self.assertIsInstance(res[0], SparseNDArray) self.assertEqual(res[0].dtype, np.int_) np.testing.assert_array_equal(res[0].toarray(), mat[..., :2].toarray()) np.testing.assert_array_equal(res[1].toarray(), mat[..., 2:].toarray())
def testCreateSparseExecution(self): mat = sps.csr_matrix([[0, 0, 2], [2, 0, 0]]) t = tensor(mat, dtype='f8', chunk_size=2) res = self.executor.execute_tensor(t) self.assertIsInstance(res[0], SparseNDArray) self.assertEqual(res[0].dtype, np.float64) np.testing.assert_array_equal(res[0].toarray(), mat[..., :2].toarray()) np.testing.assert_array_equal(res[1].toarray(), mat[..., 2:].toarray()) t2 = ones_like(t, dtype='f4') res = self.executor.execute_tensor(t2) expected = sps.csr_matrix([[0, 0, 1], [1, 0, 0]]) self.assertIsInstance(res[0], SparseNDArray) self.assertEqual(res[0].dtype, np.float32) np.testing.assert_array_equal(res[0].toarray(), expected[..., :2].toarray()) np.testing.assert_array_equal(res[1].toarray(), expected[..., 2:].toarray()) t3 = tensor(np.array([[0, 0, 2], [2, 0, 0]]), chunk_size=2).tosparse() res = self.executor.execute_tensor(t3) self.assertIsInstance(res[0], SparseNDArray) self.assertEqual(res[0].dtype, np.int_) np.testing.assert_array_equal(res[0].toarray(), mat[..., :2].toarray()) np.testing.assert_array_equal(res[1].toarray(), mat[..., 2:].toarray()) # test missing argument t4 = tensor(np.array([[0, 0, 2], [2, 0, 0]]), chunk_size=2).tosparse(missing=2) t4 = t4 + 1 expected = mat.toarray() raw = expected.copy() expected[raw == 0] += 1 expected[raw != 0] = 0 res = self.executor.execute_tensor(t4, concat=True)[0] self.assertIsInstance(res, SparseNDArray) self.assertEqual(res.dtype, np.int_) np.testing.assert_array_equal(res.toarray(), expected) # test missing argument that is np.nan t5 = tensor(np.array([[np.nan, np.nan, 2], [2, np.nan, -999]]), chunk_size=2).tosparse(missing=[np.nan, -999]) t5 = (t5 + 1).todense(fill_value=np.nan) expected = mat.toarray().astype(float) expected[expected != 0] += 1 expected[expected == 0] = np.nan res = self.executor.execute_tensor(t5, concat=True)[0] self.assertEqual(res.dtype, np.float64) np.testing.assert_array_equal(res, expected)
def test_create_sparse_execution(setup): mat = sps.csr_matrix([[0, 0, 2], [2, 0, 0]]) t = tensor(mat, dtype='f8', chunk_size=2) res = t.execute().fetch() assert isinstance(res, SparseNDArray) assert res.dtype == np.float64 np.testing.assert_array_equal(res.toarray(), mat.toarray()) t2 = ones_like(t, dtype='f4') res = t2.execute().fetch() expected = sps.csr_matrix([[0, 0, 1], [1, 0, 0]]) assert isinstance(res, SparseNDArray) assert res.dtype == np.float32 np.testing.assert_array_equal(res.toarray(), expected.toarray()) t3 = tensor(np.array([[0, 0, 2], [2, 0, 0]]), chunk_size=2).tosparse() res = t3.execute().fetch() assert isinstance(res, SparseNDArray) assert res.dtype == np.int_ np.testing.assert_array_equal(res.toarray(), mat.toarray()) # test missing argument t4 = tensor(np.array([[0, 0, 2], [2, 0, 0]]), chunk_size=2).tosparse(missing=2) t4 = t4 + 1 expected = mat.toarray() raw = expected.copy() expected[raw == 0] += 1 expected[raw != 0] = 0 res = t4.execute().fetch() assert isinstance(res, SparseNDArray) assert res.dtype == np.int_ np.testing.assert_array_equal(res.toarray(), expected) # test missing argument that is np.nan t5 = tensor(np.array([[np.nan, np.nan, 2], [2, np.nan, -999]]), chunk_size=2).tosparse(missing=[np.nan, -999]) t5 = (t5 + 1).todense(fill_value=np.nan) expected = mat.toarray().astype(float) expected[expected != 0] += 1 expected[expected == 0] = np.nan res = t5.execute().fetch() assert res.dtype == np.float64 np.testing.assert_array_equal(res, expected)