code: def _compute_weights(X, mask_img):
      """Compute the weights in direction of each axis using Euclidean distance.
  
      i.e. weights = (weight_deep, weights_right, weight_down).
  
      Notes
      -----
      Here we assume a square lattice (no diagonal connections).
  
      Parameters
      ----------
      X : ndarray, shape = [n_samples, n_features]
          Training data.
  
      mask_img : Niimg-like object
          Object used for masking the data.
  
      Returns
      -------
      weights : ndarray
          Weights corresponding to all edges in the mask.
          shape: (n_edges,).
  
      """
      n_samples, n_features = X.shape
  
      mask = get_data(mask_img).astype("bool")
      shape = mask.shape
  
      data = np.empty((shape[0], shape[1], shape[2], n_samples))
      for sample in range(n_samples):
          data[:, :, :, sample] = unmask_from_to_3d_array(
              X[sample].copy(), mask
          )
  
      weights_deep = np.sum(np.diff(data, axis=2) ** 2, axis=-1).ravel()
      weights_right = np.sum(
          np.diff(data, axis=1) ** 2, axis=-1
      ).ravel()
      weights_down = np.sum(np.diff(data, axis=0) ** 2, axis=-1).ravel()
  
      weights = np.hstack([weights_deep, weights_right, weights_down])
  
      return weights
  
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 1 for calling a global function with example usage: _compute_weights(X, mask_img) and returns: weights