Mask a 4D nib.Nifti1Image by cut_coords

I would like to show the timecourse of the voxel I center on when I plot a statistical map.

For plotting I use nilearn.plotting.plot_stat_map(), which takes cut_coords (for which I give a length-3 tuple) as arguments.

I load the 4D timeseries with nib.load(), but I can’t seem to be able to apply a mask to it without having a (manually?) constructed NIfTI on disk. I am aware that I can extract the data from the timeseries as a numpy array, but then I would be slicing in the data matrix coordinates. The cut_coords I have are in the image space.

I also realize that I could calculate all o this with the help of the values in the affine matrix. I was just thinking that maybe someone already implemented this, and I was just unable to find it. It sounds like a fairly basic feature.

This appears to be entirely agnostic of the image coordinate space. I get the impression that it doesn’t address my question in any way.

Use nilearn.input_data.NiftiSpheresMasker to create a ‘mask’ for the voxel(s) you are focusing on.
Then use fit_transform(imagefilename) on the created object.
You do not even have to use nibabel.load.

An example extracted from a piece of code of mine:

extract signal from white matter and csf_avg_sig

csfcoord = (-3, 10, 10)
wmcoord = (-28, -18, 31)
confundmask = input_data.NiftiSpheresMasker([csfcoord, wmcoord], radius=3, detrend=True, standardize=False, 
                                            high_pass=high_pass, low_pass=low_pass, t_r=t_r)
sig = confundmask.fit_transform(subj_datafname)

Christophe Pallier