Skip to content

Commit ae8d72c

Browse files
authored
Deprecation for at and axis (#170)
* at and axis replaced by [] and axes * Replace usages of axis * Fix tests
1 parent 88f3901 commit ae8d72c

File tree

6 files changed

+187
-190
lines changed

6 files changed

+187
-190
lines changed

boost_histogram/_hist.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from . import core as _core
88

9+
import warnings
910
import numpy as np
1011

1112
_histograms = (
@@ -93,27 +94,41 @@ def _compute_commonindex(self, index, expand):
9394
# Allow [bh.loc(...)] to work
9495
for i in range(len(indexes)):
9596
if hasattr(indexes[i], "value") and hasattr(indexes[i], "offset"):
96-
indexes[i] = self.axis(i).index(indexes[i].value) + indexes[i].offset
97+
indexes[i] = self._axis(i).index(indexes[i].value) + indexes[i].offset
9798
elif hasattr(indexes[i], "flow"):
9899
if indexes[i].flow == 1:
99-
indexes[i] = self.axis(i).size
100+
indexes[i] = self._axis(i).size
100101
elif indexes[i].flow == -1:
101102
indexes[i] = -1
102103
elif isinstance(indexes[i], int):
103-
if abs(indexes[i]) >= self.axis(i).size:
104+
if abs(indexes[i]) >= self._axis(i).size:
104105
raise IndexError("histogram index is out of range")
105-
indexes[i] %= self.axis(i).size
106+
indexes[i] %= self._axis(i).size
106107

107108
return indexes
108109

109110

111+
def at(self, *ind):
112+
warnings.warn(
113+
".at is deprecated, please use [] indexing instead", category=DeprecationWarning
114+
)
115+
return self._at(*ind)
116+
117+
118+
def axis(self, value):
119+
warnings.warn(
120+
".axis() is deprecated, please use axes[] instead", category=DeprecationWarning
121+
)
122+
return self._axis(value)
123+
124+
110125
def _compute_getitem(self, index):
111126

112127
indexes = _compute_commonindex(self, index, expand=True)
113128

114129
# If this is (now) all integers, return the bin contents
115130
try:
116-
return self.at(*indexes)
131+
return self._at(*indexes)
117132
except RuntimeError:
118133
pass
119134

@@ -149,10 +164,10 @@ def _compute_getitem(self, index):
149164
process_loc = (
150165
lambda x, y: y
151166
if x is None
152-
else (self.axis(i).index(x.value) if hasattr(x, "value") else x)
167+
else (self._axis(i).index(x.value) if hasattr(x, "value") else x)
153168
)
154169
begin = process_loc(ind.start, 0)
155-
end = process_loc(ind.stop, len(self.axis(i)))
170+
end = process_loc(ind.stop, len(self._axis(i)))
156171

157172
slices.append(_core.algorithm.slice_and_rebin(i, begin, end, merge))
158173

@@ -204,6 +219,8 @@ def widths(self):
204219
for h in _histograms:
205220
h.__getitem__ = _compute_getitem
206221
h.__setitem__ = _compute_setitem
207-
h.axes = property(lambda self: AxesTuple(self.axis(i) for i in range(self.rank)))
222+
h.at = at
223+
h.axis = axis
224+
h.axes = property(lambda self: AxesTuple(self._axis(i) for i in range(self.rank)))
208225

209226
del h

include/boost/histogram/python/register_histogram.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ register_histogram(py::module &m, const char *name, const char *desc) {
154154
"Return a view into the data, optionally with overflow turned on")
155155

156156
.def(
157-
"axis",
157+
"_axis",
158158
[](const histogram_t &self, int i) {
159159
unsigned ii = i < 0 ? self.rank() - (unsigned)std::abs(i) : (unsigned)i;
160160
if(ii < self.rank())
@@ -168,7 +168,7 @@ register_histogram(py::module &m, const char *name, const char *desc) {
168168
py::return_value_policy::move)
169169

170170
.def(
171-
"at",
171+
"_at",
172172
[](const histogram_t &self, py::args &args) {
173173
auto int_args = py::cast<std::vector<int>>(args);
174174
return self.at(int_args);

tests/test_histogram_internal.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def test_1D_fill_int(hist_func):
3434
assert_array_equal(hist.view(flow=False), H)
3535
assert_array_equal(hist.view(flow=True)[1:-1], H)
3636

37-
assert hist.axis(0).size == bins
38-
assert hist.axis(0).extent == bins + 2
37+
assert hist.axes[0].size == bins
38+
assert hist.axes[0].extent == bins + 2
3939

4040

4141
@pytest.mark.parametrize("hist_func", methods)
@@ -56,11 +56,11 @@ def test_2D_fill_int(hist_func):
5656
assert_array_equal(hist.view(flow=True)[1:-1, 1:-1], H)
5757
assert_array_equal(hist.view(flow=False), H)
5858

59-
assert hist.axis(0).size == bins[0]
60-
assert hist.axis(0).extent == bins[0] + 2
59+
assert hist.axes[0].size == bins[0]
60+
assert hist.axes[0].extent == bins[0] + 2
6161

62-
assert hist.axis(1).size == bins[1]
63-
assert hist.axis(1).extent == bins[1] + 2
62+
assert hist.axes[1].size == bins[1]
63+
assert hist.axes[1].extent == bins[1] + 2
6464

6565

6666
def test_edges_histogram():
@@ -108,7 +108,7 @@ def test_numpy_flow():
108108

109109
for i in range(10):
110110
for j in range(5):
111-
x, y = h.axis(0).centers[i], h.axis(1).centers[j]
111+
x, y = h.axes[0].centers[i], h.axes[1].centers[j]
112112
v = i + j * 10 + 1
113113
h.fill([x] * v, [y] * v)
114114

@@ -132,7 +132,7 @@ def test_numpy_compare():
132132
ys = []
133133
for i in range(10):
134134
for j in range(5):
135-
x, y = h.axis(0).centers[i], h.axis(1).centers[j]
135+
x, y = h.axes[0].centers[i], h.axes[1].centers[j]
136136
v = i + j * 10 + 1
137137
xs += [x] * v
138138
ys += [y] * v

tests/test_make_histogram.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ def test_make_regular_1D(opt, extent):
1111
)
1212

1313
assert hist.rank == 1
14-
assert hist.axis(0).size == 3
15-
assert hist.axis(0).extent == 3 + extent
16-
assert hist.axis(0).bin(1) == (3, 4)
14+
assert hist.axes[0].size == 3
15+
assert hist.axes[0].extent == 3 + extent
16+
assert hist.axes[0].bin(1) == (3, 4)
1717

1818

1919
def test_shortcuts():
2020
hist = bh.histogram((1, 2, 3), (10, 0, 1))
2121
assert hist.rank == 2
2222
for i in range(2):
23-
assert isinstance(hist.axis(i), bh.axis.regular)
24-
assert isinstance(hist.axis(i), bh.core.axis._regular_uoflow)
25-
assert not isinstance(hist.axis(i), bh.axis.variable)
23+
assert isinstance(hist.axes[i], bh.axis.regular)
24+
assert isinstance(hist.axes[i], bh.core.axis._regular_uoflow)
25+
assert not isinstance(hist.axes[i], bh.axis.variable)
2626

2727

2828
def test_shortcuts_with_metadata():
@@ -42,13 +42,13 @@ def test_make_regular_2D(opt, extent):
4242
)
4343

4444
assert hist.rank == 2
45-
assert hist.axis(0).size == 3
46-
assert hist.axis(0).extent == 3 + extent
47-
assert hist.axis(0).bin(1) == approx((3, 4))
45+
assert hist.axes[0].size == 3
46+
assert hist.axes[0].extent == 3 + extent
47+
assert hist.axes[0].bin(1) == approx((3, 4))
4848

49-
assert hist.axis(1).size == 5
50-
assert hist.axis(1).extent == 5 + extent
51-
assert hist.axis(1).bin(1) == approx((2, 3))
49+
assert hist.axes[1].size == 5
50+
assert hist.axes[1].extent == 5 + extent
51+
assert hist.axes[1].bin(1) == approx((2, 3))
5252

5353

5454
@pytest.mark.parametrize(
@@ -69,24 +69,24 @@ def test_make_any_hist(storage):
6969
)
7070

7171
assert hist.rank == 3
72-
assert hist.axis(0).size == 3
73-
assert hist.axis(0).extent == 5
74-
assert hist.axis(0).bin(1) == approx((2, 3))
75-
assert hist.axis(1).size == 2
76-
assert hist.axis(1).extent == 2
77-
assert hist.axis(1).bin(1) == approx((3, 4))
78-
assert hist.axis(2).size == 4
79-
assert hist.axis(2).extent == 5
80-
assert hist.axis(2).bin(1) == approx((2, 3))
72+
assert hist.axes[0].size == 3
73+
assert hist.axes[0].extent == 5
74+
assert hist.axes[0].bin(1) == approx((2, 3))
75+
assert hist.axes[1].size == 2
76+
assert hist.axes[1].extent == 2
77+
assert hist.axes[1].bin(1) == approx((3, 4))
78+
assert hist.axes[2].size == 4
79+
assert hist.axes[2].extent == 5
80+
assert hist.axes[2].bin(1) == approx((2, 3))
8181

8282

8383
def test_make_any_hist_storage():
8484

8585
assert float != type(
86-
bh.histogram(bh.axis.regular(5, 1, 2), storage=bh.storage.int()).at(0)
86+
bh.histogram(bh.axis.regular(5, 1, 2), storage=bh.storage.int())[0]
8787
)
8888
assert float == type(
89-
bh.histogram(bh.axis.regular(5, 1, 2), storage=bh.storage.double()).at(0)
89+
bh.histogram(bh.axis.regular(5, 1, 2), storage=bh.storage.double())[0]
9090
)
9191

9292

@@ -96,14 +96,14 @@ def test_issue_axis_bin_swan():
9696
bh.axis.circular(10, 0, 1, metadata="y"),
9797
)
9898

99-
b = hist.axis(1).bin(1)
99+
b = hist.axes[1].bin(1)
100100
assert repr(b) == "(0.1, 0.2)"
101101
assert b[0] == approx(0.1)
102102
assert b[1] == approx(0.2)
103103

104-
assert hist.axis(0).bin(0)[0] == 0
105-
assert hist.axis(0).bin(1)[0] == approx(0.1)
106-
assert hist.axis(0).bin(2)[0] == approx(0.4)
104+
assert hist.axes[0].bin(0)[0] == 0
105+
assert hist.axes[0].bin(1)[0] == approx(0.1)
106+
assert hist.axes[0].bin(2)[0] == approx(0.4)
107107

108108

109109
options = (

tests/test_pickle.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ def test_compare_copy_hist(metadata):
108108
new = copy.copy(orig)
109109
dnew = copy.deepcopy(orig)
110110

111-
assert orig.axis(0).metadata is new.axis(0).metadata
112-
assert orig.axis(0).metadata == dnew.axis(0).metadata
113-
assert orig.axis(0).metadata is not dnew.axis(0).metadata
111+
assert orig.axes[0].metadata is new.axes[0].metadata
112+
assert orig.axes[0].metadata == dnew.axes[0].metadata
113+
assert orig.axes[0].metadata is not dnew.axes[0].metadata
114114

115115

116116
@pytest.mark.parametrize("axis,args", axes_creations)

0 commit comments

Comments
 (0)