|
| 1 | +from dataclasses import dataclass |
| 2 | +from struct import pack |
| 3 | +from typing import List, NamedTuple, Tuple, Union |
| 4 | + |
| 5 | +import flatbuffers |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +import streaming_data_types.fbschemas.dataarray_da00.da00_Variable as VariableBuffer |
| 9 | +from streaming_data_types.fbschemas.dataarray_da00 import da00_DataArray |
| 10 | +from streaming_data_types.fbschemas.dataarray_da00.da00_dtype import da00_dtype |
| 11 | +from streaming_data_types.utils import check_schema_identifier |
| 12 | + |
| 13 | +FILE_IDENTIFIER = b"da00" |
| 14 | + |
| 15 | + |
| 16 | +def get_dtype(data: Union[np.ndarray, str, float, int]): |
| 17 | + if isinstance(data, np.ndarray): |
| 18 | + type_map = { |
| 19 | + np.dtype(x): d |
| 20 | + for x, d in ( |
| 21 | + ("int8", da00_dtype.int8), |
| 22 | + ("int16", da00_dtype.int16), |
| 23 | + ("int32", da00_dtype.int32), |
| 24 | + ("int64", da00_dtype.int64), |
| 25 | + ("uint8", da00_dtype.uint8), |
| 26 | + ("uint16", da00_dtype.uint16), |
| 27 | + ("uint32", da00_dtype.uint32), |
| 28 | + ("uint64", da00_dtype.uint64), |
| 29 | + ("float32", da00_dtype.float32), |
| 30 | + ("float64", da00_dtype.float64), |
| 31 | + ) |
| 32 | + } |
| 33 | + return type_map[data.dtype] |
| 34 | + if isinstance(data, str): |
| 35 | + return da00_dtype.c_string |
| 36 | + if isinstance(data, float): |
| 37 | + return da00_dtype.float64 |
| 38 | + if isinstance(data, int): |
| 39 | + return da00_dtype.int64 |
| 40 | + raise RuntimeError(f"Unsupported data type {type(data)} in get_dtype") |
| 41 | + |
| 42 | + |
| 43 | +def to_buffer(data: Union[np.ndarray, str, float, int]): |
| 44 | + if isinstance(data, np.ndarray): |
| 45 | + return data |
| 46 | + if isinstance(data, str): |
| 47 | + return np.frombuffer(data.encode(), np.uint8) |
| 48 | + if isinstance(data, int): |
| 49 | + return np.frombuffer(pack("q", data), np.uint8) |
| 50 | + if isinstance(data, float): |
| 51 | + return np.frombuffer(pack("d", data), np.uint8) |
| 52 | + raise RuntimeError(f"Unsupported data type {type(data)} in to_buffer") |
| 53 | + |
| 54 | + |
| 55 | +def from_buffer(fb_array) -> np.ndarray: |
| 56 | + """Convert a flatbuffer array into the correct type""" |
| 57 | + raw_data = fb_array.DataAsNumpy() |
| 58 | + type_map = { |
| 59 | + d: np.dtype(x) |
| 60 | + for x, d in ( |
| 61 | + ("int8", da00_dtype.int8), |
| 62 | + ("int16", da00_dtype.int16), |
| 63 | + ("int32", da00_dtype.int32), |
| 64 | + ("int64", da00_dtype.int64), |
| 65 | + ("uint8", da00_dtype.uint8), |
| 66 | + ("uint16", da00_dtype.uint16), |
| 67 | + ("uint32", da00_dtype.uint32), |
| 68 | + ("uint64", da00_dtype.uint64), |
| 69 | + ("float32", da00_dtype.float32), |
| 70 | + ("float64", da00_dtype.float64), |
| 71 | + ) |
| 72 | + } |
| 73 | + dtype = fb_array.DataType() |
| 74 | + if da00_dtype.c_string == dtype: |
| 75 | + return raw_data.tobytes().decode() |
| 76 | + return raw_data.view(type_map[fb_array.DataType()]) |
| 77 | + |
| 78 | + |
| 79 | +def create_optional_string(builder, string: Union[str, None]): |
| 80 | + return None if string is None else builder.CreateString(string) |
| 81 | + |
| 82 | + |
| 83 | +@dataclass |
| 84 | +class Variable: |
| 85 | + name: str |
| 86 | + data: Union[np.ndarray, str] |
| 87 | + axes: Union[List[str], None] = None |
| 88 | + shape: Union[Tuple[int, ...], None] = None |
| 89 | + unit: Union[str, None] = None |
| 90 | + label: Union[str, None] = None |
| 91 | + source: Union[str, None] = None |
| 92 | + |
| 93 | + def __post_init__(self): |
| 94 | + # Calculate the shape when used, e.g., interactively |
| 95 | + # -- but allow to read it back from the buffered object too |
| 96 | + if self.axes is None: |
| 97 | + self.axes = [] |
| 98 | + if self.shape is None: |
| 99 | + self.shape = to_buffer(self.data).shape |
| 100 | + |
| 101 | + def __eq__(self, other): |
| 102 | + if not isinstance(other, Variable): |
| 103 | + return False |
| 104 | + same_data = type(self.data) == type(other.data) # noqa: E721 |
| 105 | + if isinstance(self.data, np.ndarray): |
| 106 | + same_data &= np.array_equal(self.data, other.data) |
| 107 | + else: |
| 108 | + same_data &= self.data == other.data |
| 109 | + same_axes = len(self.axes) == len(other.axes) and all( |
| 110 | + a == b for a, b in zip(self.axes, other.axes) |
| 111 | + ) |
| 112 | + return ( |
| 113 | + same_data |
| 114 | + and same_axes |
| 115 | + and self.name == other.name |
| 116 | + and self.unit == other.unit |
| 117 | + and self.label == other.label |
| 118 | + and self.source == other.source |
| 119 | + and self.shape == other.shape |
| 120 | + ) |
| 121 | + |
| 122 | + def pack(self, builder): |
| 123 | + source_offset = create_optional_string(builder, self.source) |
| 124 | + label_offset = create_optional_string(builder, self.label) |
| 125 | + unit_offset = create_optional_string(builder, self.unit) |
| 126 | + name_offset = builder.CreateString(self.name) |
| 127 | + buf = to_buffer(self.data) |
| 128 | + shape_offset = builder.CreateNumpyVector(np.asarray(buf.shape)) |
| 129 | + data_offset = builder.CreateNumpyVector(buf.flatten().view(np.uint8)) |
| 130 | + |
| 131 | + temp_axes = [builder.CreateString(x) for x in self.axes] |
| 132 | + VariableBuffer.StartAxesVector(builder, len(temp_axes)) |
| 133 | + for dim in reversed(temp_axes): |
| 134 | + builder.PrependUOffsetTRelative(dim) |
| 135 | + axes_offset = builder.EndVector() |
| 136 | + |
| 137 | + VariableBuffer.Start(builder) |
| 138 | + VariableBuffer.AddName(builder, name_offset) |
| 139 | + if unit_offset is not None: |
| 140 | + VariableBuffer.AddUnit(builder, unit_offset) |
| 141 | + if label_offset is not None: |
| 142 | + VariableBuffer.AddLabel(builder, label_offset) |
| 143 | + if source_offset is not None: |
| 144 | + VariableBuffer.AddSource(builder, source_offset) |
| 145 | + VariableBuffer.AddDataType(builder, get_dtype(self.data)) |
| 146 | + VariableBuffer.AddAxes(builder, axes_offset) |
| 147 | + VariableBuffer.AddShape(builder, shape_offset) |
| 148 | + VariableBuffer.AddData(builder, data_offset) |
| 149 | + return VariableBuffer.End(builder) |
| 150 | + |
| 151 | + @classmethod |
| 152 | + def unpack(cls, b: VariableBuffer): |
| 153 | + data = from_buffer(b) |
| 154 | + axes = [b.Axes(i).decode() for i in range(b.AxesLength())] |
| 155 | + if len(axes): |
| 156 | + data = data.reshape(b.ShapeAsNumpy()) |
| 157 | + elif b.DataType() != da00_dtype.c_string and np.prod(data.shape) == 1: |
| 158 | + data = data.item() |
| 159 | + |
| 160 | + unit = None if b.Unit() is None else b.Unit().decode() |
| 161 | + label = None if b.Label() is None else b.Label().decode() |
| 162 | + source = None if b.Source() is None else b.Source().decode() |
| 163 | + name = b.Name().decode() |
| 164 | + # the buffered shape is NOT the shape of the numpy array in all cases |
| 165 | + buffered_shape = tuple(b.ShapeAsNumpy()) |
| 166 | + return cls( |
| 167 | + name=name, |
| 168 | + unit=unit, |
| 169 | + label=label, |
| 170 | + source=source, |
| 171 | + axes=axes, |
| 172 | + data=data, |
| 173 | + shape=buffered_shape, |
| 174 | + ) |
| 175 | + |
| 176 | + |
| 177 | +def insert_variable_list(starter, builder, objects: List[Variable]): |
| 178 | + temp = [obj.pack(builder) for obj in objects] |
| 179 | + starter(builder, len(temp)) |
| 180 | + for obj in reversed(temp): |
| 181 | + builder.PrependUOffsetTRelative(obj) |
| 182 | + return builder.EndVector() |
| 183 | + |
| 184 | + |
| 185 | +def serialise_da00( |
| 186 | + source_name: str, |
| 187 | + timestamp_ns: int, |
| 188 | + data: List[Variable], |
| 189 | +) -> bytes: |
| 190 | + if not data: |
| 191 | + raise RuntimeError("data must contain at least one Variable") |
| 192 | + builder = flatbuffers.Builder(1024) |
| 193 | + builder.ForceDefaults(True) |
| 194 | + |
| 195 | + data_offset = insert_variable_list(da00_DataArray.StartDataVector, builder, data) |
| 196 | + source_name_offset = builder.CreateString(source_name) |
| 197 | + |
| 198 | + # Build the actual buffer |
| 199 | + da00_DataArray.Start(builder) |
| 200 | + da00_DataArray.AddSourceName(builder, source_name_offset) |
| 201 | + da00_DataArray.AddTimestamp(builder, timestamp_ns) |
| 202 | + da00_DataArray.AddData(builder, data_offset) |
| 203 | + array_message = da00_DataArray.End(builder) |
| 204 | + |
| 205 | + builder.Finish(array_message, file_identifier=FILE_IDENTIFIER) |
| 206 | + return bytes(builder.Output()) |
| 207 | + |
| 208 | + |
| 209 | +da00_DataArray_t = NamedTuple( |
| 210 | + "da00_DataArray", |
| 211 | + ( |
| 212 | + ("source_name", str), |
| 213 | + ("timestamp_ns", int), |
| 214 | + ("data", List[Variable]), |
| 215 | + ), |
| 216 | +) |
| 217 | + |
| 218 | + |
| 219 | +def deserialise_da00(buffer: Union[bytearray, bytes]) -> da00_DataArray: |
| 220 | + check_schema_identifier(buffer, FILE_IDENTIFIER) |
| 221 | + |
| 222 | + da00 = da00_DataArray.da00_DataArray.GetRootAs(buffer, offset=0) |
| 223 | + data = [Variable.unpack(da00.Data(j)) for j in range(da00.DataLength())] |
| 224 | + |
| 225 | + return da00_DataArray_t( |
| 226 | + source_name=da00.SourceName().decode(), |
| 227 | + timestamp_ns=da00.Timestamp(), |
| 228 | + data=data, |
| 229 | + ) |
0 commit comments