|
| 1 | +import boost_histogram as bh |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from numpy.testing import assert_array_equal |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +def test_1D_set_bin(): |
| 10 | + |
| 11 | + h = bh.Histogram(bh.axis.Regular(10, 0, 1)) |
| 12 | + |
| 13 | + h[2] = 2 |
| 14 | + assert h[2] == 2.0 |
| 15 | + |
| 16 | + h[bh.underflow] = 3 |
| 17 | + assert h[bh.underflow] == 3.0 |
| 18 | + |
| 19 | + h[bh.overflow] = 4 |
| 20 | + assert h[bh.overflow] == 4.0 |
| 21 | + |
| 22 | + |
| 23 | +def test_2d_set_bin(): |
| 24 | + |
| 25 | + h = bh.Histogram(bh.axis.Regular(10, 0, 1), bh.axis.Regular(10, 0, 1)) |
| 26 | + |
| 27 | + h[2, 4] = 2 |
| 28 | + assert h[2, 4] == 2.0 |
| 29 | + |
| 30 | + h[bh.underflow, 5] = 3 |
| 31 | + assert h[bh.underflow, 5] == 3.0 |
| 32 | + |
| 33 | + h[bh.overflow, bh.overflow] = 4 |
| 34 | + assert h[bh.overflow, bh.overflow] == 4.0 |
| 35 | + |
| 36 | + |
| 37 | +def test_1d_set_array(): |
| 38 | + h = bh.Histogram(bh.axis.Regular(10, 0, 1)) |
| 39 | + |
| 40 | + h[...] = np.arange(10) |
| 41 | + assert_array_equal(h.view(), np.arange(10)) |
| 42 | + |
| 43 | + h[...] = np.arange(12) |
| 44 | + assert_array_equal(h.view(flow=True), np.arange(12)) |
| 45 | + |
| 46 | + with pytest.raises(ValueError): |
| 47 | + h[...] = np.arange(9) |
| 48 | + with pytest.raises(ValueError): |
| 49 | + h[...] = np.arange(11) |
| 50 | + with pytest.raises(ValueError): |
| 51 | + h[...] = np.arange(13) |
| 52 | + |
| 53 | + h[...] = 1 |
| 54 | + assert_array_equal(h.view(), np.ones(10)) |
| 55 | + |
| 56 | + |
| 57 | +def test_2d_set_array(): |
| 58 | + h = bh.Histogram(bh.axis.Regular(10, 0, 1), bh.axis.Regular(10, 0, 1)) |
| 59 | + |
| 60 | + h[...] = np.arange(10).reshape(-1, 1) |
| 61 | + assert_array_equal(h.view()[:, 2], np.arange(10)) |
| 62 | + |
| 63 | + h[...] = np.arange(12).reshape(-1, 1) |
| 64 | + assert_array_equal(h.view(flow=True)[:, 3], np.arange(12)) |
| 65 | + |
| 66 | + with pytest.raises(ValueError): |
| 67 | + h[...] = np.arange(9).reshape(-1, 1) |
| 68 | + with pytest.raises(ValueError): |
| 69 | + h[...] = np.arange(11).reshape(-1, 1) |
| 70 | + with pytest.raises(ValueError): |
| 71 | + h[...] = np.arange(13).reshape(-1, 1) |
| 72 | + |
| 73 | + h[...] = 1 |
| 74 | + assert_array_equal(h.view(), np.ones((10, 10))) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "storage, default", |
| 79 | + ( |
| 80 | + (bh.storage.Mean, bh.accumulators.Mean(1.0, 2.0, 3.0)), |
| 81 | + (bh.storage.WeightedMean, bh.accumulators.WeightedMean(1.0, 2.0, 3.0, 4.0)), |
| 82 | + (bh.storage.Weight, bh.accumulators.WeightedSum(1.0, 2)), |
| 83 | + ), |
| 84 | +) |
| 85 | +def test_set_special_dtype(storage, default): |
| 86 | + h = bh.Histogram( |
| 87 | + bh.axis.Regular(10, 0, 1), bh.axis.Regular(10, 0, 1), storage=storage() |
| 88 | + ) |
| 89 | + |
| 90 | + arr = np.full((10, 1), default) |
| 91 | + h[...] = arr |
| 92 | + assert_array_equal(h.view()[:, 1:2], arr) |
| 93 | + |
| 94 | + arr = np.full((12, 1), default) |
| 95 | + h[...] = arr |
| 96 | + assert_array_equal(h.view(flow=True)[:, 2:3], arr) |
| 97 | + |
| 98 | + arr = np.full((10, 10), default) |
| 99 | + h[...] = arr |
| 100 | + assert_array_equal(h.view(), arr) |
| 101 | + |
| 102 | + arr = np.full((10, 12), default) |
| 103 | + h[...] = arr |
| 104 | + assert_array_equal(h.view(flow=True)[1:11, :], arr) |
| 105 | + |
| 106 | + arr = np.full((12, 10), default) |
| 107 | + h[...] = arr |
| 108 | + assert_array_equal(h.view(flow=True)[:, 1:11], arr) |
| 109 | + |
| 110 | + arr = np.full((12, 12), default) |
| 111 | + h[...] = arr |
| 112 | + assert_array_equal(h.view(flow=True), arr) |
| 113 | + |
| 114 | + with pytest.raises(ValueError): |
| 115 | + arr = np.full((9, 1), default) |
| 116 | + h[...] = arr |
| 117 | + with pytest.raises(ValueError): |
| 118 | + arr = np.full((11, 1), default) |
| 119 | + h[...] = arr |
| 120 | + with pytest.raises(ValueError): |
| 121 | + arr = np.full((13, 1), default) |
| 122 | + h[...] = arr |
| 123 | + |
| 124 | + with pytest.raises(ValueError): |
| 125 | + h[...] = 1 |
| 126 | + |
| 127 | + with pytest.raises(ValueError): |
| 128 | + h[1, 1] = 1 |
0 commit comments