|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# The point of this test is to make sure that the infrastructure for supporting |
| 3 | +# custom attributes, like title in Hist, is working. |
| 4 | + |
| 5 | +import pytest |
| 6 | +import boost_histogram as bh |
| 7 | + |
| 8 | + |
| 9 | +# First, make a new family to identify your library |
| 10 | +CUSTOM_FAMILY = object() |
| 11 | + |
| 12 | + |
| 13 | +# Add named axes |
| 14 | +class NamedAxesTuple(bh.axis.AxesTuple): |
| 15 | + __slots__ = () |
| 16 | + |
| 17 | + def _get_index_by_name(self, name): |
| 18 | + if isinstance(name, str): |
| 19 | + for i, ax in enumerate(self): |
| 20 | + if ax.name == name: |
| 21 | + return i |
| 22 | + raise KeyError("{} not found in axes".format(name)) |
| 23 | + else: |
| 24 | + return name |
| 25 | + |
| 26 | + def __getitem__(self, item): |
| 27 | + if isinstance(item, slice): |
| 28 | + item = slice( |
| 29 | + self._get_index_by_name(item.start), |
| 30 | + self._get_index_by_name(item.stop), |
| 31 | + self._get_index_by_name(item.step), |
| 32 | + ) |
| 33 | + else: |
| 34 | + item = self._get_index_by_name(item) |
| 35 | + |
| 36 | + return super(NamedAxesTuple, self).__getitem__(item) |
| 37 | + |
| 38 | + @property |
| 39 | + def name(self): |
| 40 | + """ |
| 41 | + The names of the axes. May be empty strings. |
| 42 | + """ |
| 43 | + return tuple(ax.name for ax in self) |
| 44 | + |
| 45 | + |
| 46 | +# When you subclass Histogram or an Axes, you should register your family so |
| 47 | +# boost-histogram will know what to convert C++ objects into. |
| 48 | + |
| 49 | + |
| 50 | +class AxesMixin(object): |
| 51 | + __slots__ = () |
| 52 | + |
| 53 | + @property |
| 54 | + def name(self): |
| 55 | + """ |
| 56 | + Get the name for the Regular axis |
| 57 | + """ |
| 58 | + return self._ax.metadata.get("name", "") |
| 59 | + |
| 60 | + |
| 61 | +# The order of the mixin is important here - it must be first |
| 62 | +# to override bh.axis.Regular |
| 63 | +@bh.utils.set_family(CUSTOM_FAMILY) |
| 64 | +class Regular(bh.axis.Regular, AxesMixin): |
| 65 | + __slots__ = () |
| 66 | + |
| 67 | + def __init__( |
| 68 | + self, bins, start, stop, name, |
| 69 | + ): |
| 70 | + |
| 71 | + super(Regular, self).__init__( |
| 72 | + bins, start, stop, |
| 73 | + ) |
| 74 | + |
| 75 | + self._ax.metadata["name"] = name |
| 76 | + |
| 77 | + |
| 78 | +@bh.utils.set_family(CUSTOM_FAMILY) |
| 79 | +class CustomHist(bh.Histogram): |
| 80 | + def _generate_axes_(self): |
| 81 | + return NamedAxesTuple(self._axis(i) for i in range(self.ndim)) |
| 82 | + |
| 83 | + def __init__(self, *args, **kwargs): |
| 84 | + super(CustomHist, self).__init__(*args, **kwargs) |
| 85 | + valid_names = [ax.name for ax in self.axes if ax.name] |
| 86 | + if len(valid_names) != len(set(valid_names)): |
| 87 | + msg = "{} instance cannot contain axes with duplicated names".format( |
| 88 | + self.__class__.__name__ |
| 89 | + ) |
| 90 | + raise KeyError(msg) |
| 91 | + |
| 92 | + |
| 93 | +def test_hist_creation(): |
| 94 | + hist_1 = CustomHist(Regular(10, 0, 1, name="a"), Regular(20, 0, 4, name="b")) |
| 95 | + assert hist_1.axes[0].name == "a" |
| 96 | + assert hist_1.axes[1].name == "b" |
| 97 | + |
| 98 | + hist_2 = CustomHist(Regular(10, 0, 1, name=""), Regular(20, 0, 4, name="")) |
| 99 | + assert hist_2.axes[0].name == "" |
| 100 | + assert hist_2.axes[1].name == "" |
| 101 | + |
| 102 | + with pytest.raises(KeyError): |
| 103 | + CustomHist(Regular(10, 0, 1, name="a"), Regular(20, 0, 4, name="a")) |
| 104 | + |
| 105 | + |
| 106 | +def test_hist_index(): |
| 107 | + hist_1 = CustomHist(Regular(10, 0, 1, name="a"), Regular(20, 0, 4, name="b")) |
| 108 | + assert hist_1.axes[0].name == "a" |
| 109 | + assert hist_1.axes[1].name == "b" |
| 110 | + |
| 111 | + |
| 112 | +def test_hist_convert(): |
| 113 | + hist_1 = CustomHist(Regular(10, 0, 1, name="a"), Regular(20, 0, 4, name="b")) |
| 114 | + hist_bh = bh.Histogram(hist_1) |
| 115 | + |
| 116 | + assert type(hist_bh.axes[0]) == bh.axis.Regular |
| 117 | + assert hist_bh.axes[0].name == "a" |
| 118 | + assert hist_bh.axes[1].name == "b" |
| 119 | + |
| 120 | + hist_2 = CustomHist(hist_bh) |
| 121 | + |
| 122 | + assert type(hist_2.axes[0]) == Regular |
| 123 | + assert hist_2.axes[0].name == "a" |
| 124 | + assert hist_2.axes[1].name == "b" |
| 125 | + |
| 126 | + # Just verify no-op status |
| 127 | + hist_3 = CustomHist(hist_1) |
| 128 | + |
| 129 | + assert type(hist_3.axes[0]) == Regular |
| 130 | + assert hist_3.axes[0].name == "a" |
| 131 | + assert hist_3.axes[1].name == "b" |
| 132 | + |
| 133 | + |
| 134 | +def test_access(): |
| 135 | + hist = CustomHist(Regular(10, 0, 1, name="a"), Regular(20, 0, 4, name="b")) |
| 136 | + |
| 137 | + assert hist.axes["a"] == hist.axes[0] |
| 138 | + assert hist.axes["b"] == hist.axes[1] |
| 139 | + |
| 140 | + from_bh = bh.Histogram(bh.axis.Regular(10, 0, 1), bh.axis.Regular(20, 0, 4)) |
| 141 | + from_bh.axes.name = "a", "b" |
| 142 | + hist_conv = CustomHist(from_bh) |
| 143 | + |
| 144 | + assert hist_conv.axes["a"] == hist_conv.axes[0] |
| 145 | + assert hist_conv.axes["b"] == hist_conv.axes[1] |
0 commit comments