code: def _calculate_pairwise_distances(
      X, Y=None, metric="precomputed", n_neighbors=None
  ):
      if metric in ("precomputed", "ignore"):
          return X
  
      if n_neighbors is None:
          if metric == "euclidean":
              X_pairwise = pairwise_distances(
                  X, Y=Y, metric=metric, squared=True
              )
          elif metric == "correlation" or metric == "cosine":
              # An in-place version of:
              # X_pairwise = 1 - (1 - pairwise_distances(X, metric=metric)) ** 2
  
              X_pairwise = pairwise_distances(X, Y=Y, metric=metric)
              X_pairwise = numpy.subtract(1, X_pairwise, out=X_pairwise)
              X_pairwise = numpy.square(X_pairwise, out=X_pairwise)
              X_pairwise = numpy.subtract(1, X_pairwise, out=X_pairwise)
          else:
              X_pairwise = pairwise_distances(X, Y=Y, metric=metric)
      else:
          if metric == "correlation" or metric == "cosine":
              # An in-place version of:
              # X = 1 - (1 - pairwise_distances(X, metric=metric)) ** 2
  
              X = pairwise_distances(X, Y=Y, metric=metric)
              X = numpy.subtract(1, X, out=X)
              X = numpy.square(X, out=X)
              X = numpy.subtract(1, X, out=X)
              metric = "precomputed"
  
          if isinstance(n_neighbors, int):
              X_pairwise = KNeighborsTransformer(
                  n_neighbors=n_neighbors, metric=metric
              ).fit_transform(X)
  
          elif isinstance(n_neighbors, KNeighborsTransformer):
              X_pairwise = n_neighbors.fit_transform(X)
  
      if metric == "correlation" or metric == "cosine":
          if isinstance(X_pairwise, csr_matrix):
              X_pairwise.data = numpy.subtract(
                  1, X_pairwise.data, out=X_pairwise.data
              )
          else:
              X_pairwise = numpy.subtract(1, X_pairwise, out=X_pairwise)
      else:
          if isinstance(X_pairwise, csr_matrix):
              X_pairwise.data = numpy.subtract(
                  X_pairwise.max(), X_pairwise.data, out=X_pairwise.data
              )
          else:
              X_pairwise = numpy.subtract(
                  X_pairwise.max(), X_pairwise, out=X_pairwise
              )
  
      return X_pairwise
  
file_path: apricot/apricot/utils.py
imports: import numbers
  import numpy
  import itertools
  from heapq import heappush
  from heapq import heappop
  from heapq import heapify
  from heapq import heapreplace
  from scipy.sparse import csr_matrix
  from sklearn.metrics import pairwise_distances
  from sklearn.neighbors import KNeighborsTransformer
label_desc: Use image node_id 2 for calling a global function with example usage: _calculate_pairwise_distances(X, Y, metric, n_neighbors) and returns: X_pairwise, X