Skip to content

Commit 4a2f9e4

Browse files
authored
Merge pull request #11 from harp-tech/gl-dev
Add support for keeping message type
2 parents f0bf62e + e835f09 commit 4a2f9e4

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

harp/io.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
from enum import IntEnum
12
from os import PathLike
23
from typing import Any, BinaryIO, Optional, Union
34
from pandas._typing import Axes
45
import numpy as np
56
import 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

tests/test_io.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
import numpy as np
3-
from typing import Optional, Type
3+
from typing import Iterable, Optional, Type
44
from contextlib import nullcontext
55
from pytest import mark
66
from pathlib import Path
@@ -14,10 +14,12 @@
1414
class DataFileParam:
1515
path: str
1616
expected_rows: int
17+
expected_cols: Optional[Iterable[str]] = None
1718
expected_address: Optional[int] = None
1819
expected_dtype: Optional[np.dtype] = None
1920
expected_length: Optional[int] = None
2021
expected_error: Optional[Type[BaseException]] = None
22+
keep_type: bool = False
2123

2224
def __post_init__(self):
2325
self.path = datapath / self.path
@@ -43,10 +45,10 @@ def __post_init__(self):
4345
expected_length=2, # actual length is 1
4446
expected_error=ValueError,
4547
),
48+
DataFileParam(path="data/write_0.bin", expected_address=0, expected_rows=4),
4649
DataFileParam(
47-
path="data/write_0.bin",
48-
expected_address=0,
49-
expected_rows=4)
50+
path="data/write_0.bin", expected_address=0, expected_rows=4, keep_type=True
51+
),
5052
]
5153

5254

@@ -59,8 +61,15 @@ def test_read(dataFile: DataFileParam):
5961
address=dataFile.expected_address,
6062
dtype=dataFile.expected_dtype,
6163
length=dataFile.expected_length,
64+
keep_type=dataFile.keep_type,
6265
)
6366
assert len(data) == dataFile.expected_rows
67+
if dataFile.keep_type:
68+
assert "type" in data.columns and data["type"].dtype == "category"
69+
70+
if dataFile.expected_cols is not None:
71+
for col in dataFile.expected_cols:
72+
assert col in data.columns
6473

6574

6675
if __name__ == "__main__":

0 commit comments

Comments
 (0)