|
1 | 1 | import numpy as np |
2 | 2 | import pytest |
3 | 3 | from numpy.testing import assert_allclose, assert_array_equal |
| 4 | +from pytest import approx |
4 | 5 |
|
5 | 6 | import boost_histogram as bh |
6 | 7 |
|
@@ -258,3 +259,106 @@ def test_int_cat_hist(): |
258 | 259 |
|
259 | 260 | with pytest.raises(RuntimeError): |
260 | 261 | h.fill(0.5) |
| 262 | + |
| 263 | + |
| 264 | +@pytest.mark.filterwarnings("ignore:List indexing selection is experimental") |
| 265 | +def test_int_cat_hist_pick_several(): |
| 266 | + h = bh.Histogram( |
| 267 | + bh.axis.IntCategory([1, 2, 7], __dict__={"xval": 5}), storage=bh.storage.Int64() |
| 268 | + ) |
| 269 | + |
| 270 | + h.fill(1, weight=8) |
| 271 | + h.fill(2, weight=7) |
| 272 | + h.fill(7, weight=6) |
| 273 | + |
| 274 | + assert h.view() == approx(np.array([8, 7, 6])) |
| 275 | + assert h.sum() == 21 |
| 276 | + |
| 277 | + assert h[[0, 2]].values() == approx(np.array([8, 6])) |
| 278 | + assert h[[2, 0]].values() == approx(np.array([6, 8])) |
| 279 | + assert h[[1]].values() == approx(np.array([7])) |
| 280 | + |
| 281 | + assert h[[bh.loc(1), bh.loc(7)]].values() == approx(np.array([8, 6])) |
| 282 | + assert h[[bh.loc(7), bh.loc(1)]].values() == approx(np.array([6, 8])) |
| 283 | + assert h[[bh.loc(2)]].values() == approx(np.array([7])) |
| 284 | + |
| 285 | + assert tuple(h[[0, 2]].axes[0]) == (1, 7) |
| 286 | + assert tuple(h[[2, 0]].axes[0]) == (7, 1) |
| 287 | + assert tuple(h[[1]].axes[0]) == (2,) |
| 288 | + |
| 289 | + assert h.axes[0].xval == 5 |
| 290 | + assert h[[0]].axes[0].xval == 5 |
| 291 | + assert h[[0, 1, 2]].axes[0].xval == 5 |
| 292 | + |
| 293 | + |
| 294 | +@pytest.mark.filterwarnings("ignore:List indexing selection is experimental") |
| 295 | +def test_str_cat_pick_several(): |
| 296 | + h = bh.Histogram(bh.axis.StrCategory(["a", "b", "c"])) |
| 297 | + |
| 298 | + h.fill(["a", "a", "a", "b", "b", "c"], weight=0.25) |
| 299 | + |
| 300 | + assert h[[0, 1, 2]].values() == approx(np.array([0.75, 0.5, 0.25])) |
| 301 | + assert h[[2, 1, 0]].values() == approx(np.array([0.25, 0.5, 0.75])) |
| 302 | + assert h[[1, 0]].values() == approx(np.array([0.5, 0.75])) |
| 303 | + |
| 304 | + assert h[[bh.loc("a"), bh.loc("b"), bh.loc("c")]].values() == approx( |
| 305 | + np.array([0.75, 0.5, 0.25]) |
| 306 | + ) |
| 307 | + assert h[[bh.loc("c"), bh.loc("b"), bh.loc("a")]].values() == approx( |
| 308 | + np.array([0.25, 0.5, 0.75]) |
| 309 | + ) |
| 310 | + assert h[[bh.loc("b"), bh.loc("a")]].values() == approx(np.array([0.5, 0.75])) |
| 311 | + |
| 312 | + assert tuple(h[[1, 0]].axes[0]) == ("b", "a") |
| 313 | + |
| 314 | + |
| 315 | +@pytest.mark.filterwarnings("ignore:List indexing selection is experimental") |
| 316 | +def test_pick_invalid(): |
| 317 | + h = bh.Histogram(bh.axis.Regular(10, 0, 1)) |
| 318 | + with pytest.raises(RuntimeError): |
| 319 | + h[[0, 1]] |
| 320 | + |
| 321 | + h = bh.Histogram(bh.axis.Integer(0, 10)) |
| 322 | + with pytest.raises(RuntimeError): |
| 323 | + h[[0, 1]] |
| 324 | + |
| 325 | + |
| 326 | +@pytest.mark.filterwarnings("ignore:List indexing selection is experimental") |
| 327 | +def test_str_cat_pick_dual(): |
| 328 | + h = bh.Histogram( |
| 329 | + bh.axis.StrCategory(["a", "b", "c"]), bh.axis.StrCategory(["d", "e", "f"]) |
| 330 | + ) |
| 331 | + vals = np.arange(9).reshape(3, 3) |
| 332 | + h.values()[...] = vals |
| 333 | + |
| 334 | + assert h[[0], [0]].values() == approx(vals[[0]][:, [0]]) |
| 335 | + assert h[[1], [2]].values() == approx(vals[[1]][:, [2]]) |
| 336 | + assert h[[1], [0, 1]].values() == approx(vals[[1]][:, [0, 1]]) |
| 337 | + assert h[[0, 1], [1]].values() == approx(vals[[0, 1]][:, [1]]) |
| 338 | + assert h[[0, 1], [0, 1]].values() == approx(vals[[0, 1]][:, [0, 1]]) |
| 339 | + assert h[[0, 1], [2, 1]].values() == approx(vals[[0, 1]][:, [2, 1]]) |
| 340 | + |
| 341 | + |
| 342 | +@pytest.mark.filterwarnings("ignore:List indexing selection is experimental") |
| 343 | +def test_pick_multiaxis(): |
| 344 | + h = bh.Histogram( |
| 345 | + bh.axis.StrCategory(["a", "b", "c"]), |
| 346 | + bh.axis.IntCategory([-5, 0, 10]), |
| 347 | + bh.axis.Regular(5, 0, 1), |
| 348 | + bh.axis.StrCategory(["d", "e", "f"]), |
| 349 | + storage=bh.storage.Int64(), |
| 350 | + ) |
| 351 | + |
| 352 | + h.fill("b", -5, 0.65, "f") |
| 353 | + h.fill("b", -5, 0.65, "e") |
| 354 | + |
| 355 | + mini = h[[bh.loc("b"), 2], [1, bh.loc(-5)], sum, bh.loc("f")] |
| 356 | + |
| 357 | + assert mini.ndim == 2 |
| 358 | + assert tuple(mini.axes[0]) == ("b", "c") |
| 359 | + assert tuple(mini.axes[1]) == (0, -5) |
| 360 | + |
| 361 | + assert h[[1, 2], [0, 1], sum, bh.loc("f")].sum() == 1 |
| 362 | + assert h[[1, 2], [1, 0], sum, bh.loc("f")].sum() == 1 |
| 363 | + |
| 364 | + assert mini.values() == approx(np.array(((0, 1), (0, 0)))) |
0 commit comments