From 7fcd993808e978b87f22cb33d6a6f36b49eb99af Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Mon, 2 Sep 2024 11:22:19 +0200 Subject: [PATCH] Refactor `_sanitize_dtype` --- h5grove/utils.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/h5grove/utils.py b/h5grove/utils.py index 65b9093..8f38990 100644 --- a/h5grove/utils.py +++ b/h5grove/utils.py @@ -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(" 8: + if dtype.kind == "f" and dtype.itemsize > 8: return np.dtype("': + return dtype.newbyteorder("<") + + return dtype T = TypeVar("T", np.ndarray, np.number, np.bool_)