Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: faster picking when no slices are made #645

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# What's new in boost-histogram

## Version 1.1
## Version 1.2

### Version 1.1.1
### Version 1.2.0

#### User changes
* Python 3.10 officially supported, with wheels.
Expand All @@ -11,6 +11,7 @@

#### Bug fixes
* Support custom setters on AxesTuple subclasses. [#627][]
* Faster picking if slices are not also used [#645][]
* Throw an error when an AxesTuple setter is the wrong length (inspired by zip strict in Python 3.10) [#627][]
* Fix error thrown on comparison with axis and non-axis object [#631][]
* Static typing no longer thinks `storage=` is required [#604][]
Expand All @@ -26,8 +27,11 @@
[#627]: https://github.com/scikit-hep/boost-histogram/pull/627
[#631]: https://github.com/scikit-hep/boost-histogram/pull/631
[#636]: https://github.com/scikit-hep/boost-histogram/pull/636
[#645]: https://github.com/scikit-hep/boost-histogram/pull/645
[#647]: https://github.com/scikit-hep/boost-histogram/pull/647

## Version 1.1

### Version 1.1.0

#### User changes
Expand Down
12 changes: 10 additions & 2 deletions src/boost_histogram/_internal/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,16 @@ def __getitem__( # noqa: C901
assert isinstance(stop, int)
slices.append(_core.algorithm.slice_and_rebin(i, start, stop, merge))

logger.debug("Reduce with %s", slices)
reduced = self._hist.reduce(*slices)
if slices:
logger.debug("Reduce with %s", slices)
reduced = self._hist.reduce(*slices)
elif pick_set or pick_each or integrations:
# Can avoid a copy in these cases, will be copied anyway
logger.debug("Reduce is empty, but picking or slicing, so no copy needed")
reduced = self._hist
else:
logger.debug("Reduce is empty, just making a copy")
reduced = copy.copy(self._hist)

if pick_set:
warnings.warn(
Expand Down
36 changes: 36 additions & 0 deletions tests/test_benchmark_category_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,39 @@ 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)