Skip to content

Commit

Permalink
Fixed NifTI import orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWilkie committed Nov 3, 2024
1 parent 1a17d18 commit 20d0a30
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
15 changes: 13 additions & 2 deletions darwin/importer/formats/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def process_nifti(
) -> Tuple[np.ndarray, Tuple[float]]:
"""
Function that converts a nifti object to RAS orientation (if legacy), then converts to the passed ornt orientation.
The default ornt is for LPI.
The default ornt is LPI.
Args:
input_data: nibabel nifti object.
Expand All @@ -532,7 +532,18 @@ def process_nifti(
img = correct_nifti_header_if_necessary(input_data)
if legacy:
img = nib.funcs.as_closest_canonical(img)
data_array = nib.orientations.apply_orientation(img.get_fdata(), ornt)
data_array = nib.orientations.apply_orientation(img.get_fdata(), ornt)
else:
current_ornt = nib.orientations.io_orientation(img.affine)
adjusted_ornt = []
for idx, (target_axis, target_direction) in enumerate(ornt):
_, current_direction = current_ornt[idx]
if current_direction == target_direction:
adjusted_ornt.append([target_axis, 1])
else:
adjusted_ornt.append([target_axis, -1])
data_array = nib.orientations.apply_orientation(img.get_fdata(), adjusted_ornt)

pixdims = img.header.get_zooms()
return data_array, pixdims

Expand Down
31 changes: 30 additions & 1 deletion tests/darwin/importer/formats/import_nifti_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
import pytest
from scipy import ndimage
import nibabel as nib

from darwin.datatypes import (
Annotation,
Expand All @@ -17,7 +18,7 @@
SubAnnotation,
VideoAnnotation,
)
from darwin.importer.formats.nifti import get_new_axial_size, parse_path
from darwin.importer.formats.nifti import get_new_axial_size, parse_path, process_nifti
from tests.fixtures import *


Expand Down Expand Up @@ -238,6 +239,34 @@ def test_get_new_axial_size_with_isotropic():
assert new_size == (20, 10)


def test_process_nifti_orientation_legacy(team_slug_darwin_json_v2):
pass
# It's not yet clear how to write this as the desired behaviour isn't known


def test_process_nifti_orinetation_no_legacy(team_slug_darwin_json_v2):
with tempfile.TemporaryDirectory() as tmpdir:
with ZipFile("tests/data.zip") as zfile:
zfile.extractall(tmpdir)
filepath = (
Path(tmpdir)
/ team_slug_darwin_json_v2
/ "nifti"
/ "releases"
/ "latest"
/ "annotations"
/ "vol0_brain.nii.gz"
)
lpi_ornt = [[0.0, -1.0], [1.0, -1.0], [2.0, -1.0]]
ras_file = nib.load(filepath)
las_transformed_file = nib.orientations.apply_orientation(
ras_file.get_fdata(), lpi_ornt
)
processed_file, _ = process_nifti(input_data=ras_file, legacy=False)
assert not np.array_equal(processed_file, ras_file._dataobj)
assert np.array_equal(processed_file, las_transformed_file)


def serialise_annotation_file(
annotation_file: AnnotationFile, as_dict
) -> Union[str, dict]:
Expand Down

0 comments on commit 20d0a30

Please sign in to comment.