|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import functools |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from .. import axis |
| 7 | + |
| 8 | +__all__ = ["_axis_from_dict", "_axis_to_dict"] |
| 9 | + |
| 10 | + |
| 11 | +def __dir__() -> list[str]: |
| 12 | + return __all__ |
| 13 | + |
| 14 | + |
| 15 | +@functools.singledispatch |
| 16 | +def _axis_to_dict(ax: Any, /) -> dict[str, Any]: |
| 17 | + """Convert an axis to a dictionary.""" |
| 18 | + raise TypeError(f"Unsupported axis type: {type(ax)}") |
| 19 | + |
| 20 | + |
| 21 | +@_axis_to_dict.register(axis.Regular) |
| 22 | +@_axis_to_dict.register(axis.Integer) |
| 23 | +def _(ax: axis.Regular | axis.Integer, /) -> dict[str, Any]: |
| 24 | + """Convert a Regular axis to a dictionary.""" |
| 25 | + |
| 26 | + # Special handling if the axis has a transform |
| 27 | + if isinstance(ax, axis.Regular) and ax.transform is not None: |
| 28 | + data = { |
| 29 | + "type": "variable", |
| 30 | + "edges": ax.edges, |
| 31 | + "underflow": ax.traits.underflow, |
| 32 | + "overflow": ax.traits.overflow, |
| 33 | + "circular": ax.traits.circular, |
| 34 | + } |
| 35 | + else: |
| 36 | + data = { |
| 37 | + "type": "regular", |
| 38 | + "lower": ax.edges[0], |
| 39 | + "upper": ax.edges[-1], |
| 40 | + "bins": ax.size, |
| 41 | + "underflow": ax.traits.underflow, |
| 42 | + "overflow": ax.traits.overflow, |
| 43 | + "circular": ax.traits.circular, |
| 44 | + } |
| 45 | + if isinstance(ax, axis.Integer): |
| 46 | + data["writer_info"] = {"boost-histogram": {"orig_type": "Integer"}} |
| 47 | + if ax.metadata is not None: |
| 48 | + data["metadata"] = ax.metadata |
| 49 | + |
| 50 | + return data |
| 51 | + |
| 52 | + |
| 53 | +@_axis_to_dict.register |
| 54 | +def _(ax: axis.Variable, /) -> dict[str, Any]: |
| 55 | + """Convert a Variable or Integer axis to a dictionary.""" |
| 56 | + data = { |
| 57 | + "type": "variable", |
| 58 | + "edges": ax.edges, |
| 59 | + "underflow": ax.traits.underflow, |
| 60 | + "overflow": ax.traits.overflow, |
| 61 | + "circular": ax.traits.circular, |
| 62 | + } |
| 63 | + if ax.metadata is not None: |
| 64 | + data["metadata"] = ax.metadata |
| 65 | + |
| 66 | + return data |
| 67 | + |
| 68 | + |
| 69 | +@_axis_to_dict.register |
| 70 | +def _(ax: axis.IntCategory, /) -> dict[str, Any]: |
| 71 | + """Convert an IntCategory axis to a dictionary.""" |
| 72 | + data = { |
| 73 | + "type": "category_int", |
| 74 | + "categories": list(ax), |
| 75 | + "flow": ax.traits.overflow, |
| 76 | + } |
| 77 | + if ax.metadata is not None: |
| 78 | + data["metadata"] = ax.metadata |
| 79 | + |
| 80 | + return data |
| 81 | + |
| 82 | + |
| 83 | +@_axis_to_dict.register |
| 84 | +def _(ax: axis.StrCategory, /) -> dict[str, Any]: |
| 85 | + """Convert a StrCategory axis to a dictionary.""" |
| 86 | + data = { |
| 87 | + "type": "category_str", |
| 88 | + "categories": list(ax), |
| 89 | + "flow": ax.traits.overflow, |
| 90 | + } |
| 91 | + if ax.metadata is not None: |
| 92 | + data["metadata"] = ax.metadata |
| 93 | + |
| 94 | + return data |
| 95 | + |
| 96 | + |
| 97 | +@_axis_to_dict.register |
| 98 | +def _(ax: axis.Boolean, /) -> dict[str, Any]: |
| 99 | + """Convert a Boolean axis to a dictionary.""" |
| 100 | + data = { |
| 101 | + "type": "boolean", |
| 102 | + } |
| 103 | + if ax.metadata is not None: |
| 104 | + data["metadata"] = ax.metadata |
| 105 | + |
| 106 | + return data |
| 107 | + |
| 108 | + |
| 109 | +def _axis_from_dict(data: dict[str, Any], /) -> axis.Axis: |
| 110 | + hist_type = data["type"] |
| 111 | + if hist_type == "regular": |
| 112 | + return axis.Regular( |
| 113 | + data["bins"], |
| 114 | + data["lower"], |
| 115 | + data["upper"], |
| 116 | + underflow=data["underflow"], |
| 117 | + overflow=data["overflow"], |
| 118 | + circular=data["circular"], |
| 119 | + metadata=data.get("metadata"), |
| 120 | + ) |
| 121 | + if hist_type == "variable": |
| 122 | + return axis.Variable( |
| 123 | + data["edges"], |
| 124 | + underflow=data["underflow"], |
| 125 | + overflow=data["overflow"], |
| 126 | + circular=data["circular"], |
| 127 | + metadata=data.get("metadata"), |
| 128 | + ) |
| 129 | + if hist_type == "category_int": |
| 130 | + return axis.IntCategory( |
| 131 | + data["categories"], |
| 132 | + overflow=data["flow"], |
| 133 | + metadata=data.get("metadata"), |
| 134 | + ) |
| 135 | + if hist_type == "category_str": |
| 136 | + return axis.StrCategory( |
| 137 | + data["categories"], |
| 138 | + overflow=data["flow"], |
| 139 | + metadata=data.get("metadata"), |
| 140 | + ) |
| 141 | + if hist_type == "boolean": |
| 142 | + return axis.Boolean(metadata=data.get("metadata")) |
| 143 | + |
| 144 | + raise TypeError(f"Unsupported axis type: {hist_type}") |
0 commit comments