Skip to content

Task 2: calculate functional connectivity

elhava edited this page May 29, 2023 · 2 revisions

Calculating functional connectivity

The functional connectivity in this data set is the temporal correlation between two brain areas. Specifically, we can use Pearson's correlation coefficient to estimate the functional connectivity between brain locations at two distinct image voxels or between two brain areas.

To calculate the functional connectivity, some preparation must be done.

Calculate the temporal confounds

Temporal confounds are time series that is minimally related to the interested brain activity. In fMRI, typical temporal confounds include

  • Systematic time-invariant shift (a.k.a. "DC") and a linear trend.
  • Involuntary participant's motion, which can be represented by rigid-body motion in x, y, and z directions.
  • Fluctuations at tissues with minimal brain activity, such as ventricles and white matter.
  • Get the time series at ventricles and white matter. Since each participant's data has been spatially registered to a template, we can use parcellation/segmentation tools to locate ventricles and white matter and derive their time series.
  • Use linear regression to model the confounds described above and to remove them
  • Calculate the correlation coefficients between brain locations or regions. We do not recommend calculating the voxel-wise correlation coefficients because there are about 10,000 brain location in the brain surface data (.stc files). This will generate a 10,000 x 10,000 correlation coefficient matrix, too large and unstable to be used efficiently. We recommend first group time series from different brain regions using parcellation labels, each of which represents a cortical surface areas. This will reduce the correlation coefficient matrix to 150 x 150 because each hemisphere has 75 cortical areas in the parcellation.
"""calculate functional connectivity for one single fMRI image
It will be better if you calculate confound matrix and consider them as well in calculating FCs"""

import os
import numpy as np
import nibabel as nib
from nilearn.image import resample_to_img
from nilearn.datasets import load_mni152_template
from nilearn.datasets import fetch_atlas_destrieux_2009
from nilearn.maskers import NiftiLabelsMasker
from nilearn.connectome import ConnectivityMeasure

#Load data
data_path = 'path to fmri dataset main folder'
img_func = nib.load(os.path.join(data_path, 'path to one single .nii.gz fmri image'))

# Resample ROI template to functional image resolution
template = load_mni152_template(resolution=2)
img_func = resample_to_img(img_func, template)

# Download the Destrieux Atlas labels in MNI152 space
atlas_filename = fetch_atlas_destrieux_2009()['maps']

# Load the atlas labels as a Nifti image
atlas_img = nib.load(atlas_filename)

# Print the number of regions in the atlas
n_regions = nib.freesurfer.read_annot('/content/drive/MyDrive/fMRI/lh.aparc.a2009s.annot')[0]
print(f'Number of regions in Destrieux Atlas: {max(n_regions)}')

# Create a masker to extract the ROI time series data and apply some preprocessing
masker = NiftiLabelsMasker(atlas_img, standardize=True, detrend=True, low_pass=0.1, high_pass=0.01, t_r=2)

# Extract the ROI time series data
roi_time_series = masker.fit_transform(img_func)

# Compute functional connectivity
correlation_measure = ConnectivityMeasure(kind='correlation')
correlation_matrix = correlation_measure.fit_transform([roi_time_series])[0]

# Threshold the correlation matrix
thresholded_matrix = np.where(np.abs(correlation_matrix) > 0.5, correlation_matrix, 0)

# Visualize the results
import matplotlib.pyplot as plt
plt.imshow(thresholded_matrix, cmap='coolwarm', interpolation='nearest')
plt.colorbar()
plt.show()

Clone this wiki locally