code: def _nearest_neighbor_grouping(
      X, connectivity, n_clusters, threshold=1e-7
  ):
      """Cluster using nearest neighbor agglomeration.
  
      Merge clusters according to their nearest neighbors,
      then the data and the connectivity are reduced.
  
      Parameters
      ----------
      X : :class:`numpy.ndarray`
          Training data. shape = [n_samples, n_features]
  
      connectivity : a sparse matrix in COOrdinate format.
          Sparse matrix representation of the weighted adjacency graph.
  
      n_clusters : :obj:`int`
          The number of clusters to find.
  
      threshold : :obj:`float` in the close interval [0, 1], default=1e-7
          The threshold is set to handle eccentricities.
  
      Returns
      -------
      reduced_connectivity : a sparse matrix in COOrdinate format.
  
      reduced_X : :class:`numpy.ndarray`
          Data reduced with agglomerated signal for each cluster.
  
      labels : :class:`numpy.ndarray`, shape = [n_features]
          It contains the clusters assignation.
  
      """
      # Nearest neighbor connectivity
      nn_connectivity = _nn_connectivity(connectivity, threshold)
      n_features = connectivity.shape[0]
      n_components = (
          n_features - (nn_connectivity + nn_connectivity.T).nnz / 2
      )
  
      if n_components < n_clusters:
          # remove edges so that the final number of clusters is not less than
          # n_clusters (to achieve the desired number of clusters)
          n_edges = n_features - n_clusters
          nn_connectivity = nn_connectivity + nn_connectivity.T
  
          i_idx, j_idx = nn_connectivity.nonzero()
          edges = np.array([i_idx, j_idx])
  
          # select n_edges to merge.
          edge_mask = np.argsort(i_idx - j_idx)[:n_edges]
          # Set weights to 1, and the connectivity matrix symmetrical.
          weight = np.ones(2 * n_edges)
          edges = np.hstack(
              [edges[:, edge_mask], edges[::-1, edge_mask]]
          )
  
          nn_connectivity = coo_matrix(
              (weight, edges), (n_features, n_features)
          )
  
      # Clustering step: getting the connected components of the nn matrix
      n_components, labels = csgraph.connected_components(
          nn_connectivity
      )
  
      # Reduction step: reduction by averaging
      reduced_connectivity, reduced_X = _reduce_data_and_connectivity(
          X, labels, n_components, connectivity, threshold
      )
  
      return reduced_connectivity, reduced_X, labels
  
imports: import warnings
  import numpy
  from joblib import Memory
  from nibabel import Nifti1Image
  from scipy.sparse import coo_matrix, csgraph, dia_matrix
  from sklearn.base import BaseEstimator, ClusterMixin, TransformerMixin
  from sklearn.utils import check_array
  from sklearn.utils.validation import check_is_fitted
  from nilearn._utils import fill_doc
  from nilearn.image import get_data
  from nilearn.masking import unmask_from_to_3d_array
label_desc: Use image node_id 7 for calling a global function with example usage: _nearest_neighbor_grouping(X, connectivity, n_clusters, threshold) and returns: reduced_connectivity,  reduced_X,  labels