-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathio.py
More file actions
274 lines (239 loc) · 9.33 KB
/
io.py
File metadata and controls
274 lines (239 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from datetime import datetime
from enum import IntEnum
from os import PathLike
from typing import Any, BinaryIO, Optional, Union
import numpy as np
import numpy.typing as npt
import pandas as pd
from pandas._typing import Axes # pyright: ignore[reportPrivateImportUsage]
from harp.typing import _BufferLike, _FileLike
REFERENCE_EPOCH = datetime(1904, 1, 1)
"""The reference epoch for UTC harp time."""
class MessageType(IntEnum):
"""Specifies the type of a Harp message."""
NA = 0
READ = 1
WRITE = 2
EVENT = 3
_SECONDS_PER_TICK = 32e-6
_PAYLOAD_TIMESTAMP_MASK = 0x10
_messagetypes = [type.name for type in MessageType]
_dtypefrompayloadtype = {
1: np.dtype(np.uint8),
2: np.dtype(np.uint16),
4: np.dtype(np.uint32),
8: np.dtype(np.uint64),
129: np.dtype(np.int8),
130: np.dtype(np.int16),
132: np.dtype(np.int32),
136: np.dtype(np.int64),
68: np.dtype(np.float32),
}
_payloadtypefromdtype = {v: k for k, v in _dtypefrompayloadtype.items()}
def read(
file_or_buf: Union[_FileLike, _BufferLike],
address: Optional[int] = None,
dtype: Optional[np.dtype] = None,
length: Optional[int] = None,
columns: Optional[Axes] = None,
epoch: Optional[datetime] = None,
keep_type: bool = False,
):
"""Read single-register Harp data from the specified file or buffer.
Parameters
----------
file_or_buf
File path, open file object, or buffer containing binary data from
a single device register.
address
Expected register address. If specified, the address of
the first message is used for validation.
dtype
Expected data type of the register payload. If specified, the
payload type of the first message is used for validation.
length
Expected number of elements in register payload. If specified, the
payload length of the first message is used for validation.
columns
The optional column labels to use for the data values.
epoch
Reference datetime at which time zero begins. If specified,
the result data frame will have a datetime index.
keep_type
Specifies whether to include a column with the message type.
Returns
-------
A pandas data frame containing message data, sorted by time.
"""
if isinstance(file_or_buf, (str, PathLike, BinaryIO)) or hasattr(file_or_buf, "readinto"):
# TODO: in the below we ignore the type as otherwise
# we have no way to runtime check _IOProtocol
data = np.fromfile(file_or_buf, dtype=np.uint8) # type: ignore
else:
data = np.frombuffer(file_or_buf, dtype=np.uint8)
if len(data) == 0:
return pd.DataFrame(
columns=columns,
index=pd.DatetimeIndex([], name="Time")
if epoch
else pd.Index([], dtype=np.float64, name="Time"),
)
if address is not None and address != data[2]:
raise ValueError(f"expected address {address} but got {data[2]}")
index = None
stride = int(data[1] + 2)
nrows = len(data) // stride
payloadtype = data[4]
payloadoffset = 5
if payloadtype & _PAYLOAD_TIMESTAMP_MASK != 0:
seconds = np.ndarray(nrows, dtype=np.uint32, buffer=data, offset=payloadoffset, strides=stride)
payloadoffset += 4
micros = np.ndarray(nrows, dtype=np.uint16, buffer=data, offset=payloadoffset, strides=stride)
payloadoffset += 2
time = micros * _SECONDS_PER_TICK + seconds
payloadtype = payloadtype & ~np.uint8(_PAYLOAD_TIMESTAMP_MASK)
if epoch is not None:
time = epoch + pd.to_timedelta(time, "s") # type: ignore
index = pd.Series(time)
index.name = "Time"
payloadsize = stride - payloadoffset - 1
payloadtype = _dtypefrompayloadtype[payloadtype]
if dtype is not None and dtype != payloadtype:
raise ValueError(f"expected payload type {dtype} but got {payloadtype}")
elementsize = payloadtype.itemsize
payloadshape = (nrows, payloadsize // elementsize)
if length is not None and length != payloadshape[1]:
raise ValueError(f"expected payload length {length} but got {payloadshape[1]}")
payload = np.ndarray(
payloadshape,
dtype=payloadtype,
buffer=data,
offset=payloadoffset,
strides=(stride, elementsize),
)
result = pd.DataFrame(payload, index=index, columns=columns)
if keep_type:
msgtype = np.ndarray(nrows, dtype=np.uint8, buffer=data, offset=0, strides=stride)
msgtype = pd.Categorical.from_codes(msgtype, categories=_messagetypes) # type: ignore
result[MessageType.__name__] = msgtype
return result
def to_file(
data: pd.DataFrame,
file: _FileLike,
address: int,
dtype: Optional[np.dtype] = None,
length: Optional[int] = None,
port: Optional[int] = None,
epoch: Optional[datetime] = None,
message_type: Optional[MessageType] = None,
):
"""Write single-register Harp data to the specified file.
Parameters
----------
data
Pandas data frame containing message payload.
file
File path, or open file object in which to store binary data from
a single device register.
address
Register address used to identify all formatted Harp messages.
dtype
Data type of the register payload. If specified, all data will
be converted before formatting the binary payload.
length
Expected number of elements in register payload. If specified, the
number of columns in the input data frame is validated.
port
Optional port value used for all formatted Harp messages.
epoch
Reference datetime at which time zero begins. If specified,
the input data frame must have a datetime index.
message_type
Optional message type used for all formatted Harp messages.
If not specified, data must contain a MessageType column.
"""
buffer = to_buffer(data, address, dtype, port, length, epoch, message_type)
buffer.tofile(file)
def to_buffer(
data: pd.DataFrame,
address: int,
dtype: Optional[np.dtype] = None,
length: Optional[int] = None,
port: Optional[int] = None,
epoch: Optional[datetime] = None,
message_type: Optional[MessageType] = None,
) -> npt.NDArray[np.uint8]:
"""Convert single-register Harp data to a flat binary buffer.
Parameters
----------
data
Pandas data frame containing message payload.
address
Register address used to identify all formatted Harp messages.
dtype
Data type of the register payload. If specified, all data will
be converted before formatting the binary payload.
length
Expected number of elements in register payload. If specified, the
number of columns in the input data frame is validated.
port
Optional port value used for all formatted Harp messages.
epoch
Reference datetime at which time zero begins. If specified,
the input data frame must have a datetime index.
message_type
Optional message type used for all formatted Harp messages.
If not specified, data must contain a MessageType column.
Returns
-------
An array object containing message data formatted according
to the Harp binary protocol.
"""
nrows = len(data)
if nrows == 0:
return np.empty(0, dtype=np.uint8)
if MessageType.__name__ in data.columns:
msgtype = data[MessageType.__name__].cat.codes
payload = data[data.columns.drop(MessageType.__name__)].values
elif message_type is not None:
msgtype = message_type
payload = data.values
else:
raise ValueError(f"message type must be specified either in the data or as argument")
time = data.index
is_timestamped = True
if epoch is not None:
if not isinstance(time, pd.DatetimeIndex):
raise ValueError(f"expected datetime index to encode with epoch but got {time.inferred_type}")
time = (time - epoch).total_seconds()
elif isinstance(time, pd.RangeIndex):
is_timestamped = False
if dtype is not None:
payload = payload.astype(dtype, copy=False)
ncols = payload.shape[1]
if length is not None and ncols != length:
raise ValueError(f"expected payload length {length} but got {ncols}")
if port is None:
port = 255
payloadtype = _payloadtypefromdtype[payload.dtype]
payloadlength = ncols * payload.dtype.itemsize
stride = payloadlength + 6
if is_timestamped:
payloadtype |= _PAYLOAD_TIMESTAMP_MASK
stride += 6
buffer = np.empty((nrows, stride), dtype=np.uint8)
buffer[:, 0] = msgtype
buffer[:, 1:5] = [stride - 2, address, port, payloadtype]
payloadoffset = 5
if is_timestamped:
seconds = time.astype(np.uint32)
micros = np.around(((time - seconds) / _SECONDS_PER_TICK).values).astype(np.uint16)
buffer[:, 5:9] = np.ndarray((nrows, 4), dtype=np.uint8, buffer=seconds.values)
buffer[:, 9:11] = np.ndarray((nrows, 2), dtype=np.uint8, buffer=micros)
payloadoffset += 6
payloadstop = payloadoffset + payloadlength
buffer[:, payloadoffset:payloadstop] = np.ndarray(
(nrows, payloadlength), dtype=np.uint8, buffer=np.ascontiguousarray(payload)
)
buffer[:, -1] = np.sum(buffer[:, 0:-1], axis=1, dtype=np.uint8)
return buffer.reshape(-1)