Subset an atlas

I’m sure there’s a simple solution but all of the ones I’m coming up with seem to be much more complicated than need be:

I have an atlas that is warped to subject space (mice). I’d like to drop several of the ROIs and save the resultant atlas subset. Code options for selecting an ROI and creating a mask/atlas are easily available. Code for subsetting (dropping) regions seems much more cumbersome.

Anyone have any quick code for doing so? I’ll take any language.

I think nilearn.image.math_img can do what you want. I assume your atlas is a 3D nifti with different voxel values corresponding to different regions, rather than a 4D probabilistic atlas.

from nilearn import image

values_i_want_to_keep = [1, 2, 3, 10, 20, 50]
atlas_file = "old_atlas.nii.gz"

new_atlas_img = image.math_img(
    "np.isin(img, {}).astype(int)".format(values_i_want_to_keep),
    img=atlas_file,
)
new_atlas_img.to_filename("new_atlas.nii.gz")
1 Like

Thanks @tsalo

Knew I was making it more complicated than I needed to.

Also easy in AFNI with these commands. You can copy selected regions with either indices or labels. The new dataset can get the same labels too.

3dcalc -a ~/abin/MNI_Glasser_HCP_v1.0.nii.gz’<L_Area_anterior_32_prime,R_Area_anterior_32_prime>’ -expr a -datum short -nscale -prefix ant32prime.nii.gz -overwrite

3drefit -cmap INT_CMAP -copytables ~/abin/MNI_Glasser_HCP_v1.0.nii.gz ant32prime.nii.gz