From b93205ea8519fe2c7dbb8b47a64a4dc1cecb2575 Mon Sep 17 00:00:00 2001 From: Taylor Salo Date: Fri, 19 Jan 2024 15:35:44 -0500 Subject: [PATCH] Fix TR in denoised NIfTI headers (#1038) --- xcp_d/interfaces/nilearn.py | 6 ++++++ xcp_d/tests/test_cli.py | 2 +- xcp_d/tests/utils.py | 19 ++++++++++--------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/xcp_d/interfaces/nilearn.py b/xcp_d/interfaces/nilearn.py index c0b5b6e18..81951551d 100644 --- a/xcp_d/interfaces/nilearn.py +++ b/xcp_d/interfaces/nilearn.py @@ -395,6 +395,9 @@ def _run_interface(self, runtime): "uncensored_denoised.nii.gz", ) uncensored_denoised_img = masker.inverse_transform(uncensored_denoised_bold) + pixdim = list(uncensored_denoised_img.header.get_zooms()) + pixdim[3] = self.inputs.TR + uncensored_denoised_img.header.set_zooms(pixdim) uncensored_denoised_img.to_filename(self._results["uncensored_denoised_bold"]) self._results["interpolated_filtered_bold"] = os.path.join( @@ -402,6 +405,9 @@ def _run_interface(self, runtime): "filtered_denoised.nii.gz", ) filtered_denoised_img = masker.inverse_transform(interpolated_filtered_bold) + pixdim = list(filtered_denoised_img.header.get_zooms()) + pixdim[3] = self.inputs.TR + filtered_denoised_img.header.set_zooms(pixdim) filtered_denoised_img.to_filename(self._results["interpolated_filtered_bold"]) return runtime diff --git a/xcp_d/tests/test_cli.py b/xcp_d/tests/test_cli.py index f0e4cd297..a85283bc7 100644 --- a/xcp_d/tests/test_cli.py +++ b/xcp_d/tests/test_cli.py @@ -197,7 +197,7 @@ def test_ukbiobank(data_dir, output_dir, working_dir): opts.work_dir, "dset_bids/derivatives/ukb", ) - check_affines(converted_fmri_dir, out_dir, input_type="nifti") + check_affines(converted_fmri_dir, out_dir, input_type="ukb") @pytest.mark.pnc_cifti diff --git a/xcp_d/tests/utils.py b/xcp_d/tests/utils.py index 2838cbe63..2a88d035e 100644 --- a/xcp_d/tests/utils.py +++ b/xcp_d/tests/utils.py @@ -122,7 +122,7 @@ def check_affines(data_dir, out_dir, input_type): extension=".dtseries.nii", ) - elif input_type == "nifti": # Get the .nii.gz + elif input_type in ("nifti", "ukb"): # Get the .nii.gz # Problem: it's collecting native-space data denoised_files = xcp_layout.get( datatype="func", @@ -155,17 +155,18 @@ def check_affines(data_dir, out_dir, input_type): preproc_file = preproc_files[0].path denoised_file = denoised_files[0].path + img1 = nb.load(preproc_file) + img2 = nb.load(denoised_file) if input_type == "cifti": - assert ( - nb.load(preproc_file)._nifti_header.get_intent() - == nb.load(denoised_file)._nifti_header.get_intent() - ) + assert img1._nifti_header.get_intent() == img2._nifti_header.get_intent() + np.testing.assert_array_equal(img1.nifti_header.get_zooms(), img2.nifti_header.get_zooms()) else: - if not np.array_equal(nb.load(preproc_file).affine, nb.load(denoised_file).affine): - raise AssertionError(f"Affines do not match:\n\t{preproc_file}\n\t{denoised_file}") - - print("No affines changed.") + np.testing.assert_array_equal(img1.affine, img2.affine) + if input_type != "ukb": + # The UK Biobank test dataset has the wrong TR in the header. + # I'll fix it at some point, but it's not the software's fault. + np.testing.assert_array_equal(img1.header.get_zooms(), img2.header.get_zooms()) def run_command(command, env=None):