Skip to content

Commit 9367c82

Browse files
committed
refactor: bool view instead of mode
1 parent ebdd523 commit 9367c82

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/boost_histogram/_internal/hist.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,11 @@ def _compute_commonindex(self, index):
562562

563563
return indexes
564564

565-
@inject_signature("self, flow=False, *, dd=False, mode='numpy'")
565+
@inject_signature("self, flow=False, *, dd=False, view=False")
566566
def to_numpy(self, flow=False, **kwargs):
567567
"""
568-
Convert to a Numpy style tuple of return arrays. Edges are converted
569-
to exactly match NumPy standards, with upper edge inclusive, unlike
568+
Convert to a Numpy style tuple of return arrays. Edges are converted to
569+
match NumPy standards, with upper edge inclusive, unlike
570570
boost-histogram, where upper edge is exclusive.
571571
572572
Parameters
@@ -576,11 +576,11 @@ def to_numpy(self, flow=False, **kwargs):
576576
dd : bool = False
577577
Use the histogramdd return syntax, where the edges are in a tuple.
578578
Otherwise, this is the histogram/histogram2d return style.
579-
mode : Literal["numpy", "view"] = "numpy"
580-
The behavior for the return value. "numpy" will return the NumPy
579+
view : bool = False
580+
The behavior for the return value. By default, this will return
581581
array of the values only regardless of the storage (which is all
582-
NumPy's histogram function can do). "view" will leave the
583-
boost-histogram view of the storage untouched.
582+
NumPy's histogram function can do). view=True will return the
583+
boost-histogram view of the storage.
584584
585585
Return
586586
------
@@ -592,18 +592,16 @@ def to_numpy(self, flow=False, **kwargs):
592592

593593
with KWArgs(kwargs) as kw:
594594
dd = kw.optional("dd", False)
595-
mode = kw.optional("mode", "numpy")
595+
view = kw.optional("view", False)
596596

597597
# Python 3+ would be simpler
598598
return_tuple = self._hist.to_numpy(flow)
599599
hist = return_tuple[0]
600600

601-
if mode == "numpy":
602-
hist = self.values(flow=flow)
603-
elif mode == "view":
601+
if view:
604602
hist = self.view(flow=flow)
605603
else:
606-
raise KeyError("Invalid mode")
604+
hist = self.values(flow=flow)
607605

608606
if dd:
609607
return hist, return_tuple[1:]

src/boost_histogram/numpy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ def histogramdd(
7979
density = hist.view() / hist.sum() / areas
8080
return (density, hist.to_numpy()[1:])
8181

82-
# Note: this is mode="view" since users have to ask explicilty for special
83-
# storages, so mode="numpy" would throw away part of what they are asking
84-
# for. Users can use a histogram return type if they need mode="numpy".
85-
return hist if bh_cls is not None else hist.to_numpy(dd=True, mode="view")
82+
# Note: this is view=True since users have to ask explicitly for special
83+
# storages, so view=False would throw away part of what they are asking
84+
# for. Users can use a histogram return type if they need view=False.
85+
return hist if bh_cls is not None else hist.to_numpy(view=True, dd=True)
8686

8787

8888
@_inject_signature(

tests/test_internal_histogram.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ def test_numpy_weights():
146146
v = i + j * 10 + 1
147147
h.fill([x] * v, [y] * v)
148148

149-
h2, x2, y2 = h.to_numpy(mode="numpy")
150-
h1, (x1, y1) = h.to_numpy(dd=True, mode="numpy")
149+
h2, x2, y2 = h.to_numpy(view=False)
150+
h1, (x1, y1) = h.to_numpy(dd=True, view=False)
151151

152152
assert_array_equal(h1, h2)
153153
assert_array_equal(x1, x2)
154154
assert_array_equal(y1, y2)
155155

156-
h1, (x1, y1) = h.to_numpy(dd=True, mode="numpy")
157-
h2, x2, y2 = h.to_numpy(mode="view")
156+
h1, (x1, y1) = h.to_numpy(dd=True, view=False)
157+
h2, x2, y2 = h.to_numpy(view=True)
158158

159159
assert_array_equal(h1, h2.value)
160160
assert_array_equal(x1, x2)

0 commit comments

Comments
 (0)