Skip to content

Commit a577dd8

Browse files
committed
refactor: bool view instead of mode
1 parent a34c675 commit a577dd8

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
@@ -552,11 +552,11 @@ def _compute_commonindex(self, index):
552552

553553
return indexes
554554

555-
@inject_signature("self, flow=False, *, dd=False, mode='numpy'")
555+
@inject_signature("self, flow=False, *, dd=False, view=False")
556556
def to_numpy(self, flow=False, **kwargs):
557557
"""
558-
Convert to a Numpy style tuple of return arrays. Edges are converted
559-
to exactly match NumPy standards, with upper edge inclusive, unlike
558+
Convert to a Numpy style tuple of return arrays. Edges are converted to
559+
match NumPy standards, with upper edge inclusive, unlike
560560
boost-histogram, where upper edge is exclusive.
561561
562562
Parameters
@@ -566,11 +566,11 @@ def to_numpy(self, flow=False, **kwargs):
566566
dd : bool = False
567567
Use the histogramdd return syntax, where the edges are in a tuple.
568568
Otherwise, this is the histogram/histogram2d return style.
569-
mode : Literal["numpy", "view"] = "numpy"
570-
The behavior for the return value. "numpy" will return the NumPy
569+
view : bool = False
570+
The behavior for the return value. By default, this will return
571571
array of the values only regardless of the storage (which is all
572-
NumPy's histogram function can do). "view" will leave the
573-
boost-histogram view of the storage untouched.
572+
NumPy's histogram function can do). view=True will return the
573+
boost-histogram view of the storage.
574574
575575
Return
576576
------
@@ -582,18 +582,16 @@ def to_numpy(self, flow=False, **kwargs):
582582

583583
with KWArgs(kwargs) as kw:
584584
dd = kw.optional("dd", False)
585-
mode = kw.optional("mode", "numpy")
585+
view = kw.optional("view", False)
586586

587587
# Python 3+ would be simpler
588588
return_tuple = self._hist.to_numpy(flow)
589589
hist = return_tuple[0]
590590

591-
if mode == "numpy":
592-
hist = self.values(flow=flow)
593-
elif mode == "view":
591+
if view:
594592
hist = self.view(flow=flow)
595593
else:
596-
raise KeyError("Invalid mode")
594+
hist = self.values(flow=flow)
597595

598596
if dd:
599597
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)