Skip to content

Commit dce22dc

Browse files
committed
TEST: Added broken HEAD files to tests/data
Added a few "broken" .HEAD files to the testing suite for brikhead.py and implemented a few try-except blocks to catch the resultant errors. Maybe (?) resolved the issues with the AppVeyor OSError on Python 3 for numpy memmap in loading the .BRIK files. Fixed a doc-test in brikhead.py.
1 parent aee4f91 commit dce22dc

File tree

4 files changed

+301
-7
lines changed

4 files changed

+301
-7
lines changed

nibabel/brikhead.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,23 @@ def _unpack_var(var):
9292
>>> print(name, attr)
9393
TEMPLATE_SPACE ORIG
9494
"""
95-
# data type and key
96-
atype = TYPE_RE.findall(var)[0]
97-
aname = NAME_RE.findall(var)[0]
95+
err_msg = ('.HEAD file appears to contain a misformed attribute. Please '
96+
'check .HEAD file and try again.')
97+
# get data type and key; if error, bad attribute/HEAD file
98+
try:
99+
atype = TYPE_RE.findall(var)[0]
100+
aname = NAME_RE.findall(var)[0]
101+
except IndexError:
102+
raise AFNIError(err_msg)
103+
# get actual attribute value; if error, improper casting due to bad
104+
# attribute/HEAD file
98105
atype = _attr_dic.get(atype, str)
99106
attr = ' '.join(var.strip().split('\n')[3:])
100107
if atype is not str:
101-
attr = [atype(f) for f in attr.split()]
108+
try:
109+
attr = [atype(f) for f in attr.split()]
110+
except ValueError:
111+
raise AFNIError(err_msg)
102112
if len(attr) == 1:
103113
attr = attr[0]
104114
else:
@@ -388,7 +398,7 @@ class AFNIImage(SpatialImage):
388398
389399
Examples
390400
--------
391-
>>> brik = load('example4d+orig.BRIK')
401+
>>> brik = load('example4d+orig.BRIK.gz')
392402
>>> brik.shape
393403
(33, 41, 25, 3)
394404
>>> brik.affine
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
type = string-attribute
3+
name = DATASET_NAME
4+
count = 5
5+
'none~
6+
7+
type = string-attribute
8+
name = TYPESTRING
9+
count = 15
10+
'3DIM_HEAD_ANAT~
11+
12+
type = string-attribute
13+
name = IDCODE_STRING
14+
count = 27
15+
'AFN_-zxZ0OyZs8eEtm9syGBNdA~
16+
17+
type = string-attribute
18+
name = IDCODE_DATE
19+
count = 25
20+
'Sun Oct 1 21:13:09 2017~
21+
22+
type = integer-attribute
23+
name = SCENE_DATA
24+
count = 8
25+
0 2 0 -999 -999
26+
-999 -999 -999
27+
28+
type = string-attribute
29+
name = LABEL_1
30+
count = 5
31+
'none~
32+
33+
type = string-attribute
34+
name = LABEL_2
35+
count = 5
36+
'none~
37+
38+
type = integer-attribute
39+
name = ORIENT_SPECIFIC
40+
count = 3
41+
0 3 4
42+
43+
type = float-attribute
44+
name = ORIGIN
45+
count = 3
46+
-49.5 -82.312 -52.3511
47+
48+
type = float-attribute
49+
name = DELTA
50+
count = 3
51+
3 3 3
52+
53+
type = float-attribute
54+
name = IJK_TO_DICOM
55+
count = 12
56+
3 0 0 -49.5 0
57+
3 0 -82.312 0 0
58+
3 -52.3511
59+
60+
type = float-attribute
61+
name = IJK_TO_DICOM_REAL
62+
count = 12
63+
3 0 0 -49.5 0
64+
3 0 -82.312 0 0
65+
3 -52.3511
66+
67+
type = float-attribute
68+
name = BRICK_STATS
69+
count = 6
70+
0 13722 0 10051 0
71+
9968
72+
73+
type = integer-attribute
74+
name = TAXIS_NUMS
75+
count = 8
76+
3 25 77002 -999 -999
77+
-999 -999 -999
78+
79+
type = float-attribute
80+
name = TAXIS_FLOATS
81+
count = 8
82+
0 3 0 -52.3511 3
83+
-999999 -999999 -999999
84+
85+
type = float-attribute
86+
name = TAXIS_OFFSETS
87+
count = 25
88+
0.3260869 1.826087 0.3913043 1.891304 0.4565217
89+
1.956521 0.5217391 2.021739 0.5869564 2.086956
90+
0.6521738 2.152174 0.7173912 2.217391 0.7826086
91+
2.282609 0.8478259 2.347826 0.9130433 2.413044
92+
0.9782607 2.478261 1.043478 2.543479 1.108696
93+
94+
type = integer-attribute
95+
name = DATASET_RANK
96+
count = 8
97+
3 3 0 0 0
98+
0 0 0
99+
100+
type = integer-attribute
101+
name = DATASET_DIMENSIONS
102+
count = 5
103+
33 41 25 0 0
104+
105+
type = integer-attribute
106+
name = BRICK_TYPES
107+
count = 3
108+
1 1 1
109+
110+
type = float-attribute
111+
name = BRICK_FLOAT_FACS
112+
count = 3
113+
0 0 0
114+
115+
type = string-attribute
116+
name = TEMPLATE_SPACE
117+
count = 5
118+
'ORIG~
119+
120+
type = integer-attribute
121+
name = INT_CMAP
122+
count = 1
123+
0
124+
125+
type = integer-attribute
126+
name = BYTEORDER_STRING
127+
count = 10
128+
'LSB_FIRST~
129+
130+
type = string-attribute
131+
name = BRICK_LABS
132+
count = 9
133+
'#0~#1~#2~
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
type = string-attribute
3+
name = DATASET_NAME
4+
count = 5
5+
'none~
6+
7+
type = string-attribute
8+
name = TYPESTRING
9+
count = 15
10+
'3DIM_HEAD_ANAT~
11+
12+
type = string-attribute
13+
name = IDCODE_STRING
14+
count = 27
15+
'AFN_-zxZ0OyZs8eEtm9syGBNdA~
16+
17+
type = string-attribute
18+
name = IDCODE_DATE
19+
count = 25
20+
'Sun Oct 1 21:13:09 2017~
21+
22+
type = integer-attribute
23+
name = SCENE_DATA
24+
count = 8
25+
0 2 0 -999 -999
26+
-999 -999 -999
27+
28+
type = string-attribute
29+
name = LABEL_1
30+
count = 5
31+
'none~
32+
33+
type = string-attribute
34+
name = LABEL_2
35+
count = 5
36+
'none~
37+
38+
type = integer-attribute
39+
name = ORIENT_SPECIFIC
40+
count = 3
41+
0 3 4
42+
43+
type = float-attribute
44+
name = ORIGIN
45+
count = 3
46+
-49.5 -82.312 -52.3511
47+
48+
type = float-attribute
49+
name = DELTA
50+
count = 3
51+
3 3 3
52+
53+
type = float-attribute
54+
name = IJK_TO_DICOM
55+
count = 12
56+
3 0 0 -49.5 0
57+
3 0 -82.312 0 0
58+
3 -52.3511
59+
60+
type = float-attribute
61+
name = IJK_TO_DICOM_REAL
62+
count = 12
63+
3 0 0 -49.5 0
64+
3 0 -82.312 0 0
65+
3 -52.3511
66+
67+
type = float-attribute
68+
name = BRICK_STATS
69+
count = 6
70+
0 13722 0 10051 0
71+
9968
72+
73+
type = integer-attribute
74+
name = TAXIS_NUMS
75+
count = 8
76+
3 25 77002 -999 -999
77+
-999 -999 -999
78+
79+
type = float-attribute
80+
name = TAXIS_FLOATS
81+
count = 8
82+
0 3 0 -52.3511 3
83+
-999999 -999999 -999999
84+
85+
type = float-attribute
86+
name = TAXIS_OFFSETS
87+
count = 25
88+
0.3260869 1.826087 0.3913043 1.891304 0.4565217
89+
1.956521 0.5217391 2.021739 0.5869564 2.086956
90+
0.6521738 2.152174 0.7173912 2.217391 0.7826086
91+
2.282609 0.8478259 2.347826 0.9130433 2.413044
92+
0.9782607 2.478261 1.043478 2.543479 1.108696
93+
94+
type = integer-attribute
95+
name = DATASET_RANK
96+
count = 8
97+
3 3 0 0 0
98+
0 0 0
99+
100+
type = integer-attribute
101+
name = DATASET_DIMENSIONS
102+
count = 5
103+
33 41 25 0 0
104+
105+
type = integer-attribute
106+
name = BRICK_TYPES
107+
count = 3
108+
1 3 5
109+
110+
type = float-attribute
111+
name = BRICK_FLOAT_FACS
112+
count = 3
113+
0 0 0
114+
115+
type = string-attribute
116+
name = TEMPLATE_SPACE
117+
count = 5
118+
'ORIG~
119+
120+
type = integer-attribute
121+
name = INT_CMAP
122+
count = 1
123+
0
124+
125+
type = string-attribute
126+
name = BYTEORDER_STRING
127+
count = 10
128+
'LSB_FIRST~
129+
130+
type = string-attribute
131+
name = BRICK_LABS
132+
count = 9
133+
'#0~#1~#2~

nibabel/tests/test_brikhead.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@
6262
)
6363
]
6464

65+
EXAMPLE_BAD_IMAGES = [
66+
dict(
67+
head=pjoin(data_path, 'bad_datatype+orig.HEAD')
68+
),
69+
dict(
70+
head=pjoin(data_path, 'bad_attribute+orig.HEAD')
71+
)
72+
]
6573

6674
class TestAFNIHeader(object):
6775
module = brikhead
@@ -97,7 +105,7 @@ def test_brikheadfile(self):
97105
def test_load(self):
98106
# Check highest level load of brikhead works
99107
for tp in self.test_files:
100-
img = load(tp['brik'])
108+
img = self.module.load(tp['brik'])
101109
data = img.get_data()
102110
assert_equal(data.shape, tp['shape'])
103111
# min, max, mean values
@@ -110,9 +118,19 @@ def test_load(self):
110118
def test_array_proxy_slicing(self):
111119
# Test slicing of array proxy
112120
for tp in self.test_files:
113-
img = load(tp['brik'])
121+
img = self.module.load(tp['brik'])
114122
arr = img.get_data()
115123
prox = img.dataobj
116124
assert_true(prox.is_proxy)
117125
for sliceobj in slicer_samples(img.shape):
118126
assert_array_equal(arr[sliceobj], prox[sliceobj])
127+
128+
129+
class TestBadFiles(object):
130+
module = brikhead
131+
test_files = EXAMPLE_BAD_IMAGES
132+
133+
def test_brikheadfile(self):
134+
for tp in self.test_files:
135+
with assert_raises(self.module.AFNIError):
136+
self.module.load(tp['head'])

0 commit comments

Comments
 (0)