Plotting sphere from NiftiSpheresMasker

Hi :slight_smile:

I’m doing an analysis in which I’m finding the most activated voxel in a certain area, draw a sphere around it (10mm radius) and average the activity over this sphere. Having defined the voxel, I use input_data.NiftiSpheresMasker to do the extraction of activity for this sphere.

This is my code to get an overview:

for index2, (area,subarea) in enumerate(brainAreas.items()):    
            print('  Fit %s from atlas and get k voxels ' % (area))
            subjectActivity[df][contrast][area] = {}
            #Insert here choice of coord: first or mean of first and second
            xyz = kVoxels[df][contrast][area][0]
            voxMask = input_data.NiftiSpheresMasker([xyz])
            sphereMask = input_data.NiftiSpheresMasker([xyz], radius = 10)
            subjectActivity[df][contrast][area]['Voxels'] = []
            subjectActivity[df][contrast][area]['Sphere'] = []
            for ind, num in enumerate(sub_list):
                print('  Fit %s from atlas and get voxel ' % (num))
                single_scan = globals()[f"z_map{df}"][contrast][ind]
                subjectActivity[df][contrast][area]['Voxels'].append(voxMask.fit_transform(single_scan)[0][0])
                subjectActivity[df][contrast][area]['Sphere'].append(sphereMask.fit_transform(single_scan)[0][0])

I would like to plot my spheres to see that they are not going beyond the boundaries of my anatomical region (i.e. that the sphere around the most activated voxel in the amygdala does not go beyond the amygdala). Has anyone done this or can help me out how to do it?

Thanks

Max

If you have the areas where each voxel is located, then why not give that information to NiftiSpheresMasker directly? You can use the parameter mask_img to ensure that only voxels within the mask get averaged.

Hi Daniel,

thanks for your reply!

Just to be sure I understand: So you would simply give the coordinates of each voxel within a specific radius to the NiftiSpheresMasker?

And also, but if I give mask_img as a parameter, then wouldn’t voxels outside the mask be turned to 0? So that, as a result, NiftiSpheresMasker would use zero-voxels for its calculations if the sphere goes beyond the boundaries defined by the mask. Perhaps I’m misunderstanding something about masks.

Thanks!

I wouldn’t use spheres, I’d use labels in the first place, since you are interested in anatomically defined regions. Is there a biological/physiological explanation for the use of spheres?

But let’s say there is. In that case NiftiSpheresMasker allows you to constrain the averaging to only take into account voxels within a mask by setting the parameter mask_img. This doesn’t mean that zeroed voxels would be used for calculations, it means they wouldn’t be included in the calculations at all. So technically you would no longer be averaging within a sphere, but within the intersection of a sphere and a mask.

Thanks!

Will also consider using alternative methods to define my ROIs!