Python scipy.sparse.random() Examples

The following are 30 code examples of scipy.sparse.random(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.sparse , or try the search function .
Example #1
Source File: testing.py    From celer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def build_dataset(n_samples=50, n_features=200, n_targets=1, sparse_X=False):
    """Build samples and observation for linear regression problem."""
    random_state = np.random.RandomState(0)
    if n_targets > 1:
        w = random_state.randn(n_features, n_targets)
    else:
        w = random_state.randn(n_features)

    if sparse_X:
        X = sparse.random(n_samples, n_features, density=0.5, format='csc',
                          random_state=random_state)

    else:
        X = np.asfortranarray(random_state.randn(n_samples, n_features))

    y = X.dot(w)
    return X, y 
Example #2
Source File: test_indexing_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testTakeExecution(self):
        data = np.random.rand(10, 20, 30)
        t = tensor(data, chunk_size=10)

        a = t.take([4, 1, 2, 6, 200])

        res = self.executor.execute_tensor(a, concat=True)[0]
        expected = np.take(data, [4, 1, 2, 6, 200])
        np.testing.assert_array_equal(res, expected)

        a = take(t, [5, 19, 2, 13], axis=1)

        res = self.executor.execute_tensor(a, concat=True)[0]
        expected = np.take(data, [5, 19, 2, 13], axis=1)
        np.testing.assert_array_equal(res, expected)

        with self.assertRaises(ValueError):
            take(t, [1, 3, 4], out=tensor(np.random.rand(4)))

        out = tensor([1, 2, 3, 4])
        a = take(t, [4, 19, 2, 8], out=out)

        res = self.executor.execute_tensor(out, concat=True)[0]
        expected = np.take(data, [4, 19, 2, 8])
        np.testing.assert_array_equal(res, expected) 
Example #3
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testShape(self):
        raw = np.random.RandomState(0).rand(4, 3)
        x = mt.tensor(raw, chunk_size=2)

        s = shape(x)

        ctx, executor = self._create_test_context(self.executor)
        with ctx:
            result = executor.execute_tensors(s)
            self.assertSequenceEqual(result, (4, 3))

            s = shape(x[x > .5])

            result = executor.execute_tensors(s)
            expected = np.shape(raw[raw > .5])
            self.assertSequenceEqual(result, expected)

            s = shape(0)

            result = executor.execute_tensors(s)
            expected = np.shape(0)
            self.assertSequenceEqual(result, expected) 
Example #4
Source File: test_arithmetic_execution.py    From mars with Apache License 2.0 6 votes vote down vote up
def testAroundExecution(self):
        data = np.random.randn(10, 20)
        x = tensor(data, chunk_size=3)

        t = x.round(2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.around(data, decimals=2)

        np.testing.assert_allclose(res, expected)

        data = sps.random(10, 20, density=.2)
        x = tensor(data, chunk_size=3)

        t = x.round(2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.around(data.toarray(), decimals=2)

        np.testing.assert_allclose(res.toarray(), expected) 
Example #5
Source File: test_merge_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testHStackExecution(self):
        a_data = np.random.rand(10)
        b_data = np.random.rand(20)

        a = tensor(a_data, chunk_size=4)
        b = tensor(b_data, chunk_size=4)

        c = hstack([a, b])
        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.hstack([a_data, b_data])
        self.assertTrue(np.array_equal(res, expected))

        a_data = np.random.rand(10, 20)
        b_data = np.random.rand(10, 5)

        a = tensor(a_data, chunk_size=3)
        b = tensor(b_data, chunk_size=4)

        c = hstack([a, b])
        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.hstack([a_data, b_data])
        self.assertTrue(np.array_equal(res, expected)) 
Example #6
Source File: test_arithmetic_execution.py    From mars with Apache License 2.0 6 votes vote down vote up
def testCosOrderExecution(self):
        data = np.asfortranarray(np.random.rand(3, 5))
        x = tensor(data, chunk_size=2)

        t = cos(x)

        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, np.cos(data))
        self.assertFalse(res.flags['C_CONTIGUOUS'])
        self.assertTrue(res.flags['F_CONTIGUOUS'])

        t2 = cos(x, order='C')

        res2 = self.executor.execute_tensor(t2, concat=True)[0]
        np.testing.assert_allclose(res2, np.cos(data, order='C'))
        self.assertTrue(res2.flags['C_CONTIGUOUS'])
        self.assertFalse(res2.flags['F_CONTIGUOUS']) 
Example #7
Source File: test_merge_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testVStackExecution(self):
        a_data = np.random.rand(10)
        b_data = np.random.rand(10)

        a = tensor(a_data, chunk_size=4)
        b = tensor(b_data, chunk_size=4)

        c = vstack([a, b])
        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.vstack([a_data, b_data])
        self.assertTrue(np.array_equal(res, expected))

        a_data = np.random.rand(10, 20)
        b_data = np.random.rand(5, 20)

        a = tensor(a_data, chunk_size=3)
        b = tensor(b_data, chunk_size=4)

        c = vstack([a, b])
        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.vstack([a_data, b_data])
        self.assertTrue(np.array_equal(res, expected)) 
Example #8
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testArgsort(self):
        # only 1 chunk when axis = -1
        raw = np.random.rand(100, 10)
        x = tensor(raw, chunk_size=10)

        xa = argsort(x)

        r = self.executor.execute_tensor(xa, concat=True)[0]
        np.testing.assert_array_equal(np.sort(raw), np.take_along_axis(raw, r, axis=-1))

        x = tensor(raw, chunk_size=(22, 4))

        xa = argsort(x)

        r = self.executor.execute_tensor(xa, concat=True)[0]
        np.testing.assert_array_equal(np.sort(raw), np.take_along_axis(raw, r, axis=-1))

        raw = np.random.rand(100)

        x = tensor(raw, chunk_size=23)

        xa = argsort(x, axis=0)

        r = self.executor.execute_tensor(xa, concat=True)[0]
        np.testing.assert_array_equal(np.sort(raw, axis=0), raw[r]) 
Example #9
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testSortIndicesExecution(self):
        # only 1 chunk when axis = -1
        raw = np.random.rand(100, 10)
        x = tensor(raw, chunk_size=10)

        r = sort(x, return_index=True)

        sr, si = self.executor.execute_tensors(r)
        np.testing.assert_array_equal(sr, np.take_along_axis(raw, si, axis=-1))

        x = tensor(raw, chunk_size=(22, 4))

        r = sort(x, return_index=True)

        sr, si = self.executor.execute_tensors(r)
        np.testing.assert_array_equal(sr, np.take_along_axis(raw, si, axis=-1))

        raw = np.random.rand(100)

        x = tensor(raw, chunk_size=23)

        r = sort(x, axis=0, return_index=True)

        sr, si = self.executor.execute_tensors(r)
        np.testing.assert_array_equal(sr, raw[si]) 
Example #10
Source File: test_linalg_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testSolveSymPos(self):
        import scipy.linalg
        np.random.seed(1)

        data = np.random.randint(1, 10, (20, 20))
        data_l = np.tril(data)
        data1 = data_l.dot(data_l.T)
        data2 = np.random.randint(1, 10, (20, ))

        A = tensor(data1, chunk_size=5)
        b = tensor(data2, chunk_size=5)

        x = solve(A, b, sym_pos=True)

        res = self.executor.execute_tensor(x, concat=True)[0]
        np.testing.assert_allclose(res, scipy.linalg.solve(data1, data2))
        res = self.executor.execute_tensor(A.dot(x), concat=True)[0]
        np.testing.assert_allclose(res, data2) 
Example #11
Source File: test_merge_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testDStackExecution(self):
        a_data = np.random.rand(10)
        b_data = np.random.rand(10)

        a = tensor(a_data, chunk_size=4)
        b = tensor(b_data, chunk_size=4)

        c = dstack([a, b])
        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.dstack([a_data, b_data])
        self.assertTrue(np.array_equal(res, expected))

        a_data = np.random.rand(10, 20)
        b_data = np.random.rand(10, 20)

        a = tensor(a_data, chunk_size=3)
        b = tensor(b_data, chunk_size=4)

        c = dstack([a, b])
        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.dstack([a_data, b_data])
        self.assertTrue(np.array_equal(res, expected)) 
Example #12
Source File: test_merge_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testColumnStackExecution(self):
        a_data = np.array((1, 2, 3))
        b_data = np.array((2, 3, 4))
        a = tensor(a_data, chunk_size=1)
        b = tensor(b_data, chunk_size=2)

        c = column_stack((a, b))
        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.column_stack((a_data, b_data))
        np.testing.assert_equal(res, expected)

        a_data = np.random.rand(4, 2, 3)
        b_data = np.random.rand(4, 2, 3)
        a = tensor(a_data, chunk_size=1)
        b = tensor(b_data, chunk_size=2)

        c = column_stack((a, b))
        res = self.executor.execute_tensor(c, concat=True)[0]
        expected = np.column_stack((a_data, b_data))
        np.testing.assert_equal(res, expected) 
Example #13
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testArgwhereExecution(self):
        x = arange(6, chunk_size=2).reshape(2, 3)
        t = argwhere(x > 1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.argwhere(np.arange(6).reshape(2, 3) > 1)

        np.testing.assert_array_equal(res, expected)

        data = np.asfortranarray(np.random.rand(10, 20))
        x = tensor(data, chunk_size=10)

        t = argwhere(x > 0.5)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.argwhere(data > 0.5)

        np.testing.assert_array_equal(res, expected)
        self.assertTrue(res.flags['F_CONTIGUOUS'])
        self.assertFalse(res.flags['C_CONTIGUOUS']) 
Example #14
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testWhereExecution(self):
        raw_cond = np.random.randint(0, 2, size=(4, 4), dtype='?')
        raw_x = np.random.rand(4, 1)
        raw_y = np.random.rand(4, 4)

        cond, x, y = tensor(raw_cond, chunk_size=2), tensor(raw_x, chunk_size=2), tensor(raw_y, chunk_size=2)

        arr = where(cond, x, y)
        res = self.executor.execute_tensor(arr, concat=True)
        self.assertTrue(np.array_equal(res[0], np.where(raw_cond, raw_x, raw_y)))

        raw_cond = sps.csr_matrix(np.random.randint(0, 2, size=(4, 4), dtype='?'))
        raw_x = sps.random(4, 1, density=.1)
        raw_y = sps.random(4, 4, density=.1)

        cond, x, y = tensor(raw_cond, chunk_size=2), tensor(raw_x, chunk_size=2), tensor(raw_y, chunk_size=2)

        arr = where(cond, x, y)
        res = self.executor.execute_tensor(arr, concat=True)[0]
        self.assertTrue(np.array_equal(res.toarray(),
                                       np.where(raw_cond.toarray(), raw_x.toarray(), raw_y.toarray()))) 
Example #15
Source File: test_reduction_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testNanCumReduction(self):
        raw = np.random.randint(5, size=(8, 8, 8))
        raw[:2, 2:4, 4:6] = np.nan

        arr = tensor(raw, chunk_size=3)

        res1 = self.executor.execute_tensor(nancumsum(arr, axis=1), concat=True)
        res2 = self.executor.execute_tensor(nancumprod(arr, axis=1), concat=True)
        expected1 = np.nancumsum(raw, axis=1)
        expected2 = np.nancumprod(raw, axis=1)
        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

        arr = tensor(raw, chunk_size=3)

        res1 = self.executor.execute_tensor(nancumsum(arr, axis=1), concat=True)[0]
        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)
        self.assertTrue(np.allclose(res1, expected1))
        self.assertTrue(np.allclose(res2, expected2)) 
Example #16
Source File: test_reduction_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testAllAnyExecution(self):
        raw1 = np.zeros((10, 15))
        raw2 = np.ones((10, 15))
        raw3 = np.array([[True, False, True, False], [True, True, True, True],
                         [False, False, False, False], [False, True, False, True]])

        arr1 = tensor(raw1, chunk_size=3)
        arr2 = tensor(raw2, chunk_size=3)
        arr3 = tensor(raw3, chunk_size=4)

        self.assertFalse(self.executor.execute_tensor(arr1.all())[0])
        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()))
        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

        arr = tensor(raw, chunk_size=3)

        self.assertEqual(raw.A.all(), self.executor.execute_tensor(arr.all())[0])
        self.assertEqual(raw.A.any(), self.executor.execute_tensor(arr.any())[0]) 
Example #17
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testAstypeExecution(self):
        raw = np.random.random((10, 5))
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8')

        res = self.executor.execute_tensor(arr2, concat=True)
        np.testing.assert_array_equal(res[0], raw.astype('i8'))

        raw = sps.random(10, 5, density=.2)
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8')

        res = self.executor.execute_tensor(arr2, concat=True)
        self.assertTrue(np.array_equal(res[0].toarray(), raw.astype('i8').toarray()))

        raw = np.asfortranarray(np.random.random((10, 5)))
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8', order='C')

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        np.testing.assert_array_equal(res, raw.astype('i8'))
        self.assertTrue(res.flags['C_CONTIGUOUS'])
        self.assertFalse(res.flags['F_CONTIGUOUS']) 
Example #18
Source File: test_base_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testCopytoExecution(self):
        a = ones((2, 3), chunk_size=1)
        b = tensor([3, -1, 3], chunk_size=2)

        copyto(a, b, where=b > 1)

        res = self.executor.execute_tensor(a, concat=True)[0]
        expected = np.array([[3, 1, 3], [3, 1, 3]])

        np.testing.assert_equal(res, expected)

        a = ones((2, 3), chunk_size=1)
        b = tensor(np.asfortranarray(np.random.rand(2, 3)), chunk_size=2)

        copyto(b, a)

        res = self.executor.execute_tensor(b, concat=True)[0]
        expected = np.asfortranarray(np.ones((2, 3)))

        np.testing.assert_array_equal(res, expected)
        self.assertTrue(res.flags['F_CONTIGUOUS'])
        self.assertFalse(res.flags['C_CONTIGUOUS']) 
Example #19
Source File: test_euclidean_distances.py    From mars with Apache License 2.0 6 votes vote down vote up
def testEuclideanDistancesOp(self):
        x = mt.random.rand(10, 3)
        xx = mt.random.rand(1, 10)
        y = mt.random.rand(11, 3)

        d = euclidean_distances(x, X_norm_squared=xx)
        self.assertEqual(d.op.x_norm_squared.key, check_array(xx).T.key)

        d = euclidean_distances(x, y, X_norm_squared=mt.random.rand(10, 1, dtype=mt.float32),
                                Y_norm_squared=mt.random.rand(1, 11, dtype=mt.float32))
        self.assertIsNone(d.op.x_norm_squared)
        self.assertIsNone(d.op.y_norm_squared)

        # XX shape incompatible
        with self.assertRaises(ValueError):
            euclidean_distances(x, X_norm_squared=mt.random.rand(10))

        # XX shape incompatible
        with self.assertRaises(ValueError):
            euclidean_distances(x, X_norm_squared=mt.random.rand(11, 1))

        # YY shape incompatible
        with self.assertRaises(ValueError):
            euclidean_distances(x, y, Y_norm_squared=mt.random.rand(10)) 
Example #20
Source File: test_statistics_execute.py    From mars with Apache License 2.0 6 votes vote down vote up
def testPercentileExecution(self):
        raw = np.random.rand(20, 10)
        q = np.random.RandomState(0).randint(100, size=11)
        a = tensor(raw, chunk_size=7)
        r = percentile(a, q)

        result = self.executor.execute_tensor(r, concat=True)[0]
        expected = np.percentile(raw, q)
        np.testing.assert_array_equal(result, expected)

        mq = tensor(q)

        ctx, executor = self._create_test_context(self.executor)
        with ctx:
            r = percentile(a, mq)
            result = executor.execute_tensors([r])[0]

            np.testing.assert_array_equal(result, expected) 
Example #21
Source File: test_nearest_neighbors.py    From mars with Apache License 2.0 6 votes vote down vote up
def testGPUFaissNearestNeighborsExecution(self):
        rs = np.random.RandomState(0)

        raw_X = rs.rand(10, 5)
        raw_Y = rs.rand(8, 5)

        # test faiss execution
        X = mt.tensor(raw_X, chunk_size=7).to_gpu()
        Y = mt.tensor(raw_Y, chunk_size=8).to_gpu()

        nn = NearestNeighbors(n_neighbors=3, algorithm='faiss', metric='l2')
        nn.fit(X)

        ret = nn.kneighbors(Y)

        snn = SkNearestNeighbors(n_neighbors=3, algorithm='auto', metric='l2')
        snn.fit(raw_X)
        expected = snn.kneighbors(raw_Y)

        result = [r.fetch() for r in ret]
        np.testing.assert_almost_equal(result[0].get(), expected[0], decimal=6)
        np.testing.assert_almost_equal(result[1].get(), expected[1]) 
Example #22
Source File: test_0201_sparse_matmul.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_0201_sparse_matmul(self):
    """ The testbed for checking different ways of invoking matrix-matrix multiplications """

    return
    
    for n in [50, 100, 200, 400, 800, 1600, 3200]:
      print()
      for dens in [0.001, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128, 0.256]:
      
        asp = sprs.random(n, n, format='csr', density=dens)
        bsp = sprs.random(n, n, format='csr', density=dens)

        t1 = timer()
        cmat1 = np.dot(asp, bsp)
        t2 = timer(); ts =t2-t1; #print('runtime sparse ', ts)

    
        adn = asp.toarray()
        bdn = bsp.toarray()
        t1 = t2
        cmat2 = np.dot(adn, bdn)
        t2 = timer(); td =t2-t1; #print('runtime  dense ', td) 
        t1 = t2
    
        print('dens, ratio {:5d}, {:.6f} {:.6f} {:.6f} {:.6f}'.format(n, dens, td, ts, td/ts)) 
Example #23
Source File: test_reduction_execute.py    From mars with Apache License 2.0 5 votes vote down vote up
def testCumReduction(self):
        raw = np.random.randint(5, size=(8, 8, 8))

        arr = tensor(raw, chunk_size=3)

        res1 = self.executor.execute_tensor(arr.cumsum(axis=1), concat=True)
        res2 = self.executor.execute_tensor(arr.cumprod(axis=1), concat=True)
        expected1 = raw.cumsum(axis=1)
        expected2 = raw.cumprod(axis=1)
        np.testing.assert_array_equal(res1[0], expected1)
        np.testing.assert_array_equal(res2[0], expected2)

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

        arr = tensor(raw, chunk_size=3)

        res1 = self.executor.execute_tensor(arr.cumsum(axis=1), concat=True)
        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.allclose(res1[0], expected1))
        self.assertTrue(np.allclose(res2[0], expected2))

        # test order
        raw = np.asfortranarray(np.random.rand(10, 20, 30))
        arr = tensor(raw, chunk_size=13)
        arr2 = arr.cumsum(axis=-1)

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        expected = raw.cumsum(axis=-1)
        np.testing.assert_allclose(res, expected)
        self.assertEqual(res.flags['C_CONTIGUOUS'], expected.flags['C_CONTIGUOUS'])
        self.assertEqual(res.flags['F_CONTIGUOUS'], expected.flags['F_CONTIGUOUS']) 
Example #24
Source File: test_manhattan_distances.py    From mars with Apache License 2.0 5 votes vote down vote up
def testManhattanDistances(self):
        x = mt.random.randint(10, size=(10, 3), density=0.4)
        y = mt.random.randint(10, size=(11, 3), density=0.5)

        with self.assertRaises(TypeError):
            manhattan_distances(x, y, sum_over_features=False)

        x = x.todense()
        y = y.todense()

        d = manhattan_distances(x, y, sum_over_features=True)
        self.assertEqual(d.shape, (10, 11))
        d = manhattan_distances(x, y, sum_over_features=False)
        self.assertEqual(d.shape, (110, 3)) 
Example #25
Source File: test_serialize.py    From mars with Apache License 2.0 5 votes vote down vote up
def testArrowSerialize(self):
        array = np.random.rand(1000, 100)
        assert_array_equal(array, dataserializer.deserialize(dataserializer.serialize(array).to_buffer()))

        if sps:
            mat = sparse.SparseMatrix(sps.random(100, 100, 0.1, format='csr'))
            des_mat = dataserializer.deserialize(dataserializer.serialize(mat).to_buffer())
            self.assertTrue((mat.spmatrix != des_mat.spmatrix).nnz == 0)

            array = np.random.rand(1000, 100)
            mat = sparse.SparseMatrix(sps.random(100, 100, 0.1, format='csr'))
            tp = (array, mat)
            des_tp = dataserializer.deserialize(dataserializer.serialize(tp).to_buffer())
            assert_array_equal(tp[0], des_tp[0])
            self.assertTrue((tp[1].spmatrix != des_tp[1].spmatrix).nnz == 0) 
Example #26
Source File: test_arithmetic_execution.py    From mars with Apache License 2.0 5 votes vote down vote up
def testBaseOrderExecution(self):
        raw = np.asfortranarray(np.random.rand(5, 6))
        arr = tensor(raw, chunk_size=3)

        res = self.executor.execute_tensor(arr + 1, concat=True)[0]
        np.testing.assert_array_equal(res, raw + 1)
        self.assertFalse(res.flags['C_CONTIGUOUS'])
        self.assertTrue(res.flags['F_CONTIGUOUS'])

        res2 = self.executor.execute_tensor(add(arr, 1, order='C'), concat=True)[0]
        np.testing.assert_array_equal(res2, np.add(raw, 1, order='C'))
        self.assertTrue(res2.flags['C_CONTIGUOUS'])
        self.assertFalse(res2.flags['F_CONTIGUOUS']) 
Example #27
Source File: test_reduction_execute.py    From mars with Apache License 2.0 5 votes vote down vote up
def testArgReduction(self):
        raw = np.random.random((20, 20, 20))

        arr = tensor(raw, chunk_size=3)

        self.assertEqual(raw.argmax(),
                         self.executor.execute_tensor(arr.argmax())[0])
        self.assertEqual(raw.argmin(),
                         self.executor.execute_tensor(arr.argmin())[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_format = sps.random(20, 20, density=.1, format='lil')

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

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

        self.assertEqual(raw.argmax(),
                         self.executor.execute_tensor(arr.argmax())[0])
        self.assertEqual(raw.argmin(),
                         self.executor.execute_tensor(arr.argmin())[0])

        # test order
        raw = np.asfortranarray(np.random.rand(10, 20, 30))
        arr = tensor(raw, chunk_size=13)
        arr2 = arr.argmax(axis=-1)

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        expected = raw.argmax(axis=-1)
        np.testing.assert_allclose(res, expected)
        self.assertEqual(res.flags['C_CONTIGUOUS'], expected.flags['C_CONTIGUOUS'])
        self.assertEqual(res.flags['F_CONTIGUOUS'], expected.flags['F_CONTIGUOUS']) 
Example #28
Source File: test_reduction_execute.py    From mars with Apache License 2.0 5 votes vote down vote up
def testOutCumReductionExecution(self):
        raw = np.random.randint(5, size=(8, 8, 8))

        arr = tensor(raw, chunk_size=3)
        arr.cumsum(axis=0, out=arr)

        res = self.executor.execute_tensor(arr, concat=True)[0]
        expected = raw.cumsum(axis=0)

        np.testing.assert_array_equal(res, expected) 
Example #29
Source File: citation_graph.py    From dgl with Apache License 2.0 5 votes vote down vote up
def get_scipy_generator(args):
    n = args.syn_gnp_n
    p = (2 * np.log(n) / n) if args.syn_gnp_p == 0. else args.syn_gnp_p
    def _gen(seed):
        return ScipyGraph(sp.random(n, n, p, format='coo'))
    return _gen 
Example #30
Source File: test_reduction_execute.py    From mars with Apache License 2.0 5 votes vote down vote up
def testOutReductionExecution(self):
        raw = np.random.randint(5, size=(8, 8, 8))

        arr = tensor(raw, chunk_size=3)
        arr2 = ones((8, 8), dtype='i8', chunk_size=3)
        arr.sum(axis=1, out=arr2)

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        expected = raw.sum(axis=1)

        np.testing.assert_array_equal(res, expected)