Skip to content

Commit

Permalink
Added types to read_size_in_bytes; clarified some docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
iguinn committed Oct 29, 2024
1 parent 0f03c82 commit a2ef82e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/lgdo/lh5/_serializers/read/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def read_n_rows(h5o, fname, oname):


def read_size_in_bytes(h5o, fname, oname, field_mask=None):
"""Read number of rows in LH5 object"""
"""Read number size in LH5 object in memory (in B)"""
if not h5py.h5a.exists(h5o, b"datatype"):
msg = "missing 'datatype' attribute"
raise LH5DecodeError(msg, fname, oname)
Expand All @@ -116,11 +116,16 @@ def read_size_in_bytes(h5o, fname, oname, field_mask=None):
lgdotype = datatype.datatype(type_attr)

# scalars are dim-0 datasets
if lgdotype in (types.Scalar, types.Array, types.ArrayOfEqualSizedArrays):
if lgdotype in (
types.Scalar,
types.Array,
types.ArrayOfEqualSizedArrays,
types.FixedSizeArray,
):
return int(np.prod(h5o.shape) * h5o.dtype.itemsize)

# structs don't have rows
if lgdotype is types.Struct:
if lgdotype in (types.Struct, types.Histogram, types.Histogram.Axis):
size = 0
for key in h5o:
obj = h5py.h5o.open(h5o, key)
Expand All @@ -129,7 +134,7 @@ def read_size_in_bytes(h5o, fname, oname, field_mask=None):
return size

# tables should have elements with all the same length
if lgdotype is types.Table:
if lgdotype in (types.Table, types.WaveformTable):
# read out each of the fields
size = 0
if not field_mask:
Expand Down
4 changes: 2 additions & 2 deletions src/lgdo/lh5/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def read_n_rows(self, name: str, lh5_file: str | h5py.File) -> int | None:
return utils.read_n_rows(name, self.gimme_file(lh5_file, "r"))

def read_size_in_bytes(self, name: str, lh5_file: str | h5py.File) -> int:
"""Look up the size (in B) of the object. Will recursively crawl
through all objects in a Struct or Table
"""Look up the size (in B) of the object in memory. Will recursively
crawl through all objects in a Struct or Table
"""
return utils.read_size_in_bytes(name, self.gimme_file(lh5_file, "r"))
2 changes: 1 addition & 1 deletion src/lgdo/lh5/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def read_n_rows(name: str, h5f: str | h5py.File) -> int | None:


def read_size_in_bytes(name: str, h5f: str | h5py.File) -> int | None:
"""Look up the size (in B) in an LGDO object on disk. Will crawl
"""Look up the size (in B) in an LGDO object in memory. Will crawl
recursively through members of a Struct or Table
"""
if not isinstance(h5f, h5py.File):
Expand Down

0 comments on commit a2ef82e

Please sign in to comment.