Use nilearn on a VoxelData array¶
This example shows how to pass data between ConfUSIus and
nilearn.
convert_to_nifti returns an in-memory NIfTI image
and fUSI-BIDS metadata. convert_from_nifti
converts a NIfTI image back to a VoxelData array.
We use nilearn to find the clusters of a statistical map from Cybis Pereira et al. 20261. Then we plot these clusters with ConfUSIus.
nilearn is not a dependency of ConfUSIus
ConfUSIus does not install nilearn. If nilearn is not in your environment, install it before you run this example:
pip install nilearn
Load a statistical map¶
fetch_cybis_pereira_2026 downloads
the glm-speed results of one rat. The file that we load contains t-statistic maps
for the contrast between fUSI signal and animal speed. Its time dimension holds the
lag between speed and fUSI. We keep the map at a lag of 2 s as we know this is a lag
that shows a strong correlation.
from pathlib import Path
import matplotlib as mpl
import xarray as xr
from nilearn.reporting import get_clusters_table
import confusius as cf
# Adapt background color to the current Matplotlib style.
bg_color = mpl.colors.to_hex(mpl.rcParams["figure.facecolor"])
# Keep notebook output compact for large DataArray displays.
_ = xr.set_options(display_expand_attrs=False, display_expand_data=False)
subject = "rat75"
acq = "slice32"
bids_root = cf.datasets.fetch_cybis_pereira_2026(
datasets="glm-speed",
subjects=subject,
acqs=acq,
)
stat_path = (
Path(bids_root)
/ "derivatives/glm-speed"
/ f"sub-{subject}"
/ f"sub-{subject}_acq-{acq}_rec-staticsvd60clutterwindowwidth200dopplerwindowwidth40_contrast-speed_stat-t_control-bonferroni_alpha-001_statmap.nii"
)
stat = cf.load(stat_path).sel(time=2, method="nearest")
stat.attrs["long_name"] = "t statistic"
If you use this dataset in your work, please cite the following source: Cybis Pereira, F., Castedo, S. H., Meur-Diebolt, S. L., Ialy-Radio, N., Bhattacharya, S., Ferrier, J., Osmanski, B. F., Cocco, S., Monasson, R., Pezet, S., & Tanter, M. (2026). A vascular code for speed in the spatial navigation system. Cell Reports, 45(1). https://doi.org/10.1016/j.celrep.2025.116791
Convert the map to a NIfTI image¶
nilearn reads the world coordinates of an image from img.affine. NiBabel sets this
affine to the
sform when sform_code is not 0. If sform_code is 0, NiBabel uses the qform.
In this file, the qform points to the probe space. In that space, the imaging
plane is aligned with the x and y axes, which makes the cluster coordinates easy
to read. Thus we move the world coordinates to that space with
affine.apply. Then we set sform_code
to 0, and convert_to_nifti writes only the qform.
stat = stat.fusi.affine.apply("world_to_qform")
stat.attrs["sform_code"] = 0
img, metadata = stat.fusi.to_nifti()
Find the clusters with nilearn¶
get_clusters_table finds the clusters of
voxels with a value more than stat_threshold. It returns a table with the peak of
each cluster, in the world coordinates of the image. With return_label_maps=True, it
also returns the cluster labels as NIfTI images.
nilearn gives the cluster size in full cubic millimeters. As a result, a cluster smaller than 1 mm³ shows a size of 0.
| Cluster ID | X | Y | Z | Peak Stat | Cluster Size (mm3) | |
|---|---|---|---|---|---|---|
| 0 | 1 | -0.055 | 7.85824 | -8.881784e-16 | 43.928615 | 6 |
| 1 | 2 | -5.445 | 10.42080 | -8.881784e-16 | 24.196712 | 0 |
| 2 | 3 | -5.775 | 8.74528 | -8.881784e-16 | 20.813019 | 0 |
| 3 | 4 | 2.805 | 6.08416 | -8.881784e-16 | 15.179721 | 0 |
| 4 | 5 | 2.255 | 10.71648 | -8.881784e-16 | 11.706382 | 0 |
| 5 | 6 | 1.485 | 5.98560 | -8.881784e-16 | 11.415170 | 0 |
Convert the cluster labels to a VoxelData array¶
convert_from_nifti converts the label image to
a VoxelData array. This array has the same world coordinates as stat.
- k: 1
- j: 112
- i: 128
- dask.array<chunksize=(1, 112, 128), meta=np.ndarray>
Array Chunk Bytes 56.00 kiB 56.00 kiB Shape (1, 112, 128) (1, 112, 128) Dask graph 1 chunks in 2 graph layers Data type int32 numpy.ndarray - k(k)int640
array([0])
- j(j)int640 1 2 3 4 5 ... 107 108 109 110 111
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111]) - i(i)int640 1 2 3 4 5 ... 123 124 125 126 127
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127]) - z(k, j, i)float64-8.882e-16 ... -8.882e-16
- units :
- mm
[14336 values with dtype=float64]
- y(k, j, i)float645.0 5.0 5.0 ... 15.94 15.94 15.94
- units :
- mm
[14336 values with dtype=float64]
- x(k, j, i)float64-6.985 -6.875 ... 6.875 6.985
- units :
- mm
[14336 values with dtype=float64]
- kPandasIndex
PandasIndex(Index([0], dtype='int64', name='k'))
- jPandasIndex
PandasIndex(Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 102, 103, 104, 105, 106, 107, 108, 109, 110, 111], dtype='int64', name='j', length=112)) - iPandasIndex
PandasIndex(Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 118, 119, 120, 121, 122, 123, 124, 125, 126, 127], dtype='int64', name='i', length=128)) - z
y
xVoxelToWorldIndex<confusius._utils.geometry.VoxelToWorldIndex object at 0x7db6cc3744d0>
- qform_code :
- 1
- affines :
- {'world_to_qform': array([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])}
Plot the clusters¶
We plot the contour of each cluster on the statistical map. Then we mark the peak of each cluster at its coordinates from the nilearn table.
# Subpeak IDs such as "1a" belong to an existing cluster, not a new label.
peaks = table[table["Cluster ID"].astype(str).str.fullmatch(r"\d+")]
colors = {
int(row["Cluster ID"]): mpl.colors.to_hex(mpl.colormaps["tab10"](i % 10))
for i, (_, row) in enumerate(peaks.iterrows())
}
assert set(colors) == set(labels.values.ravel()) - {0}
plotter = cf.plotting.plot_stat_map(stat, bg_color=bg_color)
plotter.add_contours(labels, colors=colors)
ax = plotter.axes[0][0]
for _, row in peaks.iterrows():
cluster_id = int(row["Cluster ID"])
ax.plot(
row["X"],
row["Y"],
marker="o",
color=colors[cluster_id],
markersize=7,
label=f"Cluster ID {cluster_id}",
)
_ = ax.legend()
Total running time: 4.8 s
Launch in Binder Download .py Download .ipynb
-
See The ConfUSIus Dataset Collection for available studies, formats, licenses, and reproducible subset downloads. ↩

