From da844d497acf59b3369495f53cafd9434cd35093 Mon Sep 17 00:00:00 2001 From: John Wilkie Date: Fri, 8 Nov 2024 11:12:46 +0000 Subject: [PATCH] Simpler implementation --- darwin/importer/formats/nifti.py | 21 ++++--------------- .../importer/formats/import_nifti_test.py | 13 ++++-------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/darwin/importer/formats/nifti.py b/darwin/importer/formats/nifti.py index 8f4722164..3d6a32d17 100644 --- a/darwin/importer/formats/nifti.py +++ b/darwin/importer/formats/nifti.py @@ -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 = [] @@ -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: @@ -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 diff --git a/tests/darwin/importer/formats/import_nifti_test.py b/tests/darwin/importer/formats/import_nifti_test.py index 58cd6a28b..eebe54782 100644 --- a/tests/darwin/importer/formats/import_nifti_test.py +++ b/tests/darwin/importer/formats/import_nifti_test.py @@ -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) @@ -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(