Skip to content

Commit

Permalink
Merge pull request #8 from RSIP-Vision/elazarcoh/issue5
Browse files Browse the repository at this point in the history
Support private fields in header
  • Loading branch information
elazarcoh committed Mar 20, 2022
2 parents d7dac3c + beeef06 commit 68a025a
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions medio/backends/itk_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def read_img(
pixel_type=pixel_type,
fallback_only=True,
series=None,
private_tags=False,
):
"""
The main reader function, reads images and performs reorientation and unpacking
Expand All @@ -43,13 +44,33 @@ def read_img(
:return: numpy image and metadata object which includes pixdim, affine, original orientation string and
coordinates system
"""
if header:
# Currently, only DICOM will use imageio (GDCM). For NIfTI, we'll use ITK default reader.
imageio = itk.GDCMImageIO.New()
if private_tags:
imageio.LoadPrivateTagsOn()
else:
imageio = None
input_path = Path(input_path)
if input_path.is_dir():
# We assume that the directory contains dicom series, do imageio will work, if used.
if imageio is not None:
# 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
img = ItkIO.read_dir(
str(input_path), pixel_type, fallback_only, series=series, header=header
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)
# If the input file is not a dicom (e.g. NIfTI), fallback to not use imageio.
use_dicom_imageio = imageio.CanReadFile(str(input_path))
if use_dicom_imageio:
# 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
img = ItkIO.read_img_file(
str(input_path), pixel_type, fallback_only, imageio
)
else:
raise FileNotFoundError(f'No such file or directory: "{input_path}"')

Expand All @@ -65,8 +86,10 @@ def read_img(
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()
if imageio:
metadict = imageio.GetMetaDataDictionary()
else:
metadict = img.GetMetaDataDictionary()
metadata.header = {
key: metadict[key]
for key in metadict.GetKeys()
Expand Down Expand Up @@ -174,14 +197,15 @@ def prepare_dcm_array(image_np, is_vector=False):
)

@staticmethod
def read_img_file(filename, pixel_type=None, fallback_only=False):
def read_img_file(filename, pixel_type=None, fallback_only=False, imageio=None):
"""Common pixel types: itk.SS (int16), itk.US (uint16), itk.UC (uint8)"""
return itk.imread(filename, pixel_type, fallback_only)
return itk.imread(filename, pixel_type, fallback_only, imageio)

@staticmethod
def read_img_file_long(filename, image_type=image_type):
"""Longer version of itk.imread that returns the itk image and io engine string"""
reader = itk.ImageFileReader[image_type].New()
reader.LoadPrivateTagsOn()
reader.SetFileName(filename)
reader.Update()
image_io = str(reader.GetImageIO()).split(" ")[0]
Expand Down Expand Up @@ -285,7 +309,7 @@ def reorient(img, desired_orientation: Union[int, tuple, str, None]):

@staticmethod
def read_dir(
dirname, pixel_type=None, fallback_only=False, series=None, header=False
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
Expand All @@ -294,16 +318,7 @@ def read_dir(
>>> 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 68a025a

Please sign in to comment.