Skip to content

Commit d1e94f7

Browse files
authored
Remove pinning highest versions of monai. (#560)
* Remove pinning highest versions of holoscan and monai. This is done after having analyzed and found resolutions to issues in example apps with monai v1.5.1, and after testing the fix to the monai model inference configs that mix up RAS with LPS Signed-off-by: M Q <[email protected]> * Still need to cap holoscan at v3.5.0 needing Python 3.9 for Doc build] Signed-off-by: M Q <[email protected]> * Correct word used in constant name\ Signed-off-by: M Q <[email protected]> * Rerun notebook Signed-off-by: M Q <[email protected]> --------- Signed-off-by: M Q <[email protected]>
1 parent a1c067b commit d1e94f7

File tree

6 files changed

+269
-789
lines changed

6 files changed

+269
-789
lines changed

monai/deploy/operators/dicom_series_to_volume_operator.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class DICOMSeriesToVolumeOperator(Operator):
3939
image: Image object.
4040
"""
4141

42+
# Use constants instead of enums in monai to avoid dependency at this level.
43+
MONAI_UTIL_ENUMS_SPACEKEYS_LPS = "LPS"
44+
MONAI_TRANSFORMS_SPATIAL_METADATA_NAME = "space"
45+
METADATA_SPACE_LPS = {MONAI_TRANSFORMS_SPATIAL_METADATA_NAME: MONAI_UTIL_ENUMS_SPACEKEYS_LPS}
46+
4247
def __init__(self, fragment: Fragment, *args, **kwargs):
4348
"""Create an instance for a containing application object.
4449
@@ -84,6 +89,18 @@ def convert_to_image(self, study_selected_series_list: List[StudySelectedSeries]
8489
metadata.update(self._get_instance_properties(study_selected_series.study))
8590
selection_metadata = {"selection_name": selection_name}
8691
metadata.update(selection_metadata)
92+
# Add the metadata to specify LPS.
93+
# Previously, this was set in ImageReader class, but moving it here allows other loaders
94+
# to determine this value on its own, e.g. NIfTI loader but it does not set this
95+
# resulting in the MONAI Orientation transform to default the labels to RAS.
96+
# It is assumed that the ImageOrientationPatient will be set accordingly if the
97+
# PatientPosition is other than HFS.
98+
# NOTE: This value is properly parsed by MONAI Orientation transform from v1.5.1 onwards.
99+
# Some early MONAI model inference configs incorrectly specify orientation to RAS
100+
# due part to previous MONAI versions did not correctly parse this metadata from
101+
# the input MetaTensor and defaulting to RAS. Now with LPS properly set, the inference
102+
# configs then need to be updated to specify LPS, to achieve the same result.
103+
metadata.update(self.METADATA_SPACE_LPS)
87104

88105
voxel_data = self.generate_voxel_data(dicom_series)
89106
image = self.create_volumetric_image(voxel_data, metadata)

monai/deploy/operators/monai_bundle_inference_operator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def _init_config(self, config_names):
614614
)
615615

616616
def _get_compose(self, obj_name, disallowed_prefixes):
617-
"""Gets a Compose object containing a sequence fo transforms from item `obj_name` in `self._parser`."""
617+
"""Gets a Compose object containing a sequence of transforms from item `obj_name` in `self._parser`."""
618618

619619
if self._parser.get(obj_name) is not None:
620620
compose = self._parser.get_parsed_content(obj_name)
@@ -749,6 +749,7 @@ def compute(self, op_input, op_output, context):
749749
raise ValueError("`meta_data` must be a dict.")
750750
value = MetaTensor.ensure_torch_and_prune_meta(value, meta_data)
751751
inputs[name] = value
752+
logging.debug(f"Input MetaTensor metadata 'space': {value.meta.get('space', None)}")
752753
# Named metadata dict not needed any more, as it is in the MetaTensor
753754

754755
inputs = self.pre_process(inputs)
@@ -1062,7 +1063,8 @@ def _convert_from_image_dicom_source(self, img: Image) -> Tuple[np.ndarray, Dict
10621063
# Use defines MetaKeys directly
10631064
meta_dict[MetaKeys.ORIGINAL_AFFINE] = np.asarray(img_meta_dict.get("nifti_affine_transform", None))
10641065
meta_dict[MetaKeys.AFFINE] = meta_dict[MetaKeys.ORIGINAL_AFFINE].copy()
1065-
meta_dict[MetaKeys.SPACE] = SpaceKeys.LPS # not using SpaceKeys.RAS or affine_lps_to_ras
1066+
# Disabled setting the SPACE key below as it has changed to be set in the actual loader implementation.
1067+
# meta_dict[MetaKeys.SPACE] = SpaceKeys.LPS # not using SpaceKeys.RAS or affine_lps_to_ras
10661068

10671069
# Similarly the Image ndarray has dim order DHW, to be rearranged to WHD.
10681070
# TODO: Need to revisit this once multi-channel image is supported and the Image class itself

monai/deploy/operators/monai_seg_inference_operator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def filter_sw_kwargs(**kwargs) -> Tuple[Dict[str, Any], Dict[str, Any]]:
9090
- Are not explicitly defined in the __init__ of this class
9191
- Are not explicitly used when calling sliding_window_inference
9292
93-
2. A dicionary of named parameters to pass to the base class __init__ of this class that:
93+
2. A dictionary of named parameters to pass to the base class __init__ of this class that:
9494
- Are not used when calling sliding_window_inference
9595
- Can be successfully converted from Python --> Holoscan's C++ layer
9696
@@ -582,7 +582,8 @@ def _get_meta_dict(self, img: Image) -> Dict:
582582
# Use define metadata kyes directly
583583
meta_dict[MetaKeys.ORIGINAL_AFFINE] = np.asarray(img_meta_dict.get("nifti_affine_transform", None))
584584
meta_dict[MetaKeys.AFFINE] = meta_dict[MetaKeys.ORIGINAL_AFFINE].copy()
585-
meta_dict[MetaKeys.SPACE] = SpaceKeys.LPS # not using SpaceKeys.RAS or affine_lps_to_ras
585+
# Disabled setting the SPACE key below as it has changed to be set in the actual loader implementation.
586+
# meta_dict[MetaKeys.SPACE] = SpaceKeys.LPS # not using SpaceKeys.RAS or affine_lps_to_ras
586587
# The spatial shape, again, referring to ITKReader, it is the WHD
587588
meta_dict[MetaKeys.SPATIAL_SHAPE] = np.asarray(img.asnumpy().T.shape)
588589
# Well, no channel as the image data shape is forced to the the same as spatial shape

0 commit comments

Comments
 (0)