Created
January 23, 2021 22:41
-
-
Save morteza/20f0ec4723470a92cce491e99cc6c4eb to your computer and use it in GitHub Desktop.
Convert MNI coords to a Harvard-Oxford labels using spherical masking
This file contains 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
# %% | |
# A basic funtion to convert MNI coords (x,y,z) to harvard oxford "labels" using spherical masking. | |
# input: | |
# - list of coords of size N*3 | |
# output: | |
# - list of labels of size N | |
import numpy as np | |
from nilearn import datasets | |
from nilearn.input_data import NiftiSpheresMasker | |
def coords2labels(coords: np.array, sphere_radius=2): | |
""" Convers XYZ coordination in MNI space to labels in Harvard-Oxford atlas. | |
Note: to use different deterministic atlases or probabilisitc ones, change the line 29. | |
Args: | |
----- | |
coords: 2d numpy array of size (N,3). | |
Returns: | |
----- | |
a list strings of size N. | |
""" | |
atlas = datasets.fetch_atlas_harvard_oxford('cort-maxprob-thr25-2mm') | |
masker = NiftiSpheresMasker(seeds=coords,radius=sphere_radius,allow_overlap=True) | |
masker.fit() | |
label_indices = masker.transform_single_imgs(atlas.maps).astype(int) | |
labels = np.array(atlas.labels)[label_indices.flatten()].tolist() | |
return labels | |
# example use case: | |
sample_coords = [[0,0,35], | |
[4,5,6], | |
[-12, 20, -20] | |
] | |
coords2labels(sample_coords) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment