koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 1 | import numpy |
| 2 | from wally.statistic import rebin_histogram |
| 3 | |
| 4 | |
| 5 | def array_eq(x: numpy.array, y: numpy.array, max_diff: float = 1E-3) -> bool: |
| 6 | return numpy.abs(x - y).max() <= max_diff |
| 7 | |
| 8 | |
| 9 | def test_rebin_histo(): |
| 10 | curr_histo = numpy.empty((100,), dtype=int) |
| 11 | curr_histo[:] = 1 |
| 12 | edges = numpy.arange(100) |
| 13 | new_histo, new_edges = rebin_histogram(curr_histo, edges, 10) |
| 14 | |
| 15 | assert new_edges.shape == (10,) |
| 16 | assert new_histo.shape == (10,) |
| 17 | assert new_edges.dtype.name.startswith('float') |
| 18 | assert new_histo.dtype.name.startswith('int') |
| 19 | |
| 20 | assert array_eq(new_edges, numpy.arange(10) * 9.9) |
| 21 | assert new_histo.sum() == curr_histo.sum() |
| 22 | assert list(new_histo) == [10] * 10 |
| 23 | |
| 24 | new_histo, new_edges = rebin_histogram(curr_histo, edges, 3, |
| 25 | left_tail_idx=20, |
| 26 | right_tail_idx=50) |
| 27 | |
| 28 | assert new_edges.shape == (3,) |
| 29 | assert new_histo.shape == (3,) |
| 30 | assert array_eq(new_edges, numpy.array([20, 30, 40])) |
| 31 | assert new_histo.sum() == curr_histo.sum() |
| 32 | assert list(new_histo) == [30, 10, 60] |