Skip to content

Commit 9d67e3c

Browse files
author
Ben Cipollini
committed
Check NumberofDataArrays, warn only if it's wrong.
1 parent 2fc386c commit 9d67e3c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

nibabel/gifti/parse_gifti_fast.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import base64
1212
import sys
13+
import warnings
1314
import zlib
1415
from ..externals.six import StringIO
1516
from xml.parsers.expat import ParserCreate, ExpatError
@@ -108,6 +109,8 @@ def StartElementHandler(self, name, attrs):
108109
self.img = GiftiImage()
109110
if 'Version' in attrs:
110111
self.img.version = attrs['Version']
112+
if 'NumberOfDataArrays' in attrs:
113+
self.expected_numDA = int(attrs['NumberOfDataArrays'])
111114

112115
self.fsm_state.append('GIFTI')
113116
elif name == 'MetaData':
@@ -204,6 +207,10 @@ def EndElementHandler(self, name):
204207
if DEBUG_PRINT:
205208
print('End element:\n\t', repr(name))
206209
if name == 'GIFTI':
210+
if hasattr(self, 'expected_numDA') and self.expected_numDA != self.img.numDA:
211+
warnings.warn("Actual # of data arrays does not match "
212+
"# expected: %d != %d." % (self.expected_numDA,
213+
self.img.numDA))
207214
# remove last element of the list
208215
self.fsm_state.pop()
209216
# assert len(self.fsm_state) == 0

nibabel/gifti/tests/test_giftiio.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,23 @@ def test_labeltable_deprecations():
300300
warnings.filterwarnings('once', category=DeprecationWarning)
301301
img.set_labeltable(lt)
302302
assert_equal(lt, img.labeltable)
303+
304+
305+
def test_parse_dataarrays():
306+
fn = 'bad_daa.gii'
307+
img = gi.GiftiImage()
308+
309+
with InTemporaryDirectory():
310+
gi.write(img, fn)
311+
with open(fn, 'r') as fp:
312+
txt = fp.read()
313+
# Make a bad gifti.
314+
txt = txt.replace('NumberOfDataArrays="0"', 'NumberOfDataArrays ="1"')
315+
with open(fn, 'w') as fp:
316+
fp.write(txt)
317+
318+
with clear_and_catch_warnings() as w:
319+
warnings.filterwarnings('once', category=UserWarning)
320+
gi.read(fn)
321+
assert_equal(len(w), 1)
322+
assert_equal(img.numDA, 0)

0 commit comments

Comments
 (0)