1+ from enum import IntEnum
12from os import PathLike
23from typing import Any , BinaryIO , Optional , Union
34from pandas ._typing import Axes
45import numpy as np
56import pandas as pd
67
8+
9+ class MessageType (IntEnum ):
10+ NA = 0
11+ READ = 1
12+ WRITE = 2
13+ EVENT = 3
14+
15+
716_SECONDS_PER_TICK = 32e-6
8- payloadtypes = {
17+ _messagetypes = [type .name for type in MessageType ]
18+ _payloadtypes = {
919 1 : np .dtype (np .uint8 ),
1020 2 : np .dtype (np .uint16 ),
1121 4 : np .dtype (np .uint32 ),
@@ -24,6 +34,7 @@ def read(
2434 dtype : Optional [np .dtype ] = None ,
2535 length : Optional [int ] = None ,
2636 columns : Optional [Axes ] = None ,
37+ keep_type : bool = False ,
2738):
2839 """
2940 Read single-register Harp data from the specified file.
@@ -37,6 +48,7 @@ def read(
3748 :param length: Expected number of elements in register payload. If specified,
3849 the payload length of the first message in the file is used for validation.
3950 :param columns: The optional column labels to use for the data values.
51+ :param keep_type: Specifies whether to include a column with the message type.
4052 :return: A pandas data frame containing message data, sorted by time.
4153 """
4254 data = np .fromfile (file , dtype = np .uint8 )
@@ -68,7 +80,7 @@ def read(
6880 index .name = "time"
6981
7082 payloadsize = stride - payloadoffset - 1
71- payloadtype = payloadtypes [payloadtype ]
83+ payloadtype = _payloadtypes [payloadtype ]
7284 if dtype is not None and dtype != payloadtype :
7385 raise ValueError (f"expected payload type { dtype } but got { payloadtype } " )
7486
@@ -85,4 +97,11 @@ def read(
8597 strides = (stride , elementsize ),
8698 )
8799
88- return pd .DataFrame (payload , index = index , columns = columns )
100+ result = pd .DataFrame (payload , index = index , columns = columns )
101+ if keep_type :
102+ msgtype = np .ndarray (
103+ nrows , dtype = np .uint8 , buffer = data , offset = 0 , strides = stride
104+ )
105+ msgtype = pd .Categorical .from_codes (msgtype , categories = _messagetypes )
106+ result ["type" ] = msgtype
107+ return result
0 commit comments