Repository navigation
Fixes #9168 #9172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Fixes #9168 #9172
Changes from all commits
bf6edb0
c089055
8c9f768
f94fad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -343,28 +343,28 @@ 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. | ||
| 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()) | ||
|
|
||
| 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. | ||
| # ---> INSERT THE 2ND PART HERE <--- | ||
| 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 | ||
|
|
||
| """ | ||
| direction = itk.array_from_matrix(img.GetDirection()) | ||
| spacing = np.asarray(img.GetSpacing()) | ||
| origin = np.asarray(img.GetOrigin()) | ||
| affine = np.eye(len(spacing) + 1) | ||
| affine[: len(spacing), : len(spacing)] = direction * spacing | ||
| affine[: len(spacing), len(spacing)] = origin | ||
|
|
||
| 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] | ||
| if user_affine_lps_to_ras and len(spacing) >= 3: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Convert two-dimensional affines when requested. For a two-dimensional image, this condition skips conversion even when 🤖 Prompt for AI Agents |
||
| affine = self._lps_to_ras(affine) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win Call the existing affine conversion function. For a three-dimensional image with 🤖 Prompt for AI Agents |
||
|
|
||
| if lps_to_ras: | ||
| affine = orientation_ras_lps(affine) | ||
| return affine | ||
|
|
||
| def _get_spatial_shape(self, img): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win
Read the key from the ITK metadata dictionary.
After the signature is fixed,
sitk_img.HasKey(...)raisesAttributeErroron anitk.Image, even when the key is absent.HasKeybelongs toGetMetaDataDictionary(); read the value from that dictionary too. The currentValueErrorhandler cannot catch this failure. (docs.itk.org)🤖 Prompt for AI Agents