Exemplo n.º 1
0
    def testQuantile(self):
        raw = np.random.rand(100)
        q = np.random.rand(10)

        for dtype in [np.float32, np.int64, np.complex128]:
            raw2 = raw.astype(dtype)
            a = tensor(raw2, chunk_size=100)

            b = quantile(a, q, overwrite_input=True)
            self.assertEqual(b.shape, (10, ))
            self.assertEqual(b.dtype, np.quantile(raw2, q).dtype)

            b = b.tiles()
            self.assertEqual(len(b.chunks), 1)

        raw = np.random.rand(20, 10)
        q = np.random.rand(10)

        for dtype in [np.float32, np.int64, np.complex128]:
            for axis in (None, 0, 1, [0, 1]):
                for interpolation in INTERPOLATION_TYPES:
                    for keepdims in [True, False]:
                        raw2 = raw.astype(dtype)
                        a = tensor(raw2, chunk_size=(4, 3))

                        b = quantile(a,
                                     q,
                                     axis=axis,
                                     interpolation=interpolation,
                                     keepdims=keepdims)
                        expected = np.quantile(raw2,
                                               q,
                                               axis=axis,
                                               interpolation=interpolation,
                                               keepdims=keepdims)
                        self.assertEqual(b.shape, expected.shape)
                        self.assertEqual(b.dtype, expected.dtype)

                        b = b.tiles()
                        self.assertEqual(b.shape, expected.shape)
                        self.assertEqual(b.dtype, expected.dtype)

        a = tensor(raw, chunk_size=10)
        b = quantile(a, q)

        b = b.tiles()
        self.assertEqual(b.shape, (10, ))

        b = quantile(a, 0.3)
        self.assertEqual(b.ndim, 0)

        raw2 = np.random.rand(3, 4, 5)
        a2 = tensor(raw2, chunk_size=3)
        b2 = quantile(a2, q, axis=(0, 2))
        expected = np.quantile(raw2, q, axis=(0, 2))
        self.assertEqual(b2.shape, expected.shape)

        b2 = b2.tiles()
        self.assertEqual(b2.shape, expected.shape)

        # q has to be 1-d
        with self.assertRaises(ValueError):
            quantile(a, q.reshape(5, 2))

        # wrong out type
        with self.assertRaises(TypeError):
            quantile(a, q, out=2)

        # wrong q
        with self.assertRaises(ValueError):
            q2 = q.copy()
            q2[0] = 1.1
            quantile(a, q2)

        # wrong q, with size < 10
        with self.assertRaises(ValueError):
            q2 = np.random.rand(5)
            q2[0] = 1.1
            quantile(a, q2)

        # wrong interpolation
        with self.assertRaises(ValueError):
            quantile(a, q, interpolation='unknown')
Exemplo n.º 2
0
def test_quantile_execution(setup):
    # test 1 chunk, 1-d
    raw = np.random.rand(20)
    a = tensor(raw, chunk_size=20)

    raw2 = raw.copy()
    raw2[np.random.RandomState(0).randint(raw.size, size=3)] = np.nan
    a2 = tensor(raw2, chunk_size=20)

    for q in [
            np.random.RandomState(0).rand(),
            np.random.RandomState(0).rand(5)
    ]:
        for interpolation in INTERPOLATION_TYPES:
            for keepdims in [True, False]:
                r = quantile(a,
                             q,
                             interpolation=interpolation,
                             keepdims=keepdims)

                result = r.execute().fetch()
                expected = np.quantile(raw,
                                       q,
                                       interpolation=interpolation,
                                       keepdims=keepdims)

                np.testing.assert_array_equal(result, expected)

                r2 = quantile(a2,
                              q,
                              interpolation=interpolation,
                              keepdims=keepdims)

                result = r2.execute().fetch()
                expected = np.quantile(raw2,
                                       q,
                                       interpolation=interpolation,
                                       keepdims=keepdims)

                np.testing.assert_array_equal(result, expected)

    # test 1 chunk, 2-d
    raw = np.random.rand(20, 10)
    a = tensor(raw, chunk_size=20)

    raw2 = raw.copy()
    raw2.flat[np.random.RandomState(0).randint(raw.size, size=3)] = np.nan
    a2 = tensor(raw2, chunk_size=20)

    for q in [
            np.random.RandomState(0).rand(),
            np.random.RandomState(0).rand(5)
    ]:
        for interpolation in INTERPOLATION_TYPES:
            for keepdims in [True, False]:
                for axis in [None, 0, 1]:
                    r = quantile(a,
                                 q,
                                 axis=axis,
                                 interpolation=interpolation,
                                 keepdims=keepdims)

                    result = r.execute().fetch()
                    expected = np.quantile(raw,
                                           q,
                                           axis=axis,
                                           interpolation=interpolation,
                                           keepdims=keepdims)

                    np.testing.assert_array_equal(result, expected)

                    r2 = quantile(a2,
                                  q,
                                  axis=axis,
                                  interpolation=interpolation,
                                  keepdims=keepdims)

                    result = r2.execute().fetch()
                    expected = np.quantile(raw2,
                                           q,
                                           axis=axis,
                                           interpolation=interpolation,
                                           keepdims=keepdims)

                    np.testing.assert_array_equal(result, expected)

    # test multi chunks, 1-d
    raw = np.random.rand(20)
    a = tensor(raw, chunk_size=6)

    raw2 = raw.copy()
    raw2[np.random.RandomState(0).randint(raw.size, size=3)] = np.nan
    a2 = tensor(raw2, chunk_size=20)

    for q in [
            np.random.RandomState(0).rand(),
            np.random.RandomState(0).rand(5)
    ]:
        for interpolation in INTERPOLATION_TYPES:
            for keepdims in [True, False]:
                r = quantile(a,
                             q,
                             interpolation=interpolation,
                             keepdims=keepdims)

                result = r.execute().fetch()
                expected = np.quantile(raw,
                                       q,
                                       interpolation=interpolation,
                                       keepdims=keepdims)

                np.testing.assert_almost_equal(result, expected)

                r2 = quantile(a2,
                              q,
                              interpolation=interpolation,
                              keepdims=keepdims)

                result = r2.execute().fetch()
                expected = np.quantile(raw2,
                                       q,
                                       interpolation=interpolation,
                                       keepdims=keepdims)

                np.testing.assert_almost_equal(result, expected)

    # test multi chunk, 2-d
    raw = np.random.rand(20, 10)
    a = tensor(raw, chunk_size=(12, 6))

    raw2 = raw.copy()
    raw2.flat[np.random.RandomState(0).randint(raw.size, size=3)] = np.nan
    a2 = tensor(raw2, chunk_size=(12, 6))

    for q in [
            np.random.RandomState(0).rand(),
            np.random.RandomState(0).rand(5)
    ]:
        for interpolation in INTERPOLATION_TYPES:
            for keepdims in [True, False]:
                for axis in [None, 0, 1]:
                    r = quantile(a,
                                 q,
                                 axis=axis,
                                 interpolation=interpolation,
                                 keepdims=keepdims)

                    result = r.execute().fetch()
                    expected = np.quantile(raw,
                                           q,
                                           axis=axis,
                                           interpolation=interpolation,
                                           keepdims=keepdims)

                    np.testing.assert_almost_equal(result, expected)

                    r2 = quantile(a2,
                                  q,
                                  axis=axis,
                                  interpolation=interpolation,
                                  keepdims=keepdims)

                    result = r2.execute().fetch()
                    expected = np.quantile(raw2,
                                           q,
                                           axis=axis,
                                           interpolation=interpolation,
                                           keepdims=keepdims)

                    np.testing.assert_almost_equal(result, expected)

    # test out, 1 chunk
    raw = np.random.rand(20)
    q = np.random.rand(11)
    a = tensor(raw, chunk_size=20)
    out = empty((5, 11))
    quantile(a, q, out=out)

    result = out.execute().fetch()
    expected = np.quantile(raw, q, out=np.empty((5, 11)))
    np.testing.assert_array_equal(result, expected)

    # test out, multi chunks
    raw = np.random.rand(20)
    q = np.random.rand(11)
    a = tensor(raw, chunk_size=6)
    out = empty((5, 11))
    quantile(a, q, out=out)

    result = out.execute().fetch()
    expected = np.quantile(raw, q, out=np.empty((5, 11)))
    np.testing.assert_almost_equal(result, expected)

    # test q which is a tensor
    q_raw = np.random.RandomState(0).rand(5)
    q = tensor(q_raw, chunk_size=6)

    r = quantile(a, q, axis=None)

    result = r.execute().fetch()
    expected = np.quantile(raw, q_raw, axis=None)

    np.testing.assert_almost_equal(result, expected)

    with pytest.raises(ValueError):
        q[0] = 1.1
        r = quantile(a, q, axis=None)
        _ = r.execute()
Exemplo n.º 3
0
    def testQuantileExecution(self):
        # test 1 chunk, 1-d
        raw = np.random.rand(20)
        a = tensor(raw, chunk_size=20)

        raw2 = raw.copy()
        raw2[np.random.RandomState(0).randint(raw.size, size=3)] = np.nan
        a2 = tensor(raw2, chunk_size=20)

        for q in [
                np.random.RandomState(0).rand(),
                np.random.RandomState(0).rand(5)
        ]:
            for interpolation in INTERPOLATION_TYPES:
                for keepdims in [True, False]:
                    r = quantile(a,
                                 q,
                                 interpolation=interpolation,
                                 keepdims=keepdims)

                    result = self.executor.execute_tensor(r, concat=True)[0]
                    expected = np.quantile(raw,
                                           q,
                                           interpolation=interpolation,
                                           keepdims=keepdims)

                    np.testing.assert_array_equal(result, expected)

                    r2 = quantile(a2,
                                  q,
                                  interpolation=interpolation,
                                  keepdims=keepdims)

                    result = self.executor.execute_tensor(r2, concat=True)[0]
                    expected = np.quantile(raw2,
                                           q,
                                           interpolation=interpolation,
                                           keepdims=keepdims)

                    np.testing.assert_array_equal(result, expected)

        # test 1 chunk, 2-d
        raw = np.random.rand(20, 10)
        a = tensor(raw, chunk_size=20)

        raw2 = raw.copy()
        raw2.flat[np.random.RandomState(0).randint(raw.size, size=3)] = np.nan
        a2 = tensor(raw2, chunk_size=20)

        for q in [
                np.random.RandomState(0).rand(),
                np.random.RandomState(0).rand(5)
        ]:
            for interpolation in INTERPOLATION_TYPES:
                for keepdims in [True, False]:
                    for axis in [None, 0, 1]:
                        r = quantile(a,
                                     q,
                                     axis=axis,
                                     interpolation=interpolation,
                                     keepdims=keepdims)

                        result = self.executor.execute_tensor(r,
                                                              concat=True)[0]
                        expected = np.quantile(raw,
                                               q,
                                               axis=axis,
                                               interpolation=interpolation,
                                               keepdims=keepdims)

                        np.testing.assert_array_equal(result, expected)

                        r2 = quantile(a2,
                                      q,
                                      axis=axis,
                                      interpolation=interpolation,
                                      keepdims=keepdims)

                        result = self.executor.execute_tensor(r2,
                                                              concat=True)[0]
                        expected = np.quantile(raw2,
                                               q,
                                               axis=axis,
                                               interpolation=interpolation,
                                               keepdims=keepdims)

                        np.testing.assert_array_equal(result, expected)

        # test multi chunks, 1-d
        raw = np.random.rand(20)
        a = tensor(raw, chunk_size=3)

        raw2 = raw.copy()
        raw2[np.random.RandomState(0).randint(raw.size, size=3)] = np.nan
        a2 = tensor(raw2, chunk_size=20)

        for q in [
                np.random.RandomState(0).rand(),
                np.random.RandomState(0).rand(5)
        ]:
            for interpolation in INTERPOLATION_TYPES:
                for keepdims in [True, False]:
                    r = quantile(a,
                                 q,
                                 interpolation=interpolation,
                                 keepdims=keepdims)

                    result = self.executor.execute_tensor(r, concat=True)[0]
                    expected = np.quantile(raw,
                                           q,
                                           interpolation=interpolation,
                                           keepdims=keepdims)

                    np.testing.assert_almost_equal(result, expected)

                    r2 = quantile(a2,
                                  q,
                                  interpolation=interpolation,
                                  keepdims=keepdims)

                    result = self.executor.execute_tensor(r2, concat=True)[0]
                    expected = np.quantile(raw2,
                                           q,
                                           interpolation=interpolation,
                                           keepdims=keepdims)

                    np.testing.assert_almost_equal(result, expected)

        # test multi chunk, 2-d
        raw = np.random.rand(20, 10)
        a = tensor(raw, chunk_size=(12, 6))

        raw2 = raw.copy()
        raw2.flat[np.random.RandomState(0).randint(raw.size, size=3)] = np.nan
        a2 = tensor(raw2, chunk_size=(12, 6))

        for q in [
                np.random.RandomState(0).rand(),
                np.random.RandomState(0).rand(5)
        ]:
            for interpolation in INTERPOLATION_TYPES:
                for keepdims in [True, False]:
                    for axis in [None, 0, 1]:
                        r = quantile(a,
                                     q,
                                     axis=axis,
                                     interpolation=interpolation,
                                     keepdims=keepdims)

                        result = self.executor.execute_tensor(r,
                                                              concat=True)[0]
                        expected = np.quantile(raw,
                                               q,
                                               axis=axis,
                                               interpolation=interpolation,
                                               keepdims=keepdims)

                        np.testing.assert_almost_equal(result, expected)

                        r2 = quantile(a2,
                                      q,
                                      axis=axis,
                                      interpolation=interpolation,
                                      keepdims=keepdims)

                        result = self.executor.execute_tensor(r2,
                                                              concat=True)[0]
                        expected = np.quantile(raw2,
                                               q,
                                               axis=axis,
                                               interpolation=interpolation,
                                               keepdims=keepdims)

                        np.testing.assert_almost_equal(result, expected)

        # test out, 1 chunk
        raw = np.random.rand(20)
        q = np.random.rand(11)
        a = tensor(raw, chunk_size=20)
        out = empty((5, 11))
        quantile(a, q, out=out)

        result = self.executor.execute_tensor(out, concat=True)[0]
        expected = np.quantile(raw, q, out=np.empty((5, 11)))
        np.testing.assert_array_equal(result, expected)

        # test out, multi chunks
        raw = np.random.rand(20)
        q = np.random.rand(11)
        a = tensor(raw, chunk_size=3)
        out = empty((5, 11))
        quantile(a, q, out=out)

        result = self.executor.execute_tensor(out, concat=True)[0]
        expected = np.quantile(raw, q, out=np.empty((5, 11)))
        np.testing.assert_almost_equal(result, expected)

        # test q which is a tensor
        q_raw = np.random.RandomState(0).rand(5)
        q = tensor(q_raw, chunk_size=3)

        ctx, executor = self._create_test_context(self.executor)
        with ctx:
            r = quantile(a, q, axis=None)

            result = executor.execute_tensors([r])[0]
            expected = np.quantile(raw, q_raw, axis=None)

            np.testing.assert_almost_equal(result, expected)

            with self.assertRaises(ValueError):
                q[0] = 1.1
                r = quantile(a, q, axis=None)
                _ = executor.execute_tensors(r)[0]
Exemplo n.º 4
0
def test_quantile():
    raw = np.random.rand(100)
    q = np.random.rand(10)

    for dtype in [np.float32, np.int64, np.complex128]:
        raw2 = raw.astype(dtype)
        a = tensor(raw2, chunk_size=100)

        b = quantile(a, q, overwrite_input=True)
        assert b.shape == (10,)
        assert b.dtype == np.quantile(raw2, q).dtype

        b = tile(b)
        assert len(b.chunks) == 1

    raw = np.random.rand(20, 10)
    q = np.random.rand(10)

    for dtype in [np.float32, np.int64, np.complex128]:
        for axis in (None, 0, 1, [0, 1]):
            for interpolation in INTERPOLATION_TYPES:
                for keepdims in [True, False]:
                    raw2 = raw.astype(dtype)
                    a = tensor(raw2, chunk_size=(8, 6))

                    b = quantile(a, q, axis=axis,
                                 interpolation=interpolation, keepdims=keepdims)
                    expected = np.quantile(raw2, q, axis=axis,
                                           interpolation=interpolation,
                                           keepdims=keepdims)
                    assert b.shape == expected.shape
                    assert b.dtype == expected.dtype

    a = tensor(raw, chunk_size=10)
    b = quantile(a, q)

    b = tile(b)
    assert b.shape == (10,)

    b = quantile(a, 0.3)
    assert b.ndim == 0

    raw2 = np.random.rand(3, 4, 5)
    a2 = tensor(raw2, chunk_size=3)
    b2 = quantile(a2, q, axis=(0, 2))
    expected = np.quantile(raw2, q, axis=(0, 2))
    assert b2.shape == expected.shape

    b2 = tile(b2)
    assert b2.shape == expected.shape

    # q has to be 1-d
    with pytest.raises(ValueError):
        quantile(a, q.reshape(5, 2))

    # wrong out type
    with pytest.raises(TypeError):
        quantile(a, q, out=2)

    # wrong q
    with pytest.raises(ValueError):
        q2 = q.copy()
        q2[0] = 1.1
        quantile(a, q2)

    # wrong q, with size < 10
    with pytest.raises(ValueError):
        q2 = np.random.rand(5)
        q2[0] = 1.1
        quantile(a, q2)

    # wrong interpolation
    with pytest.raises(ValueError):
        quantile(a, q, interpolation='unknown')