Skip to content

Commit 8ed4aca

Browse files
committed
RF: Reduce concatenations further, moderate cleanups
1 parent 62c0cda commit 8ed4aca

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

nibabel/streamlines/tck.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ 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
389+
# Align batch_size to be multiple of 3 within the specified buffer size
390390
batch_size = int(buffer_size * MEGABYTE / dtype.itemsize / 3) * 3
391391

392392
with Opener(fileobj) as f:
@@ -397,38 +397,36 @@ def _read(cls, fileobj, header, buffer_size=4):
397397

398398
eof = False
399399
n_streams = 0
400-
leftover = np.empty((0,3), dtype='<f4')
400+
leftover = np.empty((0, 3), dtype='<f4')
401401
while not eof:
402402

403403
# read raw files from file
404404
raw_values = np.fromfile(f.fobj, dtype, batch_size)
405405
if len(raw_values) < batch_size:
406406
eof = True
407407

408-
# Convert raw_values into a list of little-endian tuples (for x,y,z coord)
409-
coords = raw_values.astype('<f4', copy=False).reshape([-1, 3])
408+
# Convert raw_values into a list of little-endian triples (for x,y,z coord)
409+
coords = raw_values.astype('<f4', copy=False).reshape((-1, 3))
410410

411-
# find stream delimiter locations (all NaNs)
412-
delims = np.where(np.all(np.isnan(coords), axis=1))[0]
411+
# Find stream delimiter locations (all NaNs)
412+
delims = np.where(np.isnan(coords).all(axis=1))[0]
413+
414+
if leftover.size:
415+
delims += leftover.shape[0]
416+
coords = np.vstack((leftover, coords))
413417

414-
# for each delimiters, yeild new streams
415418
begin = 0
416-
for i in range(0, len(delims)):
417-
end = delims[i]
418-
if i == 0:
419-
stream = np.vstack((leftover, coords[begin:end]))
420-
else:
421-
stream = coords[begin:end]
422-
leftover = np.empty((0,3), dtype='<f4')
423-
yield stream
424-
n_streams += 1
425-
426-
begin = end+1 #skip the delimiter
419+
for delim in delims:
420+
pts = coords[begin:delim]
421+
if pts.size:
422+
yield coords[begin:delim]
423+
n_streams += 1
424+
begin = delim + 1
427425

428426
# the rest gets appended to the leftover
429-
leftover = np.vstack((leftover, coords[begin:]))
427+
leftover = coords[begin:]
430428

431-
if not np.all(np.isinf(leftover), axis=1):
429+
if not np.isinf(leftover).all():
432430
raise DataError("Expecting end-of-file marker 'inf inf inf'")
433431

434432
# In case the 'count' field was not provided.

0 commit comments

Comments
 (0)