1414
1515from .array_sequence import ArraySequence
1616from .tractogram_file import TractogramFile
17- from .tractogram_file import HeaderError , DataWarning
17+ from .tractogram_file import HeaderError , DataWarning , DataError
1818from .tractogram import TractogramItem , Tractogram , LazyTractogram
1919from .header import Field
2020
@@ -206,8 +206,8 @@ def save(self, fileobj):
206206
207207 for t in tractogram :
208208 s = t .streamline .astype (dtype )
209- f . write ( asbytes ( np .r_ [s . astype ( '<f4' ),
210- self . FIBER_DELIMITER ] .tostring ()))
209+ data = np .r_ [s , self . FIBER_DELIMITER ]
210+ f . write ( asbytes ( data .tostring ()))
211211 nb_streamlines += 1
212212
213213 header [Field .NB_STREAMLINES ] = nb_streamlines
@@ -281,7 +281,7 @@ def _read_header(fileobj):
281281
282282 with Opener (fileobj ) as f :
283283 # Read magic number
284- buf = asstr (f .fobj .readline ())
284+ magic_number = asstr (f .fobj .readline ())
285285
286286 # Read all key-value pairs contained in the header.
287287 buf = asstr (f .fobj .readline ())
@@ -290,6 +290,7 @@ def _read_header(fileobj):
290290
291291 # Build header dictionary from the buffer.
292292 hdr = dict (item .split (': ' ) for item in buf .rstrip ().split ('\n ' )[:- 1 ])
293+ hdr [Field .MAGIC_NUMBER ] = magic_number
293294
294295 # Check integrity of TCK header.
295296 if 'datatype' not in hdr :
@@ -379,7 +380,15 @@ def _read(fileobj, header, buffer_size=4):
379380 pts = pts .reshape ([- 1 , 3 ])
380381 idx_nan = np .arange (len (pts ))[np .isnan (pts [:, 0 ])]
381382
383+ # Make sure we've read enough to find a streamline delimiter.
382384 if len (idx_nan ) == 0 :
385+ # If we've read the whole file, then fail.
386+ if eof and not np .all (np .isinf (pts )):
387+ msg = ("Cannot find a streamline delimiter. This file"
388+ " might be corrupted." )
389+ raise DataError (msg )
390+
391+ # Otherwise read a bit more.
383392 continue
384393
385394 nb_pts_total = 0
0 commit comments