77 >>> filepath = os.path.dirname( os.path.realpath( __file__ ) )
88 >>> datadir = os.path.realpath(os.path.join(filepath, '../../testing/data'))
99 >>> os.chdir(datadir)
10-
1110"""
1211from __future__ import print_function , division , unicode_literals , absolute_import
13- from builtins import open
12+ from io import open
1413
1514import os .path as op
1615import nibabel as nb
@@ -55,10 +54,11 @@ def read_mrtrix_tracks(in_file, as_generator=True):
5554
5655
5756def read_mrtrix_header (in_file ):
58- fileobj = open (in_file , 'r ' )
57+ fileobj = open (in_file , 'rb ' )
5958 header = {}
6059 iflogger .info ('Reading header data...' )
6160 for line in fileobj :
61+ line = line .decode ()
6262 if line == 'END\n ' :
6363 iflogger .info ('Reached the end of the header!' )
6464 break
@@ -78,7 +78,7 @@ def read_mrtrix_header(in_file):
7878def read_mrtrix_streamlines (in_file , header , as_generator = True ):
7979 offset = header ['offset' ]
8080 stream_count = header ['count' ]
81- fileobj = open (in_file , 'r ' )
81+ fileobj = open (in_file , 'rb ' )
8282 fileobj .seek (offset )
8383 endianness = native_code
8484 f4dt = np .dtype (endianness + 'f4' )
@@ -138,9 +138,14 @@ def track_gen(track_points):
138138 if n_streams == stream_count :
139139 iflogger .info ('100% : {n} tracks read' .format (n = n_streams ))
140140 raise StopIteration
141- if n_streams % int (stream_count / 100 ) == 0 :
142- percent = int (float (n_streams ) / float (stream_count ) * 100 )
143- iflogger .info ('{p}% : {n} tracks read' .format (p = percent , n = n_streams ))
141+ try :
142+ if n_streams % int (stream_count / 100 ) == 0 :
143+ percent = int (float (n_streams ) / float (stream_count ) * 100 )
144+ iflogger .info ('{p}% : {n} tracks read' .format (p = percent ,
145+ n = n_streams ))
146+ except ZeroDivisionError :
147+ iflogger .info ('{} stream read out of {}' .format (n_streams ,
148+ stream_count ))
144149 track_points , nonfinite_list = points_per_track (offset )
145150 fileobj .seek (offset )
146151 streamlines = track_gen (track_points )
@@ -166,10 +171,8 @@ class MRTrix2TrackVis(BaseInterface):
166171 """
167172 Converts MRtrix (.tck) tract files into TrackVis (.trk) format
168173 using functions from dipy
169-
170174 Example
171175 -------
172-
173176 >>> import nipype.interfaces.mrtrix as mrt
174177 >>> tck2trk = mrt.MRTrix2TrackVis()
175178 >>> tck2trk.inputs.in_file = 'dwi_CSD_tracked.tck'
0 commit comments