1
1
2
- class StreamlineFile :
3
- @staticmethod
4
- def get_magic_number ():
5
- raise NotImplementedError ()
2
+ from nibabel .streamlines .header import Field
6
3
7
- @staticmethod
8
- def is_correct_format (cls , fileobj ):
9
- raise NotImplementedError ()
4
+ from ..externals .six .moves import zip_longest
5
+
6
+
7
+ class HeaderError (Exception ):
8
+ pass
9
+
10
+
11
+ class DataError (Exception ):
12
+ pass
13
+
14
+
15
+ class Streamlines (object ):
16
+ ''' Class containing information about streamlines.
17
+
18
+ Streamlines objects have three main properties: ``points``, ``scalars``
19
+ and ``properties``. Streamlines objects can be iterate over producing
20
+ tuple of ``points``, ``scalars`` and ``properties`` for each streamline.
21
+
22
+ Parameters
23
+ ----------
24
+ points : sequence of ndarray of shape (N, 3)
25
+ Sequence of T streamlines. One streamline is an ndarray of shape (N, 3)
26
+ where N is the number of points in a streamline.
27
+
28
+ scalars : sequence of ndarray of shape (N, M)
29
+ Sequence of T ndarrays of shape (N, M) where T is the number of
30
+ streamlines defined by ``points``, N is the number of points
31
+ for a particular streamline and M is the number of scalars
32
+ associated to each point (excluding the three coordinates).
33
+
34
+ properties : sequence of ndarray of shape (P,)
35
+ Sequence of T ndarrays of shape (P,) where T is the number of
36
+ streamlines defined by ``points``, P is the number of properties
37
+ associated to each streamlines.
38
+
39
+ hdr : dict
40
+ Header containing meta information about the streamlines. For a list
41
+ of common header's fields to use as keys see `nibabel.streamlines.Field`.
42
+ '''
43
+ def __init__ (self , points = [], scalars = [], properties = [], hdr = {}):
44
+ self .hdr = hdr
45
+
46
+ self .points = points
47
+ self .scalars = scalars
48
+ self .properties = properties
49
+ self .data = lambda : zip_longest (self .points , self .scalars , self .properties , fillvalue = [])
50
+
51
+ try :
52
+ self .length = len (points )
53
+ except :
54
+ if Field .NB_STREAMLINES in hdr :
55
+ self .length = hdr [Field .NB_STREAMLINES ]
56
+ else :
57
+ raise HeaderError (("Neither parameter 'points' nor 'hdr' contain information about"
58
+ " number of streamlines. Use key '{0}' to set the number of "
59
+ "streamlines in 'hdr'." ).format (Field .NB_STREAMLINES ))
10
60
11
61
def get_header (self ):
12
- raise NotImplementedError ()
62
+ return self .hdr
63
+
64
+ @property
65
+ def points (self ):
66
+ return self ._points ()
67
+
68
+ @points .setter
69
+ def points (self , value ):
70
+ self ._points = value if callable (value ) else (lambda : value )
71
+
72
+ @property
73
+ def scalars (self ):
74
+ return self ._scalars ()
75
+
76
+ @scalars .setter
77
+ def scalars (self , value ):
78
+ self ._scalars = value if callable (value ) else lambda : value
79
+
80
+ @property
81
+ def properties (self ):
82
+ return self ._properties ()
13
83
14
- def get_streamlines (self , as_generator = False ):
84
+ @properties .setter
85
+ def properties (self , value ):
86
+ self ._properties = value if callable (value ) else lambda : value
87
+
88
+ def __iter__ (self ):
89
+ return self .data ()
90
+
91
+ def __len__ (self ):
92
+ return self .length
93
+
94
+
95
+ class StreamlinesFile :
96
+ ''' Convenience class to encapsulate streamlines file format. '''
97
+
98
+ @classmethod
99
+ def get_magic_number (cls ):
100
+ ''' Return streamlines file's magic number. '''
15
101
raise NotImplementedError ()
16
102
17
- def get_scalars (self , as_generator = False ):
103
+ @classmethod
104
+ def is_correct_format (cls , fileobj ):
105
+ ''' Check if the file has the right streamlines file format.
106
+
107
+ Parameters
108
+ ----------
109
+ fileobj : string or file-like object
110
+ If string, a filename; otherwise an open file-like object
111
+ pointing to a streamlines file (and ready to read from the
112
+ beginning of the header)
113
+
114
+ Returns
115
+ -------
116
+ is_correct_format : boolean
117
+ Returns True if `fileobj` is in the right streamlines file format.
118
+ '''
18
119
raise NotImplementedError ()
19
120
20
- def get_properties (self , as_generator = False ):
121
+ @classmethod
122
+ def get_empty_header (cls ):
123
+ ''' Return an empty streamlines file's header. '''
21
124
raise NotImplementedError ()
22
125
23
126
@classmethod
24
- def load (cls , fileobj ):
127
+ def load (cls , fileobj , lazy_load = True ):
128
+ ''' Loads streamlines from a file-like object.
129
+
130
+ Parameters
131
+ ----------
132
+ fileobj : string or file-like object
133
+ If string, a filename; otherwise an open file-like object
134
+ pointing to a streamlines file (and ready to read from the
135
+ beginning of the header)
136
+
137
+ lazy_load : boolean
138
+ Load streamlines in a lazy manner i.e. they will not be kept
139
+ in memory. For postprocessing speed, turn off this option.
140
+
141
+ Returns
142
+ -------
143
+ streamlines : Streamlines object
144
+ Returns an object containing streamlines' data and header
145
+ information. See 'nibabel.Streamlines'.
146
+ '''
25
147
raise NotImplementedError ()
26
148
27
- def save (self , filename ):
149
+ @classmethod
150
+ def save (cls , streamlines , fileobj ):
151
+ ''' Saves streamlines to a file-like object.
152
+
153
+ Parameters
154
+ ----------
155
+ streamlines : Streamlines object
156
+ Object containing streamlines' data and header information.
157
+ See 'nibabel.Streamlines'.
158
+
159
+ fileobj : string or file-like object
160
+ If string, a filename; otherwise an open file-like object
161
+ opened and ready to write.
162
+ '''
28
163
raise NotImplementedError ()
29
164
30
- def __iter__ (self ):
165
+ @staticmethod
166
+ def pretty_print (streamlines ):
167
+ ''' Gets a formatted string contaning header's information
168
+ relevant to the streamlines file format.
169
+
170
+ Parameters
171
+ ----------
172
+ streamlines : Streamlines object
173
+ Object containing streamlines' data and header information.
174
+ See 'nibabel.Streamlines'.
175
+
176
+ Returns
177
+ -------
178
+ info : string
179
+ Header's information relevant to the streamlines file format.
180
+ '''
31
181
raise NotImplementedError ()
32
182
33
183
34
- class DynamicStreamlineFile (StreamlineFile ):
184
+ class DynamicStreamlineFile (StreamlinesFile ):
185
+ ''' Convenience class to encapsulate streamlines file format
186
+ that supports appending streamlines to an existing file.
187
+ '''
188
+
35
189
def append (self , streamlines ):
36
190
raise NotImplementedError ()
37
191
38
192
def __iadd__ (self , streamlines ):
39
- return self .append (streamlines )
193
+ return self .append (streamlines )
0 commit comments