45
45
46
46
47
47
class ImageReader (ABC ):
48
- """Abstract class to define interface APIs to load image files.
49
- users need to call `read` to load image and then use `get_data`
50
- to get the image data and properties from meta data.
48
+ """
49
+ An abstract class defines APIs to load image files.
50
+
51
+ Typical usage of an implementation of this class is:
52
+
53
+ .. code-block:: python
54
+
55
+ image_reader = MyImageReader()
56
+ img_obj = image_reader.read(path_to_image)
57
+ img_data, meta_data = image_reader.get_data(img_obj)
58
+
59
+ - The `read` call converts image filenames into image objects,
60
+ - The `get_data` call fetches the image data, as well as meta data.
61
+ - A reader should implement `verify_suffix` with the logic of checking the input filename
62
+ by the filename extensions.
51
63
52
64
"""
53
65
54
66
@abstractmethod
55
67
def verify_suffix (self , filename : Union [Sequence [str ], str ]) -> bool :
56
68
"""
57
- Verify whether the specified file or files format is supported by current reader.
69
+ Verify whether the specified `filename` is supported by the current reader.
70
+ This method should return True if the reader is able to read the format suggested by the
71
+ `filename`.
58
72
59
73
Args:
60
74
filename: file name or a list of file names to read.
@@ -67,7 +81,7 @@ def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
67
81
def read (self , data : Union [Sequence [str ], str ], ** kwargs ) -> Union [Sequence [Any ], Any ]:
68
82
"""
69
83
Read image data from specified file or files.
70
- Note that it returns the raw data, so different readers return different image data type .
84
+ Note that it returns a data object or a sequence of data objects .
71
85
72
86
Args:
73
87
data: file name or a list of file names to read.
@@ -80,7 +94,8 @@ def read(self, data: Union[Sequence[str], str], **kwargs) -> Union[Sequence[Any]
80
94
def get_data (self , img ) -> Tuple [np .ndarray , Dict ]:
81
95
"""
82
96
Extract data array and meta data from loaded image and return them.
83
- This function must return 2 objects, first is numpy array of image data, second is dict of meta data.
97
+ This function must return two objects, the first is a numpy array of image data,
98
+ the second is a dictionary of meta data.
84
99
85
100
Args:
86
101
img: an image object loaded from an image file or a list of image objects.
@@ -124,7 +139,7 @@ def _stack_images(image_list: List, meta_dict: Dict):
124
139
class ITKReader (ImageReader ):
125
140
"""
126
141
Load medical images based on ITK library.
127
- All the supported image formats can be found:
142
+ All the supported image formats can be found at :
128
143
https://github.com/InsightSoftwareConsortium/ITK/tree/master/Modules/IO
129
144
The loaded data array will be in C order, for example, a 3D image NumPy
130
145
array index order will be `CDWH`.
@@ -396,7 +411,10 @@ def _get_meta_dict(self, img) -> Dict:
396
411
397
412
"""
398
413
# swap to little endian as PyTorch doesn't support big endian
399
- header = img .header .as_byteswapped ("<" )
414
+ try :
415
+ header = img .header .as_byteswapped ("<" )
416
+ except ValueError :
417
+ header = img .header
400
418
return dict (header )
401
419
402
420
def _get_affine (self , img ):
@@ -419,11 +437,18 @@ def _get_spatial_shape(self, img):
419
437
420
438
"""
421
439
# swap to little endian as PyTorch doesn't support big endian
422
- header = img .header .as_byteswapped ("<" )
423
- ndim = header ["dim" ][0 ]
440
+ try :
441
+ header = img .header .as_byteswapped ("<" )
442
+ except ValueError :
443
+ header = img .header
444
+ dim = header .get ("dim" , None )
445
+ if dim is None :
446
+ dim = header .get ("dims" ) # mgh format?
447
+ dim = np .insert (dim , 0 , 3 )
448
+ ndim = dim [0 ]
424
449
spatial_rank = min (ndim , 3 )
425
450
# the img data should have no channel dim or the last dim is channel
426
- return np .asarray (header [ " dim" ] [1 : spatial_rank + 1 ])
451
+ return np .asarray (dim [1 : spatial_rank + 1 ])
427
452
428
453
def _get_array_data (self , img ):
429
454
"""
0 commit comments