Exemple #1
0
def test_block_execution(setup):
    # arrays is a tuple.
    with pytest.raises(TypeError):
        block((1, 2, 3))

    # List depths are mismatched.
    with pytest.raises(ValueError):
        block([[1, 2], [[3, 4]]])

    # List at arrays cannot be empty.
    with pytest.raises(ValueError):
        block([])

    # List at arrays[1] cannot be empty.
    with pytest.raises(ValueError):
        block([[1, 2], []])

    # Mismatched array shapes.
    with pytest.raises(ValueError):
        block([eye(512), eye(512), ones((511, 1))])

    # Test large block.
    block([eye(512), eye(512), ones((512, 1))])

    # Test block inputs a single array.
    c = block(array([1, 2, 3]))
    r = c.execute().fetch()
    np.testing.assert_array_equal(r, array([1, 2, 3]))

    a = eye(2) * 2
    b = eye(3) * 3
    c = block([[a, zeros((2, 3))], [ones((3, 2)), b]])
    r = c.execute().fetch()
    expected = array([[2., 0., 0., 0., 0.], [0., 2., 0., 0., 0.],
                      [1., 1., 3., 0., 0.], [1., 1., 0., 3., 0.],
                      [1., 1., 0., 0., 3.]])
    np.testing.assert_array_equal(r, expected)

    # eye with different chunk sizes
    a = eye(5, chunk_size=2) * 2
    b = eye(4, chunk_size=3) * 3
    c = block([[a, zeros((5, 4), chunk_size=4)],
               [ones((4, 5), chunk_size=5), b]])
    r = c.execute().fetch()
    expected = array([[2., 0., 0., 0., 0., 0., 0., 0., 0.],
                      [0., 2., 0., 0., 0., 0., 0., 0., 0.],
                      [0., 0., 2., 0., 0., 0., 0., 0., 0.],
                      [0., 0., 0., 2., 0., 0., 0., 0., 0.],
                      [0., 0., 0., 0., 2., 0., 0., 0., 0.],
                      [1., 1., 1., 1., 1., 3., 0., 0., 0.],
                      [1., 1., 1., 1., 1., 0., 3., 0., 0.],
                      [1., 1., 1., 1., 1., 0., 0., 3., 0.],
                      [1., 1., 1., 1., 1., 0., 0., 0., 3.]])
    np.testing.assert_array_equal(r, expected)

    # hstack([1, 2, 3])
    c = block([1, 2, 3])
    r = c.execute().fetch()
    expected = array([1, 2, 3])
    np.testing.assert_array_equal(r, expected)

    # hstack([a, b, 10])
    a = array([1, 2, 3])
    b = array([2, 3, 4])
    c = block([a, b, 10])
    r = c.execute().fetch()
    expected = array([1, 2, 3, 2, 3, 4, 10])
    np.testing.assert_array_equal(r, expected)

    # hstack([a, b, 10]) with different chunk sizes
    a = array([1, 2, 3, 4, 5, 6, 7], chunk_size=3)
    b = array([2, 3, 4, 5], chunk_size=4)
    c = block([a, b, 10])
    r = c.execute().fetch()
    expected = array([1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 10])
    np.testing.assert_array_equal(r, expected)

    # hstack([A, B])
    A = ones((2, 2), int)
    B = 2 * A
    c = block([A, B])
    r = c.execute().fetch()
    expected = array([[1, 1, 2, 2], [1, 1, 2, 2]])
    np.testing.assert_array_equal(r, expected)

    # vstack([a, b])
    a = array([1, 2, 3])
    b = array([2, 3, 4])
    c = block([[a], [b]])
    r = c.execute().fetch()
    expected = array([[1, 2, 3], [2, 3, 4]])
    np.testing.assert_array_equal(r, expected)

    # vstack([a, b]) with different chunk sizes
    a = array([1, 2, 3, 4, 5, 6, 7], chunk_size=5)
    b = array([2, 3, 4, 5, 6, 7, 8], chunk_size=6)
    c = block([[a], [b]])
    r = c.execute().fetch()
    expected = array([[1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8]])
    np.testing.assert_array_equal(r, expected)

    # vstack([A, B])
    A = ones((2, 2), int)
    B = 2 * A
    c = block([[A], [B]])
    r = c.execute().fetch()
    expected = array([[1, 1], [1, 1], [2, 2], [2, 2]])
    np.testing.assert_array_equal(r, expected)

    a = array(0)
    b = array([1])
    # atleast_1d(a)
    c = block([a])
    r = c.execute().fetch()
    expected = array([0])
    np.testing.assert_array_equal(r, expected)
    # atleast_1d(b)
    c = block([b])
    r = c.execute().fetch()
    expected = array([1])
    np.testing.assert_array_equal(r, expected)
    # atleast_2d(a)
    c = block([[a]])
    r = c.execute().fetch()
    expected = array([[0]])
    np.testing.assert_array_equal(r, expected)
    # atleast_2d(b)
    c = block([[b]])
    r = c.execute().fetch()
    expected = array([[1]])
    np.testing.assert_array_equal(r, expected)
    def testEyeExecution(self):
        t = eye(5, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5)
        np.testing.assert_equal(res, expected)

        t = eye(5, k=1, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, k=1)
        np.testing.assert_equal(res, expected)

        t = eye(5, k=2, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, k=2)
        np.testing.assert_equal(res, expected)

        t = eye(5, k=-1, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, k=-1)
        np.testing.assert_equal(res, expected)

        t = eye(5, k=-3, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, k=-3)
        np.testing.assert_equal(res, expected)

        t = eye(5, M=3, k=1, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, M=3, k=1)
        np.testing.assert_equal(res, expected)

        t = eye(5, M=3, k=-3, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, M=3, k=-3)
        np.testing.assert_equal(res, expected)

        t = eye(5, M=7, k=1, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, M=7, k=1)
        np.testing.assert_equal(res, expected)

        t = eye(5, M=8, k=-3, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, M=8, k=-3)
        np.testing.assert_equal(res, expected)

        t = eye(2, dtype=int)

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertEqual(res.dtype, np.int_)

        # test sparse
        t = eye(5, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, k=1, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, k=1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, k=2, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, k=2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, k=-1, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, k=-1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, k=-3, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, k=-3)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, M=3, k=1, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, M=3, k=1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, M=3, k=-3, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, M=3, k=-3)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, M=7, k=1, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, M=7, k=1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, M=8, k=-3, sparse=True, chunk_size=2)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.eye(5, M=8, k=-3)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        t = eye(5, M=9, k=-3, chunk_size=2, order='F')

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(res.flags['C_CONTIGUOUS'])
        self.assertFalse(res.flags['F_CONTIGUOUS'])
def test_eye_execution(setup):
    t = eye(5, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5)
    np.testing.assert_equal(res, expected)

    t = eye(5, k=1, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, k=1)
    np.testing.assert_equal(res, expected)

    t = eye(5, k=2, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, k=2)
    np.testing.assert_equal(res, expected)

    t = eye(5, k=-1, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, k=-1)
    np.testing.assert_equal(res, expected)

    t = eye(5, k=-3, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, k=-3)
    np.testing.assert_equal(res, expected)

    t = eye(5, M=3, k=1, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, M=3, k=1)
    np.testing.assert_equal(res, expected)

    t = eye(5, M=3, k=-3, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, M=3, k=-3)
    np.testing.assert_equal(res, expected)

    t = eye(5, M=7, k=1, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, M=7, k=1)
    np.testing.assert_equal(res, expected)

    t = eye(5, M=8, k=-3, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, M=8, k=-3)
    np.testing.assert_equal(res, expected)

    t = eye(2, dtype=int)

    res = t.execute().fetch()
    assert res.dtype == np.int_

    # test sparse
    t = eye(5, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, k=1, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, k=1)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, k=2, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, k=2)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, k=-1, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, k=-1)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, k=-3, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, k=-3)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, M=3, k=1, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, M=3, k=1)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, M=3, k=-3, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, M=3, k=-3)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, M=7, k=1, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, M=7, k=1)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, M=8, k=-3, sparse=True, chunk_size=2)

    res = t.execute().fetch()
    expected = np.eye(5, M=8, k=-3)
    assert isinstance(res, SparseNDArray)
    np.testing.assert_equal(res.toarray(), expected)

    t = eye(5, M=9, k=-3, chunk_size=2, order='F')

    res = t.execute().fetch()
    assert res.flags['C_CONTIGUOUS'] is True
    assert res.flags['F_CONTIGUOUS'] is False