Skip to content

Commit 4d93676

Browse files
committed
Addressed's @jchoude's comments
1 parent 9228075 commit 4d93676

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

nibabel/streamlines/tck.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
from .array_sequence import ArraySequence
1717
from .tractogram_file import TractogramFile
18-
from .tractogram_file import HeaderError, DataWarning, DataError
18+
from .tractogram_file import HeaderWarning, DataWarning
19+
from .tractogram_file import HeaderError, DataError
1920
from .tractogram import TractogramItem, Tractogram, LazyTractogram
2021
from .header import Field
2122

@@ -306,21 +307,29 @@ def _read_header(fileobj):
306307
while not buf.rstrip().endswith("END"):
307308
buf += asstr(f.fobj.readline())
308309

310+
offset_data = f.tell()
311+
309312
# Build header dictionary from the buffer.
310313
hdr = dict(item.split(': ') for item in buf.rstrip().split('\n')[:-1])
311314
hdr[Field.MAGIC_NUMBER] = magic_number
312315

313316
# Check integrity of TCK header.
314317
if 'datatype' not in hdr:
315-
raise HeaderError("Missing 'datatype' attribute in TCK header.")
318+
msg = ("Missing 'datatype' attribute in TCK header."
319+
" Assuming it is Float32LE.")
320+
warnings.warn(msg, HeaderWarning)
321+
hdr['datatype'] = "Float32LE"
316322

317323
if not hdr['datatype'].startswith('Float32'):
318-
msg = ("TCK only supports float32 dtype but 'dataype: {}' was"
324+
msg = ("TCK only supports float32 dtype but 'datatype: {}' was"
319325
" specified in the header.").format(hdr['datatype'])
320326
raise HeaderError(msg)
321327

322328
if 'file' not in hdr:
323-
raise HeaderError("Missing 'file' attribute in TCK header.")
329+
msg = ("Missing 'file' attribute in TCK header."
330+
" Will try to guess it.")
331+
warnings.warn(msg, HeaderWarning)
332+
hdr['file'] = '. {}'.format(offset_data)
324333

325334
if hdr['file'].split()[0] != '.':
326335
msg = ("TCK only supports single-file - in other words the"

nibabel/streamlines/tests/test_tck.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
import numpy as np
44
from os.path import join as pjoin
55

6-
from nibabel.externals.six import BytesIO
6+
from six import BytesIO
77
from nibabel.py3k import asbytes
88

99
from ..array_sequence import ArraySequence
1010
from ..tractogram import Tractogram
11-
from ..tractogram_file import DataError
11+
from ..tractogram_file import HeaderWarning, DataError
1212

13+
from .. import tck as tck_module
1314
from ..tck import TckFile
1415

1516
from nose.tools import assert_equal, assert_raises, assert_true
1617
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
1819
from .test_tractogram import assert_tractogram_equal
1920

2021
DATA = {}
@@ -88,6 +89,27 @@ def test_load_file_with_wrong_information(self):
8889
asbytes("Float32BE"))
8990
assert_raises(DataError, TckFile.load, BytesIO(new_tck_file))
9091

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+
91113
def test_write_empty_file(self):
92114
tractogram = Tractogram(affine_to_rasmm=np.eye(4))
93115

@@ -125,15 +147,6 @@ def test_write_simple_file(self):
125147
assert_equal(tck_file.read(),
126148
open(DATA['simple_tck_fname'], 'rb').read())
127149

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-
137150
def test_load_write_file(self):
138151
for fname in [DATA['empty_tck_fname'],
139152
DATA['simple_tck_fname']]:

0 commit comments

Comments
 (0)