Skip to content

Commit

Permalink
Use file.readinto instead of np.fromfile when reading data (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreeve committed Oct 25, 2021
1 parent 194d22b commit bfbf854
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions nptdms/base_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,19 @@ def scaler_data(data):


def fromfile(file, dtype, count, *args, **kwargs):
"""Wrapper around np.fromfile to support any file-like object"""

try:
return np.fromfile(file, dtype=dtype, count=count, *args, **kwargs)
except (TypeError, IOError, UnsupportedOperation):
return np.frombuffer(
file.read(int(count * np.dtype(dtype).itemsize)),
dtype=dtype, count=count, *args, **kwargs)
"""Read from any file-like object into a numpy array"""

itemsize = np.dtype(dtype).itemsize
buffer = np.zeros(count * itemsize, np.uint8)
bytes_read = -1
offset = 0
while bytes_read != 0:
bytes_read = file.readinto(buffer[offset:])
offset += bytes_read
rounded_bytes = (offset // itemsize) * itemsize
buffer = buffer[:rounded_bytes]
buffer.dtype = dtype
return buffer


def read_interleaved_segment_bytes(f, bytes_per_row, num_values):
Expand Down

0 comments on commit bfbf854

Please sign in to comment.