Example script on performing second level GLM with connectivity data in Python

Hi everyone,

For my resting state data I’ve done the preprocessing with fMRIPrep and calculated connectivity coefficients using nilearn.connectome. Now I need to do the second level GLM to compare two groups. Amazingly, despite there being lots of great nilearn tutorials and documentation on running a GLM for contrasting two task based conditions, I found zero guidance on doing it for connectivity coefficients. I also searched github and found no examples there either. I tried following the tutorial for task based GLM and adapted this piece of code with my connectivity coefficients which are in a subject by paired regions dataframe (so one row of coefficients per subject, and one columns for each set of paired regions):

    from nilearn.glm.second_level import SecondLevelModel
    
    second_level_model = SecondLevelModel(n_jobs=2).fit(
        connectivity_coefficients, design_matrix=design_matrix
    )
    
    z_map = second_level_model.compute_contrast(output_type="z_score")

But this doesn’t work because the input data is not in the same format.

Does anyone have some experience with this, or does anyone even have a python piece of code they would share that takes a design matrix and a dataframe with one row per subject which then runs a GLM?

Many thanks in advance!

not sure I understand, your dataframe contains

one row of coefficients per subject, and one columns for each set of paired regions

so each row has n values (n being the number of regions) ?

In that case nilearn is not the tool you need since it will only work at the second level if you have images for each subject

Yes that’s correct, so one row per subject, and one column per coefficient. E.g. one column might give the connectivity value between the ACC and the DLPFC. Yes I could see that the my data format is different from what SecondLevelModel expects, but I’m amazed that there is no way to adapt it for connectivity data, or that there isn’t a clear Python implementation of second level analysis for connectivity data.

well at this stage you should probably just use the stats part of scipy, no?

https://docs.scipy.org/doc/scipy/reference/stats.html

or statsmodels
https://www.statsmodels.org/stable/index.html

Thanks yes. I can do this. I had just also hoped to also find some working examples out there, so I could have some reassurance that I’m doing it correctly.

1 Like