Skip to content

Commit

Permalink
[DAR-1649][Internal] Update process_nifti to take custom ornt (#818)
Browse files Browse the repository at this point in the history
* update process_nifti to take custom ornt

* docstring with details
  • Loading branch information
dorfmanrobert committed Apr 19, 2024
1 parent b97208b commit ec7a9a4
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions darwin/importer/formats/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,21 +470,28 @@ def correct_nifti_header_if_necessary(img_nii):


def process_nifti(
input_data: Union[Sequence[nib.nifti1.Nifti1Image], nib.nifti1.Nifti1Image]
):
input_data: nib.nifti1.Nifti1Image,
ornt: Optional[List[List[float]]] = [[0.0, -1.0], [1.0, -1.0], [2.0, -1.0]],
) -> Tuple[np.ndarray, Tuple[float]]:
"""
Function which takes in a single nifti path or a list of nifti paths
and returns the pixel_array, affine and pixdim
Function that converts a nifti object to RAS orientation, then converts to the passed ornt orientation.
The default ornt is for LPI.
Args:
input_data: nibabel nifti object.
ornt: (n,2) orientation array.
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]).
Returns:
data_array: pixel array with orientation ornt.
pixdims: tuple of nifti header zoom values.
"""
if isinstance(input_data, nib.nifti1.Nifti1Image):
img = correct_nifti_header_if_necessary(input_data)
img = nib.funcs.as_closest_canonical(img)
nib.orientations.aff2axcodes(img.affine)
# TODO: Future feature to pass custom ornt could go here.
ornt = [[0.0, -1.0], [1.0, -1.0], [1.0, -1.0]]
data_array = nib.orientations.apply_orientation(img.get_fdata(), ornt)
pixdims = img.header.get_zooms()
return data_array, pixdims
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()
return data_array, pixdims


def convert_to_dense_rle(raster: np.ndarray) -> List[int]:
Expand Down

0 comments on commit ec7a9a4

Please sign in to comment.