Skip to content

Commit

Permalink
feat: storage shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Feb 20, 2021
1 parent d8a1f8e commit 66831b7
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Hist currently provides everything boost-histogram provides, and the following e
- The `Hist` class augments `bh.Histogram` with reduced typing construction:
- Optional import-free construction system
- `flow=False` is a fast way to turn off flow
- Storages can be given by string
- `storage=` can be omitted

- Hist implements UHI+; an extension to the UHI (Unified Histogram Indexing) system designed for import-free interactivity:
- Uses `j` suffix to switch to data coordinates in access or slices
Expand Down
9 changes: 6 additions & 3 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ Changelog
Version 2.1.0
--------------------

* Support shortcuts for setting storages by string or position

Updated dependencies:

- `boost-histogram` 0.11.0 to 0.13.0.
* ``boost-histogram`` 0.11.0 to 0.13.0.
- major new features, including PlottableProtocol
- `histoprint` >=1.4 to >=1.6.
- `mplhep` >=0.2.16 when `[plot]` given

* ``histoprint`` >=1.4 to >=1.6.
* ``mplhep`` >=0.2.16 when ``[plot]`` given


Version 2.0.1
Expand Down
23 changes: 20 additions & 3 deletions src/hist/basehist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .quick_construct import MetaConstructor
from .utils import set_family, HIST_FAMILY
from .storage import Storage
from .axis import AxisProtocol

import warnings
import functools
Expand Down Expand Up @@ -36,15 +37,31 @@ def _proc_kw_for_lw(kwargs):
class BaseHist(bh.Histogram, metaclass=MetaConstructor):
__slots__ = ()

def __init__(self, *args, storage: Optional[Storage] = None, metadata=None):
def __init__(
self,
*args: Union[AxisProtocol, Storage, str, Tuple[int, float, float]],
storage: Optional[Union[Storage, str]] = None,
metadata=None,
):
"""
Initialize BaseHist object. Axis params can contain the names.
"""
self._hist: Any = None
self.axes: NamedAxesTuple

if len(args):
if isinstance(storage, type):
if args and storage is None and isinstance(args[-1], (Storage, str)):
storage = args[-1]
args = args[:-1]

if args:
if isinstance(storage, str):
storage_str = storage.title()
if storage_str == "Atomicint64":
storage_str = "AtomicInt64"
elif storage_str == "Weightedmean":
storage_str = "WeightedMean"
storage = getattr(bh.storage, storage_str)()
elif isinstance(storage, type):
msg = (
f"Please use '{storage.__name__}()' instead of '{storage.__name__}'"
)
Expand Down
92 changes: 91 additions & 1 deletion tests/test_general.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hist import Hist, axis
from hist import Hist, axis, storage

import boost_histogram as bh
import pytest
Expand Down Expand Up @@ -432,6 +432,19 @@ def test_double(self):
with pytest.raises(Exception):
h.Double()

assert (
Hist(axis.Regular(10, 0, 1, name="x"), "double")._storage_type
== storage.Double
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage="DouBle")._storage_type
== storage.Double
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage.Double())._storage_type
== storage.Double
)

def test_int64(self):
h = Hist.new.Reg(10, 0, 1, name="x").Int64().fill([0.5, 0.5])
assert h[0.5j] == 2
Expand All @@ -441,6 +454,19 @@ def test_int64(self):
with pytest.raises(Exception):
h.Int64()

assert (
Hist(axis.Regular(10, 0, 1, name="x"), "int64")._storage_type
== storage.Int64
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage="INT64")._storage_type
== storage.Int64
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage.Int64())._storage_type
== storage.Int64
)

def test_atomic_int64(self):
h = Hist.new.Reg(10, 0, 1, name="x").AtomicInt64().fill([0.5, 0.5])
assert h[0.5j] == 2
Expand All @@ -450,6 +476,19 @@ def test_atomic_int64(self):
with pytest.raises(Exception):
h.AtomicInt64()

assert (
Hist(axis.Regular(10, 0, 1, name="x"), "atomicint64")._storage_type
== storage.AtomicInt64
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage="AtomicINT64")._storage_type
== storage.AtomicInt64
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage.AtomicInt64())._storage_type
== storage.AtomicInt64
)

def test_weight(self):
h = Hist.new.Reg(10, 0, 1, name="x").Weight().fill([0.5, 0.5])
assert h[0.5j].variance == 2
Expand All @@ -459,6 +498,19 @@ def test_weight(self):
with pytest.raises(Exception):
h.Weight()

assert (
Hist(axis.Regular(10, 0, 1, name="x"), "WeighT")._storage_type
== storage.Weight
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage="weight")._storage_type
== storage.Weight
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage.Weight())._storage_type
== storage.Weight
)

def test_mean(self):
h = (
Hist.new.Reg(10, 0, 1, name="x")
Expand All @@ -473,6 +525,18 @@ def test_mean(self):
with pytest.raises(Exception):
h.Mean()

assert (
Hist(axis.Regular(10, 0, 1, name="x"), "MEAn")._storage_type == storage.Mean
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage="mean")._storage_type
== storage.Mean
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage.Mean())._storage_type
== storage.Mean
)

def test_weighted_mean(self):
h = (
Hist.new.Reg(10, 0, 1, name="x")
Expand All @@ -488,6 +552,19 @@ def test_weighted_mean(self):
with pytest.raises(Exception):
h.WeightedMean()

assert (
Hist(axis.Regular(10, 0, 1, name="x"), "WeighTEDMEAn")._storage_type
== storage.WeightedMean
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage="weightedMean")._storage_type
== storage.WeightedMean
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage.WeightedMean())._storage_type
== storage.WeightedMean
)

def test_unlimited(self):
h = Hist.new.Reg(10, 0, 1, name="x").Unlimited().fill([0.5, 0.5])
assert h[0.5j] == 2
Expand All @@ -496,6 +573,19 @@ def test_unlimited(self):
with pytest.raises(Exception):
h.Unlimited()

assert (
Hist(axis.Regular(10, 0, 1, name="x"), "unlimited")._storage_type
== storage.Unlimited
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage="UNLImited")._storage_type
== storage.Unlimited
)
assert (
Hist(axis.Regular(10, 0, 1, name="x"), storage.Unlimited())._storage_type
== storage.Unlimited
)


def test_general_transform_proxy():
"""
Expand Down

0 comments on commit 66831b7

Please sign in to comment.