Nilearn FirstLevelModel sample_masks

Hi there,

I am hoping to create a sample_masks input for the FirstLevelModel function in nilearn.

I would like to generate the sample_masks from a confounds.tsv file generated from fmriprep. Are there any examples of this anyone could point me to?

Thanks in advance!

Hi @sgranger,

sample_mask is defined as

array_like, or list of array_like, optional
shape of array: (number of scans - number of volumes removed, ) Indices of retained volumes.

So a full sample_mask would just be the a list of [0, 1, 2, ....n_vols-1]. This can be generated with something like

import numpy as np
import pandas as pd

confounds_path = "/path/to/confounds.tsv"
confounds_data = pd.read_csv(confounds_path, delimiter="\t")
n_vols = len(confounds_data)
sample_mask_all = np.arange(n_vols)

This isn’t specific enough to code a more specific example. Based on what from the confounds file? Motion outliers, FD, DVARS, something else? In either case, if you know what index or indexes of volumes you want to remove, you can do something like:

sample_mask_trimmed = np.delete(sample_mask_all, [inds_to_remove])


Thank you! This worked great.