23
23
MEGABYTE = 1024 * 1024
24
24
25
25
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
-
37
26
class TckFile (TractogramFile ):
38
27
""" Convenience class to encapsulate TCK file format.
39
28
40
29
Notes
41
30
-----
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
47
32
to be in world space (RAS+ and mm space). MRtrix refers to that space
48
33
as the "real" or "scanner" space [1]_.
49
34
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
+
50
43
References
51
44
----------
52
45
[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
53
47
"""
54
-
55
48
# Constants
56
49
MAGIC_NUMBER = "mrtrix tracks"
57
50
SUPPORTS_DATA_PER_POINT = False # Not yet
@@ -72,12 +65,15 @@ def __init__(self, tractogram, header=None):
72
65
73
66
Notes
74
67
-----
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.
78
74
"""
79
75
if header is None :
80
- header = create_empty_header ()
76
+ header = self . create_empty_header ()
81
77
82
78
super (TckFile , self ).__init__ (tractogram , header )
83
79
@@ -105,6 +101,17 @@ def is_correct_format(cls, fileobj):
105
101
106
102
return magic_number .strip () == cls .MAGIC_NUMBER
107
103
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
+
108
115
@classmethod
109
116
def load (cls , fileobj , lazy_load = False ):
110
117
""" Loads streamlines from a filename or file-like object.
@@ -128,9 +135,12 @@ def load(cls, fileobj, lazy_load=False):
128
135
129
136
Notes
130
137
-----
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.
134
144
"""
135
145
hdr = cls ._read_header (fileobj )
136
146
@@ -169,7 +179,7 @@ def save(self, fileobj):
169
179
"""
170
180
# Enforce float32 in little-endian byte order for data.
171
181
dtype = np .dtype ('<f4' )
172
- header = create_empty_header ()
182
+ header = self . create_empty_header ()
173
183
174
184
# Override hdr's fields by those contained in `header`.
175
185
header .update (self .header )
@@ -437,13 +447,13 @@ def _read(cls, fileobj, header, buffer_size=4):
437
447
f .seek (start_position , os .SEEK_CUR )
438
448
439
449
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.
441
451
442
452
Returns
443
453
-------
444
454
info : string
445
455
Header information relevant to the TCK format.
446
- '''
456
+ """
447
457
hdr = self .header
448
458
449
459
info = ""
0 commit comments