88
99from ..array_sequence import ArraySequence
1010from ..tractogram import Tractogram
11- from ..tractogram_file import HeaderWarning , DataError
11+ from ..tractogram_file import HeaderWarning , HeaderError
12+ from ..tractogram_file import DataError
1213
1314from .. import tck as tck_module
1415from ..tck import TckFile
@@ -84,15 +85,20 @@ def test_load_simple_file_in_big_endian(self):
8485 def test_load_file_with_wrong_information (self ):
8586 tck_file = open (DATA ['simple_tck_fname' ], 'rb' ).read ()
8687
87- # Simulate a TCK file where `datatype` was incorrectly provided .
88+ # Simulate a TCK file where `datatype` has not the right endianness .
8889 new_tck_file = tck_file .replace (asbytes ("Float32LE" ),
8990 asbytes ("Float32BE" ))
9091 assert_raises (DataError , TckFile .load , BytesIO (new_tck_file ))
9192
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+
9298 # Simulate a TCK file with no `datatype` field.
9399 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 " )
96102 with clear_and_catch_warnings (record = True , modules = [tck_module ]) as w :
97103 tck = TckFile .load (BytesIO (new_tck_file ))
98104 assert_equal (len (w ), 1 )
@@ -101,7 +107,6 @@ def test_load_file_with_wrong_information(self):
101107 assert_array_equal (tck .header ['datatype' ], "Float32LE" )
102108
103109 # Simulate a TCK file with no `file` field.
104- # Adjust data offset
105110 new_tck_file = tck_file .replace (b"\n file: . 67" , b"" )
106111 with clear_and_catch_warnings (record = True , modules = [tck_module ]) as w :
107112 tck = TckFile .load (BytesIO (new_tck_file ))
@@ -110,6 +115,26 @@ def test_load_file_with_wrong_information(self):
110115 assert_true ("Missing 'file'" in str (w [0 ].message ))
111116 assert_array_equal (tck .header ['file' ], ". 56" )
112117
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+
113138 def test_write_empty_file (self ):
114139 tractogram = Tractogram (affine_to_rasmm = np .eye (4 ))
115140
@@ -147,6 +172,15 @@ def test_write_simple_file(self):
147172 assert_equal (tck_file .read (),
148173 open (DATA ['simple_tck_fname' ], 'rb' ).read ())
149174
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+
150184 def test_load_write_file (self ):
151185 for fname in [DATA ['empty_tck_fname' ],
152186 DATA ['simple_tck_fname' ]]:
0 commit comments