Dual-Echo Denoising with nilearn#
Dual-echo fMRI leverages one of the same principles motivating multi-echo fMRI; namely, that BOLD contrast increases with echo time, so earlier echoes tend to be more affected by non-BOLD noise than later ones. At an early enough echo time (<5ms for 3T scanners), the signal is almost entirely driven by non-BOLD noise. When it comes to denoising, this means that, if you acquire data with both an early echo time and a more typical echo time (~30ms for 3T), you can simply regress the earlier echo’s time series out of the later echo’s time series, which will remove a lot of non-BOLD noise.
Additionally, dual-echo fMRI comes at no real cost in terms of temporal or spatial resolution, unlike multi-echo fMRI. For multi-echo denoising to work, you need to have at least one echo time that is later than the typical echo time, which means decreasing your temporal resolution, all else remaining equal. In the case of dual-echo fMRI, you only need a shorter echo time, which occurs in what is essentially “dead time” in the pulse sequence.
Dual-echo denoising was originally proposed in Bright & Murphy (2013).
import json
import os
from glob import glob
import matplotlib.pyplot as plt
import nibabel as nb
from book_utils import regress_one_image_out_of_another
from myst_nb import glue
from nilearn import plotting
data_path = os.path.abspath('../DATA')
func_dir = os.path.join(data_path, "ds006185/sub-24053/ses-1/func/")
te1_img = os.path.join(
func_dir,
"sub-24053_ses-1_task-rat_rec-nordic_dir-PA_run-01_echo-1_part-mag_desc-preproc_bold.nii.gz",
)
te2_img = os.path.join(
func_dir,
"sub-24053_ses-1_task-rat_rec-nordic_dir-PA_run-01_echo-2_part-mag_desc-preproc_bold.nii.gz",
)
mask_img = os.path.join(
func_dir,
"sub-24053_ses-1_task-rat_rec-nordic_dir-PA_run-01_part-mag_desc-brain_mask.nii.gz"
)
denoised_img = regress_one_image_out_of_another(te2_img, te1_img, mask_img)
/home/runner/work/multi-echo-data-analysis/multi-echo-data-analysis/content/book_utils.py:10: FutureWarning: From release 0.13.0 onwards, this function will, by default, copy the header of the input image to the output. Currently, the header is reset to the default Nifti1Header. To suppress this warning and use the new behavior, set `copy_header=True`.
mean_data_img = image.mean_img(data_img)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[2], line 14
6 te2_img = os.path.join(
7 func_dir,
8 "sub-24053_ses-1_task-rat_rec-nordic_dir-PA_run-01_echo-2_part-mag_desc-preproc_bold.nii.gz",
9 )
10 mask_img = os.path.join(
11 func_dir,
12 "sub-24053_ses-1_task-rat_rec-nordic_dir-PA_run-01_part-mag_desc-brain_mask.nii.gz"
13 )
---> 14 denoised_img = regress_one_image_out_of_another(te2_img, te1_img, mask_img)
File ~/work/multi-echo-data-analysis/multi-echo-data-analysis/content/book_utils.py:10, in regress_one_image_out_of_another(data_img, nuis_img, mask_img)
8 """Do what it says on the tin."""
9 # First, mean-center each image over time
---> 10 mean_data_img = image.mean_img(data_img)
11 mean_nuis_img = image.mean_img(nuis_img)
13 data_img_mc = image.math_img(
14 "img - avg_img[..., None]",
15 img=data_img,
16 avg_img=mean_data_img,
17 )
File /opt/hostedtoolcache/Python/3.12.12/x64/lib/python3.12/site-packages/nilearn/image/image.py:593, in mean_img(imgs, target_affine, target_shape, verbose, n_jobs, copy_header)
588 imgs = [
589 imgs,
590 ]
592 imgs_iter = iter(imgs)
--> 593 first_img = check_niimg(next(imgs_iter))
595 # Compute the first mean to retrieve the reference
596 # target_affine and target_shape if_needed
597 n_imgs = 1
File /opt/hostedtoolcache/Python/3.12.12/x64/lib/python3.12/site-packages/nilearn/_utils/niimg_conversions.py:302, in check_niimg(niimg, ensure_ndim, atleast_4d, dtype, return_iterator, wildcards)
300 raise ValueError(message)
301 else:
--> 302 raise ValueError(f"File not found: '{niimg}'")
303 elif not Path(niimg).exists():
304 raise ValueError(f"File not found: '{niimg}'")
ValueError: File not found: '/home/runner/work/multi-echo-data-analysis/multi-echo-data-analysis/DATA/ds006185/sub-24053/ses-1/func/sub-24053_ses-1_task-rat_rec-nordic_dir-PA_run-01_echo-2_part-mag_desc-preproc_bold.nii.gz'
fig, axes = plt.subplots(figsize=(16, 16), nrows=3)
plotting.plot_carpet(te2_img, axes=axes[0], figure=fig)
axes[0].set_title("First Echo (BAD)", fontsize=20)
plotting.plot_carpet(te1_img, axes=axes[1], figure=fig)
axes[1].set_title("Second Echo (GOOD)", fontsize=20)
plotting.plot_carpet(denoised_img, axes=axes[2], figure=fig)
axes[2].set_title("Denoised Data (GREAT)", fontsize=20)
axes[0].xaxis.set_visible(False)
axes[1].xaxis.set_visible(False)
axes[0].spines["bottom"].set_visible(False)
axes[1].spines["bottom"].set_visible(False)
fig.tight_layout()
glue("figure_dual_echo_results", fig, display=False)