Skip to content

Commit a6465a8

Browse files
committed
Add type hinting and conform to black formatter
1 parent c0b556d commit a6465a8

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

harp/io.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1+
from os import PathLike
2+
from typing import Any, Optional, Union
3+
from pandas._typing import Axes
14
import numpy as np
25
import pandas as pd
36

47
_SECONDS_PER_TICK = 32e6
58
payloadtypes = {
6-
1 : np.dtype(np.uint8),
7-
2 : np.dtype(np.uint16),
8-
4 : np.dtype(np.uint32),
9-
8 : np.dtype(np.uint64),
10-
129 : np.dtype(np.int8),
11-
130 : np.dtype(np.int16),
12-
132 : np.dtype(np.int32),
13-
136 : np.dtype(np.int64),
14-
68 : np.dtype(np.float32)
9+
1: np.dtype(np.uint8),
10+
2: np.dtype(np.uint16),
11+
4: np.dtype(np.uint32),
12+
8: np.dtype(np.uint64),
13+
129: np.dtype(np.int8),
14+
130: np.dtype(np.int16),
15+
132: np.dtype(np.int32),
16+
136: np.dtype(np.int64),
17+
68: np.dtype(np.float32),
1518
}
1619

17-
def read(file: str, columns=None):
18-
'''
20+
21+
def read(
22+
file: Union[str, bytes, PathLike[Any], np._IOProtocol],
23+
columns: Optional[Axes] = None,
24+
):
25+
"""
1926
Read single-register Harp data from the specified file.
20-
21-
:param str file: The path to a Harp binary file containing data from a single device register.
22-
:param str or array-like names: The optional column labels to use for the data values.
27+
28+
:param str file or str or Path: Open file object or filename containing
29+
binary data from a single device register.
30+
:param str or array-like names: The optional column labels to use for
31+
the data values.
2332
:return: A pandas data frame containing harp event data, sorted by time.
24-
'''
33+
"""
2534
data = np.fromfile(file, dtype=np.uint8)
2635
if len(data) == 0:
2736
return pd.DataFrame(
28-
columns=columns,
29-
index=pd.Index([], dtype=np.float64, name='time'))
37+
columns=columns, index=pd.Index([], dtype=np.float64, name="time")
38+
)
3039

3140
stride = data[1] + 2
3241
length = len(data) // stride
@@ -40,8 +49,10 @@ def read(file: str, columns=None):
4049
payload = np.ndarray(
4150
payloadshape,
4251
dtype=payloadtype,
43-
buffer=data, offset=11,
44-
strides=(stride, elementsize))
52+
buffer=data,
53+
offset=11,
54+
strides=(stride, elementsize),
55+
)
4556
time = pd.Series(seconds)
46-
time.name = 'time'
47-
return pd.DataFrame(payload, index=time, columns=columns)
57+
time.name = "time"
58+
return pd.DataFrame(payload, index=time, columns=columns)

0 commit comments

Comments
 (0)