Skip to content

Commit

Permalink
add histogram LGDO
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelHu committed Jul 10, 2024
1 parent 5fbe2f2 commit bbdd101
Show file tree
Hide file tree
Showing 12 changed files with 511 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies = [
"colorlog",
"h5py>=3.2",
"hdf5plugin",
"hist",
"numba!=0.53.*,!=0.54.*",
"numexpr",
"numpy>=1.21",
Expand Down
4 changes: 4 additions & 0 deletions src/lgdo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
ArrayOfEncodedEqualSizedArrays,
ArrayOfEqualSizedArrays,
FixedSizeArray,
Histogram,
HistogramAxis,
Scalar,
Struct,
Table,
Expand All @@ -63,6 +65,8 @@
"ArrayOfEqualSizedArrays",
"ArrayOfEncodedEqualSizedArrays",
"FixedSizeArray",
"Histogram",
"HistogramAxis",
"LGDO",
"Scalar",
"Struct",
Expand Down
2 changes: 2 additions & 0 deletions src/lgdo/lh5/_serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_h5_read_ndarray,
)
from .read.composite import (
_h5_read_histogram,
_h5_read_lgdo,
_h5_read_struct,
_h5_read_table,
Expand All @@ -32,6 +33,7 @@
"_h5_read_array_of_equalsized_arrays",
"_h5_read_struct",
"_h5_read_table",
"_h5_read_histogram",
"_h5_read_scalar",
"_h5_read_array_of_encoded_equalsized_arrays",
"_h5_read_vector_of_encoded_vectors",
Expand Down
42 changes: 42 additions & 0 deletions src/lgdo/lh5/_serializers/read/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ArrayOfEncodedEqualSizedArrays,
ArrayOfEqualSizedArrays,
FixedSizeArray,
Histogram,
Scalar,
Struct,
Table,
Expand Down Expand Up @@ -168,6 +169,17 @@ def _h5_read_lgdo(
decompress=decompress,
)

if lgdotype is Histogram:
return _h5_read_histogram(
h5o,
start_row=start_row,
n_rows=n_rows,
idx=idx,
use_h5idx=use_h5idx,
field_mask=field_mask,
decompress=decompress,
)

if lgdotype is ArrayOfEncodedEqualSizedArrays:
return _h5_read_array_of_encoded_equalsized_arrays(
h5o,
Expand Down Expand Up @@ -385,3 +397,33 @@ def _h5_read_table(
utils.check_obj_buf_attrs(obj_buf.attrs, attrs, h5g)

return obj_buf, n_rows_read


def _h5_read_histogram(
h5g,
start_row=0,
n_rows=sys.maxsize,
idx=None,
use_h5idx=False,
field_mask=None,
decompress=True,
):
struct, n_rows_read = _h5_read_struct(
h5g,
start_row,
n_rows,
idx,
use_h5idx,
field_mask,
decompress,
)
isdensity = struct.isdensity.value
binning = [
(a.binedges.first, a.binedges.last, a.binedges.step, a.closedleft)
for _, a in struct.binning.items()
]
binning = [tuple(v.value for v in b) for b in binning]
weights = struct.weights.view_as("np")
histogram = Histogram(weights, binning, isdensity)

return histogram, n_rows_read
6 changes: 5 additions & 1 deletion src/lgdo/lh5/_serializers/write/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ def _h5_write_lgdo(
msg = f"can't overwrite '{name}' in wo_mode 'write_safe'"
raise LH5EncodeError(msg, lh5_file, group, name)

# struct or table or waveform table
# struct, table, waveform table or histogram.
if isinstance(obj, types.Struct):
if isinstance(obj, types.Histogram) and wo_mode not in ["w", "of"]:
msg = f"can't append-write histogram in wo_mode '{wo_mode}'"
raise LH5EncodeError(msg, lh5_file, group, name)

return _h5_write_struct(
obj,
name,
Expand Down
1 change: 1 addition & 0 deletions src/lgdo/lh5/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
lgdo.ArrayOfEncodedEqualSizedArrays,
r"^array_of_encoded_equalsized_arrays<1,1>\{.+\}$",
),
(lgdo.Histogram, r"^struct\{binning,weights,isdensity\}$"),
(lgdo.Struct, r"^struct\{.*\}$"),
(lgdo.Table, r"^table\{.*\}$"),
(lgdo.FixedSizeArray, r"^fixedsize_array<\d+>\{.+\}$"),
Expand Down
1 change: 1 addition & 0 deletions src/lgdo/lh5_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ArrayOfEncodedEqualSizedArrays, # noqa: F401
ArrayOfEqualSizedArrays, # noqa: F401
FixedSizeArray, # noqa: F401
Histogram, # noqa: F401
Scalar,
Struct,
Table, # noqa: F401
Expand Down
3 changes: 3 additions & 0 deletions src/lgdo/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .arrayofequalsizedarrays import ArrayOfEqualSizedArrays
from .encoded import ArrayOfEncodedEqualSizedArrays, VectorOfEncodedVectors
from .fixedsizearray import FixedSizeArray
from .histogram import Histogram, HistogramAxis
from .lgdo import LGDO
from .scalar import Scalar
from .struct import Struct
Expand All @@ -18,6 +19,8 @@
"ArrayOfEqualSizedArrays",
"ArrayOfEncodedEqualSizedArrays",
"FixedSizeArray",
"Histogram",
"HistogramAxis",
"LGDO",
"Scalar",
"Struct",
Expand Down
Loading

0 comments on commit bbdd101

Please sign in to comment.