Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add features from boost-histogram 1.4 #535

Merged
merged 2 commits into from
Sep 12, 2023
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
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ jobs:
- "3.9"
- "3.10"
- "3.11"
- "3.12"
name: Check Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true

- name: Requirements check
run: python -m pip list
Expand All @@ -55,9 +57,9 @@ jobs:
run: python -m pytest

- name: Install plotting requirements too
if: matrix.python-version != '3.10'
if: matrix.python-version != '3.12'
run: python -m pip install -e ".[test,plot]"

- name: Test plotting too
if: matrix.python-version != '3.10'
if: matrix.python-version != '3.12'
run: python -m pytest --mpl
2 changes: 1 addition & 1 deletion docs/user-guide/notebooks/Histogram.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
" Unif=np.random.uniform(size=1000),\n",
" Greet=[\"hi\"] * 800 + [\"hello\"] * 200,\n",
" Yes=[True] * 600 + [False] * 400,\n",
" Int=np.ones(1000),\n",
" Int=np.ones(1000, dtype=int),\n",
")"
]
},
Expand Down
10 changes: 6 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Information Analysis",
"Topic :: Scientific/Engineering :: Mathematics",
Expand All @@ -37,7 +38,8 @@ requires-python = ">=3.7"
dependencies = [
"boost-histogram>=1.3.1,<1.5",
"histoprint>=2.2.0",
"numpy>=1.14.5",
'numpy>=1.14.5;python_version<"3.12"',
'numpy>=1.26.0b1;python_version>="3.12"',
'typing-extensions>=4;python_version<"3.11"',
]
dynamic = ["version"]
Expand Down Expand Up @@ -72,8 +74,8 @@ dask = [
test = [
"pytest >=6",
"pytest-mpl >=0.12",
"dask[dataframe] >=2022; python_version>='3.8'",
"dask_histogram >=2023.1; python_version>='3.8'",
"dask[dataframe] >=2022; python_version>='3.8' and python_version<'3.12'",
"dask_histogram >=2023.1; python_version>='3.8' and python_version<'3.12'",
]
dev = [
"pytest >=6",
Expand Down Expand Up @@ -185,7 +187,7 @@ messages_control.disable = [
[tool.ruff]
select = [
"E", "F", "W", # flake8
"B", "B904", # flake8-bugbear
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
Expand Down
23 changes: 23 additions & 0 deletions src/hist/axis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Any, Iterable

import boost_histogram as bh
import boost_histogram.axis as bha

import hist
Expand Down Expand Up @@ -212,12 +213,23 @@ def __init__(
label: str = "",
metadata: Any = None,
growth: bool = False,
flow: bool = True,
overflow: bool | None = None,
__dict__: dict[str, Any] | None = None,
) -> None:
has_flow = flow if overflow is None else overflow
if tuple(int(x) for x in bh.__version__.split(".")[:2]) < (1, 4):
if not has_flow:
msg = "Boost-histogram 1.4+ required for flowless Category axes"
raise TypeError(msg)
kwargs = {}
else:
kwargs = {"overflow": has_flow}
super().__init__(
categories,
metadata=metadata,
growth=growth,
**kwargs,
__dict__=__dict__,
)
self._ax.metadata["name"] = name
Expand All @@ -235,12 +247,23 @@ def __init__(
label: str = "",
metadata: Any = None,
growth: bool = False,
flow: bool = True,
overflow: bool | None = None,
__dict__: dict[str, Any] | None = None,
) -> None:
has_flow = flow if overflow is None else overflow
if tuple(int(x) for x in bh.__version__.split(".")[:2]) < (1, 4):
if not has_flow:
msg = "Boost-histogram 1.4+ required for flowless Category axes"
raise TypeError(msg)
kwargs = {}
else:
kwargs = {"overflow": has_flow}
super().__init__(
categories,
metadata=metadata,
growth=growth,
**kwargs,
__dict__=__dict__,
)
self._ax.metadata["name"] = name
Expand Down
18 changes: 13 additions & 5 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,19 @@ def test_general_fill():

# IntCategory
h = Hist(
axis.IntCategory(range(10), name="x"),
axis.IntCategory(range(10), name="y"),
axis.IntCategory(range(10), name="x", flow=False),
axis.IntCategory(range(10), name="y", overflow=False),
axis.IntCategory(range(2), name="z"),
).fill(
x=[3, 3, 3, 4, 5, 5, 5],
y=[3, 3, 4, 4, 4, 4, 4],
z=[0, 0, 1, 1, 1, 1, 1],
)

assert not h.axes[0].traits.overflow
assert not h.axes[1].traits.overflow
assert h.axes[2].traits.overflow

z_one_only = h[{2: bh.loc(1)}]
for idx_x in range(10):
for idx_y in range(10):
Expand All @@ -219,15 +223,19 @@ def test_general_fill():

# StrCategory
h = Hist(
axis.StrCategory("FT", name="x"),
axis.StrCategory(list("FT"), name="y"),
axis.StrCategory("FT", name="x", flow=False),
axis.StrCategory(list("FT"), name="y", overflow=False),
axis.StrCategory(["F", "T"], name="z"),
).fill(
["T", "T", "T", "T", "T", "F", "T"],
["F", "T", "T", "F", "F", "T", "F"],
["F", "F", "T", "T", "T", "T", "T"],
)

assert not h.axes[0].traits.overflow
assert not h.axes[1].traits.overflow
assert h.axes[2].traits.overflow

z_one_only = h[{2: bh.loc("T")}]
assert z_one_only[bh.loc("F"), bh.loc("F")] == 0
assert z_one_only[bh.loc("F"), bh.loc("T")] == 1
Expand Down Expand Up @@ -282,7 +290,7 @@ def test_general_access():
h = Hist(
axis.Regular(50, -5, 5, name="Norm", label="normal distribution"),
axis.Regular(50, -5, 5, name="Unif", label="uniform distribution"),
axis.StrCategory(["hi", "hello"], name="Greet"),
axis.StrCategory(["hi", "hello"], name="Greet", flow=False),
axis.Boolean(name="Yes"),
axis.Integer(0, 1000, name="Int"),
).fill(
Expand Down