def testHistogramExecution(self):
        rs = np.random.RandomState(0)

        raw = rs.randint(10, size=(20,))
        a = tensor(raw, chunk_size=3)
        raw_weights = rs.random(20)
        weights = tensor(raw_weights, chunk_size=4)

        # range provided
        for range_ in [(0, 10), (3, 11), (3, 7)]:
            bin_edges = histogram(a, range=range_)[0]
            result = self.executor.execute_tensor(bin_edges)[0]
            expected = np.histogram(raw, range=range_)[0]
            np.testing.assert_array_equal(result, expected)

        for wt in (raw_weights, weights):
            for density in (True, False):
                bins = [1, 4, 6, 9]
                bin_edges = histogram(a, bins=bins, weights=wt, density=density)[0]
                result = self.executor.execute_tensor(bin_edges)[0]
                expected = np.histogram(
                    raw, bins=bins, weights=raw_weights, density=density)[0]
                np.testing.assert_almost_equal(result, expected)

        this = self

        class MockSession:
            def __init__(self):
                self.executor = this.executor

        ctx = LocalContext(MockSession())
        executor = ExecutorForTest('numpy', storage=ctx)
        with ctx:
            raw2 = rs.randint(10, size=(1,))
            b = tensor(raw2)
            raw3 = rs.randint(10, size=(0,))
            c = tensor(raw3)
            for t, r in [(a, raw), (b, raw2), (c, raw3), (sort(a), raw)]:
                for density in (True, False):
                    test_bins = [10, 'stone', 'auto', 'doane', 'fd',
                                 'rice', 'scott', 'sqrt', 'sturges']
                    for bins in test_bins:
                        hist = histogram(t, bins=bins, density=density)[0]

                        if r.size > 0:
                            with self.assertRaises(TilesError):
                                executor.execute_tensor(hist)

                        result = executor.execute_tensors([hist])[0]
                        expected = np.histogram(r, bins=bins, density=density)[0]
                        np.testing.assert_array_equal(result, expected)

                    test_bins = [[0, 4, 8], tensor([0, 4, 8], chunk_size=2)]
                    for bins in test_bins:
                        hist = histogram(t, bins=bins, density=density)[0]
                        result = executor.execute_tensors([hist])[0]
                        expected = np.histogram(r, bins=[0, 4, 8], density=density)[0]
                        np.testing.assert_array_equal(result, expected)
    def testHistogramExecution(self):
        rs = np.random.RandomState(0)

        raw = rs.randint(10, size=(20, ))
        a = tensor(raw, chunk_size=3)
        raw_weights = rs.random(20)
        weights = tensor(raw_weights, chunk_size=4)

        # range provided
        for range_ in [(0, 10), (3, 11), (3, 7)]:
            bin_edges = histogram(a, range=range_)[0]
            result = self.executor.execute_tensor(bin_edges)[0]
            expected = np.histogram(raw, range=range_)[0]
            np.testing.assert_array_equal(result, expected)

        for wt in (raw_weights, weights):
            for density in (True, False):
                bins = [1, 4, 6, 9]
                bin_edges = histogram(a,
                                      bins=bins,
                                      weights=wt,
                                      density=density)[0]
                result = self.executor.execute_tensor(bin_edges)[0]
                expected = np.histogram(raw,
                                        bins=bins,
                                        weights=raw_weights,
                                        density=density)[0]
                np.testing.assert_almost_equal(result, expected)

        ctx, executor = self._create_test_context(self.executor)
        with ctx:
            raw2 = rs.randint(10, size=(1, ))
            b = tensor(raw2)
            raw3 = rs.randint(10, size=(0, ))
            c = tensor(raw3)
            for t, r in [(a, raw), (b, raw2), (c, raw3), (sort(a), raw)]:
                for density in (True, False):
                    test_bins = [
                        10, 'stone', 'auto', 'doane', 'fd', 'rice', 'scott',
                        'sqrt', 'sturges'
                    ]
                    for bins in test_bins:
                        hist = histogram(t, bins=bins, density=density)[0]

                        if r.size > 0:
                            with self.assertRaises(TilesError):
                                executor.execute_tensor(hist)

                        result = executor.execute_tensors([hist])[0]
                        expected = np.histogram(r, bins=bins,
                                                density=density)[0]
                        np.testing.assert_array_equal(result, expected)

                    test_bins = [[0, 4, 8], tensor([0, 4, 8], chunk_size=2)]
                    for bins in test_bins:
                        hist = histogram(t, bins=bins, density=density)[0]
                        result = executor.execute_tensors([hist])[0]
                        expected = np.histogram(r,
                                                bins=[0, 4, 8],
                                                density=density)[0]
                        np.testing.assert_array_equal(result, expected)

            # test unknown shape
            raw4 = rs.rand(10)
            d = tensor(raw4, chunk_size=3)
            d = d[d < 0.9]
            hist = histogram(d)
            result = executor.execute_tensors(hist)[0]
            expected = np.histogram(raw4[raw4 < 0.9])[0]
            np.testing.assert_array_equal(result, expected)

            raw5 = np.arange(3, 10)
            e = arange(10, chunk_size=3)
            e = e[e >= 3]
            hist = histogram(e)
            result = executor.execute_tensors(hist)[0]
            expected = np.histogram(raw5)[0]
            np.testing.assert_array_equal(result, expected)
def test_histogram_execution(setup):
    rs = np.random.RandomState(0)

    raw = rs.randint(10, size=(20, ))
    a = tensor(raw, chunk_size=6)
    raw_weights = rs.random(20)
    weights = tensor(raw_weights, chunk_size=8)

    # range provided
    for range_ in [(0, 10), (3, 11), (3, 7)]:
        bin_edges = histogram(a, range=range_)[0]
        result = bin_edges.execute().fetch()
        expected = np.histogram(raw, range=range_)[0]
        np.testing.assert_array_equal(result, expected)

    for wt in (raw_weights, weights):
        for density in (True, False):
            bins = [1, 4, 6, 9]
            bin_edges = histogram(a, bins=bins, weights=wt, density=density)[0]
            result = bin_edges.execute().fetch()
            expected = np.histogram(raw,
                                    bins=bins,
                                    weights=raw_weights,
                                    density=density)[0]
            np.testing.assert_almost_equal(result, expected)

    raw2 = rs.randint(10, size=(1, ))
    b = tensor(raw2)
    raw3 = rs.randint(10, size=(0, ))
    c = tensor(raw3)
    for t, r in [(a, raw), (b, raw2), (c, raw3), (sort(a), raw)]:
        for density in (True, False):
            test_bins = [
                10, 'stone', 'auto', 'doane', 'fd', 'rice', 'scott', 'sqrt',
                'sturges'
            ]
            for bins in test_bins:
                hist = histogram(t, bins=bins, density=density)[0]
                result = hist.execute().fetch()
                expected = np.histogram(r, bins=bins, density=density)[0]
                np.testing.assert_array_equal(result, expected)

            test_bins = [[0, 4, 8], tensor([0, 4, 8], chunk_size=2)]
            for bins in test_bins:
                hist = histogram(t, bins=bins, density=density)[0]
                result = hist.execute().fetch()
                expected = np.histogram(r, bins=[0, 4, 8], density=density)[0]
                np.testing.assert_array_equal(result, expected)

        # test unknown shape
        raw4 = rs.rand(10)
        d = tensor(raw4, chunk_size=6)
        d = d[d < 0.9]
        hist = histogram(d)
        result = hist.execute().fetch()[0]
        expected = np.histogram(raw4[raw4 < 0.9])[0]
        np.testing.assert_array_equal(result, expected)

        raw5 = np.arange(3, 10)
        e = arange(10, chunk_size=6)
        e = e[e >= 3]
        hist = histogram(e)
        result = hist.execute().fetch()[0]
        expected = np.histogram(raw5)[0]
        np.testing.assert_array_equal(result, expected)