Skip to content

Commit 210eb2e

Browse files
author
cindeem
committed
NF: added access to mlist and subheader from img, and exceptions for saving
1 parent c914f5e commit 210eb2e

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

nibabel/ecat.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@
181181
(13, 'ECAT7_3DNORM'),
182182
(14, 'ECAT7_3DSCANFIT'))
183183

184+
patient_orient_defs = ( #code, description
185+
(0, 'ECAT7_Feet_First_Prone'),
186+
(1, 'ECAT7_Head_First_Prone'),
187+
(2, 'ECAT7_Feet_First_Supine'),
188+
(3, 'ECAT7_Head_First_Supine'),
189+
(4, 'ECAT7_Feet_First_Decubitus_Right'),
190+
(5, 'ECAT7_Head_First_Decubitus_Right'),
191+
(6, 'ECAT7_Feet_First_Decubitus_Left'),
192+
(7, 'ECAT7_Head_First_Decubitus_Left'),
193+
(8, 'ECAT7_Unknown_Orientation'))
194+
195+
196+
184197
class EcatHeader(object):
185198
"""Class for basic Ecat PET header
186199
Sub-parts of standard Ecat File
@@ -199,6 +212,7 @@ class EcatHeader(object):
199212

200213
_dtype = hdr_dtype
201214
_ft_defs = ft_defs
215+
_patient_orient_defs = patient_orient_defs
202216

203217
def __init__(self,
204218
fileobj=None,
@@ -345,6 +359,15 @@ def __setitem__(self, item, value):
345359
'''
346360
self._header_data[item] = value
347361

362+
def get_patient_orient(self):
363+
""" gets orientation of patient based on code stored
364+
in header, not always reliable"""
365+
orient_code = dict(self._patient_orient_defs)
366+
code = self._header_data['patient_orientation'].item()
367+
if not orient_code.has_key(code):
368+
raise KeyError('Ecat Orientation CODE %d not recognized'%code)
369+
return orient_code[code]
370+
348371
def get_filetype(self):
349372
""" gets type of ECAT Matrix File from
350373
code stored in header"""
@@ -592,7 +615,8 @@ def raw_data_from_fileobj(self, frame=0):
592615
fid_obj = self.fileobj
593616
raw_data = array_from_file(shape, dtype, fid_obj, offset=offset)
594617
## put data into neurologic orientation
595-
#fid_obj.close()
618+
### ASSUME Patient orientation is HFS as patient orientation
619+
### is not always set in header
596620
raw_data = raw_data[::-1,::-1,::-1]
597621
return raw_data
598622

@@ -730,10 +754,16 @@ def get_shape(self):
730754
nframes = self._subheader.get_nframes()
731755
return(x, y, z, nframes)
732756

757+
def get_mlist(self):
758+
""" get access to the mlist """
759+
return self._mlist
760+
761+
def get_subheaders(self):
762+
"""get access to subheaders"""
763+
return self._subheader
733764

734765
@classmethod
735766
def from_filespec(klass, filespec):
736-
737767
return klass.from_filename(filespec)
738768

739769

@@ -777,18 +807,18 @@ def from_file_map(klass, file_map):
777807
aff = subheaders.get_frame_affine()
778808
img = klass(data, aff, header, subheaders, mlist, extra=None, file_map = file_map)
779809
return img
780-
810+
811+
812+
def to_filename(self, filename):
813+
"""nibabel does not support writing to Ecat filetypes at this time"""
814+
raise NotImplementedError('nibabel does not allow saving to Ecat'\
815+
' at this time ')
781816

782817
@classmethod
783818
def from_image(klass, img):
784-
raise NotImplementedError("Ecat images must be generated from files")
785-
orig_hdr = img.get_header()
786-
return klass(img.get_data(),
787-
img.get_affine(),
788-
img.get_header(),
789-
img.extra)
790-
791-
819+
raise NotImplementedError("Ecat images can only be generated "\
820+
"from file objects")
821+
792822
@classmethod
793823
def load(klass, filespec):
794824
return klass.from_filename(filespec)

nibabel/tests/test_ecat.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ def test_dtype(self):
4444
hdr = self.header_class()
4545
yield assert_raises(NotImplementedError,
4646
hdr.get_data_dtype)
47-
def test_filetype(self):
48-
hdr = self.header_class()
47+
48+
def test_header_codes(self):
49+
fid = open(ecat_file)
50+
hdr = self.header_class()
51+
newhdr = hdr.from_fileobj(fid)
52+
fid.close()
53+
yield assert_true(newhdr.get_filetype() == 'ECAT7_VOLUME16')
54+
yield assert_equal(newhdr.get_patient_orient(),
55+
'ECAT7_Unknown_Orientation')
4956

5057
def test_copy(self):
5158
hdr = self.header_class()
@@ -142,5 +149,5 @@ def test_data(self):
142149
yield assert_equal(dat.shape, self.img.get_shape())
143150
frame = self.img.get_frame(0)
144151
yield assert_array_equal(frame, dat[:,:,:,0])
145-
152+
146153

0 commit comments

Comments
 (0)