code: def test_agglomerative_clustering_distances(
      n_clusters, compute_distances, distance_threshold, linkage
  ):
      # Check that when `compute_distances` is True or `distance_threshold` is
      # given, the fitted model has an attribute `distances_`.
      rng = np.random.RandomState(0)
      mask = np.ones([10, 10], dtype=bool)
      n_samples = 100
      X = rng.randn(n_samples, 50)
      connectivity = grid_to_graph(*mask.shape)
  
      clustering = AgglomerativeClustering(
          n_clusters=n_clusters,
          connectivity=connectivity,
          linkage=linkage,
          distance_threshold=distance_threshold,
          compute_distances=compute_distances,
      )
      clustering.fit(X)
      if compute_distances or (distance_threshold is not None):
          assert hasattr(clustering, "distances_")
          n_children = clustering.children_.shape[0]
          n_nodes = n_children + 1
          assert clustering.distances_.shape == (n_nodes - 1,)
      else:
          assert not hasattr(clustering, "distances_")
  
imports: import itertools
  import shutil
  from functools import partial
  from tempfile import mkdtemp
  import numpy
  import pytest
  from scipy.cluster import hierarchy
  from scipy.sparse.csgraph import connected_components
  from sklearn.cluster import AgglomerativeClustering, FeatureAgglomeration, ward_tree
  from sklearn.cluster._agglomerative import _TREE_BUILDERS, _fix_connectivity, _hc_cut, linkage_tree
  from sklearn.cluster._hierarchical_fast import average_merge, max_merge, mst_linkage_core
  from sklearn.datasets import make_circles, make_moons
  from sklearn.feature_extraction.image import grid_to_graph
  from sklearn.metrics import DistanceMetric
  from sklearn.metrics.cluster import adjusted_rand_score, normalized_mutual_info_score
  from sklearn.metrics.pairwise import PAIRED_DISTANCES, cosine_distances, manhattan_distances, pairwise_distances
  from sklearn.metrics.tests.test_dist_metrics import METRICS_DEFAULT_PARAMS
  from sklearn.neighbors import kneighbors_graph
  from sklearn.utils._fast_dict import IntFloatDict
  from sklearn.utils._testing import assert_almost_equal, assert_array_almost_equal, assert_array_equal, create_memmap_backed_data, ignore_warnings
  from sklearn.utils.fixes import LIL_CONTAINERS
label_desc: Use image node_id 6 for calling a global function with example usage: test_agglomerative_clustering_distances(n_clusters, compute_distances, distance_threshold, linkage) without return types