Skip to content

Commit

Permalink
Fix TR in denoised NIfTI headers (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo authored Jan 19, 2024
1 parent 81970c5 commit b93205e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 6 additions & 0 deletions xcp_d/interfaces/nilearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,19 @@ 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(
runtime.cwd,
"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
2 changes: 1 addition & 1 deletion xcp_d/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions xcp_d/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b93205e

Please sign in to comment.