Skip to content

Commit

Permalink
fix test case (#29)
Browse files Browse the repository at this point in the history
* fix test case
  • Loading branch information
hekaisheng authored and qinxuye committed Dec 13, 2018
1 parent 298c56b commit ffe306b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
12 changes: 6 additions & 6 deletions mars/tensor/execution/tests/test_linalg_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def testNormExecution(self):
inside = (norm(d, axis=1) < 0.5).sum().astype(float)
t = inside / 500 * 4
res = self.executor.execute_tensor(t)[0]
self.assertAlmostEqual(res, 3.14, delta=0.3)
self.assertAlmostEqual(res, 3.14, delta=1)

def testTensordotExecution(self):
a_data = np.arange(60).reshape(3, 4, 5)
Expand Down Expand Up @@ -392,31 +392,31 @@ def testSparseDotExecution(self):

res = self.executor.execute_tensor(c, concat=True)[0]
self.assertTrue(issparse(res))
np.testing.assert_array_equal(res.toarray(), a_data.dot(b_data).toarray())
np.testing.assert_allclose(res.toarray(), a_data.dot(b_data).toarray())

c2 = dot(a, b, sparse=False)

res = self.executor.execute_tensor(c2, concat=True)[0]
self.assertFalse(issparse(res))
np.testing.assert_array_equal(res, a_data.dot(b_data).toarray())
np.testing.assert_allclose(res, a_data.dot(b_data).toarray())

c3 = tensordot(a, b.T, (-1, -1), sparse=False)

res = self.executor.execute_tensor(c3, concat=True)[0]
self.assertFalse(issparse(res))
np.testing.assert_array_equal(res, a_data.dot(b_data).toarray())
np.testing.assert_allclose(res, a_data.dot(b_data).toarray())

c = inner(a, b.T)

res = self.executor.execute_tensor(c, concat=True)[0]
self.assertTrue(issparse(res))
np.testing.assert_array_equal(res.toarray(), a_data.dot(b_data).toarray())
np.testing.assert_allclose(res.toarray(), a_data.dot(b_data).toarray())

c = inner(a, b.T, sparse=False)

res = self.executor.execute_tensor(c, concat=True)[0]
self.assertFalse(issparse(res))
np.testing.assert_array_equal(res, a_data.dot(b_data).toarray())
np.testing.assert_allclose(res, a_data.dot(b_data).toarray())

def testVdotExecution(self):
a_data = np.array([1 + 2j, 3 + 4j])
Expand Down
60 changes: 33 additions & 27 deletions mars/tensor/execution/tests/test_reduction_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def testMaxMinExecution(self):
self.assertEqual([raw.max()], self.executor.execute_tensor(arr.max()))
self.assertEqual([raw.min()], self.executor.execute_tensor(arr.min()))

self.assertTrue(np.array_equal(raw.max(axis=0),
self.executor.execute_tensor(arr.max(axis=0), concat=True)[0]))
self.assertTrue(np.array_equal(raw.min(axis=0),
self.executor.execute_tensor(arr.min(axis=0), concat=True)[0]))
np.testing.assert_array_equal(
raw.max(axis=0), self.executor.execute_tensor(arr.max(axis=0), concat=True)[0])
np.testing.assert_array_equal(
raw.min(axis=0), self.executor.execute_tensor(arr.min(axis=0), concat=True)[0])

self.assertTrue(np.array_equal(raw.max(axis=(1, 2)),
self.executor.execute_tensor(arr.max(axis=(1, 2)), concat=True)[0]))
self.assertTrue(np.array_equal((raw.min(axis=(1, 2))),
self.executor.execute_tensor(arr.min(axis=(1, 2)), concat=True)[0]))
np.testing.assert_array_equal(
raw.max(axis=(1, 2)), self.executor.execute_tensor(arr.max(axis=(1, 2)), concat=True)[0])
np.testing.assert_array_equal(
raw.min(axis=(1, 2)), self.executor.execute_tensor(arr.min(axis=(1, 2)), concat=True)[0])

raw = sps.random(10, 10, density=.5)

Expand All @@ -85,10 +85,10 @@ def testAllAnyExecution(self):
self.assertTrue(self.executor.execute_tensor(arr2.all())[0])
self.assertFalse(self.executor.execute_tensor(arr1.any())[0])
self.assertTrue(self.executor.execute_tensor(arr1.any()))
self.assertTrue(np.array_equal(raw3.all(axis=1),
self.executor.execute_tensor(arr3.all(axis=1))[0]))
self.assertTrue(np.array_equal(raw3.any(axis=0),
self.executor.execute_tensor(arr3.any(axis=0))[0]))
np.testing.assert_array_equal(raw3.all(axis=1),
self.executor.execute_tensor(arr3.all(axis=1))[0])
np.testing.assert_array_equal(raw3.any(axis=0),
self.executor.execute_tensor(arr3.any(axis=0))[0])

raw = sps.random(10, 10, density=.5) > .5

Expand Down Expand Up @@ -262,13 +262,19 @@ def testArgReduction(self):
self.assertEqual(raw.argmin(),
self.executor.execute_tensor(arr.argmin())[0])

self.assertTrue(np.array_equal(raw.argmax(axis=0),
self.executor.execute_tensor(arr.argmax(axis=0), concat=True)[0]))
self.assertTrue(np.array_equal(raw.argmin(axis=0),
self.executor.execute_tensor(arr.argmin(axis=0), concat=True)[0]))
np.testing.assert_array_equal(
raw.argmax(axis=0), self.executor.execute_tensor(arr.argmax(axis=0), concat=True)[0])
np.testing.assert_array_equal(
raw.argmin(axis=0), self.executor.execute_tensor(arr.argmin(axis=0), concat=True)[0])

raw = sps.random(20, 20, density=.1)
raw_format = sps.random(20, 20, density=.1, format='lil')

random_min = np.random.randint(0, 200)
random_max = np.random.randint(200, 400)
raw[np.unravel_index(random_min, raw.shape)] = -1
raw[np.unravel_index(random_max, raw.shape)] = 2

raw = raw_format.tocoo()
arr = tensor(raw, chunks=3)

self.assertEqual(raw.argmax(),
Expand Down Expand Up @@ -346,8 +352,8 @@ def testCumReduction(self):
res2 = self.executor.execute_tensor(arr.cumprod(axis=1), concat=True)
expected1 = raw.cumsum(axis=1)
expected2 = raw.cumprod(axis=1)
self.assertTrue(np.array_equal(res1[0], expected1))
self.assertTrue(np.array_equal(res2[0], expected2))
np.testing.assert_array_equal(res1[0], expected1)
np.testing.assert_array_equal(res2[0], expected2)

raw = sps.random(8, 8, density=.1)

Expand All @@ -357,8 +363,8 @@ def testCumReduction(self):
res2 = self.executor.execute_tensor(arr.cumprod(axis=1), concat=True)
expected1 = raw.A.cumsum(axis=1)
expected2 = raw.A.cumprod(axis=1)
self.assertTrue(np.array_equal(res1[0], expected1))
self.assertTrue(np.array_equal(res2[0], expected2))
self.assertTrue(np.allclose(res1[0], expected1))
self.assertTrue(np.allclose(res2[0], expected2))

def testNanCumReduction(self):
raw = np.random.randint(5, size=(8, 8, 8))
Expand All @@ -370,8 +376,8 @@ def testNanCumReduction(self):
res2 = self.executor.execute_tensor(nancumprod(arr, axis=1), concat=True)
expected1 = np.nancumsum(raw, axis=1)
expected2 = np.nancumprod(raw, axis=1)
self.assertTrue(np.array_equal(res1[0], expected1))
self.assertTrue(np.array_equal(res2[0], expected2))
np.testing.assert_array_equal(res1[0], expected1)
np.testing.assert_array_equal(res2[0], expected2)

raw = sps.random(8, 8, density=.1, format='lil')
raw[:2, 2:4] = np.nan
Expand All @@ -382,8 +388,8 @@ def testNanCumReduction(self):
res2 = self.executor.execute_tensor(nancumprod(arr, axis=1), concat=True)[0]
expected1 = np.nancumsum(raw.A, axis=1)
expected2 = np.nancumprod(raw.A, axis=1)
np.testing.assert_equal(res1, expected1)
np.testing.assert_equal(res2, expected2)
self.assertTrue(np.allclose(res1, expected1))
self.assertTrue(np.allclose(res2, expected2))

def testOutReductionExecution(self):
raw = np.random.randint(5, size=(8, 8, 8))
Expand All @@ -395,7 +401,7 @@ def testOutReductionExecution(self):
res = self.executor.execute_tensor(arr2, concat=True)[0]
expected = raw.sum(axis=1)

self.assertTrue(np.array_equal(res, expected))
np.testing.assert_array_equal(res, expected)

def testOutCumReductionExecution(self):
raw = np.random.randint(5, size=(8, 8, 8))
Expand All @@ -406,7 +412,7 @@ def testOutCumReductionExecution(self):
res = self.executor.execute_tensor(arr, concat=True)[0]
expected = raw.cumsum(axis=0)

self.assertTrue(np.array_equal(res, expected))
np.testing.assert_array_equal(res, expected)

def testCountNonzeroExecution(self):
raw = [[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]]
Expand Down

0 comments on commit ffe306b

Please sign in to comment.