I am trying to create a function mainly in matlab to find the peak coordinates in a zmap within a region eg, PCC and use it as a seed region for an individual (single subject. I don’t mind using python too.
In Nilearn you can load your zmap with
zmap = nilearn.image.load_img("path/to/zmap.nii.gz") and get the data matrix with
z_data = zmap.get_fdata(). You can then load your PCC mask with
pcc_mask = nilearn.image.load_img("path/to/pcc.nii.gz") and get the data matrix in a similar way. Assuming your mask is binary, you can then multiply the two data matrixes to only get the z stats within the PCC. Within this matrix, you can find the index where the max stat occurs with
numpy.argmax. The I,J,K coordinate of that index in the matrix can be translated to an X,Y,Z coordinate based on the voxel size. Then after that, it depends on what kind of ROI you want to make. Did you have a sphere in mind?
Thank you. This was very helpful. Yes I have a sphere in mind.
You can see here for creating spheres: Masking Example — nltools 0.4.7 documentation
Just a quick question with the argmax function I thinking of something like this argmax(z_data,z_data, z_data) or argmax(z_data) assuming z_data is my final matrix data. Just getting started with nilearn so trying to think through the analysis. Also, how do I convert the argmax into MNI coordinates
Might make more sense to instead do something like:
max_index = numpy.where(z_data == numpy.amax(z_data))
Assuming your data are already in MNI, it should be a product of the matrix index and the voxel sizes (so X index multiplied by X voxel size, etc). Though you might also have to center the data at the origin (that is, if your image size has 101 voxels in the X direction, voxels 0-49 are negative, voxel 50 is at the origin, and 51-100 are positive).