# Create surface-based mask using KNN and geodesic distance around MNI coordinates

Hi! Inspired by the insightful discussion from this previous post, I’ve adapted a script to generate a binary mask within nilearn’s GM mask employing scikit-learn’s k-nearest neighbors (KNN). In the KNN algorithm, the `cluster_size` is defined around a specified number of `seeds` represented by MNI coordinates. The script snippets are provided below.

Now, I’m seeking assistance regarding this challenge: How can I create a binary mask, ensuring that KNN creates clusters around each seed by at least a centimeter in geodesic space? Since geodesic distance is required, I assume the mask should be created in surface space and I am already struggling in mapping MNI coordinates onto the surface space.

Any insights and guidance are highly appreciated!

``````# Binary mask delimited to GM created via scikit-learn nearest-neighbors
################################################################################

## Function: Get in-mask coordinates ###########################################
'''Get in-mask voxel coordinates'''

# get data indices for all '1' voxels inside mask data array

# return coordinates for those '1' voxels

affine)

## Loop for nearest-neighbors ##################################################
for cluster_size in cluster_sizes:
cluster_arrays = []
cluster_arrays_summed = None

for seed in seeds:
clf = skl.neighbors.NearestNeighbors(n_neighbors=cluster_size)

# Sum the adjacency matrices for each seed
if cluster_arrays_summed is None:
cluster_arrays_summed = nearest_neighbors
else:
cluster_arrays_summed += nearest_neighbors

# inverse-transform cluster arrays

# Show results
nl.plotting.plot_roi(clusters_mask_img, title=f'Mask with clusters of {cluster_size} voxels')
print(f'Cluster {cluster_size} voxels, non-zero voxels', np.count_nonzero(clusters_mask_img.get_fdata()))
print(f'Cluster {cluster_size} voxels, non-zero voxels', np.unique(clusters_mask_img.get_fdata()))

# Binarize image