File: test_benchmark_category_axis.py

package info (click to toggle)
python-boost-histogram 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,236 kB
  • sloc: python: 7,940; cpp: 3,243; makefile: 22; sh: 1
file content (72 lines) | stat: -rw-r--r-- 1,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from __future__ import annotations

import numpy as np
import pytest

import boost_histogram as bh


@pytest.mark.benchmark(group="IntCategory")
@pytest.mark.parametrize("dtype", ["i", tuple])
def test_IntCategory(benchmark, growth, dtype):
    np.random.seed(42)
    values = np.random.choice(np.arange(5), size=[100000])
    h = bh.Histogram(
        bh.axis.IntCategory([] if growth else np.arange(4), growth=growth),
        storage=bh.storage.Double(),
    )

    def run(h, data):
        h.fill(data)

    benchmark(run, h, tuple(values) if dtype is tuple else values.astype(dtype))


@pytest.mark.benchmark(group="StrCategory")
@pytest.mark.parametrize("dtype", ["S", "U", "O", tuple])
def test_StrCategory(benchmark, growth, dtype):
    np.random.seed(42)
    values = np.random.choice(["A", "B", "C", "D", "E"], size=[100000])
    h = bh.Histogram(
        bh.axis.StrCategory([] if growth else ["A", "B", "C", "D"], growth=growth),
        storage=bh.storage.Double(),
    )

    def run(h, data):
        h.fill(data)

    benchmark(run, h, tuple(values) if dtype is tuple else values.astype(dtype))


@pytest.mark.benchmark(group="Pick")
def test_pick_only(benchmark):
    h = bh.Histogram(
        bh.axis.StrCategory([str(i) for i in range(32)]),
        bh.axis.StrCategory([str(i) for i in range(32)]),
        bh.axis.StrCategory([str(i) for i in range(32)]),
        bh.axis.Regular(32, 0, 320),
    )

    h[...] = 1.0

    def run(h):
        return h[bh.loc("13"), bh.loc("13"), bh.loc("13"), :].view()

    benchmark(run, h)


@pytest.mark.benchmark(group="Pick")
def test_pick_and_slice(benchmark):
    h = bh.Histogram(
        bh.axis.StrCategory([str(i) for i in range(32)]),
        bh.axis.StrCategory([str(i) for i in range(32)]),
        bh.axis.StrCategory([str(i) for i in range(32)]),
        bh.axis.Regular(32, 0, 320),
    )

    h[...] = 1.0

    def run(h):
        return h[3:29, bh.loc("13"), bh.loc("13"), :].view()

    benchmark(run, h)