示例#1
0
    def testFrexpExecution(self):
        data1 = np.random.random((5, 9, 4))

        arr1 = tensor(data1.copy(), chunk_size=3)

        o1, o2 = frexp(arr1)
        o = o1 + o2

        res = self.executor.execute_tensor(o, concat=True)[0]
        expected = sum(np.frexp(data1))
        self.assertTrue(np.allclose(res, expected))

        arr1 = tensor(data1.copy(), chunk_size=3)
        o1 = zeros(data1.shape, chunk_size=3)
        o2 = zeros(data1.shape, dtype='i8', chunk_size=3)
        frexp(arr1, o1, o2)
        o = o1 + o2

        res = self.executor.execute_tensor(o, concat=True)[0]
        expected = sum(np.frexp(data1))
        self.assertTrue(np.allclose(res, expected))

        data1 = sps.random(5, 9, density=.1)

        arr1 = tensor(data1.copy(), chunk_size=3)

        o1, o2 = frexp(arr1)
        o = o1 + o2

        res = self.executor.execute_tensor(o, concat=True)[0]
        expected = sum(np.frexp(data1.toarray()))
        np.testing.assert_equal(res.toarray(), expected)
示例#2
0
def test_frexp_execution(setup):
    data1 = np.random.RandomState(0).random((5, 9, 4))

    arr1 = tensor(data1.copy(), chunk_size=3)

    o1, o2 = frexp(arr1)
    o = o1 + o2

    res = o.execute().fetch()
    expected = sum(np.frexp(data1))
    np.testing.assert_array_almost_equal(res, expected)

    arr1 = tensor(data1.copy(), chunk_size=3)
    o1 = zeros(data1.shape, chunk_size=3)
    o2 = zeros(data1.shape, dtype='i8', chunk_size=3)
    frexp(arr1, o1, o2)
    o = o1 + o2

    res = o.execute().fetch()
    expected = sum(np.frexp(data1))
    np.testing.assert_array_almost_equal(res, expected, decimal=3)

    data1 = sps.random(5, 9, density=.1)

    arr1 = tensor(data1.copy(), chunk_size=3)

    o1, o2 = frexp(arr1)
    o = o1 + o2

    res = o.execute().fetch()
    expected = sum(np.frexp(data1.toarray()))
    np.testing.assert_equal(res.toarray(), expected)
示例#3
0
    def testZerosExecution(self):
        t = zeros((20, 30), dtype='i8', chunk_size=5)

        res = self.executor.execute_tensor(t, concat=True)
        self.assertTrue(np.array_equal(res[0], np.zeros((20, 30), dtype='i8')))
        self.assertEqual(res[0].dtype, np.int64)

        t2 = zeros_like(t)
        res = self.executor.execute_tensor(t2, concat=True)
        self.assertTrue(np.array_equal(res[0], np.zeros((20, 30), dtype='i8')))
        self.assertEqual(res[0].dtype, np.int64)

        t = zeros((20, 30), dtype='i4', chunk_size=5, sparse=True)
        res = self.executor.execute_tensor(t, concat=True)

        self.assertEqual(res[0].nnz, 0)
示例#4
0
def test_modf_execution(setup):
    data1 = np.random.random((5, 9))

    arr1 = tensor(data1.copy(), chunk_size=3)

    o1, o2 = modf(arr1)
    o = o1 + o2

    res = o.execute().fetch()
    expected = sum(np.modf(data1))
    np.testing.assert_array_almost_equal(res, expected)

    o1, o2 = modf([0, 3.5])
    o = o1 + o2

    res = o.execute().fetch()
    expected = sum(np.modf([0, 3.5]))
    np.testing.assert_array_almost_equal(res, expected)

    arr1 = tensor(data1.copy(), chunk_size=3)
    o1 = zeros(data1.shape, chunk_size=3)
    o2 = zeros(data1.shape, chunk_size=3)
    modf(arr1, o1, o2)
    o = o1 + o2

    res = o.execute().fetch()
    expected = sum(np.modf(data1))
    np.testing.assert_array_almost_equal(res, expected)

    data1 = sps.random(5, 9, density=.1)

    arr1 = tensor(data1.copy(), chunk_size=3)

    o1, o2 = modf(arr1)
    o = o1 + o2

    res = o.execute().fetch()
    expected = sum(np.modf(data1.toarray()))
    np.testing.assert_equal(res.toarray(), expected)
示例#5
0
    def testZerosExecution(self):
        t = zeros((20, 30), dtype='i8', chunk_size=5)

        res = self.executor.execute_tensor(t, concat=True)
        np.testing.assert_array_equal(res[0], np.zeros((20, 30), dtype='i8'))
        self.assertEqual(res[0].dtype, np.int64)

        t2 = zeros_like(t)
        res = self.executor.execute_tensor(t2, concat=True)
        np.testing.assert_array_equal(res[0], np.zeros((20, 30), dtype='i8'))
        self.assertEqual(res[0].dtype, np.int64)

        t = zeros((20, 30), dtype='i4', chunk_size=5, sparse=True)
        res = self.executor.execute_tensor(t, concat=True)

        self.assertEqual(res[0].nnz, 0)

        t = zeros((20, 30), dtype='i8', chunk_size=6, order='F')
        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.zeros((20, 30), dtype='i8', order='F')
        self.assertEqual(res.flags['C_CONTIGUOUS'], expected.flags['C_CONTIGUOUS'])
        self.assertEqual(res.flags['F_CONTIGUOUS'], expected.flags['F_CONTIGUOUS'])
示例#6
0
def test_zeros_execution(setup):
    t = zeros((20, 30), dtype='i8', chunk_size=10)

    res = t.execute().fetch()
    np.testing.assert_array_equal(res, np.zeros((20, 30), dtype='i8'))
    assert res[0].dtype == np.int64

    t2 = zeros_like(t)
    res = t2.execute().fetch()
    np.testing.assert_array_equal(res, np.zeros((20, 30), dtype='i8'))
    assert res[0].dtype == np.int64

    t = zeros((20, 30), dtype='i4', chunk_size=5, sparse=True)
    res = t.execute().fetch()

    assert res[0].nnz == 0

    t = zeros((20, 30), dtype='i8', chunk_size=6, order='F')
    res = t.execute().fetch()
    expected = np.zeros((20, 30), dtype='i8', order='F')
    assert res.flags['C_CONTIGUOUS'] == expected.flags['C_CONTIGUOUS']
    assert res.flags['F_CONTIGUOUS'] == expected.flags['F_CONTIGUOUS']
示例#7
0
    def testMoveaxisExecution(self):
        x = zeros((3, 4, 5), chunk_size=2)

        t = moveaxis(x, 0, -1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertEqual(res.shape, (4, 5, 3))

        t = moveaxis(x, -1, 0)

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertEqual(res.shape, (5, 3, 4))

        t = moveaxis(x, [0, 1], [-1, -2])

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertEqual(res.shape, (5, 4, 3))

        t = moveaxis(x, [0, 1, 2], [-1, -2, -3])

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertEqual(res.shape, (5, 4, 3))
示例#8
0
    def testSetItemExecution(self):
        rs = np.random.RandomState(0)

        raw = data = rs.randint(0, 10, size=(11, 8, 12, 13))
        arr = tensor(raw.copy(), chunk_size=3)
        raw = raw.copy()

        idx = slice(2, 9, 2), slice(3, 7), slice(-1, -9, -2), 2
        arr[idx] = 20
        res = self.executor.execute_tensor(arr, concat=True)[0]

        raw[idx] = 20
        np.testing.assert_array_equal(res, raw)
        self.assertEqual(res.flags['C_CONTIGUOUS'], raw.flags['C_CONTIGUOUS'])
        self.assertEqual(res.flags['F_CONTIGUOUS'], raw.flags['F_CONTIGUOUS'])

        raw = data
        shape = raw[idx].shape

        arr2 = tensor(raw.copy(), chunk_size=3)
        raw = raw.copy()

        replace = rs.randint(10, 20, size=shape[:-1] + (1, )).astype('f4')
        arr2[idx] = tensor(replace, chunk_size=4)
        res = self.executor.execute_tensor(arr2, concat=True)[0]

        raw[idx] = replace
        np.testing.assert_array_equal(res, raw)

        raw = np.asfortranarray(np.random.randint(0, 10, size=(11, 8, 12, 13)))
        arr = tensor(raw.copy('A'), chunk_size=3)
        raw = raw.copy('A')

        idx = slice(2, 9, 2), slice(3, 7), slice(-1, -9, -2), 2
        arr[idx] = 20
        res = self.executor.execute_tensor(arr, concat=True)[0]

        raw[idx] = 20
        np.testing.assert_array_equal(res, raw)
        self.assertEqual(res.flags['C_CONTIGUOUS'], raw.flags['C_CONTIGUOUS'])
        self.assertEqual(res.flags['F_CONTIGUOUS'], raw.flags['F_CONTIGUOUS'])

        # test bool indexing set
        raw = data

        arr = tensor(raw.copy(), chunk_size=3)
        raw1 = rs.rand(11)
        arr[tensor(raw1, chunk_size=4) < 0.6, 2:7] = 3
        res = self.executor.execute_tileable(arr, concat=True)[0]

        raw[raw1 < 0.6, 2:7] = 3
        np.testing.assert_array_equal(res, raw)

        raw = np.random.randint(3, size=10).astype(np.int64)
        raw2 = np.arange(3)

        arr = zeros((10, 3))
        arr[tensor(raw) == 1, tensor(raw2) == 1] = 1
        res = self.executor.execute_tileable(arr, concat=True)[0]

        expected = np.zeros((10, 3))
        expected[raw == 1, raw2 == 1] = 1
        np.testing.assert_array_equal(res, expected)

        ctx, executor = self._create_test_context(self.executor)
        with ctx:
            raw = data

            arr = tensor(raw.copy(), chunk_size=3)
            raw1 = rs.rand(11)
            set_data = rs.rand((raw1 < 0.8).sum(), 8, 12, 13)
            arr[tensor(raw1, chunk_size=4) < 0.8] = tensor(set_data)

            res = self.executor.execute_tileables([arr])[0]

            raw[raw1 < 0.8] = set_data
            np.testing.assert_array_equal(res, raw)

        # test error
        with self.assertRaises(ValueError):
            t = tensor(raw, chunk_size=3)
            t[0, 0, 0, 0] = zeros(2, chunk_size=10)
            _ = self.executor.execute_tensor(t)
示例#9
0
def test_setitem_execution(setup):
    rs = np.random.RandomState(0)

    raw = data = rs.randint(0, 10, size=(11, 8, 12, 13))
    arr = tensor(raw.copy(), chunk_size=6)
    raw = raw.copy()

    idx = slice(2, 9, 2), slice(3, 7), slice(-1, -9, -2), 2
    arr[idx] = 20
    res = arr.execute().fetch()

    raw[idx] = 20
    np.testing.assert_array_equal(res, raw)
    assert res.flags['C_CONTIGUOUS'] == raw.flags['C_CONTIGUOUS']
    assert res.flags['F_CONTIGUOUS'] == raw.flags['F_CONTIGUOUS']

    raw = data
    shape = raw[idx].shape

    arr2 = tensor(raw.copy(), chunk_size=6)
    raw = raw.copy()

    replace = rs.randint(10, 20, size=shape[:-1] + (1,)).astype('f4')
    arr2[idx] = tensor(replace, chunk_size=7)
    res = arr2.execute().fetch()

    raw[idx] = replace
    np.testing.assert_array_equal(res, raw)

    raw = np.asfortranarray(np.random.randint(0, 10, size=(11, 8, 12, 13)))
    arr = tensor(raw.copy('A'), chunk_size=6)
    raw = raw.copy('A')

    idx = slice(2, 9, 2), slice(3, 7), slice(-1, -9, -2), 2
    arr[idx] = 20
    res = arr.execute().fetch()

    raw[idx] = 20
    np.testing.assert_array_equal(res, raw)
    assert res.flags['C_CONTIGUOUS'] == raw.flags['C_CONTIGUOUS']
    assert res.flags['F_CONTIGUOUS'] == raw.flags['F_CONTIGUOUS']

    # test bool indexing set
    raw = data

    arr = tensor(raw.copy(), chunk_size=6)
    raw1 = rs.rand(11)
    arr[tensor(raw1, chunk_size=4) < 0.6, 2: 7] = 3
    res = arr.execute().fetch()

    raw[raw1 < 0.6, 2: 7] = 3
    np.testing.assert_array_equal(res, raw)

    raw = np.random.randint(3, size=10).astype(np.int64)
    raw2 = np.arange(3)

    arr = zeros((10, 3))
    arr[tensor(raw) == 1, tensor(raw2) == 1] = 1
    res = arr.execute().fetch()

    expected = np.zeros((10, 3))
    expected[raw == 1, raw2 == 1] = 1
    np.testing.assert_array_equal(res, expected)

    raw = data

    arr = tensor(raw.copy(), chunk_size=6)
    raw1 = rs.rand(11)
    set_data = rs.rand((raw1 < 0.8).sum(), 8, 12, 13)
    arr[tensor(raw1, chunk_size=4) < 0.8] = tensor(set_data)

    res = arr.execute().fetch()

    raw[raw1 < 0.8] = set_data
    np.testing.assert_array_equal(res, raw)

    # test error
    with pytest.raises(ValueError):
        t = tensor(raw, chunk_size=3)
        t[0, 0, 0, 0] = zeros(2, chunk_size=10)
        t.execute()
示例#10
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)