Skip to content

Commit

Permalink
Merge pull request #124 from ManuelHu/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
gipert authored Dec 8, 2024
2 parents 94cb680 + b44409d commit 96b68d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lgdo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def get_element_type(obj: object) -> str:
return "complex"
if kind in ["S", "U"]:
return "string"
if (
kind == "O"
and dt.metadata is not None
and dt.metadata.get("vlen", None) in (str, bytes)
):
# variable length strings in HDF5 are read as numpy object arrays in h5py.
# see also h5py.check_vlen_dtype.
return "string"

# couldn't figure it out
msg = "cannot determine lgdo element_type for object of type"
Expand Down
5 changes: 5 additions & 0 deletions tests/test_lgdo_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from __future__ import annotations

import h5py
import numpy as np

from lgdo import utils


def test_get_element_type():
# variable length HD5 string datatype.
h5py_str_dtype = h5py.string_dtype(encoding="ascii", length=None)

objs = [
("hi", "string"),
(True, "bool"),
Expand All @@ -16,6 +20,7 @@ def test_get_element_type():
(1 + 1j, "complex"),
(b"hi", "string"),
(np.array(["hi"]), "string"),
(np.array([b"hi"], h5py_str_dtype), "string"),
]

for obj, name in objs:
Expand Down

0 comments on commit 96b68d5

Please sign in to comment.