code: def test_agglomerative_clustering(global_random_seed, lil_container):
      # Check that we obtain the correct number of clusters with
      # agglomerative clustering.
      rng = np.random.RandomState(global_random_seed)
      mask = np.ones([10, 10], dtype=bool)
      n_samples = 100
      X = rng.randn(n_samples, 50)
      connectivity = grid_to_graph(*mask.shape)
      for linkage in ("ward", "complete", "average", "single"):
          clustering = AgglomerativeClustering(
              n_clusters=10, connectivity=connectivity, linkage=linkage
          )
          clustering.fit(X)
          # test caching
          try:
              tempdir = mkdtemp()
              clustering = AgglomerativeClustering(
                  n_clusters=10,
                  connectivity=connectivity,
                  memory=tempdir,
                  linkage=linkage,
              )
              clustering.fit(X)
              labels = clustering.labels_
              assert np.size(np.unique(labels)) == 10
          finally:
              shutil.rmtree(tempdir)
          # Turn caching off now
          clustering = AgglomerativeClustering(
              n_clusters=10, connectivity=connectivity, linkage=linkage
          )
          # Check that we obtain the same solution with early-stopping of the
          # tree building
          clustering.compute_full_tree = False
          clustering.fit(X)
          assert_almost_equal(
              normalized_mutual_info_score(clustering.labels_, labels),
              1,
          )
          clustering.connectivity = None
          clustering.fit(X)
          assert np.size(np.unique(clustering.labels_)) == 10
          # Check that we raise a TypeError on dense matrices
          clustering = AgglomerativeClustering(
              n_clusters=10,
              connectivity=lil_container(
                  connectivity.toarray()[:10, :10]
              ),
              linkage=linkage,
          )
          with pytest.raises(ValueError):
              clustering.fit(X)
  
      # Test that using ward with another metric than euclidean raises an
      # exception
      clustering = AgglomerativeClustering(
          n_clusters=10,
          connectivity=connectivity.toarray(),
          metric="manhattan",
          linkage="ward",
      )
      with pytest.raises(ValueError):
          clustering.fit(X)
  
      # Test using another metric than euclidean works with linkage complete
      for metric in PAIRED_DISTANCES.keys():
          # Compare our (structured) implementation to scipy
          clustering = AgglomerativeClustering(
              n_clusters=10,
              connectivity=np.ones((n_samples, n_samples)),
              metric=metric,
              linkage="complete",
          )
          clustering.fit(X)
          clustering2 = AgglomerativeClustering(
              n_clusters=10,
              connectivity=None,
              metric=metric,
              linkage="complete",
          )
          clustering2.fit(X)
          assert_almost_equal(
              normalized_mutual_info_score(
                  clustering2.labels_, clustering.labels_
              ),
              1,
          )
  
      # Test that using a distance matrix (affinity = 'precomputed') has same
      # results (with connectivity constraints)
      clustering = AgglomerativeClustering(
          n_clusters=10, connectivity=connectivity, linkage="complete"
      )
      clustering.fit(X)
      X_dist = pairwise_distances(X)
      clustering2 = AgglomerativeClustering(
          n_clusters=10,
          connectivity=connectivity,
          metric="precomputed",
          linkage="complete",
      )
      clustering2.fit(X_dist)
      assert_array_equal(clustering.labels_, clustering2.labels_)
  
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 7 for calling a global function with example usage: test_agglomerative_clustering(global_random_seed, lil_container) without return types