code: def _nn_connectivity(connectivity, threshold=1e-7):
      """Fast implementation of nearest neighbor connectivity.
  
      Parameters
      ----------
      connectivity : a sparse matrix in COOrdinate format.
          Sparse matrix representation of the weighted adjacency graph.
  
      threshold : float in the close interval [0, 1], default=1e-7
          The threshold is set to handle eccentricities.
  
      Returns
      -------
      nn_connectivity : a sparse matrix in COOrdinate format.
  
      """
      n_features = connectivity.shape[0]
  
      connectivity_ = coo_matrix(
          (1.0 / connectivity.data, connectivity.nonzero()),
          (n_features, n_features),
      ).tocsr()
  
      # maximum on the axis = 0
      max_connectivity = connectivity_.max(axis=0).toarray()[0]
      inv_max = dia_matrix(
          (1.0 / max_connectivity, 0), shape=(n_features, n_features)
      )
  
      connectivity_ = inv_max * connectivity_
  
      # Dealing with eccentricities, there are probably many nearest neighbors
      edge_mask = connectivity_.data > 1 - threshold
  
      j_idx = connectivity_.nonzero()[1][edge_mask]
      i_idx = connectivity_.nonzero()[0][edge_mask]
  
      # Set weights to 1
      weight = np.ones_like(j_idx)
      edges = np.array([i_idx, j_idx])
  
      nn_connectivity = coo_matrix(
          (weight, edges), (n_features, n_features)
      )
  
      return nn_connectivity
  
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 5 for calling a global function with example usage: _nn_connectivity(connectivity, threshold) and returns: nn_connectivity