Skip to content

Commit 649eff3

Browse files
committed
RF: small streamlines API PR edits
A little refactoring of the Trk file ``_read_header`` method to save an unnecessary seek. Some rewriting and expansion of docstrings.
1 parent a944957 commit 649eff3

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

nibabel/streamlines/trk.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,12 @@ def _read_header(fileobj):
541541
Returns
542542
-------
543543
header : dict
544-
Metadata associated to this tractogram file.
544+
Metadata associated with this tractogram file.
545545
"""
546+
# Record start position if this is a file-like object
547+
start_position = fileobj.tell() if hasattr(fileobj, 'tell') else None
548+
546549
with Opener(fileobj) as f:
547-
start_position = f.tell()
548550

549551
# Read the header in one block.
550552
header_str = f.read(header_2_dtype.itemsize)
@@ -568,7 +570,8 @@ def _read_header(fileobj):
568570
elif header_rec['version'] == 2:
569571
pass # Nothing more to do.
570572
else:
571-
raise HeaderError('NiBabel only supports versions 1 and 2.')
573+
raise HeaderError('NiBabel only supports versions 1 and 2 of '
574+
'the Trackvis file format')
572575

573576
# Convert the first record of `header_rec` into a dictionnary
574577
header = dict(zip(header_rec.dtype.names, header_rec[0]))
@@ -601,14 +604,15 @@ def _read_header(fileobj):
601604
# Keep the file position where the data begin.
602605
header['_offset_data'] = f.tell()
603606

604-
# Set the file position where it was (in case it was already open).
605-
f.seek(start_position, os.SEEK_CUR)
607+
# Set the file position where it was, if it was previously open
608+
if start_position is not None:
609+
fileobj.seek(start_position, os.SEEK_CUR)
606610

607-
return header
611+
return header
608612

609613
@staticmethod
610614
def _read(fileobj, header):
611-
""" Reads TRK data from a file.
615+
""" Return generator that reads TRK data from `fileobj` given `header`
612616
613617
Parameters
614618
----------
@@ -618,15 +622,17 @@ def _read(fileobj, header):
618622
of the TRK header). Note that calling this function
619623
does not change the file position.
620624
header : dict
621-
Metadata associated to this tractogram file.
625+
Metadata associated with this tractogram file.
622626
623627
Yields
624628
------
625629
data : tuple of ndarrays
626-
Streamline data: points, scalars, properties.
627-
points: ndarray of shape (n_pts, 3)
628-
scalars: ndarray of shape (n_pts, nb_scalars_per_point)
629-
properties: ndarray of shape (nb_properties_per_point,)
630+
Length 3 tuple of streamline data of form (points, scalars,
631+
properties), where:
632+
633+
* points: ndarray of shape (n_pts, 3)
634+
* scalars: ndarray of shape (n_pts, nb_scalars_per_point)
635+
* properties: ndarray of shape (nb_properties_per_point,)
630636
"""
631637
i4_dtype = np.dtype(header[Field.ENDIANNESS] + "i4")
632638
f4_dtype = np.dtype(header[Field.ENDIANNESS] + "f4")

nibabel/tests/data/gen_standard.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
""" Generate mask and testing tractogram in known formats:
2+
3+
* mask: standard.nii.gz
4+
* tractogram:
5+
6+
* standard.trk
7+
"""
18
import numpy as np
29
import nibabel as nib
310

0 commit comments

Comments
 (0)