Beispiel #1
0
def test_searchsorted():
    raw = np.sort(np.random.randint(100, size=(16,)))
    arr = tensor(raw, chunk_size=3).cumsum()

    t1 = searchsorted(arr, 10)

    assert t1.shape == ()
    assert t1.flags['C_CONTIGUOUS'] == np.searchsorted(raw.cumsum(), 10).flags['C_CONTIGUOUS']
    assert t1.flags['F_CONTIGUOUS'] == np.searchsorted(raw.cumsum(), 10).flags['F_CONTIGUOUS']

    t1 = tile(t1)

    assert t1.nsplits == ()
    assert len(t1.chunks) == 1
    assert t1.chunks[0].op.stage == OperandStage.reduce

    with pytest.raises(ValueError):
        searchsorted(np.random.randint(10, size=(14, 14)), 1)

    with pytest.raises(ValueError):
        searchsorted(arr, 10, side='both')

    with pytest.raises(ValueError):
        searchsorted(arr.tosparse(), 10)

    raw2 = np.asfortranarray(np.sort(np.random.randint(100, size=(16,))))
    arr = tensor(raw2, chunk_size=3)
    to_search = np.asfortranarray([[1, 2], [3, 4]])

    t1 = searchsorted(arr, to_search)
    expected = np.searchsorted(raw2, to_search)

    assert t1.shape == to_search.shape
    assert t1.flags['C_CONTIGUOUS'] == expected.flags['C_CONTIGUOUS']
    assert t1.flags['F_CONTIGUOUS'] == expected.flags['F_CONTIGUOUS']
Beispiel #2
0
    def testSearchsorted(self):
        raw = np.sort(np.random.randint(100, size=(16, )))
        arr = tensor(raw, chunk_size=3).cumsum()

        t1 = searchsorted(arr, 10)

        self.assertEqual(t1.shape, ())
        self.assertEqual(
            t1.flags['C_CONTIGUOUS'],
            np.searchsorted(raw.cumsum(), 10).flags['C_CONTIGUOUS'])
        self.assertEqual(
            t1.flags['F_CONTIGUOUS'],
            np.searchsorted(raw.cumsum(), 10).flags['F_CONTIGUOUS'])

        t1 = t1.tiles()

        self.assertEqual(t1.nsplits, ())
        self.assertEqual(len(t1.chunks), 1)
        self.assertEqual(t1.chunks[0].op.stage, OperandStage.reduce)

        with self.assertRaises(ValueError):
            searchsorted(np.random.randint(10, size=(14, 14)), 1)

        with self.assertRaises(ValueError):
            searchsorted(arr, 10, side='both')

        with self.assertRaises(ValueError):
            searchsorted(arr.tosparse(), 10)

        raw2 = np.asfortranarray(np.sort(np.random.randint(100, size=(16, ))))
        arr = tensor(raw2, chunk_size=3)
        to_search = np.asfortranarray([[1, 2], [3, 4]])

        t1 = searchsorted(arr, to_search)
        expected = np.searchsorted(raw2, to_search)

        self.assertEqual(t1.shape, to_search.shape)
        self.assertEqual(t1.flags['C_CONTIGUOUS'],
                         expected.flags['C_CONTIGUOUS'])
        self.assertEqual(t1.flags['F_CONTIGUOUS'],
                         expected.flags['F_CONTIGUOUS'])
Beispiel #3
0
    def testSearchsortedExecution(self):
        raw = np.sort(np.random.randint(100, size=(16, )))

        # test different chunk_size, 3 will have combine, 6 will skip combine
        for chunk_size in (3, 6):
            arr = tensor(raw, chunk_size=chunk_size)

            # test scalar, with value in the middle
            t1 = searchsorted(arr, 20)

            res = self.executor.execute_tensor(t1, concat=True)[0]
            expected = np.searchsorted(raw, 20)
            np.testing.assert_array_equal(res, expected)

            # test scalar, with value larger than 100
            t2 = searchsorted(arr, 200)

            res = self.executor.execute_tensor(t2, concat=True)[0]
            expected = np.searchsorted(raw, 200)
            np.testing.assert_array_equal(res, expected)

            # test scalar, side left, with value exact in the middle of the array
            t3 = searchsorted(arr, raw[10], side='left')

            res = self.executor.execute_tensor(t3, concat=True)[0]
            expected = np.searchsorted(raw, raw[10], side='left')
            np.testing.assert_array_equal(res, expected)

            # test scalar, side right, with value exact in the middle of the array
            t4 = searchsorted(arr, raw[10], side='right')

            res = self.executor.execute_tensor(t4, concat=True)[0]
            expected = np.searchsorted(raw, raw[10], side='right')
            np.testing.assert_array_equal(res, expected)

            # test scalar, side left, with value exact in the end of the array
            t5 = searchsorted(arr, raw[15], side='left')

            res = self.executor.execute_tensor(t5, concat=True)[0]
            expected = np.searchsorted(raw, raw[15], side='left')
            np.testing.assert_array_equal(res, expected)

            # test scalar, side right, with value exact in the end of the array
            t6 = searchsorted(arr, raw[15], side='right')

            res = self.executor.execute_tensor(t6, concat=True)[0]
            expected = np.searchsorted(raw, raw[15], side='right')
            np.testing.assert_array_equal(res, expected)

            # test scalar, side left, with value exact in the start of the array
            t7 = searchsorted(arr, raw[0], side='left')

            res = self.executor.execute_tensor(t7, concat=True)[0]
            expected = np.searchsorted(raw, raw[0], side='left')
            np.testing.assert_array_equal(res, expected)

            # test scalar, side right, with value exact in the start of the array
            t8 = searchsorted(arr, raw[0], side='right')

            res = self.executor.execute_tensor(t8, concat=True)[0]
            expected = np.searchsorted(raw, raw[0], side='right')
            np.testing.assert_array_equal(res, expected)

            raw2 = np.random.randint(100, size=(3, 4))

            # test tensor, side left
            t9 = searchsorted(arr, tensor(raw2, chunk_size=2), side='left')

            res = self.executor.execute_tensor(t9, concat=True)[0]
            expected = np.searchsorted(raw, raw2, side='left')
            np.testing.assert_array_equal(res, expected)

            # test tensor, side right
            t10 = searchsorted(arr, tensor(raw2, chunk_size=2), side='right')

            res = self.executor.execute_tensor(t10, concat=True)[0]
            expected = np.searchsorted(raw, raw2, side='right')
            np.testing.assert_array_equal(res, expected)

        # test one chunk
        arr = tensor(raw, chunk_size=16)

        # test scalar, tensor to search has 1 chunk
        t11 = searchsorted(arr, 20)
        res = self.executor.execute_tensor(t11, concat=True)[0]
        expected = np.searchsorted(raw, 20)
        np.testing.assert_array_equal(res, expected)

        # test tensor with 1 chunk, tensor to search has 1 chunk
        t12 = searchsorted(arr, tensor(raw2, chunk_size=4))

        res = self.executor.execute_tensor(t12, concat=True)[0]
        expected = np.searchsorted(raw, raw2)
        np.testing.assert_array_equal(res, expected)

        # test tensor with more than 1 chunk, tensor to search has 1 chunk
        t13 = searchsorted(arr, tensor(raw2, chunk_size=2))

        res = self.executor.execute_tensor(t13, concat=True)[0]
        expected = np.searchsorted(raw, raw2)
        np.testing.assert_array_equal(res, expected)

        # test sorter
        raw3 = np.random.randint(100, size=(16, ))
        arr = tensor(raw3, chunk_size=3)
        order = np.argsort(raw3)
        order_arr = tensor(order, chunk_size=4)

        t14 = searchsorted(arr, 20, sorter=order_arr)

        res = self.executor.execute_tensor(t14, concat=True)[0]
        expected = np.searchsorted(raw3, 20, sorter=order)
        np.testing.assert_array_equal(res, expected)