Skip to content

Commit 66aa5e2

Browse files
committed
feat: Include way to broadcast arrays
1 parent e2c264c commit 66aa5e2

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

notebooks/BoostHistogramHandsOn.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
"cell_type": "markdown",
458458
"metadata": {},
459459
"source": [
460-
"This is transposed because pcolormesh requires ij indexing instead of xy."
460+
"This is transposed because [`pcolormesh` expects it](https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.pcolormesh.html#axes-pcolormesh-grid-orientation)."
461461
]
462462
},
463463
{

src/boost_histogram/_internal/axistuple.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def __dir__(self):
2020
def __call__(self, *args, **kwargs):
2121
return self.__class__(a(*args, **kwargs) for a in self)
2222

23+
def broadcast(self):
24+
"""
25+
The arrays in this tuple will be compressed if possible to save memory.
26+
Use this method to broadcast them out into their full memory
27+
representation.
28+
"""
29+
return self.__class__(np.broadcast_arrays(*self))
30+
2331

2432
class AxesTuple(tuple):
2533
__slots__ = ()

tests/test_axes_object.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,32 @@ def test_axes_all_at_once():
2626

2727
centers = h.axes.centers
2828
answers = np.ogrid[0.5:10, 0.5:5, 0.5:2]
29+
full_answers = np.mgrid[0.5:10, 0.5:5, 0.5:2]
2930

3031
for i in range(3):
32+
np.testing.assert_allclose(centers.broadcast()[i], full_answers[i])
3133
np.testing.assert_allclose(centers[i], answers[i])
3234
np.testing.assert_allclose(centers.T[i], answers[i].T)
3335
np.testing.assert_allclose(centers.flatten()[i], answers[i].flatten())
3436
np.testing.assert_allclose(h.axes[i].centers, answers[i].ravel())
3537

3638
edges = h.axes.edges
3739
answers = np.ogrid[0:11, 0:6, 0:3]
40+
full_answers = np.mgrid[0:11, 0:6, 0:3]
3841

3942
for i in range(3):
43+
np.testing.assert_allclose(edges.broadcast()[i], full_answers[i])
4044
np.testing.assert_allclose(edges[i], answers[i])
4145
np.testing.assert_allclose(edges.T[i], answers[i].T)
4246
np.testing.assert_allclose(edges.ravel()[i], answers[i].ravel())
4347
np.testing.assert_allclose(h.axes[i].edges, answers[i].ravel())
4448

4549
widths = h.axes.widths
4650
answers = np.ogrid[1:1:10j, 1:1:5j, 1:1:2j]
51+
full_answers = np.mgrid[1:1:10j, 1:1:5j, 1:1:2j]
4752

4853
for i in range(3):
54+
np.testing.assert_allclose(widths.broadcast()[i], full_answers[i])
4955
np.testing.assert_allclose(widths[i], answers[i])
5056
np.testing.assert_allclose(widths.T[i], answers[i].T)
5157
np.testing.assert_allclose(widths.ravel()[i], answers[i].ravel())

0 commit comments

Comments
 (0)