From 7216c9d599f6c5fa2f019ce123187a46d1ad6035 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 1 Aug 2023 15:53:31 -0400 Subject: [PATCH] feat: add a .T shortcut Signed-off-by: Henry Schreiner --- src/hist/basehist.py | 4 ++++ tests/test_general.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/hist/basehist.py b/src/hist/basehist.py index cc008b7a..de19f9e8 100644 --- a/src/hist/basehist.py +++ b/src/hist/basehist.py @@ -204,6 +204,10 @@ def project(self, *args: int | str) -> Self | float | bh.accumulators.Accumulato int_args = [self._name_to_index(a) if isinstance(a, str) else a for a in args] return super().project(*int_args) + @property + def T(self) -> Self: + return self.project(*reversed(range(self.ndim))) # type: ignore[return-value] + def fill( self, *args: ArrayLike, diff --git a/tests/test_general.py b/tests/test_general.py index 092a3262..bff882c3 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -953,3 +953,23 @@ def test_integrate(): assert h1[{"x": 4j}] == 2 assert h1[{"x": 2j}] == 1 assert h2[{"x": 1j}] == 3 + + +def test_T_property(): + # Create a 2D histogram with some data + hist_data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + h = hist.Hist( + hist.axis.Regular(3, 0, 1, flow=False), + hist.axis.Regular(3, 5, 6, flow=False), + data=hist_data, + ) + + assert h.T.values() == approx(h.values().T) + assert h.T.axes[0] == h.axes[1] + assert h.T.axes[1] == h.axes[0] + + +def test_T_empty(): + hist_empty = hist.Hist() + hist_T_empty = hist_empty.T + assert hist_empty == hist_T_empty