Extracting mean BOLD response from time course

Hey all.
I’m extracting ROI’s time series in a fMRI scan, in which there are four events in specific times.
Although by looking at the timecourse it’s clear that the haemodynamic response to the activation periods is present, I would like to extract the mean BOLD response (i.e the mean haemodynamic response).
So far I simply divided the time course’s data into four equal vectors and calculated a mean, but was wondering whether there’s a better way to extract this data.
(I’m using both fsl tools and spm, so the solution in hand is welcome to originate from any source)

Thanks in advance!

Ahoi hoi @Gal_Hershkovitz,

I think nilearn’s NiftiMasker might be able to help you with that. Once you’ve extracted your time series, you can simply index the resulting array to get specific times and calculate the mean across time.

Something like this:

# import 
from nilearn import input_data

# setup of NiftiMasker (remember to think about smoothing, detrending, standardization and if you need/want it)
nii_masker = input_data.NiftiMasker(smoothing_fwhm=6,
        detrend=True, standardize=True, mask_img = your_roi,
        low_pass=0.1, high_pass=0.01, t_r=2.,
        memory='nilearn_cache', memory_level=1, verbose=0)

# apply NiftiMasker, extracting time series, including confounds if you need/want
time_series = nii_masker.fit_transform(your_fmri_file, 

# get specific times, including all voxels of the ROI
time_series_1 = time_series[time_1_begin:time_2_end,:]

# compute mean signal across time
time_series_1_mean = np.mean(time_series_1)

HTH, cheers, Peer

1 Like

Thank you for the comment.
for some reason the second part of your suggestion raises a Memory Error (although there shouldn’t be a memory problem with the computer I’m using):

time_series = nii_masker.fit_transform(my_fmri_file)
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nilearn\input_data\base_masker.py”, line 207, in fit_transform
return self.fit(**fit_params).transform(X, confounds=confounds)
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nilearn\input_data\nifti_masker.py”, line 249, in fit
self.mask_img_ = _utils.check_niimg_3d(self.mask_img)
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nilearn_utils\niimg_conversions.py”, line 322, in check_niimg_3d
return check_niimg(niimg, ensure_ndim=3, dtype=dtype)
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nilearn_utils\niimg_conversions.py”, line 271, in check_niimg
niimg = load_niimg(niimg, dtype=dtype)
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nilearn_utils\niimg.py”, line 116, in load_niimg
dtype = _get_target_dtype(niimg.get_data().dtype, dtype)
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nibabel\dataobj_images.py”, line 203, in get_data
data = np.asanyarray(self._dataobj)
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\numpy\core\numeric.py”, line 591, in asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nibabel\arrayproxy.py”, line 355, in array
raw_data = self.get_unscaled()
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nibabel\arrayproxy.py”, line 350, in get_unscaled
File “C:\Users\Owner\Desktop\FSL_pipeline\venv\lib\site-packages\nibabel\volumeutils.py”, line 523, in array_from_file
data_bytes = bytearray(n_bytes)

Ahoi hoi @Gal_Hershkovitz,

could you maybe provide a) more information on your data and b) your code, i.e. how you set up NiftiMasker? There are a lot of reasons for memory errors, sure the main underlying one is that you computer runs out of memory, but that can be caused by multiple circumstances (i.e. your code).

Best, Peer