|
2 | 2 | import numpy as np |
3 | 3 | import pytest |
4 | 4 | from numpy.testing import assert_array_equal |
| 5 | +from pytest import approx |
5 | 6 |
|
6 | 7 | import boost_histogram as bh |
7 | 8 |
|
@@ -216,3 +217,45 @@ def test_modify_weights_by_view(): |
216 | 217 |
|
217 | 218 | assert hist.view().value[0] == pytest.approx(1.5) |
218 | 219 | assert hist.view().value[1] == pytest.approx(2) |
| 220 | + |
| 221 | + |
| 222 | +# Issue #531 |
| 223 | +def test_summing_mean_storage(): |
| 224 | + np.random.seed(42) |
| 225 | + values = np.random.normal(loc=1.3, scale=0.1, size=1000) |
| 226 | + samples = np.random.normal(loc=1.3, scale=0.1, size=1000) |
| 227 | + |
| 228 | + h1 = bh.Histogram(bh.axis.Regular(20, -1, 3), storage=bh.storage.Mean()) |
| 229 | + h1.fill(values, sample=samples) |
| 230 | + |
| 231 | + h2 = bh.Histogram(bh.axis.Regular(1, -1, 3), storage=bh.storage.Mean()) |
| 232 | + h2.fill(values, sample=samples) |
| 233 | + |
| 234 | + s1 = h1.sum() |
| 235 | + s2 = h2.sum() |
| 236 | + |
| 237 | + assert s1.value == approx(s2.value) |
| 238 | + assert s1.count == approx(s2.count) |
| 239 | + assert s1.variance == approx(s2.variance) |
| 240 | + |
| 241 | + |
| 242 | +# Issue #531 |
| 243 | +def test_summing_weighted_mean_storage(): |
| 244 | + np.random.seed(42) |
| 245 | + values = np.random.normal(loc=1.3, scale=0.1, size=1000) |
| 246 | + samples = np.random.normal(loc=1.3, scale=0.1, size=1000) |
| 247 | + weights = np.random.uniform(0.1, 5, size=1000) |
| 248 | + |
| 249 | + h1 = bh.Histogram(bh.axis.Regular(20, -1, 3), storage=bh.storage.WeightedMean()) |
| 250 | + h1.fill(values, sample=samples, weight=weights) |
| 251 | + |
| 252 | + h2 = bh.Histogram(bh.axis.Regular(1, -1, 3), storage=bh.storage.WeightedMean()) |
| 253 | + h2.fill(values, sample=samples, weight=weights) |
| 254 | + |
| 255 | + s1 = h1.sum() |
| 256 | + s2 = h2.sum() |
| 257 | + |
| 258 | + assert s1.value == approx(s2.value) |
| 259 | + assert s1.sum_of_weights == approx(s2.sum_of_weights) |
| 260 | + assert s1.sum_of_weights_squared == approx(s2.sum_of_weights_squared) |
| 261 | + assert s1.variance == approx(s2.variance) |
0 commit comments