Skip to content

Commit 4e7768e

Browse files
committed
Use bundle_path property for consistency, and added warning note on nii loader
Signed-off-by: M Q <[email protected]>
1 parent a735c45 commit 4e7768e

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

monai/deploy/operators/monai_bundle_inference_operator.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ def __init__(
494494
# Complete the init if the bundle path is known, otherwise delay till the compute function is called
495495
# and try to get the model/bundle path from the execution context.
496496
try:
497+
# Original design is to rely on the Model Factory to find and load the model bundle file path, hence,
498+
# Complete the init if the bundle path is known, otherwise delay till the compute function is called
499+
# and try to get the model/bundle path from the execution context.
500+
# But supporting directory-based bundle with the app generator CLI may require the bundle path to be set
501+
497502
self._bundle_path = Path(bundle_path) if bundle_path and len(str(bundle_path).strip()) > 0 else None
498503

499504
if self._bundle_path and self._bundle_path.is_file():
@@ -537,9 +542,10 @@ def bundle_path(self) -> Union[Path, None]:
537542

538543
@bundle_path.setter
539544
def bundle_path(self, bundle_path: Union[str, Path]):
540-
if not bundle_path or not Path(bundle_path).expanduser().is_file():
545+
if bundle_path and (Path(bundle_path).expanduser().is_file() or Path(bundle_path).expanduser().is_dir()):
546+
self._bundle_path = Path(bundle_path).expanduser().resolve()
547+
else:
541548
raise ValueError(f"Value, {bundle_path}, is not a valid file path.")
542-
self._bundle_path = Path(bundle_path).expanduser().resolve()
543549

544550
@property
545551
def parser(self) -> Union[ConfigParser, None]:
@@ -561,12 +567,10 @@ def _init_config(self, config_names):
561567
"""
562568

563569
# Ensure bundle root is on sys.path for directory-based bundles
564-
if self._bundle_path:
565-
bundle_path_obj = Path(self._bundle_path)
566-
if bundle_path_obj.is_dir():
567-
_ensure_bundle_in_sys_path(bundle_path_obj)
570+
if self.bundle_path and self.bundle_path.is_dir():
571+
_ensure_bundle_in_sys_path(self.bundle_path)
568572

569-
parser = get_bundle_config(str(self._bundle_path), config_names)
573+
parser = get_bundle_config(str(self.bundle_path), config_names)
570574
self._parser = parser
571575

572576
meta = self.parser["_meta_"]
@@ -699,34 +703,34 @@ def compute(self, op_input, op_output, context):
699703
if not self._init_completed:
700704
with self._lock:
701705
if not self._init_completed:
702-
self._bundle_path = Path(self._model_network.path)
703-
logging.info(f"Parsing from bundle_path: {self._bundle_path}")
706+
# Use property setter to set the bundle path for consistency
707+
self.bundle_path = Path(self._model_network.path)
708+
logging.info(f"Parsing from bundle_path: {self.bundle_path}")
704709
self._init_config(self._bundle_config_names.config_names)
705710
self._init_completed = True
706-
elif self._bundle_path:
711+
elif self.bundle_path:
707712
# For the case of local dev/testing when the bundle path is not passed in as an exec cmd arg.
708713
# When run as a MAP docker, the bundle file is expected to be in the context, even if the model
709714
# network is loaded on a remote inference server (when the feature is introduced).
710-
logging.debug(f"Model network not loaded. Trying to load from model path: {self._bundle_path}")
715+
logging.debug(f"Model network not loaded. Trying to load from model path: {self.bundle_path}")
711716

712717
# Check if bundle_path is a directory
713-
bundle_path_obj = Path(self._bundle_path)
714-
if bundle_path_obj.is_dir():
718+
if self.bundle_path.is_dir():
715719
# Ensure device is set
716720
if not hasattr(self, "_device"):
717721
self._device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
718722

719723
# Initialize config for directory bundles if not already done
720724
if not self._init_completed:
721-
logging.info(f"Initializing config from directory bundle: {self._bundle_path}")
725+
logging.info(f"Initializing config from directory bundle: {self.bundle_path}")
722726
self._init_config(self._bundle_config_names.config_names)
723727
self._init_completed = True
724728

725729
# Load model using helper function
726-
self._model_network = _load_model_from_directory_bundle(bundle_path_obj, self._device, self._parser)
730+
self._model_network = _load_model_from_directory_bundle(self.bundle_path, self._device, self._parser)
727731
else:
728732
# Original ZIP bundle handling
729-
self._model_network = torch.jit.load(bundle_path_obj, map_location=self._device).eval()
733+
self._model_network = torch.jit.load(self.bundle_path, map_location=self._device).eval()
730734
else:
731735
raise IOError("Model network is not load and model file not found.")
732736

monai/deploy/operators/nii_data_loader_operator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323

2424
# @md.env(pip_packages=["SimpleITK>=2.0.2"])
2525
class NiftiDataLoader(Operator):
26+
# NOTE:
27+
# This loader has very limited use because it does not capture or embed the metadata of a NIfTI file.
28+
# Future improvements are needed to retrieve the metadata and return an Image object with the metadata,
29+
# or use monai image transform to load the image and return a meta tensor.
30+
# Also SITK GetArrayFromImage() returns an NDArray, and the newly added transpose here is to change the
31+
# index ordering to match the index ordering of the DCOM pixel array. Existing client of this operator,
32+
# if any, will likely have issues.
2633
"""
2734
This operator reads a nifti image, extracts the numpy array and forwards it to the next operator
2835

0 commit comments

Comments
 (0)