|
3 | 3 | import numpy as np
|
4 | 4 | from os.path import join as pjoin
|
5 | 5 |
|
6 |
| -from nibabel.externals.six import BytesIO |
| 6 | +from six import BytesIO |
7 | 7 | from nibabel.py3k import asbytes
|
8 | 8 |
|
9 | 9 | from ..array_sequence import ArraySequence
|
10 | 10 | from ..tractogram import Tractogram
|
11 |
| -from ..tractogram_file import DataError |
| 11 | +from ..tractogram_file import HeaderWarning, DataError |
12 | 12 |
|
| 13 | +from .. import tck as tck_module |
13 | 14 | from ..tck import TckFile
|
14 | 15 |
|
15 | 16 | from nose.tools import assert_equal, assert_raises, assert_true
|
16 | 17 | from numpy.testing import assert_array_equal
|
17 |
| -from nibabel.testing import data_path |
| 18 | +from nibabel.testing import data_path, clear_and_catch_warnings |
18 | 19 | from .test_tractogram import assert_tractogram_equal
|
19 | 20 |
|
20 | 21 | DATA = {}
|
@@ -88,6 +89,27 @@ def test_load_file_with_wrong_information(self):
|
88 | 89 | asbytes("Float32BE"))
|
89 | 90 | assert_raises(DataError, TckFile.load, BytesIO(new_tck_file))
|
90 | 91 |
|
| 92 | + # Simulate a TCK file with no `datatype` field. |
| 93 | + new_tck_file = tck_file.replace(b"datatype: Float32LE\n", b"") |
| 94 | + # Adjust data offset |
| 95 | + new_tck_file = new_tck_file.replace(b"\nfile: . 67\n", b"\nfile: . 47\n") |
| 96 | + with clear_and_catch_warnings(record=True, modules=[tck_module]) as w: |
| 97 | + tck = TckFile.load(BytesIO(new_tck_file)) |
| 98 | + assert_equal(len(w), 1) |
| 99 | + assert_true(issubclass(w[0].category, HeaderWarning)) |
| 100 | + assert_true("Missing 'datatype'" in str(w[0].message)) |
| 101 | + assert_array_equal(tck.header['datatype'], "Float32LE") |
| 102 | + |
| 103 | + # Simulate a TCK file with no `file` field. |
| 104 | + # Adjust data offset |
| 105 | + new_tck_file = tck_file.replace(b"\nfile: . 67", b"") |
| 106 | + with clear_and_catch_warnings(record=True, modules=[tck_module]) as w: |
| 107 | + tck = TckFile.load(BytesIO(new_tck_file)) |
| 108 | + assert_equal(len(w), 1) |
| 109 | + assert_true(issubclass(w[0].category, HeaderWarning)) |
| 110 | + assert_true("Missing 'file'" in str(w[0].message)) |
| 111 | + assert_array_equal(tck.header['file'], ". 56") |
| 112 | + |
91 | 113 | def test_write_empty_file(self):
|
92 | 114 | tractogram = Tractogram(affine_to_rasmm=np.eye(4))
|
93 | 115 |
|
@@ -125,15 +147,6 @@ def test_write_simple_file(self):
|
125 | 147 | assert_equal(tck_file.read(),
|
126 | 148 | open(DATA['simple_tck_fname'], 'rb').read())
|
127 | 149 |
|
128 |
| - # # Add custom header fields. |
129 |
| - # tck_file = BytesIO() |
130 |
| - # tck = TckFile(tractogram) |
131 |
| - # # tck.header['Custom_field'] = "Some_value" |
132 |
| - # tck.save(tck_file) |
133 |
| - # tck_file.seek(0, os.SEEK_SET) |
134 |
| - # new_tck = TckFile.load(tck_file) |
135 |
| - # assert_equal(tck.header, new_tck.header) |
136 |
| - |
137 | 150 | def test_load_write_file(self):
|
138 | 151 | for fname in [DATA['empty_tck_fname'],
|
139 | 152 | DATA['simple_tck_fname']]:
|
|
0 commit comments