code: def _hc_cut(n_clusters, children, n_leaves):
      """Function cutting the ward tree for a given number of clusters.
  
      Parameters
      ----------
      n_clusters : int or ndarray
          The number of clusters to form.
  
      children : ndarray of shape (n_nodes-1, 2)
          The children of each non-leaf node. Values less than `n_samples`
          correspond to leaves of the tree which are the original samples.
          A node `i` greater than or equal to `n_samples` is a non-leaf
          node and has children `children_[i - n_samples]`. Alternatively
          at the i-th iteration, children[i][0] and children[i][1]
          are merged to form node `n_samples + i`.
  
      n_leaves : int
          Number of leaves of the tree.
  
      Returns
      -------
      labels : array [n_samples]
          Cluster labels for each point.
      """
      if n_clusters > n_leaves:
          raise ValueError(
              "Cannot extract more clusters than samples: "
              "%s clusters where given for a tree with %s leaves."
              % (n_clusters, n_leaves)
          )
      # In this function, we store nodes as a heap to avoid recomputing
      # the max of the nodes: the first element is always the smallest
      # We use negated indices as heaps work on smallest elements, and we
      # are interested in largest elements
      # children[-1] is the root of the tree
      nodes = [-(max(children[-1]) + 1)]
      for _ in range(n_clusters - 1):
          # As we have a heap, nodes[0] is the smallest element
          these_children = children[-nodes[0] - n_leaves]
          # Insert the 2 children and remove the largest node
          heappush(nodes, -these_children[0])
          heappushpop(nodes, -these_children[1])
      label = np.zeros(n_leaves, dtype=np.intp)
      for i, node in enumerate(nodes):
          label[
              _hierarchical._hc_get_descendent(
                  -node, children, n_leaves
              )
          ] = i
      return label
  
imports: import warnings
  from heapq import heapify, heappop, heappush, heappushpop
  from numbers import Integral, Real
  import numpy
  from scipy import sparse
  from scipy.sparse.csgraph import connected_components
  from ..base import BaseEstimator, ClassNamePrefixFeaturesOutMixin, ClusterMixin, _fit_context
  from ..metrics import DistanceMetric
  from ..metrics._dist_metrics import METRIC_MAPPING64
  from ..metrics.pairwise import _VALID_METRICS, paired_distances
  from ..utils import check_array
  from ..utils._fast_dict import IntFloatDict
  from ..utils._param_validation import HasMethods, Hidden, Interval, StrOptions, validate_params
  from ..utils.graph import _fix_connected_components
  from ..utils.validation import check_memory
  from .None import _hierarchical_fast
  from ._feature_agglomeration import AgglomerationTransform
label_desc: Use image node_id 8 for calling a global function with example usage: _hc_cut(n_clusters, children, n_leaves) and returns: label