How to use nilearn to analyze fMRI data from multiple runs

Dear experts,

I am a novice user of nilearn and I have an MRI data with 4 runs. I would like to know how to analyze it using nilearn. So far, the tutorials I’ve seen only have a single run. Is it OK to merge multiple func imgs directly? In addition to the onset time of the first run, should the onset time of other runs be added with the time of the preceding runs? :face_with_spiral_eyes: :face_with_spiral_eyes: :face_with_spiral_eyes:

Thank you in advance for your input and patience with a newbie.

CFZ

1 Like

Hi @cfz and welcome to neurostars!

I would not merge the time series together, as there could be big differences in magnitude between runs due to low frequency drift that happens over the course of the scanning session. This tutorial might be helpful, showing that you can model a GLM using inputs from multiple runs Simple example of two-session fMRI model fitting - Nilearn.

Best,
Steven

1 Like

Thanks! That was quite helpful :smiling_face_with_three_hearts:.But when I analyzed the data as an example, I ran into a problem.My experiment was an event-dependent design, and in some runs, some conditions did not appear. This makes when I run nilearn.glm.first_level.FirstLevelModel.com pute_contrastsg, an error has occurred: t contrasts should be length P=18, but this is length 20. How can I solve it?

In addition, I have a question about what should be inputed in SecondLevelModel.fit. I did first-level analysis of data on afni, and I ran into this problem when I wanted to do a further analysis at nilearn. In fact, after performing the first level analysis on nilearn I was also confused about what to input for the second level analysis. :smiling_face_with_tear: :smiling_face_with_tear: :smiling_face_with_tear:

Thank you in advance for your input and patience with a newbie.

CFZ

For this, you can pad the smaller design matrix with columns of 0s to make the design matrices between runs have the same number of columns. Just make sure that the column order between runs is the same! That is, column X should correspond to the same condition across runs.

This depends on what questions you want to ask with your data. What are you planning to analyze? A correlation, one sample T-test, groups difference, something else? Second level is also where subject level covariates (such as age or sex) are included.

Best,
Steven

2 Likes

Thank you for your reply. I actually want to run a one sample T-test on my data. So what should be inputed in SecondLevelModel.fit.? beta map the same as spm or other?

Thank you in advance for your input and patience with a newbie again.

CFZ

Yes, first level beta maps are typically input into second level models.

eff_map = fmri_glm.compute_contrast(L_active_minus_rest, output_type=‘effect_size’) is beta map?

Thank you in advance for your input and patience with a newbie again.

CFZ

Yes, the effect size outputs are beta maps.

I’ve never used the zero-padding thing, another possibility to run multi-run analysis is to analyse each of them completely randomly, and then obtain subject subject-level statistics by calling the compute_fixed_effects routine.

Indeed, you should input (fixed-)effect size estimates to second-level analysis.

I’m curious about your experience with Nilearn, please keep us posted !
HTH,
Bertrand

2 Likes

Hi Steven,

I encountered a similar situation. I have 4 runs per subject, and these data are preprocessed using frmiprep. I included motion parameters and cosineXX parameters in the design matrix of each run. Sometimes one run has more cosineXX parameters than the other runs in one subject. So I padded additional columns of 0s to make sure the number of columns and their order are consistent in each subject. But then, in the first level analysis, I found this warning message,
/Users/shengjie/anaconda3/lib/python3.11/site-packages/nilearn/glm/_utils.py:176: RuntimeWarning: divide by zero encountered in scalar divide cond = smax / smin /Users/shengjie/anaconda3/lib/python3.11/site-packages/nilearn/glm/_utils.py:180: UserWarning: Matrix is singular at working precision, regularizing... warn("Matrix is singular at working precision, regularizing...") .

I found in the final design matrix, those columns with 0s were regularized so their values are not 0s but replaced with some tiny values. I am quite new to nilearn, is this something that I should pay attention to?

best,
Shengjie