|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import h5py |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +from sift_client.sift_types.channel import ChannelDataType |
| 9 | +from sift_client.sift_types.data_import import Hdf5DataColumn, Hdf5ImportConfig, TimeFormat |
| 10 | + |
| 11 | +# Common HDF5 attribute names used to detect channel metadata. |
| 12 | +_NAME_ATTRS = ["Name", "name", "Title", "title", "Sensor", "sensor", "Channel", "channel"] |
| 13 | +_UNIT_ATTRS = ["Unit", "unit", "Units", "units"] |
| 14 | +_DESCRIPTION_ATTRS = ["Description", "description"] |
| 15 | + |
| 16 | +_NUMPY_TO_SIFT: dict[type, ChannelDataType] = { |
| 17 | + np.bool_: ChannelDataType.BOOL, |
| 18 | + np.int8: ChannelDataType.INT_32, |
| 19 | + np.int16: ChannelDataType.INT_32, |
| 20 | + np.int32: ChannelDataType.INT_32, |
| 21 | + np.int64: ChannelDataType.INT_64, |
| 22 | + np.uint8: ChannelDataType.UINT_32, |
| 23 | + np.uint16: ChannelDataType.UINT_32, |
| 24 | + np.uint32: ChannelDataType.UINT_32, |
| 25 | + np.uint64: ChannelDataType.UINT_64, |
| 26 | + np.float32: ChannelDataType.FLOAT, |
| 27 | + np.float64: ChannelDataType.DOUBLE, |
| 28 | + np.datetime64: ChannelDataType.INT_64, |
| 29 | + np.complex64: ChannelDataType.FLOAT, |
| 30 | + np.complex128: ChannelDataType.DOUBLE, |
| 31 | + np.str_: ChannelDataType.STRING, |
| 32 | + # HDF5/TDMS fixed-length strings are stored as np.bytes_; use STRING, not |
| 33 | + # BYTES (np.void below handles truly opaque binary data). |
| 34 | + np.bytes_: ChannelDataType.STRING, |
| 35 | + # Numpy uses object dtype for variable-length strings; TDMS/HDF5 files |
| 36 | + # cannot produce non-string object arrays. |
| 37 | + np.object_: ChannelDataType.STRING, |
| 38 | + np.void: ChannelDataType.BYTES, |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +def _detect_attr(dataset: h5py.Dataset, candidates: list[str], default: str = "") -> str: |
| 43 | + """Return the first matching HDF5 attribute value, or *default*.""" |
| 44 | + possible = [dataset.attrs.get(attr) for attr in candidates if dataset.attrs.get(attr)] |
| 45 | + return str(possible[0]) if possible else default |
| 46 | + |
| 47 | + |
| 48 | +def _numpy_to_sift_type(dtype: np.dtype) -> ChannelDataType: |
| 49 | + """Map a numpy dtype to a Sift ChannelDataType.""" |
| 50 | + sift_type = _NUMPY_TO_SIFT.get(dtype.type) |
| 51 | + if sift_type is None: |
| 52 | + raise ValueError(f"Unsupported numpy dtype: {dtype}") |
| 53 | + return sift_type |
| 54 | + |
| 55 | + |
| 56 | +def detect_hdf5_config(file_path: str | Path) -> Hdf5ImportConfig: |
| 57 | + """Detect an HDF5 import config by inspecting the file's datasets. |
| 58 | +
|
| 59 | + Traverses the HDF5 file and produces (time dataset, value dataset) pairs. |
| 60 | + For compound datasets with multiple fields, the first field is assumed to |
| 61 | + be time and remaining fields become value channels. For simple datasets, |
| 62 | + a root-level ``time`` dataset is used if present. |
| 63 | + """ |
| 64 | + path = Path(file_path) |
| 65 | + |
| 66 | + with h5py.File(path, "r") as h5file: |
| 67 | + columns: list[Hdf5DataColumn] = [] |
| 68 | + seen_names: set[str] = set() |
| 69 | + has_root_time = "time" in h5file |
| 70 | + |
| 71 | + def _visit(dataset_name: str, obj: object) -> None: |
| 72 | + if not isinstance(obj, h5py.Dataset): |
| 73 | + return |
| 74 | + |
| 75 | + # Skip root "time" dataset — it's used as the time source, not a value channel. |
| 76 | + if dataset_name == "time" and obj.parent == h5file: |
| 77 | + return |
| 78 | + |
| 79 | + n_fields = len(obj.dtype.names) if obj.dtype.names else 0 |
| 80 | + |
| 81 | + if n_fields > 1: |
| 82 | + # Compound type: first field is time, remaining are value channels. |
| 83 | + for value_index in range(1, n_fields): |
| 84 | + channel_name = _detect_attr(obj, _NAME_ATTRS, dataset_name) |
| 85 | + if channel_name in seen_names: |
| 86 | + channel_name = f"{channel_name}.{dataset_name}.{value_index}" |
| 87 | + |
| 88 | + columns.append( |
| 89 | + Hdf5DataColumn( |
| 90 | + name=channel_name, |
| 91 | + data_type=_numpy_to_sift_type(obj.dtype[value_index]), |
| 92 | + units=_detect_attr(obj, _UNIT_ATTRS), |
| 93 | + description=_detect_attr(obj, _DESCRIPTION_ATTRS), |
| 94 | + time_dataset=dataset_name, |
| 95 | + value_dataset=dataset_name, |
| 96 | + time_index=0, |
| 97 | + value_index=0, |
| 98 | + time_field=obj.dtype.names[0], |
| 99 | + value_field=obj.dtype.names[value_index], |
| 100 | + ) |
| 101 | + ) |
| 102 | + seen_names.add(channel_name) |
| 103 | + |
| 104 | + elif n_fields in (0, 1): |
| 105 | + # Single column. Use root "time" as time dataset if available. |
| 106 | + channel_name = _detect_attr(obj, _NAME_ATTRS, dataset_name) |
| 107 | + if channel_name in seen_names: |
| 108 | + channel_name = f"{channel_name}.{dataset_name}" |
| 109 | + |
| 110 | + columns.append( |
| 111 | + Hdf5DataColumn( |
| 112 | + name=channel_name, |
| 113 | + data_type=_numpy_to_sift_type(obj.dtype), |
| 114 | + units=_detect_attr(obj, _UNIT_ATTRS), |
| 115 | + description=_detect_attr(obj, _DESCRIPTION_ATTRS), |
| 116 | + time_dataset="time" if has_root_time else "", |
| 117 | + value_dataset=dataset_name, |
| 118 | + time_index=0, |
| 119 | + value_index=0, |
| 120 | + ) |
| 121 | + ) |
| 122 | + seen_names.add(channel_name) |
| 123 | + |
| 124 | + h5file.visititems(_visit) |
| 125 | + |
| 126 | + return Hdf5ImportConfig( |
| 127 | + asset_name="", |
| 128 | + time_format=TimeFormat.ABSOLUTE_UNIX_NANOSECONDS, |
| 129 | + data=columns, |
| 130 | + ) |
0 commit comments