code: def templateLengthProcessWithMetric(
      path_to_file,
      initial_medoids,
      expected_cluster_length,
      metric,
      ccore_flag,
      **kwargs,
  ):
      sample = read_sample(path_to_file)
      data_type = kwargs.get("data_type", "points")
      input_type = kwargs.get("input_type", "list")
      initialize_medoids = kwargs.get("initialize_medoids", None)
      itermax = kwargs.get("itermax", 200)
  
      if metric is None:
          metric = distance_metric(type_metric.EUCLIDEAN_SQUARE)
  
      input_data = sample
      if data_type == "distance_matrix":
          input_data = calculate_distance_matrix(sample, metric)
  
          if input_type == "numpy":
              input_data = numpy.array(input_data)
  
      testing_result = False
      testing_attempts = 1
      if (
          initialize_medoids is not None
      ):  # in case center initializer randomization appears
          testing_attempts = 10
  
      for _ in range(testing_attempts):
          if initialize_medoids is not None:
              initial_medoids = kmeans_plusplus_initializer(
                  sample, initialize_medoids
              ).initialize(return_index=True)
  
          kmedoids_instance = kmedoids(
              input_data,
              initial_medoids,
              0.001,
              ccore=ccore_flag,
              metric=metric,
              data_type=data_type,
              itermax=itermax,
          )
          kmedoids_instance.process()
  
          clusters = kmedoids_instance.get_clusters()
          medoids = kmedoids_instance.get_medoids()
  
          if itermax == 0:
              assertion.eq(0, kmedoids_instance.get_iterations())
              assertion.eq(0.0, kmedoids_instance.get_total_deviation())
              assertion.eq([], clusters)
              assertion.eq(medoids, initial_medoids)
              return
  
          if len(clusters) != len(medoids):
              continue
  
          if len(set(medoids)) != len(medoids):
              continue
  
          obtained_cluster_sizes = [
              len(cluster) for cluster in clusters
          ]
          if len(sample) != sum(obtained_cluster_sizes):
              continue
  
          for cluster in clusters:
              if len(cluster) == 0:
                  continue
  
          if expected_cluster_length is not None:
              obtained_cluster_sizes.sort()
              expected_cluster_length.sort()
              if obtained_cluster_sizes != expected_cluster_length:
                  continue
  
          assertion.gt(kmedoids_instance.get_iterations(), 0)
  
          expected_total_deviation = 0.0
          for index_cluster in range(len(clusters)):
              index_point_medoid = medoids[index_cluster]
              for index_point in clusters[index_cluster]:
                  if index_point == index_point_medoid:
                      continue
  
                  expected_total_deviation += metric(
                      sample[index_point_medoid], sample[index_point]
                  )
  
          assertion.eq_float(
              expected_total_deviation,
              kmedoids_instance.get_total_deviation(),
              0.000001,
          )
  
          labels = kmedoids_instance.get_labels()
          assertion.eq(len(sample), len(labels))
          for index_point in range(len(labels)):
              actual_index_cluster = labels[index_point]
              assertion.true(
                  index_point in clusters[actual_index_cluster]
              )
  
          testing_result = True
  
      assertion.true(testing_result)
  
file_path: pyclustering/pyclustering/cluster/tests/kmedoids_templates.py
imports: import math
  import numpy
  from pyclustering.tests.assertion import assertion
  from pyclustering.cluster.kmedoids import kmedoids, build
  from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
  from pyclustering.samples import answer_reader
  from pyclustering.utils import read_sample, calculate_distance_matrix
  from pyclustering.utils.metric import distance_metric, type_metric
  from random import random, randint
label_desc: Use image node_id 2 for calling the kmedoids_test_template obj's underlying member method code with example usage: obj.templateLengthProcessWithMetric(path_to_file, initial_medoids, expected_cluster_length, metric, ccore_flag) without return types