Python script to fmri contrast analysis (FSL)

I have a data with 12 runs, each 16 trials per subject, where every trial is one of two conditions (High vs. Low).
I want to build a script to run three level of analysis:
at the run level, the subject level, the group level.
I was looking for tools but there are so many, I am not sure where to start and which is the most adequate to my problem.
It seems that fitlins might be very well suited but I didn’t find any end-to-end example, including result and visualization of the contrast on brain image. Also nilearn, nipype seems useful. What are the differences / advantages of each?
Can someone please advice me what tool is the most up-to-date? I don’t mind to mix several tools but I do want to build a script to fully automate the pipeline, from BIDS data (before or after fmriprep) until getting the contrast results on the brain maps (the stats map).

Hi @o_aug

I’m not up-to-date on fitlins so I’ll let experts like @effigies or @tsalo jump in there.

For nilearn, I wanted to point to a few examples that might be relevant to your use case:

  1. First-level analyses of a BIDS dataset
  2. Second-level analyses
  3. Statistical testing of second-level results

Hopefully these are useful, but of course happy to answer any specific questions about using nilearn, here !



@emdupre Thanks! I saw these examples, and there are also so many nipype examplesm, I just wonder what are the consideration when choosing between all the packages?
Did these example in nilearn worked good for you? were you able to use them with your own data?

Hi @o_aug

For full disclosure, I’ve contributed to both nilearn and nipype. So I’d say they’re both great tools ! But their use cases are slightly different, which gets back to your first question:

  • nipype is a pipelining framework. That is, it doesn’t include any code itself for preprocessing. Instead, it creates a uniform interface for calling many different tools (for example, from FSL, AFNI, and even nilearn) all in the same processing pipeline. nipype is the framework underlying fMRIPrep, for example.
  • nilearn, meanwhile, is a Python package for statistical learning and analysis of (f)MRI images. It includes the functionality that used to be in nistats, including GLMs. It also wraps some sklearn functionality such that you can also do machine learning analyses, if necessary.

One difference between nilearn and fitlins of note: fitlins is designed for fitting linear models within the BIDS specification. That is, the statistical models themselves will be in BIDS. Nilearn is not as closely tied to BIDS (although there is some bridging, such as the first-level example I shared).

Hope that helps, and happy new year !


@emdupre Thanks! Can you please explain what you mean by fitting linear models within the BIDS specification?
Assuming I have data in BIDS format, post fmriprep, and I want to build a script to run a group-level analysis (compare activity in certain region between 2 conditions), which shpuld be more useful / faster ramp-up between nilearn and fitlins?

Ahoi hoi,

not @emdupre, but I’m trying to provide some pointers anyway, hehe.

One general thing that should be discussed/thought about is the specific model you want to run during your analyses. As @emdupre already outlined: nipype is a pipeline framework and allows the combination/usage of a variety of software packages (given that those are respectively installed on your machine). Thus, the number of models (including the actual test and thresholding) you can set up through that is definitely greater than in fitlins as it uses nistats under the hood, which is already fantastic but does not support some tests and thresholding (yet).

As you’re probably aware: BIDS is constantly extended through so-called BIDS extension proposals or BEPs. One of those BEPs (BEP002) aims at model specifications, that is precisely describing statistical models via JSON files. The fitlins documentation has a nice section on that here.

Together, these points bring us back to your question. Given the analyses you want to run and that you preprocessed your data using fmriprep, nistats/nilearn and thus fitlins should be able to get you there and would definitely be faster than writing your own nipype workflow. Please check the fitlins docs sections here and here re how to define/setup your model-<label>_[desc-<description>]_smdl.json file, as well as the walk-through usage example. If you don’t specify a JSON file, fitlins will use pybids’ function to generate a default model which will get you baseline contrasts for each condition at the run, subject and group level. Another benefit of fitlins: the automatically generated super informative reports.

HTH, cheers, Peer

1 Like

@PeerHerholz Thanks! I understand that nipype is probably an overkill but just one small questions please, what did you mean by

nistats / nilearn and thus fitlins should be able to get you there

I was under the impression that they are two separate packages? (And by the rest of your comment, I understand what fitlins will be more suited to my needs?)

Hi @o_aug

I’d agree with most everything @PeerHerholz already pointed out :slightly_smiling_face:

I just want to add, though, that this forum isn’t the best place to find strong recommendations for your specific use case – we won’t have all of the context that you and your collaborators do. Instead, I’d recommend you use NeuroStars to learn more about your available options, which here include (at least) nipype, nilearn, and fitlins. All of these are separate packages, though they can and do interact at various levels.

Hopefully it’s a bit clearer now what the relationships are between them, but just to summarize:

  • nipype is a pipelining framework, with which you can call other existing tools including nilearn or fitlins
  • fitlins is a python package to compute statistical models in BIDS format. It calls nilearn internally.
  • nilearn is a python package to compute statistical models and perform statistical analysis / learning




Great, Thank you very much for your help! @emdupre @PeerHerholz