Skip to content

Commit aee4f91

Browse files
committed
TEST: Added tests/files for brikhead.py
Created new tests for brikhead.py and added/modified datafiles to support them. Notably, added scaled+tlrc.BRIK/HEAD to test inclusion of data scaling factors and gzipped exampled4d+orig.BRIK to accommodate loading gzipped files.
1 parent 8d9e414 commit aee4f91

File tree

6 files changed

+175
-37
lines changed

6 files changed

+175
-37
lines changed

nibabel/brikhead.py

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,6 @@
5151
5: 'D',
5252
}
5353

54-
_orient_dict = {
55-
0: 'R',
56-
1: 'L',
57-
2: 'P',
58-
3: 'A',
59-
4: 'I',
60-
5: 'S'
61-
}
62-
6354
space_codes = Recoder((
6455
(0, 'unknown', ''),
6556
(1, 'scanner', 'ORIG'),
@@ -222,15 +213,15 @@ def scaling(self):
222213

223214
def __array__(self):
224215
raw_data = self.get_unscaled()
225-
# apply volume specific scaling
216+
# apply volume specific scaling (may change datatype!)
226217
if self._scaling is not None:
227-
return raw_data * self._scaling.astype(self.dtype)
218+
return raw_data * self._scaling
228219

229220
return raw_data
230221

231222
def __getitem__(self, slicer):
232223
raw_data = super(AFNIArrayProxy, self).__getitem__(slicer)
233-
# apply volume specific scaling
224+
# apply volume specific scaling (may change datatype!)
234225
if self._scaling is not None:
235226
scaling = self._scaling.copy()
236227
fake_data = strided_scalar(self._shape)
@@ -312,20 +303,6 @@ def _calc_zooms(self):
312303

313304
return xyz_step + t_step
314305

315-
def get_orient(self):
316-
"""Returns orientation of data
317-
318-
Three letter string of {('L','R'), ('P','A'), ('I','S')} specifying
319-
data orientation
320-
321-
Returns
322-
-------
323-
orient : str
324-
"""
325-
orient = [_orient_dict[f] for f in self.info['ORIENT_SPECIFIC']]
326-
327-
return ''.join(orient)
328-
329306
def get_space(self):
330307
"""Returns space of dataset
331308
@@ -334,7 +311,7 @@ def get_space(self):
334311
space : str
335312
"""
336313
listed_space = self.info.get('TEMPLATE_SPACE', 0)
337-
space = space_codes.label[listed_space]
314+
space = space_codes.space[listed_space]
338315

339316
return space
340317

@@ -363,14 +340,15 @@ def get_data_scaling(self):
363340
364341
Examples
365342
--------
366-
>>> header = AFNIHeader(parse_AFNI_header('example4d+orig.HEAD'))
343+
>>> header = AFNIHeader(parse_AFNI_header('scaled+tlrc.HEAD'))
367344
>>> header.get_data_scaling()
345+
array([ 3.88336300e-08])
368346
"""
369347
floatfacs = self.info.get('BRICK_FLOAT_FACS', None)
370348
if floatfacs is None or not np.any(floatfacs):
371349
return None
372350
scale = np.ones(self.info['DATASET_RANK'][1])
373-
floatfacs = np.asarray(floatfacs)
351+
floatfacs = np.atleast_1d(floatfacs)
374352
scale[floatfacs.nonzero()] = floatfacs[floatfacs.nonzero()]
375353

376354
return scale
@@ -419,7 +397,7 @@ class AFNIImage(SpatialImage):
419397
[ 0. , 0. , 3. , -52.3511],
420398
[ 0. , 0. , 0. , 1. ]])
421399
>>> head = load('example4d+orig.HEAD')
422-
>>> (head.get_data() == brik.get_data()).all()
400+
>>> np.array_equal(head.get_data(), brik.get_data())
423401
True
424402
"""
425403

-198 KB
Binary file not shown.
173 KB
Binary file not shown.

nibabel/tests/data/scaled+tlrc.BRIK

213 KB
Binary file not shown.

nibabel/tests/data/scaled+tlrc.HEAD

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
type = string-attribute
3+
name = HISTORY_NOTE
4+
count = 304
5+
'[[email protected]: Tue Jan 23 20:04:42 2018] {AFNI_18.0.09:macosx_10.7_Intel_64} 3dcalc -short -a 'mreg/subj-020_task-rest_bold.nii.gz[0]' -expr a -prefix scaled\n[[email protected]: Tue Jan 23 20:05:10 2018] {AFNI_18.0.09:macosx_10.7_Intel_64} 3dAutobox -prefix scaled+tlrc scaled+tlrc~
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_vLKn9e5VumKelWXNeq4SWA~
16+
17+
type = string-attribute
18+
name = IDCODE_DATE
19+
count = 25
20+
'Tue Jan 23 20:05:10 2018~
21+
22+
type = integer-attribute
23+
name = SCENE_DATA
24+
count = 8
25+
2 2 0 -999 -999
26+
-999 -999 -999
27+
28+
type = string-attribute
29+
name = LABEL_1
30+
count = 5
31+
'zyxt~
32+
33+
type = string-attribute
34+
name = LABEL_2
35+
count = 5
36+
'zyxt~
37+
38+
type = string-attribute
39+
name = DATASET_NAME
40+
count = 5
41+
'zyxt~
42+
43+
type = integer-attribute
44+
name = ORIENT_SPECIFIC
45+
count = 3
46+
1 2 4
47+
48+
type = float-attribute
49+
name = ORIGIN
50+
count = 3
51+
66 87 -54
52+
53+
type = float-attribute
54+
name = DELTA
55+
count = 3
56+
-3 -3 3
57+
58+
type = float-attribute
59+
name = IJK_TO_DICOM
60+
count = 12
61+
-3 0 0 66 0
62+
-3 0 87 0 0
63+
3 -54
64+
65+
type = float-attribute
66+
name = IJK_TO_DICOM_REAL
67+
count = 12
68+
-3 0 0 66 0
69+
-3 0 87 0 0
70+
3 -54
71+
72+
type = float-attribute
73+
name = BRICK_STATS
74+
count = 2
75+
1.941682e-07 0.001272461
76+
77+
type = integer-attribute
78+
name = DATASET_RANK
79+
count = 8
80+
3 1 0 0 0
81+
0 0 0
82+
83+
type = integer-attribute
84+
name = DATASET_DIMENSIONS
85+
count = 5
86+
47 54 43 0 0
87+
88+
type = integer-attribute
89+
name = BRICK_TYPES
90+
count = 1
91+
1
92+
93+
type = float-attribute
94+
name = BRICK_FLOAT_FACS
95+
count = 1
96+
3.883363e-08
97+
98+
type = string-attribute
99+
name = BRICK_LABS
100+
count = 3
101+
'#0~
102+
103+
type = string-attribute
104+
name = BRICK_KEYWORDS
105+
count = 1
106+
'~
107+
108+
type = string-attribute
109+
name = TEMPLATE_SPACE
110+
count = 5
111+
'TLRC~
112+
113+
type = integer-attribute
114+
name = INT_CMAP
115+
count = 1
116+
0
117+
118+
type = string-attribute
119+
name = BYTEORDER_STRING
120+
count = 10
121+
'LSB_FIRST~

nibabel/tests/test_brikhead.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .. import load, Nifti1Image
1616
from .. import brikhead
1717

18-
from nose.tools import (assert_true, assert_equal)
18+
from nose.tools import (assert_true, assert_equal, assert_raises)
1919
from numpy.testing import assert_array_equal
2020
from ..testing import data_path
2121

@@ -24,41 +24,80 @@
2424

2525
EXAMPLE_IMAGES = [
2626
dict(
27-
fname=pjoin(data_path, 'example4d+orig.BRIK'),
27+
head=pjoin(data_path, 'example4d+orig.HEAD'),
28+
brik=pjoin(data_path, 'example4d+orig.BRIK.gz'),
2829
shape=(33, 41, 25, 3),
2930
dtype=np.int16,
3031
affine=np.array([[-3.0,0,0,49.5],
3132
[0,-3.0,0,82.312],
3233
[0,0,3.0,-52.3511],
3334
[0,0,0,1.0]]),
3435
zooms=(3., 3., 3., 3.),
35-
# These values from SPM2
3636
data_summary=dict(
3737
min=0,
3838
max=13722,
3939
mean=4266.76024636),
40-
is_proxy=True)
40+
is_proxy=True,
41+
space='ORIG',
42+
labels=['#0', '#1', '#2'],
43+
scaling=None),
44+
dict(
45+
head=pjoin(data_path, 'scaled+tlrc.HEAD'),
46+
brik=pjoin(data_path, 'scaled+tlrc.BRIK'),
47+
shape=(47, 54, 43),
48+
dtype=np.int16,
49+
affine=np.array([[3.0,0,0,-66.],
50+
[0,3.0,0,-87.],
51+
[0,0,3.0,-54.],
52+
[0,0,0,1.0]]),
53+
zooms=(3., 3., 3.),
54+
data_summary=dict(
55+
min=1.9416814999999998e-07,
56+
max=0.0012724615542099998,
57+
mean=0.00023919645351876782),
58+
is_proxy=True,
59+
space='TLRC',
60+
labels=['#0'],
61+
scaling=np.array([ 3.88336300e-08]),
62+
)
4163
]
4264

4365

66+
class TestAFNIHeader(object):
67+
module = brikhead
68+
test_files = EXAMPLE_IMAGES
69+
70+
def test_makehead(self):
71+
for tp in self.test_files:
72+
head1 = self.module.AFNIHeader.from_fileobj(tp['head'])
73+
head2 = self.module.AFNIHeader.from_header(head1)
74+
assert_equal(head1, head2)
75+
with assert_raises(self.module.AFNIError):
76+
self.module.AFNIHeader.from_header(header=None)
77+
with assert_raises(self.module.AFNIError):
78+
self.module.AFNIHeader.from_header(tp['brik'])
79+
80+
4481
class TestAFNIImage(object):
4582
module = brikhead
4683
test_files = EXAMPLE_IMAGES
4784

4885
def test_brikheadfile(self):
4986
for tp in self.test_files:
50-
brik = self.module.load(tp['fname'])
87+
brik = self.module.load(tp['brik'])
5188
assert_equal(brik.get_data_dtype().type, tp['dtype'])
5289
assert_equal(brik.shape, tp['shape'])
5390
assert_equal(brik.header.get_zooms(), tp['zooms'])
5491
assert_array_equal(brik.affine, tp['affine'])
92+
assert_equal(brik.header.get_space(), tp['space'])
5593
data = brik.get_data()
5694
assert_equal(data.shape, tp['shape'])
95+
assert_array_equal(brik.dataobj.scaling, tp['scaling'])
5796

5897
def test_load(self):
5998
# Check highest level load of brikhead works
6099
for tp in self.test_files:
61-
img = load(tp['fname'])
100+
img = load(tp['brik'])
62101
data = img.get_data()
63102
assert_equal(data.shape, tp['shape'])
64103
# min, max, mean values
@@ -71,7 +110,7 @@ def test_load(self):
71110
def test_array_proxy_slicing(self):
72111
# Test slicing of array proxy
73112
for tp in self.test_files:
74-
img = load(tp['fname'])
113+
img = load(tp['brik'])
75114
arr = img.get_data()
76115
prox = img.dataobj
77116
assert_true(prox.is_proxy)

0 commit comments

Comments
 (0)