Skip to content

Commit

Permalink
Simpler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWilkie committed Nov 8, 2024
1 parent 20d0a30 commit da844d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
21 changes: 4 additions & 17 deletions darwin/importer/formats/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _parse_nifti(
is_mpr: bool,
legacy: bool = False,
) -> dt.AnnotationFile:
img, pixdims = process_nifti(nib.load(nifti_path), legacy=legacy)
img, pixdims = process_nifti(nib.load(nifti_path))

processed_class_map = process_class_map(class_map)
video_annotations = []
Expand Down Expand Up @@ -513,10 +513,9 @@ def correct_nifti_header_if_necessary(img_nii):
def process_nifti(
input_data: nib.nifti1.Nifti1Image,
ornt: Optional[List[List[float]]] = [[0.0, -1.0], [1.0, -1.0], [2.0, -1.0]],
legacy: bool = False,
) -> Tuple[np.ndarray, Tuple[float]]:
"""
Function that converts a nifti object to RAS orientation (if legacy), then converts to the passed ornt orientation.
Function that converts a nifti object to the RAS orientation, then converts to the passed ornt orientation.
The default ornt is LPI.
Args:
Expand All @@ -530,20 +529,8 @@ def process_nifti(
pixdims: tuple of nifti header zoom values.
"""
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)
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)

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

Expand Down
13 changes: 4 additions & 9 deletions tests/darwin/importer/formats/import_nifti_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,7 @@ 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):
def test_process_nifti_orinetation(team_slug_darwin_json_v2):
with tempfile.TemporaryDirectory() as tmpdir:
with ZipFile("tests/data.zip") as zfile:
zfile.extractall(tmpdir)
Expand All @@ -259,12 +254,12 @@ def test_process_nifti_orinetation_no_legacy(team_slug_darwin_json_v2):
)
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(
lpi_transformed_file = nib.orientations.apply_orientation(
ras_file.get_fdata(), lpi_ornt
)
processed_file, _ = process_nifti(input_data=ras_file, legacy=False)
processed_file, _ = process_nifti(input_data=ras_file)
assert not np.array_equal(processed_file, ras_file._dataobj)
assert np.array_equal(processed_file, las_transformed_file)
assert np.array_equal(processed_file, lpi_transformed_file)


def serialise_annotation_file(
Expand Down

0 comments on commit da844d4

Please sign in to comment.