Skip to content

Commit

Permalink
Bring support for header in+private tags in series
Browse files Browse the repository at this point in the history
  • Loading branch information
elazarcoh committed Feb 28, 2022
1 parent a3d6e4e commit c020180
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions medio/backends/itk_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ def read_img(input_path, desired_axcodes=None, header=False, components_axis=Non
:return: numpy image and metadata object which includes pixdim, affine, original orientation string and
coordinates system
"""
if private_tags:
if header:
imageio = itk.GDCMImageIO.New()
imageio.LoadPrivateTagsOn()
if private_tags:
imageio.LoadPrivateTagsOn()
# we need to set the imageio to the reader, but with fallback_only=True it will not set it unless it fails
fallback_only = False
else:
imageio = None
input_path = Path(input_path)
if input_path.is_dir():
img = ItkIO.read_dir(str(input_path), pixel_type, fallback_only, series=series, header=header)
img = ItkIO.read_dir(str(input_path), pixel_type, fallback_only, series, imageio)
elif input_path.is_file():
img = ItkIO.read_img_file(str(input_path), pixel_type, fallback_only, imageio)
else:
Expand All @@ -61,8 +62,7 @@ def read_img(input_path, desired_axcodes=None, header=False, components_axis=Non
image_np, affine = ItkIO.unpack_img(img)
metadata = MetaData(affine=affine, orig_ornt=orig_ornt, coord_sys=ItkIO.coord_sys)
if header:
# TODO: not implemented for a series (returns an empty dictionary), see ItkIO.read_dir
metadict = img.GetMetaDataDictionary()
metadict = imageio.GetMetaDataDictionary()
metadata.header = {key: metadict[key] for key in metadict.GetKeys() if not key.startswith('ITK_')}

# TODO: consider unifying with PdcmIO.move_channels_axis
Expand Down Expand Up @@ -239,22 +239,15 @@ def reorient(img, desired_orientation: Union[int, tuple, str, None]):
return reoriented_itk_img, original_orientation_code

@staticmethod
def read_dir(dirname, pixel_type=None, fallback_only=False, series=None, header=False):
def read_dir(dirname, pixel_type=None, fallback_only=False, series=None, imageio=None):
"""
Read a dicom directory. If there is more than one series in the directory an error is raised
(unless the series argument is used properly).
Shorter option for a single series (provided the slices order is known):
>>> itk.imread([filename0, filename1, ...])
"""
filenames = ItkIO.extract_series(dirname, series)
if header and isinstance(filenames, (tuple, list)):
# TODO: to extract the metadata dictionary array use:
# reader = itk.ImageSeriesReader.New(FileNames=filenames)
# reader.Update()
# metadict_arr = reader.GetMetaDataDictionaryArray()
# (See also itk.imread source code)
raise NotImplementedError("header=True is currently not supported for a series")
return itk.imread(filenames, pixel_type, fallback_only)
return itk.imread(filenames, pixel_type, fallback_only, imageio)

@staticmethod
def extract_series(dirname, series=None):
Expand Down

0 comments on commit c020180

Please sign in to comment.