@@ -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" )
0 commit comments