Created
January 8, 2018 17:31
-
-
Save makism/35075ae82a56001534e73bdbe54b11b4 to your computer and use it in GitHub Desktop.
MNI coordinates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import nilearn | |
| from nilearn import datasets | |
| from nilearn.input_data import NiftiMasker | |
| from nilearn.datasets import load_mni152_template | |
| from nilearn.image import resample_to_img | |
| from nilearn.image import load_img | |
| from nilearn.image import image | |
| ### Load MNI template. | |
| template = load_mni152_template() | |
| template_img = load_img(template) | |
| ### Fetch first subject's fMRI and ROI. | |
| haxby_dataset = datasets.fetch_haxby(subjects=(1,)) | |
| fmri_filename = haxby_dataset.func[0] | |
| mask_filename = haxby_dataset.mask_vt[0] | |
| ### Resample fMRI to MNI template. | |
| fmri_resampled = resample_to_img(fmri_filename, template) | |
| ### Apply mask. | |
| masker = NiftiMasker(mask_img=mask_filename, standardize=True) | |
| fmri_masked = masker.fit_transform(fmri_resampled) | |
| num_samples, num_voxels = np.shape(fmri_masked) | |
| ### Fetch binary mask data. | |
| binary_mask = masker.mask_img_.get_data() | |
| indices = np.where(binary_mask == 1) | |
| # convert into x,y,z triplets | |
| mask_coords = zip(indices[0], indices[1], indices[2]) | |
| mask_coords_mtx = np.reshape(mask_coords, (num_voxels, 3)) | |
| mask_coords_mtx_MNI = np.zeros_like(mask_coords_mtx) | |
| # convert from pixel indices to MNI coordinates | |
| for i, coords in enumerate(mask_coords_mtx): | |
| ix, iy, iz = coords | |
| x, y, z = nilearn.image.coord_transform(ix, iy, iz, template.affine) | |
| mask_coords_mtx_MNI[i] = [x, y, z] | |
| print "(x, y, z) = ({0}, {1}, {2})".format(x, y, z) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment