Skip to content

Commit

Permalink
Refactor _sanitize_dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Sep 2, 2024
1 parent d8cd78e commit 7fcd993
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions h5grove/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,21 @@ def get_type_metadata(type_id: h5py.h5t.TypeID) -> TypeMetadata:


def _sanitize_dtype(dtype: np.dtype) -> np.dtype:
"""Sanitize numpy dtype to one with a matching typed array in modern JavaScript.
:raises ValueError: If trying to sanitize a non-numeric numpy dtype
"""
if dtype.kind not in ("f", "i", "u", "b"):
raise ValueError(f"Unsupported numpy dtype `{dtype}`. Expected numeric dtype.")

# Convert to little endian
result = dtype.newbyteorder("<")
"""Sanitize numpy dtype to one with a matching typed array in modern JavaScript."""

# Convert float16 to float32
if result.kind == "f" and result.itemsize < 4:
if dtype.kind == "f" and dtype.itemsize < 4:
return np.dtype("<f4")

# Convert float128 to float64 (unavoidable loss of precision)
if result.kind == "f" and result.itemsize > 8:
if dtype.kind == "f" and dtype.itemsize > 8:
return np.dtype("<f8")

return result
# Convert big-endian to little-endian
if dtype.byteorder == '>':
return dtype.newbyteorder("<")

return dtype


T = TypeVar("T", np.ndarray, np.number, np.bool_)
Expand Down

0 comments on commit 7fcd993

Please sign in to comment.