Computing the group average connectivity matrix

Hi everyone,

I extracted the timeseries of each subject (100 timepoint x 400 ROIs) using nilearn and saved them as outer CSV file. Now I want to load these timeseries into memory and compute the group average timeseries for functional connectivity calculation.

I planned to read each of the CSV file with numpy.loadtxt as 2D array and combine all 2D array into a 3D array. The group average could be calculated by computing the mean over the third dimension of the 3D array. However, I am not an expert in Python, so I have no idea how to implement it in coding.

Do you know the way to calculate the group-average FC? Any ideas will be appreciated.

Best,
Qunjun

Make a list of your CSV file names, store it as files. Then:

import numpy
all_connectivity_matrices = [numpy.loadtxt(file) for file in files]
avg_matrix = numpy.average(all_connectivity_matrices,axis=2)

Best,
Steven

1 Like

Thank you so much, that is really helpful!

Moreover, I checked the document of the nilearn.ConnectivityMeasure and found that the function could compute multiple subjects’ FC in a list. Therefore, I first collected all subjects’ timeseries into one list and inputed the list into correlationMeasure.fit_transform(), resulting in a 3D array of subject x roi x roi. Finally, calling numpy.mean to obtain the group-mean FC along the subject dimension.

Your idea is more efficient than mine, I really appreciate that.

Best,
Qunjun