Skip to content

Commit 74c3410

Browse files
committed
FIX: Return to bytearray/frombuffer approach
1 parent 8ed4aca commit 74c3410

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

nibabel/streamlines/tck.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,10 @@ def _read(cls, fileobj, header, buffer_size=4):
386386
"""
387387
dtype = header["_dtype"]
388388

389-
# Align batch_size to be multiple of 3 within the specified buffer size
390-
batch_size = int(buffer_size * MEGABYTE / dtype.itemsize / 3) * 3
389+
coordinate_size = 3 * dtype.itemsize
390+
# Make buffer_size an integer and a multiple of coordinate_size.
391+
buffer_size = int(buffer_size * MEGABYTE)
392+
buffer_size += coordinate_size - (buffer_size % coordinate_size)
391393

392394
with Opener(fileobj) as f:
393395
start_position = f.tell()
@@ -399,11 +401,14 @@ def _read(cls, fileobj, header, buffer_size=4):
399401
n_streams = 0
400402
leftover = np.empty((0, 3), dtype='<f4')
401403
while not eof:
404+
buff = bytearray(buffer_size)
405+
n_read = f.readinto(buff)
406+
eof = n_read != buffer_size
407+
if eof:
408+
buff = buff[:n_read]
402409

403410
# read raw files from file
404-
raw_values = np.fromfile(f.fobj, dtype, batch_size)
405-
if len(raw_values) < batch_size:
406-
eof = True
411+
raw_values = np.frombuffer(buff, dtype=dtype)
407412

408413
# Convert raw_values into a list of little-endian triples (for x,y,z coord)
409414
coords = raw_values.astype('<f4', copy=False).reshape((-1, 3))

0 commit comments

Comments
 (0)