From bf6edb0e3ab928c055c0481a8d95eb2823a62917 Mon Sep 17 00:00:00 2001 From: Aprameyaraya <91876432+Aprameyaraya@users.noreply.github.com> Date: Wed, 14 Oct 2026 10:41:00 +0530 Subject: [PATCH 1/4] Refactor _get_affine method for improved clarity Signed-off-by: Aprameyaraya <91876432+Aprameyaraya@users.noreply.github.com> --- monai/data/image_reader.py | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 0a853372f43..1e55712cf1e 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -343,28 +343,24 @@ def _get_meta_dict(self, img) -> dict: meta_dict["spacing"] = np.asarray(img.GetSpacing()) return meta_dict - def _get_affine(self, img, lps_to_ras: bool = True): - """ - Get or construct the affine matrix of the image, it can be used to correct - spacing, orientation or execute spatial transforms. - - Args: - img: an ITK image object loaded from an image file. - lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to True. + def _get_affine(self, sitk_img: Any) -> np.ndarray: + spacing = np.array(sitk_img.GetSpacing()) + direction = np.array(sitk_img.GetDirection()).reshape(len(spacing), len(spacing)) + origin = np.array(sitk_img.GetOrigin()) - """ - direction = itk.array_from_matrix(img.GetDirection()) - spacing = np.asarray(img.GetSpacing()) - origin = np.asarray(img.GetOrigin()) - - direction = np.asarray(direction) - sr = min(max(direction.shape[0], 1), 3) - affine: np.ndarray = np.eye(sr + 1) - affine[:sr, :sr] = direction[:sr, :sr] @ np.diag(spacing[:sr]) - affine[:sr, -1] = origin[:sr] + # Fix for negative SpacingBetweenSlices (Issue #9168) + if sitk_img.HasKey("ITK_SpacingBetweenSlices"): + try: + s_slice = float(sitk_img.GetMetaData("ITK_SpacingBetweenSlices")) + if len(spacing) >= 3 and s_slice < 0: + spacing[2] = abs(s_slice) + direction[:, 2] = -direction[:, 2] + except ValueError: + pass - if lps_to_ras: - affine = orientation_ras_lps(affine) + affine = np.eye(len(spacing) + 1) + affine[:len(spacing), :len(spacing)] = direction * spacing + affine[:len(spacing), len(spacing)] = origin return affine def _get_spatial_shape(self, img): From c089055a98048ec39170cdc25528543fb039457c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 Oct 2026 05:13:39 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/data/image_reader.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 1e55712cf1e..2118be19b2c 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -359,8 +359,8 @@ def _get_affine(self, sitk_img: Any) -> np.ndarray: pass affine = np.eye(len(spacing) + 1) - affine[:len(spacing), :len(spacing)] = direction * spacing - affine[:len(spacing), len(spacing)] = origin + affine[: len(spacing), : len(spacing)] = direction * spacing + affine[: len(spacing), len(spacing)] = origin return affine def _get_spatial_shape(self, img): From 8c9f76841e80eec239f129c55b61c7cc01b55a25 Mon Sep 17 00:00:00 2001 From: Aprameyaraya <91876432+Aprameyaraya@users.noreply.github.com> Date: Wed, 14 Oct 2026 11:06:09 +0530 Subject: [PATCH 3/4] Update image_reader.py Signed-off-by: Aprameyaraya <91876432+Aprameyaraya@users.noreply.github.com> --- monai/data/image_reader.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 2118be19b2c..af4fd42870b 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -343,12 +343,12 @@ def _get_meta_dict(self, img) -> dict: meta_dict["spacing"] = np.asarray(img.GetSpacing()) return meta_dict - def _get_affine(self, sitk_img: Any) -> np.ndarray: + def _get_affine(self, sitk_img: Any, user_affine_lps_to_ras: bool = True) -> np.ndarray: spacing = np.array(sitk_img.GetSpacing()) direction = np.array(sitk_img.GetDirection()).reshape(len(spacing), len(spacing)) origin = np.array(sitk_img.GetOrigin()) - # Fix for negative SpacingBetweenSlices (Issue #9168) + # ---> INSERT THE 2ND PART HERE <--- if sitk_img.HasKey("ITK_SpacingBetweenSlices"): try: s_slice = float(sitk_img.GetMetaData("ITK_SpacingBetweenSlices")) @@ -359,8 +359,12 @@ def _get_affine(self, sitk_img: Any) -> np.ndarray: pass affine = np.eye(len(spacing) + 1) - affine[: len(spacing), : len(spacing)] = direction * spacing - affine[: len(spacing), len(spacing)] = origin + affine[:len(spacing), :len(spacing)] = direction * spacing + affine[:len(spacing), len(spacing)] = origin + + if user_affine_lps_to_ras and len(spacing) >= 3: + affine = self._lps_to_ras(affine) + return affine def _get_spatial_shape(self, img): From f94fad27b6410194021321a0d36546ceff65c2d3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 Oct 2026 05:39:08 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/data/image_reader.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index af4fd42870b..720d47ddfa5 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -359,12 +359,12 @@ def _get_affine(self, sitk_img: Any, user_affine_lps_to_ras: bool = True) -> np. pass affine = np.eye(len(spacing) + 1) - affine[:len(spacing), :len(spacing)] = direction * spacing - affine[:len(spacing), len(spacing)] = origin - + affine[: len(spacing), : len(spacing)] = direction * spacing + affine[: len(spacing), len(spacing)] = origin + if user_affine_lps_to_ras and len(spacing) >= 3: affine = self._lps_to_ras(affine) - + return affine def _get_spatial_shape(self, img):