2323MEGABYTE = 1024 * 1024
2424
2525
26- def create_empty_header ():
27- ''' Return an empty compliant TCK header. '''
28- header = {}
29-
30- # Default values
31- header [Field .MAGIC_NUMBER ] = TckFile .MAGIC_NUMBER
32- header [Field .NB_STREAMLINES ] = 0
33- header ['datatype' ] = "Float32LE"
34- return header
35-
36-
3726class TckFile (TractogramFile ):
3827 """ Convenience class to encapsulate TCK file format.
3928
4029 Notes
4130 -----
42- MRtrix (so its file format: TCK) considers the streamline coordinate
43- (0,0,0) to be in the center of the voxel which is also the case for
44- NiBabel's streamlines internal representation.
45-
46- Moreover, streamlines coordinates coming from a TCK file are considered
31+ MRtrix (so its file format: TCK) considers streamlines coordinates
4732 to be in world space (RAS+ and mm space). MRtrix refers to that space
4833 as the "real" or "scanner" space [1]_.
4934
35+ Moreover, when streamlines are mapped back to voxel space [2]_, a
36+ streamline point located at an integer coordinate (i,j,k) is considered
37+ to be at the center of the corresponding voxel. This is in contrast with
38+ TRK's internal convention where it would have referred to a corner.
39+
40+ NiBabel's streamlines internal representation follows the same
41+ convention as MRtrix.
42+
5043 References
5144 ----------
5245 [1] http://www.nitrc.org/pipermail/mrtrix-discussion/2014-January/000859.html
46+ [2] http://nipy.org/nibabel/coordinate_systems.html#voxel-coordinates-are-in-voxel-space
5347 """
54-
5548 # Constants
5649 MAGIC_NUMBER = "mrtrix tracks"
5750 SUPPORTS_DATA_PER_POINT = False # Not yet
@@ -72,12 +65,15 @@ def __init__(self, tractogram, header=None):
7265
7366 Notes
7467 -----
75- Streamlines of the tractogram are assumed to be in *RAS+*
76- and *mm* space where coordinate (0,0,0) refers to the center
77- of the voxel.
68+ Streamlines of the tractogram are assumed to be in *RAS+* and *mm*
69+ space. It is also assumed that when streamlines are mapped back to
70+ voxel space, a streamline point located at an integer coordinate
71+ (i,j,k) is considered to be at the center of the corresponding voxel.
72+ This is in contrast with TRK's internal convention where it would
73+ have referred to a corner.
7874 """
7975 if header is None :
80- header = create_empty_header ()
76+ header = self . create_empty_header ()
8177
8278 super (TckFile , self ).__init__ (tractogram , header )
8379
@@ -105,6 +101,17 @@ def is_correct_format(cls, fileobj):
105101
106102 return magic_number .strip () == cls .MAGIC_NUMBER
107103
104+ @classmethod
105+ def create_empty_header (cls ):
106+ """ Return an empty compliant TCK header. """
107+ header = {}
108+
109+ # Default values
110+ header [Field .MAGIC_NUMBER ] = cls .MAGIC_NUMBER
111+ header [Field .NB_STREAMLINES ] = 0
112+ header ['datatype' ] = "Float32LE"
113+ return header
114+
108115 @classmethod
109116 def load (cls , fileobj , lazy_load = False ):
110117 """ Loads streamlines from a filename or file-like object.
@@ -128,9 +135,12 @@ def load(cls, fileobj, lazy_load=False):
128135
129136 Notes
130137 -----
131- Streamlines of the tractogram are assumed to be in *RAS+*
132- and *mm* space where coordinate (0,0,0) refers to the center
133- of the voxel.
138+ Streamlines of the tractogram are assumed to be in *RAS+* and *mm*
139+ space. It is also assumed that when streamlines are mapped back to
140+ voxel space, a streamline point located at an integer coordinate
141+ (i,j,k) is considered to be at the center of the corresponding voxel.
142+ This is in contrast with TRK's internal convention where it would
143+ have referred to a corner.
134144 """
135145 hdr = cls ._read_header (fileobj )
136146
@@ -169,7 +179,7 @@ def save(self, fileobj):
169179 """
170180 # Enforce float32 in little-endian byte order for data.
171181 dtype = np .dtype ('<f4' )
172- header = create_empty_header ()
182+ header = self . create_empty_header ()
173183
174184 # Override hdr's fields by those contained in `header`.
175185 header .update (self .header )
@@ -437,13 +447,13 @@ def _read(cls, fileobj, header, buffer_size=4):
437447 f .seek (start_position , os .SEEK_CUR )
438448
439449 def __str__ (self ):
440- ''' Gets a formatted string of the header of a TCK file.
450+ """ Gets a formatted string of the header of a TCK file.
441451
442452 Returns
443453 -------
444454 info : string
445455 Header information relevant to the TCK format.
446- '''
456+ """
447457 hdr = self .header
448458
449459 info = ""
0 commit comments