Is there a way to obtain the label of the brain region corresponding to a voxel's coordinates?

51 Views Asked by At

I've performed a searchlight analysis with Python using machine learning techniques, and I've obtained the coordinates of specific voxels based on a threshold. Is there a way to obtain the labels of the corresponding brain regions for these coordinates?(e.g. Right Cerebral Cortex..)

array([[20, 12, 25],
       [20, 13, 21],
       [20, 13, 24],
       ...,
       [20, 66, 26],
       [20, 66, 29],
       [20, 67, 23]], dtype=int64)

First, I performed with GPT then GPT gave me the code, but not work.

from nilearn import datasets, image

# Harvard-Oxford Atlas
atlas = datasets.fetch_atlas_harvard_oxford('cort-maxprob-thr25-2mm')

# Create an empty list to store the region names for the coordinates
mapped_regions = []

# Load the Harvard-Oxford Atlas image
atlas_img = atlas.maps

# Define the reference image, which should have the same resolution as your bold data

# Resample the atlas image to the resolution of the reference image
atlas_img_resampled = image.resample_to_img(atlas_img, bold)

# Iterate through the coordinates
for coord in above_threshold_coords:
    x, y, z = coord  # Coordinates directly from above_threshold_coords
    
    # Extract the region value from the resampled atlas image
    region_value = atlas_img_resampled.get_fdata()[x, y, z]
    
    # Get the region name from the atlas labels
    region_name = atlas.labels[int(region_value)]
    
    # Append the region name to the list
    mapped_regions.append(region_name)

# The mapped_regions list now contains the names of brain regions corresponding to the coordinates.
print(mapped_regions)


IndexError Traceback (most recent call last) Cell In[78], line 25 22 region_value = atlas_img_resampled.get_fdata()[x, y, z] 24 # Get the region name from the atlas labels ---> 25 region_name = atlas.labels[int(region_value)] 27 # Append the region name to the list 28 mapped_regions.append(region_name)

IndexError: list index out of range

0

There are 0 best solutions below