Skip to content
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

[DAR-4932][External] correct pixdim application in mask to polygon #968

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions darwin/importer/formats/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def adjust_for_pixdims(x, y, pixdims):
else:
return {"x": y, "y": x}
else:
return {"x": y * pixdims[1], "y": x * pixdims[0]}
return {"x": y * pixdims[0], "y": x * pixdims[1]}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the real change in logic.


_labels, external_paths, _internal_paths = find_contours(mask)
if len(external_paths) > 1:
Expand Down Expand Up @@ -515,12 +515,12 @@ def process_nifti(
ornt: Optional[List[List[float]]] = [[0.0, -1.0], [1.0, -1.0], [2.0, -1.0]],
) -> Tuple[np.ndarray, Tuple[float]]:
"""
Function that converts a nifti object to the RAS orientation, then converts to the passed ornt orientation.
Converts a nifti object of any orientation to the passed ornt orientation.
The default ornt is LPI.

Args:
input_data: nibabel nifti object.
ornt: (n,2) orientation array.
ornt: (n,2) orientation array. It defines a transformation from RAS.
ornt[N,1] is a flip of axis N of the array, where 1 means no flip and -1 means flip.
ornt[:,0] is the transpose that needs to be done to the implied array, as in arr.transpose(ornt[:,0]).

Expand All @@ -529,9 +529,14 @@ def process_nifti(
pixdims: tuple of nifti header zoom values.
"""
img = correct_nifti_header_if_necessary(input_data)
img = nib.funcs.as_closest_canonical(img)
data_array = nib.orientations.apply_orientation(img.get_fdata(), ornt)
pixdims = img.header.get_zooms()
orig_ax_codes = nib.orientations.aff2axcodes(img.affine)
orig_ornt = nib.orientations.axcodes2ornt(orig_ax_codes)
transform = nib.orientations.ornt_transform(orig_ornt, ornt)
reoriented_img = img.as_reoriented(transform)

data_array = reoriented_img.get_fdata()
pixdims = reoriented_img.header.get_zooms()

Copy link
Contributor Author

@dorfmanrobert dorfmanrobert Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes to process_nifti are unrelated and make the function more explicitly able to handle arbitrarily oriented outputs, even though this is not needed for importing to a fixed orientation of LPI.

Before, the pixdims were taken from volume reoriented to RAS, meaning the output orientation needed to be in RAS order as well for the pixdims to be ordered correctly. Now the output's axes can be any order.

return data_array, pixdims


Expand Down
Loading