<commit_before>import unittest

import numpy as np
import numpy.testing as nptesting

from rebin import rebin


class TestRebin(unittest.TestCase):
    def setUp(self):
        np.random.seed(20160711)

    def test_rebin(self):
        shape = (24, 18)
        a = 2*np.random.rand(24, 18)-1
        bin_shape = (4, 3)
        actual = rebin(a, bin_shape)
        n0, n1 = (i//j for i, j in zip(shape, bin_shape))
        bin0, bin1 = bin_shape
        expected = np.zeros((n0, n1), dtype=np.float64)
        for i0 in range(n0):
            for i1 in range(n1):
                for j0 in range(bin0):
                    for j1 in range(bin1):
                        expected[i0, i1] += a[bin0*i0+j0, bin1*i1+j1]
        nptesting.assert_array_equal(expected, actual)


if __name__ == '__main__':
    unittest.main()<commit_msg>Basic tests with 2D arrays.<commit_after>import unittest

import numpy as np

from numpy.testing import assert_array_almost_equal_nulp
from numpy.testing import assert_array_equal

from rebin import rebin


def my_rebin(a, bins, func=np.mean):
    new_shape = tuple(n // b for n, b in zip(a.shape, bins))
    bin0, bin1 = bins

    def compute_cell(i0, i1):
        return func(a[bin0*i0:bin0*(i0+1), bin1*i1:bin1*(i1+1)])

    out = np.empty(new_shape, dtype=a.dtype)
    it = np.nditer(out, flags=['multi_index'], op_flags=['readwrite'])
    while not it.finished:
        it.value[...] = compute_cell(*it.multi_index)
        it.iternext()
    return out


class TestRebin(unittest.TestCase):
    def setUp(self):
        np.random.seed(20160711)

    def test_rebin1(self):
        a = np.linspace(1, 24, num=24,
                        dtype=np.float64).reshape(4, 6)
        actual = rebin(a, (2, 3))
        expected = np.array([[5., 8.],
                             [17., 20.]])
        assert_array_equal(expected, actual)

    def test_rebin2(self):
        a = np.linspace(1, 24, num=24,
                        dtype=np.float64).reshape(4, 6)
        actual = rebin(a, 2)
        expected = np.array([[4.5, 6.5, 8.5],
                             [16.5, 18.5, 20.5]])
        assert_array_equal(expected, actual)

    def test_invalid_bins(self):
        a = np.array([[1., 2., 3.],
                      [4., 5., 6.]])
        with self.assertRaises(ValueError):
            rebin(a, bins=(1, 2, 3))

    def test_random(self):
        shape = (24, 18)
        bins = (4, 3)
        a = 2*np.random.rand(*shape)-1
        actual = rebin(a, bins, np.sum)
        expected = my_rebin(a, bins, np.sum)
        assert_array_almost_equal_nulp(expected, actual, nulp=3)


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.13586751975912684, 'fraction_numerical': 0.04855099736544975, 'mean_word_length': 2.134433962264151, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4408175', 'n_tokens_mistral': 1027, 'n_tokens_neox': 947, 'n_words': 217}