Skip to content

Commit f5e5b22

Browse files
author
Ben Cipollini
committed
RF: combine BaseHeader/Image with FileBasedHeader/Image.
1 parent 097d3b1 commit f5e5b22

File tree

1 file changed

+26
-47
lines changed

1 file changed

+26
-47
lines changed

nibabel/filebasedimages.py

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
from .openers import ImageOpener
2525

2626

27-
class BaseHeader(object):
27+
class ImageFileError(Exception):
28+
pass
29+
30+
31+
class FileBasedHeader(object):
2832
''' Template class to implement header protocol '''
2933

3034
@classmethod
@@ -62,7 +66,7 @@ def copy(self):
6266
raise NotImplementedError
6367

6468

65-
class BaseImage(object):
69+
class FileBasedImage(object):
6670
'''
6771
It also has a ``header`` - some standard set of meta-data that is specific to
6872
the image format, and ``extra`` - a dictionary container for any other
@@ -84,52 +88,8 @@ class BaseImage(object):
8488
You cannot slice an image, and trying to slice an image generates an
8589
informative TypeError.
8690
'''
87-
header_class = None
88-
89-
def __init__(self, header=None, extra=None):
90-
''' Initialize image
91-
92-
The image is a combination of (header), with
93-
optional metadata in `extra`.
94-
95-
Parameters
96-
----------
97-
header : None or mapping or header instance, optional
98-
metadata for this image format
99-
extra : None or mapping, optional
100-
metadata to associate with image that cannot be stored in the
101-
metadata of this image type
102-
'''
103-
if header or self.header_class:
104-
self._header = self.header_class.from_header(header)
105-
else:
106-
self._header = None
107-
if extra is None:
108-
extra = {}
109-
self.extra = extra
110-
111-
@property
112-
def header(self):
113-
return self._header
114-
115-
116-
def __getitem__(self):
117-
''' No slicing or dictionary interface for images
118-
'''
119-
raise TypeError("Cannot slice image objects; consider slicing image "
120-
"array data with `img.dataobj[slice]` or "
121-
"`img.get_data()[slice]`")
122-
123-
124-
class ImageFileError(Exception):
125-
pass
126-
12791

128-
class FileBasedHeader(BaseHeader):
129-
'''Abstract class'''
13092

131-
132-
class FileBasedImage(BaseImage):
13393
'''
13494
This abstract image class defines an interface for loading/saving images
13595
from disk. It doesn't define any image properties.
@@ -260,11 +220,30 @@ def __init__(self, header=None, extra=None, file_map=None):
260220
file_map : mapping, optional
261221
mapping giving file information for this image format
262222
'''
263-
super(FileBasedImage, self).__init__(header=header, extra=extra)
223+
224+
if header or self.header_class:
225+
self._header = self.header_class.from_header(header)
226+
else:
227+
self._header = None
228+
if extra is None:
229+
extra = {}
230+
self.extra = extra
231+
264232
if file_map is None:
265233
file_map = self.__class__.make_file_map()
266234
self.file_map = file_map
267235

236+
@property
237+
def header(self):
238+
return self._header
239+
240+
def __getitem__(self):
241+
''' No slicing or dictionary interface for images
242+
'''
243+
raise TypeError("Cannot slice image objects; consider slicing image "
244+
"array data with `img.dataobj[slice]` or "
245+
"`img.get_data()[slice]`")
246+
268247
def get_header(self):
269248
""" Get header from image
270249

0 commit comments

Comments
 (0)