Skip to content

Commit 21e59fc

Browse files
committed
Enhanced nii loader operator after testing with a single operator app.
Signed-off-by: M Q <mingmelvinq@nvidia.com>
1 parent 91652fc commit 21e59fc

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

monai/deploy/operators/nii_data_loader_operator.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12+
13+
import logging
1214
from pathlib import Path
1315

1416
import numpy as np
1517

16-
import monai.deploy.core as md
1718
from monai.deploy.core import ConditionType, Fragment, Operator, OperatorSpec
1819
from monai.deploy.utils.importutil import optional_import
1920

@@ -28,19 +29,20 @@ class NiftiDataLoader(Operator):
2829
This operator reads a nifti image, extracts the numpy array and forwards it to the next operator
2930
3031
Named input:
31-
image_path: Path to the image file, optional, used to override the path set in the object.
32+
image_path: Path to the image file, optional. Use it to override the input path set on the object.
3233
3334
Named output:
34-
image: A Numpy object in memory. Downstream receiver optional.
35+
image: A Numpy array object. Downstream receiver optional.
3536
"""
3637

3738
def __init__(self, fragment: Fragment, *args, input_path: Path, **kwargs) -> None:
3839
"""Creates an instance with the file path to load image from.
3940
4041
Args:
4142
fragment (Fragment): An instance of the Application class which is derived from Fragment.
42-
input_path (Path): The Path to read the image file from, overriden by the named input.
43+
input_path (Path): The file Path to read from, overridden by valid named input on compute.
4344
"""
45+
self._logger = logging.getLogger("{}.{}".format(__name__, type(self).__name__))
4446
self.input_path = input_path # Allow to be None, to be overridden when compute is called.
4547
self.input_name_path = "image_path"
4648
self.output_name_image = "image"
@@ -53,14 +55,17 @@ def setup(self, spec: OperatorSpec):
5355
spec.output(self.output_name_image).condition(ConditionType.NONE) # Fine for no or not-ready receiver ports.
5456

5557
def compute(self, op_input, op_output, context):
56-
input_path = None
57-
try:
58-
input_path = op_input.receive(self.input_name_path)
59-
except Exception:
60-
if self.input_path and not self.input_path.is_file():
58+
"""Performs computation with the provided context."""
59+
60+
# The named input port is optional, so must check for and validate the data
61+
input_path = op_input.receive(self.input_name_path)
62+
if not input_path or not Path(input_path).is_file:
63+
self._logger.info(f"No or invalid file path from the optional input port: {input_path}")
64+
# Try to fall back to use the object attribute if it is valid
65+
if self.input_path and self.input_path.is_file():
6166
input_path = self.input_path
6267
else:
63-
raise ValueError("No path given to load image from.")
68+
raise ValueError(f"No valid file path from input port or obj attribute: {self.input_path}")
6469

6570
image_np = self.convert_and_save(input_path)
6671
op_output.emit(image_np, self.output_name_image)
@@ -77,7 +82,8 @@ def convert_and_save(self, nii_path):
7782

7883

7984
def test():
80-
filepath = "/home/mqin/src/monai-deploy-app-sdk/inputs/lung_seg_ct/nii/volume-covid19-A-0001.nii" # "/home/gupta/Documents/mni_icbm152_nlin_sym_09a/mni_icbm152_gm_tal_nlin_sym_09a.nii"
85+
# Make sure the file path is correct.
86+
filepath = Path(__file__).parent.resolve() / "../../../inputs/lung_seg_ct/nii/volume-covid19-A-0001.nii"
8187
fragment = Fragment()
8288
nii_operator = NiftiDataLoader(fragment, input_path=filepath)
8389
_ = nii_operator.convert_and_save(filepath)

0 commit comments

Comments
 (0)