Skip to content

Commit

Permalink
Merge pull request #28 from legend-exp/pydataobj-update
Browse files Browse the repository at this point in the history
Update to latest legend-pydataobj changes
  • Loading branch information
gipert authored Jan 2, 2024
2 parents 24b2010 + 7521d7f commit 48ed314
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 373 deletions.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ classifiers =
[options]
packages = find:
install_requires =
dspeed>=1.1
dspeed@git+https://github.com/legend-exp/dspeed@main
h5py>=3.2.0
hdf5plugin
legend-pydataobj>=1.4.1
legend-pydataobj>=1.5.0a1
numpy>=1.21
pyfcutils
tqdm>=4.27
Expand Down
4 changes: 2 additions & 2 deletions src/daq2lh5/buffer_processor/buffer_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def buffer_processor(rb: RawBuffer) -> Table:
``"compression": {"lgdo": "codec_name" [, ...]}`` `(dict)`
Updates the `compression` attribute of `lgdo` to `codec_name`. The
attribute sets the compression algorithm applied by
:func:`~.lgdo.lh5_store.LH5Store.read_object` before writing `lgdo` to
:func:`~.lgdo.lh5.LH5Store.read` before writing `lgdo` to
disk. Can be used to apply custom waveform compression algorithms from
:mod:`lgdo.compression`.
``"hdf5_settings": {"lgdo": { <HDF5 settings> }}`` `(dict)`
Updates the `hdf5_settings` attribute of `lgdo`. The attribute sets the
HDF5 dataset options applied by
:func:`~.lgdo.lh5_store.LH5Store.read_object` before writing `lgdo` to
:func:`~.lgdo.lh5.LH5Store.read` before writing `lgdo` to
disk.
Parameters
Expand Down
24 changes: 10 additions & 14 deletions src/daq2lh5/buffer_processor/lh5_buffer_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import h5py
import lgdo
from lgdo import LH5Store
from lgdo import lh5

from ..buffer_processor.buffer_processor import buffer_processor
from ..raw_buffer import RawBuffer, RawBufferLibrary
Expand Down Expand Up @@ -54,36 +54,34 @@ def lh5_buffer_processor(
"""

# Initialize the input raw file
raw_store = LH5Store()
raw_store = lh5.LH5Store()
lh5_file = raw_store.gimme_file(lh5_raw_file_in, "r")
if lh5_file is None:
raise ValueError(f"input file not found: {lh5_raw_file_in}")
return

# List the groups in the raw file
lh5_groups = lgdo.ls(lh5_raw_file_in)
lh5_groups = lh5.ls(lh5_raw_file_in)
lh5_tables = []

# check if group points to raw data; sometimes 'raw' is nested, e.g g024/raw
for tb in lh5_groups:
# Make sure that the upper level key isn't a dataset
if isinstance(lh5_file[tb], h5py.Dataset):
lh5_tables.append(f"{tb}")
elif "raw" not in tb and lgdo.ls(lh5_file, f"{tb}/raw"):
elif "raw" not in tb and lh5.ls(lh5_file, f"{tb}/raw"):
lh5_tables.append(f"{tb}/raw")
# Look one layer deeper for a :meth:`lgdo.Table` if necessary
elif lgdo.ls(lh5_file, f"{tb}"):
elif lh5.ls(lh5_file, f"{tb}"):
# Check to make sure that this isn't a table itself
maybe_table, _ = raw_store.read_object(f"{tb}", lh5_file)
maybe_table, _ = raw_store.read(f"{tb}", lh5_file)
if isinstance(maybe_table, lgdo.Table):
lh5_tables.append(f"{tb}")
del maybe_table
# otherwise, go deeper
else:
for sub_table in lgdo.ls(lh5_file, f"{tb}"):
maybe_table, _ = raw_store.read_object(
f"{tb}/{sub_table}", lh5_file
)
for sub_table in lh5.ls(lh5_file, f"{tb}"):
maybe_table, _ = raw_store.read(f"{tb}/{sub_table}", lh5_file)
if isinstance(maybe_table, lgdo.Table):
lh5_tables.append(f"{tb}/{sub_table}")
del maybe_table
Expand Down Expand Up @@ -114,7 +112,7 @@ def lh5_buffer_processor(

# Write everything in the raw file to the new file, check for proc_spec under either the group name, out_name, or the name
for tb in lh5_tables:
lgdo_obj, _ = raw_store.read_object(f"{tb}", lh5_file)
lgdo_obj, _ = raw_store.read(f"{tb}", lh5_file)

# Find the out_name.
# If the top level group has an lgdo table in it, then the out_name is group
Expand Down Expand Up @@ -198,6 +196,4 @@ def lh5_buffer_processor(
pass

# Write the (possibly processed) lgdo_obj to a file
raw_store.write_object(
lgdo_obj, out_name, lh5_file=proc_file_name, group=group_name
)
raw_store.write(lgdo_obj, out_name, lh5_file=proc_file_name, group=group_name)
6 changes: 3 additions & 3 deletions src/daq2lh5/build_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import os
import time

import lgdo
import numpy as np
from lgdo import lh5
from tqdm.auto import tqdm

from .compass.compass_streamer import CompassStreamer
Expand Down Expand Up @@ -77,7 +77,7 @@ def build_raw(
hdf5_settings
keyword arguments (as a dict) forwarded to
:meth:`~.lgdo.lh5_store.LH5Store.write_object`.
:meth:`~.lgdo.lh5.LH5Store.write`.
**kwargs
sent to :class:`.RawBufferLibrary` generation as `kw_dict` argument.
Expand Down Expand Up @@ -224,7 +224,7 @@ def build_raw(
os.remove(out_file_glob[0])

# Write header data
lh5_store = lgdo.LH5Store(keep_open=True)
lh5_store = lh5.LH5Store(keep_open=True)
write_to_lh5_and_clear(header_data, lh5_store, **hdf5_settings)

# Now loop through the data
Expand Down
22 changes: 10 additions & 12 deletions src/daq2lh5/data_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
"""
from __future__ import annotations

from typing import Union

import lgdo
import numpy as np
from lgdo import LH5Store

LGDO = Union[lgdo.Scalar, lgdo.Struct, lgdo.Array, lgdo.VectorOfVectors]
from lgdo import LGDO
from lgdo.lh5 import LH5Store


class DataDecoder:
Expand All @@ -18,15 +15,16 @@ class DataDecoder:
Most decoders will repeatedly decode the same set of values from each
packet. The values that get decoded need to be described by a dict stored
in `self.decoded_values` that helps determine how to set up the buffers and
write them to file as :class:`~.lgdo.LGDO`\ s. :class:`~.lgdo.table.Table`\ s
are made whose columns correspond to the elements of `decoded_values`, and
packet data gets pushed to the end of the table one row at a time.
write them to file as :class:`~.lgdo.types.lgdo.LGDO`\ s.
:class:`~.lgdo.types.table.Table`\ s are made whose columns correspond to
the elements of `decoded_values`, and packet data gets pushed to the end of
the table one row at a time.
Any key-value entry in a configuration dictionary attached to an element
of `decoded_values` is typically interpreted as an attribute to be attached
to the corresponding LGDO. This feature can be for example exploited to
specify HDF5 dataset settings used by
:meth:`~.lgdo.lh5_store.LH5Store.write_object` to write LGDOs to disk.
:meth:`~.lgdo.lh5.LH5Store.write` to write LGDOs to disk.
For example ::
Expand Down Expand Up @@ -119,7 +117,7 @@ def make_lgdo(self, key: int | str = None, size: int = None) -> LGDO:
"""Make an LGDO for this :class:`DataDecoder` to fill.
This default version of this function allocates a
:class:`~.lgdo.table.Table` using the `decoded_values` for key. If a
:class:`~.lgdo.types.table.Table` using the `decoded_values` for key. If a
different type of LGDO object is required for this decoder, overload
this function.
Expand Down Expand Up @@ -207,7 +205,7 @@ def make_lgdo(self, key: int | str = None, size: int = None) -> LGDO:
continue

# Parse datatype for remaining lgdos
datatype, shape, elements = lgdo.lgdo_utils.parse_datatype(datatype)
datatype, shape, elements = lgdo.lh5.utils.parse_datatype(datatype)

# ArrayOfEqualSizedArrays
if datatype == "array_of_equalsized_arrays":
Expand Down Expand Up @@ -258,7 +256,7 @@ def write_out_garbage(
n_rows = self.garbage_table.loc
if n_rows == 0:
return
lh5_store.write_object(
lh5_store.write(
self.garbage_table, "garbage", filename, group, n_rows=n_rows, append=True
)
self.garbage_table.clear()
Expand Down
2 changes: 1 addition & 1 deletion src/daq2lh5/fc/fc_config_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FCConfigDecoder(DataDecoder):
>>> decoder = FCConfigDecoder()
>>> config = decoder.decode_config(fc)
>>> type(config)
lgdo.struct.Struct
lgdo.types.struct.Struct
"""

def __init__(self, *args, **kwargs) -> None:
Expand Down
18 changes: 8 additions & 10 deletions src/daq2lh5/raw_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,19 @@
from __future__ import annotations

import os
from typing import Union

import lgdo
from lgdo import LH5Store
from lgdo import LGDO
from lgdo.lh5 import LH5Store

from .buffer_processor.buffer_processor import buffer_processor

LGDO = Union[lgdo.Scalar, lgdo.Struct, lgdo.Array, lgdo.VectorOfVectors]


class RawBuffer:
r"""Base class to represent a buffer of raw data.
A :class:`RawBuffer` is in essence a an LGDO object (typically a
:class:`~.lgdo.table.Table`) to which decoded data will be written, along
:class:`~.lgdo.types.table.Table`) to which decoded data will be written, along
with some meta-data distinguishing what data goes into it, and where the
LGDO gets written out. Also holds on to the current location in the buffer
for writing.
Expand All @@ -88,7 +86,7 @@ class RawBuffer:
----------
lgdo
the LGDO used as the actual buffer. Typically a
:class:`~.lgdo.table.Table`. Set to ``None`` upon creation so that the
:class:`~.lgdo.types.table.Table`. Set to ``None`` upon creation so that the
user or a decoder can initialize it later.
key_list
a list of keys (e.g. channel numbers) identifying data to be written
Expand All @@ -107,7 +105,7 @@ class RawBuffer:
proc_spec
a dictionary containing the following:
- a DSP config file, passed as a dictionary, or as a path to a JSON file
- an array containing: the name of an :class:`~.lgdo` object stored in the :class:`.RawBuffer` to be sliced,
- an array containing: the name of an LGDO object stored in the :class:`.RawBuffer` to be sliced,
the start and end indices of the slice, and the new name for the sliced object
- a dictionary of fields to drop
- a dictionary of new fields and their return datatype
Expand Down Expand Up @@ -440,11 +438,11 @@ def write_to_lh5_and_clear(
files (saves some time opening / closing files).
**kwargs
keyword-arguments forwarded to
:meth:`.lgdo.lh5_store.LH5Store.write_object`.
:meth:`.lgdo.lh5.LH5Store.write`.
See Also
--------
.lgdo.lh5_store.LH5Store.write_object
.lgdo.lh5.LH5Store.write
"""
if lh5_store is None:
lh5_store = lgdo.LH5Store()
Expand All @@ -470,7 +468,7 @@ def write_to_lh5_and_clear(

# write if requested...
if filename != "":
lh5_store.write_object(
lh5_store.write(
lgdo_to_write,
rb.out_name,
filename,
Expand Down
Loading

0 comments on commit 48ed314

Please sign in to comment.