Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/bh_python/register_axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ py::class_<A> register_axis(py::module& m, Args&&... args) {
[](const A& self) { return bh::axis::traits::ordered(self); })

.def_property(
"metadata",
"raw_metadata",
[](const A& self) { return self.metadata(); },
[](A& self, const metadata_t& label) { self.metadata() = label; },
"Set the axis label")
"Set the metadata")

.def_property_readonly(
"size",
Expand Down
6 changes: 3 additions & 3 deletions src/boost_histogram/_core/axis/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class _BaseAxis:
@property
def traits_ordered(self) -> bool: ...
@property
def metadata(self) -> Any: ...
@metadata.setter
def metadata(self, item: Any) -> None: ...
def raw_metadata(self) -> Any: ...
@raw_metadata.setter
def raw_metadata(self, item: Any) -> None: ...
@property
def size(self) -> int: ...
@property
Expand Down
14 changes: 7 additions & 7 deletions src/boost_histogram/axis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init_subclass__(cls, *, family: object) -> None:

def __setattr__(self, attr: str, value: Any) -> None:
if attr == "__dict__":
self._ax.metadata = value
self._ax.raw_metadata = value
object.__setattr__(self, attr, value)

def __getattr__(self, attr: str) -> Any:
Expand Down Expand Up @@ -120,23 +120,23 @@ def __init__(
"Cannot provide metadata by keyword and __dict__, use __dict__ only"
)
if __dict__ is not None:
self._ax.metadata = __dict__
self._ax.raw_metadata = __dict__
elif metadata is not None:
self._ax.metadata["metadata"] = metadata
self._ax.raw_metadata["metadata"] = metadata

self.__dict__ = self._ax.metadata
self.__dict__ = self._ax.raw_metadata

def __setstate__(self, state: dict[str, Any]) -> None:
self._ax = state["_ax"]
self.__dict__ = self._ax.metadata
self.__dict__ = self._ax.raw_metadata

def __getstate__(self) -> dict[str, Any]:
return {"_ax": self._ax}

def __copy__(self: T) -> T:
other: T = self.__class__.__new__(self.__class__)
other._ax = copy.copy(self._ax)
other.__dict__ = other._ax.metadata
other.__dict__ = other._ax.raw_metadata
return other

def index(self, value: float | str) -> int:
Expand Down Expand Up @@ -176,7 +176,7 @@ def __ne__(self, other: object) -> bool:
def _convert_cpp(cls: type[T], cpp_object: Any) -> T:
nice_ax: T = cls.__new__(cls)
nice_ax._ax = cpp_object
nice_ax.__dict__ = cpp_object.metadata
nice_ax.__dict__ = cpp_object.raw_metadata
return nice_ax

def __len__(self) -> int:
Expand Down
14 changes: 8 additions & 6 deletions src/boost_histogram/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ def _clone(

for ax in self.axes:
if memo is NOTHING:
ax.__dict__ = copy.copy(ax._ax.metadata)
ax.__dict__ = copy.copy(ax._ax.raw_metadata)
else:
ax.__dict__ = copy.deepcopy(ax._ax.metadata, memo)
ax.__dict__ = copy.deepcopy(ax._ax.raw_metadata, memo)
return self

def _new_hist(self: H, _hist: CppHistogram, memo: Any = NOTHING) -> H:
Expand All @@ -380,7 +380,7 @@ def _from_histogram_object(self, other: Histogram) -> None:
self.__dict__ = copy.copy(other.__dict__)
self.axes = self._generate_axes_()
for ax in self.axes:
ax.__dict__ = copy.copy(ax._ax.metadata)
ax.__dict__ = copy.copy(ax._ax.raw_metadata)

# Allow custom behavior on either "from" or "to"
other._export_bh_(self)
Expand Down Expand Up @@ -714,7 +714,9 @@ def __setstate__(self, state: Any) -> None:
self._variance_known = True
self.metadata = state.get("metadata", None)
for i in range(self._hist.rank()):
self._hist.axis(i).metadata = {"metadata": self._hist.axis(i).metadata}
self._hist.axis(i).raw_metadata = {
"metadata": self._hist.axis(i).raw_metadata
}

self.axes = self._generate_axes_()

Expand Down Expand Up @@ -986,7 +988,7 @@ def __getitem__(self: H, index: IndexingExpr) -> H | float | Accumulator:
if new_axis is None:
new_axis = Variable(
new_axes_indices,
__dict__=axes[i].metadata,
__dict__=axes[i].raw_metadata,
underflow=axes[i].traits_underflow,
overflow=axes[i].traits_overflow,
)
Expand Down Expand Up @@ -1072,7 +1074,7 @@ def __getitem__(self: H, index: IndexingExpr) -> H | float | Accumulator:
selection.append(ax.size)

new_axis = axes[i].__class__([axes[i].value(j) for j in pick_set[i]]) # type: ignore[call-arg]
new_axis.metadata = axes[i].metadata
new_axis.raw_metadata = axes[i].raw_metadata
axes[i] = new_axis
reduced_view = np.take(reduced_view, selection, axis=i)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_minihist_title.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def name(self):
@name.setter
def name(self, values):
for ax, val in zip(self, values):
ax._ax.metadata["name"] = f"test: {val}"
ax._ax.raw_metadata["name"] = f"test: {val}"


# When you subclass Histogram or an Axes, you should register your family so
Expand All @@ -65,7 +65,7 @@ def name(self):
"""
Get the name for the Regular axis
"""
return self._ax.metadata.get("name", "")
return self._ax.raw_metadata.get("name", "")


# The order of the mixin is important here - it must be first
Expand All @@ -78,15 +78,15 @@ class Regular(bh.axis.Regular, AxesMixin, family=CUSTOM_FAMILY):

def __init__(self, bins, start, stop, name):
super().__init__(bins, start, stop)
self._ax.metadata["name"] = name
self._ax.raw_metadata["name"] = name


class Integer(AxesMixin, bh.axis.Integer, family=CUSTOM_FAMILY):
__slots__ = ()

def __init__(self, start, stop, name):
super().__init__(start, stop)
self._ax.metadata["name"] = name
self._ax.raw_metadata["name"] = name


class CustomHist(bh.Histogram, family=CUSTOM_FAMILY):
Expand Down