code: def _k_init_metric(
      X, n_clusters, cdist_metric, random_state, n_local_trials=None
  ):
      """Init n_clusters seeds according to k-means++ with a custom distance
      metric.
  
      Parameters
      ----------
      X : array, shape (n_samples, n_timestamps, n_features)
          The data to pick seeds for.
  
      n_clusters : integer
          The number of seeds to choose
  
      cdist_metric : function
          Function to be called for cross-distance computations
  
      random_state : RandomState instance
          Generator used to initialize the centers.
  
      n_local_trials : integer, optional
          The number of seeding trials for each center (except the first),
          of which the one reducing inertia the most is greedily chosen.
          Set to None to make the number of trials depend logarithmically
          on the number of seeds (2+log(k)); this is the default.
  
      Notes
      -----
      Selects initial cluster centers for k-mean clustering in a smart way
      to speed up convergence. see: Arthur, D. and Vassilvitskii, S.
      "k-means++: the advantages of careful seeding". ACM-SIAM symposium
      on Discrete algorithms. 2007
  
      Version adapted from scikit-learn for use with a custom metric in place of
      Euclidean distance.
      """
      n_samples, n_timestamps, n_features = X.shape
  
      centers = numpy.empty(
          (n_clusters, n_timestamps, n_features), dtype=X.dtype
      )
  
      # Set the number of local seeding trials if none is given
      if n_local_trials is None:
          # This is what Arthur/Vassilvitskii tried, but did not report
          # specific results for other than mentioning in the conclusion
          # that it helped.
          n_local_trials = 2 + int(numpy.log(n_clusters))
  
      # Pick first center randomly
      center_id = random_state.randint(n_samples)
      centers[0] = X[center_id]
  
      # Initialize list of closest distances and calculate current potential
      closest_dist_sq = cdist_metric(centers[0, numpy.newaxis], X) ** 2
      current_pot = closest_dist_sq.sum()
  
      # Pick the remaining n_clusters-1 points
      for c in range(1, n_clusters):
          # Choose center candidates by sampling with probability proportional
          # to the squared distance to the closest existing center
          rand_vals = (
              random_state.random_sample(n_local_trials) * current_pot
          )
          candidate_ids = numpy.searchsorted(
              stable_cumsum(closest_dist_sq), rand_vals
          )
          # XXX: numerical imprecision can result in a candidate_id out of range
          numpy.clip(
              candidate_ids,
              None,
              closest_dist_sq.size - 1,
              out=candidate_ids,
          )
  
          # Compute distances to center candidates
          distance_to_candidates = (
              cdist_metric(X[candidate_ids], X) ** 2
          )
  
          # update closest distances squared and potential for each candidate
          numpy.minimum(
              closest_dist_sq,
              distance_to_candidates,
              out=distance_to_candidates,
          )
          candidates_pot = distance_to_candidates.sum(axis=1)
  
          # Decide which candidate is the best
          best_candidate = numpy.argmin(candidates_pot)
          current_pot = candidates_pot[best_candidate]
          closest_dist_sq = distance_to_candidates[best_candidate]
          best_candidate = candidate_ids[best_candidate]
  
          # Permanently add best center candidate found in local tries
          centers[c] = X[best_candidate]
  
      return centers
  
imports: import warnings
  import numpy
  import sklearn
  from scipy.spatial.distance import cdist
  from sklearn.base import ClusterMixin, TransformerMixin
  from sklearn.metrics.pairwise import pairwise_kernels
  from sklearn.utils import check_random_state
  from sklearn.utils.extmath import stable_cumsum
  from sklearn.utils.validation import _check_sample_weight
  from sklearn.utils import check_array
  from sklearn.utils.validation import check_is_fitted
  from tslearn.barycenters import dtw_barycenter_averaging, euclidean_barycenter, softdtw_barycenter
  from tslearn.bases import BaseModelPackage, TimeSeriesBaseEstimator
  from tslearn.metrics import cdist_dtw, cdist_gak, cdist_soft_dtw, sigma_gak
  from tslearn.utils import check_dims, to_sklearn_dataset, to_time_series_dataset
  from .utils import EmptyClusterError, TimeSeriesCentroidBasedClusteringMixin, _check_full_length, _check_initial_guess, _check_no_empty_cluster, _compute_inertia
label_desc: Use image node_id 1 for calling a global function with example usage: _k_init_metric(X, n_clusters, cdist_metric, random_state, n_local_trials) and returns: centers