8
8
9
9
from ..array_sequence import ArraySequence
10
10
from ..tractogram import Tractogram
11
- from ..tractogram_file import HeaderWarning , DataError
11
+ from ..tractogram_file import HeaderWarning , HeaderError
12
+ from ..tractogram_file import DataError
12
13
13
14
from .. import tck as tck_module
14
15
from ..tck import TckFile
@@ -84,15 +85,20 @@ def test_load_simple_file_in_big_endian(self):
84
85
def test_load_file_with_wrong_information (self ):
85
86
tck_file = open (DATA ['simple_tck_fname' ], 'rb' ).read ()
86
87
87
- # Simulate a TCK file where `datatype` was incorrectly provided .
88
+ # Simulate a TCK file where `datatype` has not the right endianness .
88
89
new_tck_file = tck_file .replace (asbytes ("Float32LE" ),
89
90
asbytes ("Float32BE" ))
90
91
assert_raises (DataError , TckFile .load , BytesIO (new_tck_file ))
91
92
93
+ # Simulate a TCK file with unsupported `datatype`.
94
+ new_tck_file = tck_file .replace (asbytes ("Float32LE" ),
95
+ asbytes ("int32" ))
96
+ assert_raises (HeaderError , TckFile .load , BytesIO (new_tck_file ))
97
+
92
98
# Simulate a TCK file with no `datatype` field.
93
99
new_tck_file = tck_file .replace (b"datatype: Float32LE\n " , b"" )
94
- # Adjust data offset
95
- new_tck_file = new_tck_file .replace (b"\n file : . 67\n " , b"\n file : . 47\n " )
100
+ # Need to adjust data offset.
101
+ new_tck_file = new_tck_file .replace (b"file : . 67\n " , b"file : . 47\n " )
96
102
with clear_and_catch_warnings (record = True , modules = [tck_module ]) as w :
97
103
tck = TckFile .load (BytesIO (new_tck_file ))
98
104
assert_equal (len (w ), 1 )
@@ -101,7 +107,6 @@ def test_load_file_with_wrong_information(self):
101
107
assert_array_equal (tck .header ['datatype' ], "Float32LE" )
102
108
103
109
# Simulate a TCK file with no `file` field.
104
- # Adjust data offset
105
110
new_tck_file = tck_file .replace (b"\n file: . 67" , b"" )
106
111
with clear_and_catch_warnings (record = True , modules = [tck_module ]) as w :
107
112
tck = TckFile .load (BytesIO (new_tck_file ))
@@ -110,6 +115,26 @@ def test_load_file_with_wrong_information(self):
110
115
assert_true ("Missing 'file'" in str (w [0 ].message ))
111
116
assert_array_equal (tck .header ['file' ], ". 56" )
112
117
118
+ # Simulate a TCK file with `file` field pointing to another file.
119
+ new_tck_file = tck_file .replace (b"file: . 67\n " ,
120
+ b"file: dummy.mat 75\n " )
121
+ assert_raises (HeaderError , TckFile .load , BytesIO (new_tck_file ))
122
+
123
+ # Simulate a TCK file which is missing a streamline delimiter.
124
+ eos = TckFile .FIBER_DELIMITER .tostring ()
125
+ eof = TckFile .EOF_DELIMITER .tostring ()
126
+ new_tck_file = tck_file [:- (len (eos ) + len (eof ))] + tck_file [- len (eof ):]
127
+
128
+ # Force TCK loading to use buffering.
129
+ buffer_size = 1. / 1024 ** 2 # 1 bytes
130
+ hdr = TckFile ._read_header (BytesIO (new_tck_file ))
131
+ tck_reader = TckFile ._read (BytesIO (new_tck_file ), hdr , buffer_size )
132
+ assert_raises (DataError , list , tck_reader )
133
+
134
+ # Simulate a TCK file which is missing the end-of-file delimiter.
135
+ new_tck_file = tck_file [:- len (eof )]
136
+ assert_raises (DataError , TckFile .load , BytesIO (new_tck_file ))
137
+
113
138
def test_write_empty_file (self ):
114
139
tractogram = Tractogram (affine_to_rasmm = np .eye (4 ))
115
140
@@ -147,6 +172,15 @@ def test_write_simple_file(self):
147
172
assert_equal (tck_file .read (),
148
173
open (DATA ['simple_tck_fname' ], 'rb' ).read ())
149
174
175
+ # TCK file containing not well formatted entries in its header.
176
+ tck_file = BytesIO ()
177
+ tck = TckFile (tractogram )
178
+ tck .header ['new_entry' ] = 'value\n ' # \n not allowed
179
+ assert_raises (HeaderError , tck .save , tck_file )
180
+
181
+ tck .header ['new_entry' ] = 'val:ue' # : not allowed
182
+ assert_raises (HeaderError , tck .save , tck_file )
183
+
150
184
def test_load_write_file (self ):
151
185
for fname in [DATA ['empty_tck_fname' ],
152
186
DATA ['simple_tck_fname' ]]:
0 commit comments