Skip to content

Commit 28ebf14

Browse files
committed
Raise error when header=True for a series
1 parent 9b8b136 commit 28ebf14

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

medio/backends/itk_io.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def read_img(input_path, desired_axcodes=None, header=False, components_axis=Non
3838
"""
3939
input_path = Path(input_path)
4040
if input_path.is_dir():
41-
img = ItkIO.read_dir(str(input_path), pixel_type, fallback_only, series=series)
41+
img = ItkIO.read_dir(str(input_path), pixel_type, fallback_only, series=series, header=header)
4242
elif input_path.is_file():
4343
img = ItkIO.read_img_file(str(input_path), pixel_type, fallback_only)
4444
else:
@@ -54,6 +54,7 @@ def read_img(input_path, desired_axcodes=None, header=False, components_axis=Non
5454
image_np, affine = ItkIO.unpack_img(img)
5555
metadata = MetaData(affine=affine, orig_ornt=orig_ornt, coord_sys=ItkIO.coord_sys)
5656
if header:
57+
# TODO: not implemented for a series (returns an empty dictionary), see ItkIO.read_dir
5758
metadict = img.GetMetaDataDictionary()
5859
metadata.header = {key: metadict[key] for key in metadict.GetKeys() if not key.startswith('ITK_')}
5960

@@ -230,14 +231,21 @@ def reorient(img, desired_orientation: Union[int, tuple, str, None]):
230231
return reoriented_itk_img, original_orientation_code
231232

232233
@staticmethod
233-
def read_dir(dirname, pixel_type=None, fallback_only=False, series=None):
234+
def read_dir(dirname, pixel_type=None, fallback_only=False, series=None, header=False):
234235
"""
235236
Read a dicom directory. If there is more than one series in the directory an error is raised
236237
(unless the series argument is used properly).
237238
Shorter option for a single series (provided the slices order is known):
238239
>>> itk.imread([filename0, filename1, ...])
239240
"""
240241
filenames = ItkIO.extract_series(dirname, series)
242+
if header and isinstance(filenames, (tuple, list)):
243+
# TODO: to extract the metadata dictionary array use:
244+
# reader = itk.ImageSeriesReader.New(FileNames=filenames)
245+
# reader.Update()
246+
# metadict_arr = reader.GetMetaDataDictionaryArray()
247+
# (See also itk.imread source code)
248+
raise NotImplementedError("header=True is currently not supported for a series")
241249
return itk.imread(filenames, pixel_type, fallback_only)
242250

243251
@staticmethod

medio/backends/pdcm_io.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ def read_img(input_path, desired_ornt=None, header=False, channels_axis=None, gl
3939
input_path = Path(input_path)
4040
temp_channels_axis = -1 # if there are channels, they must be in the last axis for the reorientation
4141
if input_path.is_dir():
42-
# TODO: add header support
43-
img, metadata, channeled = PdcmIO.read_dcm_dir(input_path, globber, channels_axis=temp_channels_axis,
44-
series=series)
42+
img, metadata, channeled = PdcmIO.read_dcm_dir(input_path, header, globber,
43+
channels_axis=temp_channels_axis, series=series)
4544
else:
4645
img, metadata, channeled = PdcmIO.read_dcm_file(
4746
input_path, header, allow_default_affine=allow_default_affine, channels_axis=temp_channels_axis)
@@ -73,7 +72,7 @@ def read_dcm_file(filename, header=False, allow_default_affine=False, channels_a
7372
return img, metadata, samples_per_pixel > 1
7473

7574
@staticmethod
76-
def read_dcm_dir(input_dir, globber='*', channels_axis=None, series=None):
75+
def read_dcm_dir(input_dir, header=False, globber='*', channels_axis=None, series=None):
7776
"""
7877
Reads a 3D dicom image: input path can be a file or directory (DICOM series).
7978
Return the image array, metadata, and whether it has channels
@@ -82,6 +81,10 @@ def read_dcm_dir(input_dir, globber='*', channels_axis=None, series=None):
8281
slices = PdcmIO.extract_slices(input_dir, globber=globber, series=series)
8382
img, affine = combine_slices(slices)
8483
metadata = PdcmIO.aff2meta(affine)
84+
if header:
85+
# TODO: add header support, something like
86+
# metdata.header = [{str(key): ds[key] for key in ds.keys()} for ds in slices]
87+
raise NotImplementedError("header=True is currently not supported for a series")
8588
samples_per_pixel = slices[0].SamplesPerPixel
8689
img = PdcmIO.move_channels_axis(img, samples_per_pixel=samples_per_pixel, channels_axis=channels_axis,
8790
planar_configuration=slices[0].get('PlanarConfiguration', None),

0 commit comments

Comments
 (0)