code: def _fix_connectivity(
      X, connectivity, n_components=None, affinity="euclidean"
  ):
      """
      Fixes the connectivity matrix
  
          - copies it
          - makes it symmetric
          - converts it to LIL if necessary
          - completes it if necessary
      """
      n_samples = X.shape[0]
      if (
          connectivity.shape[0] != n_samples
          or connectivity.shape[1] != n_samples
      ):
          raise ValueError(
              "Wrong shape for connectivity matrix: %s "
              "when X is %s" % (connectivity.shape, X.shape)
          )
  
      # Make the connectivity matrix symmetric:
      connectivity = connectivity + connectivity.T
  
      # Convert connectivity matrix to LIL
      if not sparse.isspmatrix_lil(connectivity):
          if not sparse.isspmatrix(connectivity):
              connectivity = sparse.lil_matrix(connectivity)
          else:
              connectivity = connectivity.tolil()
  
      # Compute the number of nodes
      n_components, labels = connected_components(connectivity)
  
      if n_components > 1:
          warnings.warn(
              "the number of connected components of the "
              "connectivity matrix is %d > 1. Completing it to avoid "
              "stopping the tree early." % n_components,
              stacklevel=2,
          )
          # XXX: Can we do without completing the matrix?
          for i in xrange(n_components):
              idx_i = np.where(labels == i)[0]
              Xi = X[idx_i]
              for j in xrange(i):
                  idx_j = np.where(labels == j)[0]
                  Xj = X[idx_j]
                  D = pairwise_distances(Xi, Xj, metric=affinity)
                  ii, jj = np.where(D == np.min(D))
                  ii = ii[0]
                  jj = jj[0]
                  connectivity[idx_i[ii], idx_j[jj]] = True
                  connectivity[idx_j[jj], idx_i[ii]] = True
  
      return connectivity, n_components
  
imports: from heapq import heapify, heappop, heappush, heappushpop
  import warnings
  import numpy
  from scipy import sparse
  from ..base import BaseEstimator, ClusterMixin
  from ..externals.joblib import Memory
  from ..externals import six
  from ..metrics.pairwise import paired_distances, pairwise_distances
  from ..utils import check_array
  from ..utils.sparsetools import connected_components
  from .None import _hierarchical
  from ._feature_agglomeration import AgglomerationTransform
  from ..utils.fast_dict import IntFloatDict
  from ..externals.six.moves import xrange
label_desc: Use image node_id 1 for calling a global function with example usage: _fix_connectivity(X, connectivity, n_components, affinity) and returns: connectivity,  n_components