From 62ab388bf1e65e1a1700c6194740f057adb4b6b1 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Mon, 14 Mar 2022 18:37:44 +0800 Subject: [PATCH 1/3] [DLMED] enhance reader Signed-off-by: Nic Ma --- monai/transforms/io/array.py | 19 +++++++++++++------ tests/test_load_image.py | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/monai/transforms/io/array.py b/monai/transforms/io/array.py index 7127b054db..b74ec1f76a 100644 --- a/monai/transforms/io/array.py +++ b/monai/transforms/io/array.py @@ -107,9 +107,12 @@ def __init__( reader: reader to load image file and meta data - if `reader` is None, a default set of `SUPPORTED_READERS` will be used. - - if `reader` is a string, the corresponding item in `SUPPORTED_READERS` will be used, - and a reader instance will be constructed with the `*args` and `**kwargs` parameters. - the supported reader names are: "nibabelreader", "pilreader", "itkreader", "numpyreader". + - if `reader` is a string, it will follow the order to get target reader class: + 1. search all the reader classes in `monai.data` module. + 2. for any other subclass of `ImageImage`, provide the full path, like "my.custom.Reader". + 3. search the corresponding item in `SUPPORTED_READERS`, supported reader names are: + "nibabelreader", "pilreader", "itkreader", "numpyreader". + a reader instance will be constructed with the `*args` and `**kwargs` parameters. - if `reader` is a reader class/instance, it will be registered to this loader accordingly. image_only: if True return only the image volume, otherwise return image data array and header dict. @@ -153,15 +156,19 @@ def __init__( for _r in ensure_tuple(reader): if isinstance(_r, str): - the_reader = look_up_option(_r.lower(), SUPPORTED_READERS) + the_reader, has_built_in = optional_import("monai.data", name=f"{_r}") # search built-in + if not has_built_in: + the_reader = locate(f"{_r}") # search dotted path + if the_reader is None: + the_reader = look_up_option(_r.lower(), SUPPORTED_READERS) try: self.register(the_reader(*args, **kwargs)) except OptionalImportError: warnings.warn( - f"required package for reader {r} is not installed, or the version doesn't match requirement." + f"required package for reader {_r} is not installed, or the version doesn't match requirement." ) except TypeError: # the reader doesn't have the corresponding args/kwargs - warnings.warn(f"{r} is not supported with the given parameters {args} {kwargs}.") + warnings.warn(f"{_r} is not supported with the given parameters {args} {kwargs}.") self.register(the_reader()) elif inspect.isclass(_r): self.register(_r(*args, **kwargs)) diff --git a/tests/test_load_image.py b/tests/test_load_image.py index 2c8638ebbe..201fe2fd5b 100644 --- a/tests/test_load_image.py +++ b/tests/test_load_image.py @@ -126,7 +126,7 @@ def get_data(self, _obj): TEST_CASE_16 = [{"reader": "itkreader", "channel_dim": 0}, "test_image.nii.gz", (3, 128, 128, 128)] -TEST_CASE_17 = [{"reader": "ITKReader", "channel_dim": -1}, "test_image.nii.gz", (128, 128, 128, 3)] +TEST_CASE_17 = [{"reader": "monai.data.ITKReader", "channel_dim": -1}, "test_image.nii.gz", (128, 128, 128, 3)] TEST_CASE_18 = [ {"reader": "ITKReader", "channel_dim": 2, "ensure_channel_first": True}, From 4c6a55bddcba216d7d5d5b3b56b9618e6316c49f Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Tue, 15 Mar 2022 21:47:19 +0800 Subject: [PATCH 2/3] [DLMED] update according to comments Signed-off-by: Nic Ma --- monai/transforms/io/array.py | 8 +++----- monai/transforms/io/dictionary.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/monai/transforms/io/array.py b/monai/transforms/io/array.py index b74ec1f76a..f267d268c7 100644 --- a/monai/transforms/io/array.py +++ b/monai/transforms/io/array.py @@ -107,11 +107,9 @@ def __init__( reader: reader to load image file and meta data - if `reader` is None, a default set of `SUPPORTED_READERS` will be used. - - if `reader` is a string, it will follow the order to get target reader class: - 1. search all the reader classes in `monai.data` module. - 2. for any other subclass of `ImageImage`, provide the full path, like "my.custom.Reader". - 3. search the corresponding item in `SUPPORTED_READERS`, supported reader names are: - "nibabelreader", "pilreader", "itkreader", "numpyreader". + - if `reader` is a string, it's treated as a class name or dotted path + (such as ``"monai.data.ITKReader"``), the supported built-in reader classes are + ``"ITKReader"``, ``"NibabelReader"``, ``"NumpyReader"``. a reader instance will be constructed with the `*args` and `**kwargs` parameters. - if `reader` is a reader class/instance, it will be registered to this loader accordingly. diff --git a/monai/transforms/io/dictionary.py b/monai/transforms/io/dictionary.py index 32ccbb5313..db9629f3ce 100644 --- a/monai/transforms/io/dictionary.py +++ b/monai/transforms/io/dictionary.py @@ -82,10 +82,15 @@ def __init__( Args: keys: keys of the corresponding items to be transformed. See also: :py:class:`monai.transforms.compose.MapTransform` - reader: register reader to load image file and meta data, if None, still can register readers - at runtime or use the default readers. If a string of reader name provided, will construct - a reader object with the `*args` and `**kwargs` parameters, supported reader name: "NibabelReader", - "PILReader", "ITKReader", "NumpyReader". + reader: reader to load image file and meta data + + - if `reader` is None, a default set of `SUPPORTED_READERS` will be used. + - if `reader` is a string, it's treated as a class name or dotted path + (such as ``"monai.data.ITKReader"``), the supported built-in reader classes are + ``"ITKReader"``, ``"NibabelReader"``, ``"NumpyReader"``. + a reader instance will be constructed with the `*args` and `**kwargs` parameters. + - if `reader` is a reader class/instance, it will be registered to this loader accordingly. + dtype: if not None, convert the loaded image data to this data type. meta_keys: explicitly indicate the key to store the corresponding meta data dictionary. the meta data is a dictionary object which contains: filename, original_shape, etc. From 3c0720b2a904a6d02422411b2f33a5a8992935dd Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Tue, 15 Mar 2022 22:03:06 +0800 Subject: [PATCH 3/3] [DLMED] fix docs Signed-off-by: Nic Ma --- monai/transforms/io/array.py | 4 +--- monai/transforms/io/dictionary.py | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/monai/transforms/io/array.py b/monai/transforms/io/array.py index f267d268c7..5bafd84eaf 100644 --- a/monai/transforms/io/array.py +++ b/monai/transforms/io/array.py @@ -105,14 +105,12 @@ def __init__( """ Args: reader: reader to load image file and meta data - - if `reader` is None, a default set of `SUPPORTED_READERS` will be used. - if `reader` is a string, it's treated as a class name or dotted path (such as ``"monai.data.ITKReader"``), the supported built-in reader classes are ``"ITKReader"``, ``"NibabelReader"``, ``"NumpyReader"``. - a reader instance will be constructed with the `*args` and `**kwargs` parameters. + a reader instance will be constructed with the `*args` and `**kwargs` parameters. - if `reader` is a reader class/instance, it will be registered to this loader accordingly. - image_only: if True return only the image volume, otherwise return image data array and header dict. dtype: if not None convert the loaded image to this data type. ensure_channel_first: if `True` and loaded both image array and meta data, automatically convert diff --git a/monai/transforms/io/dictionary.py b/monai/transforms/io/dictionary.py index db9629f3ce..30dedc7810 100644 --- a/monai/transforms/io/dictionary.py +++ b/monai/transforms/io/dictionary.py @@ -83,14 +83,12 @@ def __init__( keys: keys of the corresponding items to be transformed. See also: :py:class:`monai.transforms.compose.MapTransform` reader: reader to load image file and meta data - - if `reader` is None, a default set of `SUPPORTED_READERS` will be used. - if `reader` is a string, it's treated as a class name or dotted path (such as ``"monai.data.ITKReader"``), the supported built-in reader classes are ``"ITKReader"``, ``"NibabelReader"``, ``"NumpyReader"``. - a reader instance will be constructed with the `*args` and `**kwargs` parameters. + a reader instance will be constructed with the `*args` and `**kwargs` parameters. - if `reader` is a reader class/instance, it will be registered to this loader accordingly. - dtype: if not None, convert the loaded image data to this data type. meta_keys: explicitly indicate the key to store the corresponding meta data dictionary. the meta data is a dictionary object which contains: filename, original_shape, etc.