Skip to content

Commit eb4f390

Browse files
author
cindeem
committed
NF adding ecat tests, fix endian typo
1 parent bc772d7 commit eb4f390

File tree

3 files changed

+90
-19
lines changed

3 files changed

+90
-19
lines changed

nibabel/ecat.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,17 @@ class EcatHeader(object):
164164
subheaders specific to each frame
165165
with possibly-variable sized data blocks
166166
167-
This just reads the main Ecat Header, and matrixlist
167+
This just reads the main Ecat Header,
168168
it does not load the data
169-
or read any sub headers
169+
or read the mlist or any sub headers
170170
171171
"""
172172

173173
_dtype = hdr_dtype
174-
_subhdrdtype = subhdr_dtype
175-
176-
174+
177175
def __init__(self,
178176
fileobj=None,
179-
endianness=None,
180-
check=False):
177+
endianness=None):
181178
"""Initialize Ecat header from file object
182179
183180
Parameters
@@ -188,9 +185,7 @@ def __init__(self,
188185
endianness : {None, '<', '>', other endian code}, optional
189186
endian code of binary block, If None, guess endianness
190187
from the data
191-
check : bool optional
192-
Whether to check content of header in intialization.
193-
Default is False """
188+
"""
194189
if fileobj is None:
195190
self._header_data = self._empty_headerdata(endianness)
196191
return
@@ -274,13 +269,23 @@ def _empty_headerdata(self,endianness=None):
274269

275270
def get_data_dtype(self):
276271
""" Get numpy dtype for data from header"""
277-
pass
272+
raise NotImplementedError("dtype is only valid from subheaders")
273+
278274

279275
def copy(self):
280276
return self.__class__(
281277
self.binaryblock,
282278
self.endianness,
283279
check=False)
280+
281+
def __eq__(self, other):
282+
""" checks for equality between two headers"""
283+
self_end = self.endianness
284+
self_bb = self.binaryblock
285+
if self_end == other.endianness:
286+
return self_bb == other.binaryblock
287+
other_bb = other._header_data.byteswap().tostring()
288+
return self_bb == other_bb
284289

285290
def __ne__(self, other):
286291
''' equality between two headers defined by ``header_data``
@@ -294,8 +299,8 @@ def __getitem__(self, item):
294299
295300
Examples
296301
--------
297-
>>> hdr = AnalyzeHeader()
298-
>>> hdr['sizeof_hdr'] == 348
302+
>>> hdr = EcatHeader()
303+
>>> hdr['magic_number'] == 'MATRIX72'
299304
True
300305
'''
301306
return self._header_data[item]
@@ -305,10 +310,10 @@ def __setitem__(self, item, value):
305310
306311
Examples
307312
--------
308-
>>> hdr = AnalyzeHeader()
309-
>>> hdr['descrip'] = 'description'
310-
>>> str(hdr['descrip'])
311-
'description'
313+
>>> hdr = EcatHeader()
314+
>>> hdr['num_frames'] = 2
315+
>>> hdr['num_frames']
316+
2
312317
'''
313318
self._header_data[item] = value
314319

@@ -364,7 +369,7 @@ def get_mlist(self, fileobj):
364369
done = False
365370
while not done: #mats['matlist'][0,1] == 2:
366371

367-
mats = np.recarray(shape=(32,4), dtype=dt.newbyteorder(), buf=dat)
372+
mats = np.recarray(shape=(32,4), dtype=dt, buf=dat)
368373
if not (mats['matlist'][0,0] + mats['matlist'][0,3]) == 31:
369374
mlist = []
370375
return mlist
@@ -422,7 +427,7 @@ def __init__(self, hdr, mlist, fileobj):
422427
self.subheaders = self.get_subheaders()
423428

424429
def get_subheaders(self):
425-
"""retreive all subheaders and return list of subheader dictionaries
430+
"""retreive all subheaders and return list of subheader recarrays
426431
"""
427432
subheaders = []
428433
header = self._header

nibabel/tests/data/tinypet.v

2.09 KB
Binary file not shown.

nibabel/tests/test_ecat.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
4+
#
5+
# See COPYING file distributed along with the NiBabel package for the
6+
# copyright and license terms.
7+
#
8+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9+
import os
10+
from StringIO import StringIO
11+
import numpy as np
12+
13+
from ..testing import assert_equal, assert_not_equal, \
14+
assert_true, assert_false, assert_raises
15+
16+
from numpy.testing import (assert_array_equal,
17+
assert_array_almost_equal)
18+
19+
20+
from ..ecat import EcatHeader, EcatMlist, EcatSubHeader, EcatImage
21+
import test_binary as tb
22+
from ..testing import parametric, data_path, ParametricTestCase
23+
24+
ecat_file = os.path.join(data_path, 'tinypet.v')
25+
26+
class TestEcatHeader(ParametricTestCase):
27+
header_class = EcatHeader
28+
example_file = ecat_file
29+
30+
def test_header_size(self):
31+
yield assert_equal(self.header_class._dtype.itemsize, 502)
32+
33+
def test_empty(self):
34+
hdr = self.header_class()
35+
yield assert_true(len(hdr.binaryblock) == 502)
36+
yield assert_true(hdr['magic_number'] == 'MATRIX72')
37+
yield assert_true(hdr['sw_version'] == 74)
38+
yield assert_true(hdr['num_frames'] == 0)
39+
yield assert_true(hdr['file_type'] == 0)
40+
yield assert_true(hdr['ecat_calibration_factor'] == 1.0)
41+
42+
def test_dtype(self):
43+
hdr = self.header_class()
44+
yield assert_raises(NotImplementedError,
45+
hdr.get_data_dtype)
46+
47+
def test_copy(self):
48+
hdr = self.header_class()
49+
hdr2 = hdr.copy()
50+
yield assert_true(hdr == hdr2)
51+
yield assert_true(not hdr.binaryblock == hdr2._header_data.byteswap().tostring())
52+
yield assert_true(hdr.keys() == hdr2.keys())
53+
54+
def test_update(self):
55+
hdr = self.header_class()
56+
yield assert_true(hdr['num_frames'] == 0)
57+
hdr['num_frames'] = 2
58+
yield assert_true(hdr['num_frames'] == 2)
59+
60+
def test_endianness(self):
61+
fid = open(ecat_file)
62+
hdr = self.header_class()
63+
newhdr = hdr.from_fileobj(fid)
64+
fid.close()
65+
yield assert_true(hdr.endianness == '<')
66+
yield assert_true(newhdr.endianness == '>')

0 commit comments

Comments
 (0)