code: def test_ward_linkage_tree_return_distance(global_random_seed):
      # Test return_distance option on linkage and ward trees
  
      # test that return_distance when set true, gives same
      # output on both structured and unstructured clustering.
      n, p = 10, 5
      rng = np.random.RandomState(global_random_seed)
  
      connectivity = np.ones((n, n))
      for i in range(5):
          X = 0.1 * rng.normal(size=(n, p))
          X -= 4.0 * np.arange(n)[:, np.newaxis]
          X -= X.mean(axis=1)[:, np.newaxis]
  
          out_unstructured = ward_tree(X, return_distance=True)
          out_structured = ward_tree(
              X, connectivity=connectivity, return_distance=True
          )
  
          # get children
          children_unstructured = out_unstructured[0]
          children_structured = out_structured[0]
  
          # check if we got the same clusters
          assert_array_equal(children_unstructured, children_structured)
  
          # check if the distances are the same
          dist_unstructured = out_unstructured[-1]
          dist_structured = out_structured[-1]
  
          assert_array_almost_equal(dist_unstructured, dist_structured)
  
          for linkage in ["average", "complete", "single"]:
              structured_items = linkage_tree(
                  X,
                  connectivity=connectivity,
                  linkage=linkage,
                  return_distance=True,
              )[-1]
              unstructured_items = linkage_tree(
                  X, linkage=linkage, return_distance=True
              )[-1]
              structured_dist = structured_items[-1]
              unstructured_dist = unstructured_items[-1]
              structured_children = structured_items[0]
              unstructured_children = unstructured_items[0]
              assert_array_almost_equal(
                  structured_dist, unstructured_dist
              )
              assert_array_almost_equal(
                  structured_children, unstructured_children
              )
  
      # test on the following dataset where we know the truth
      # taken from scipy/cluster/tests/hierarchy_test_data.py
      X = np.array(
          [
              [1.43054825, -7.5693489],
              [6.95887839, 6.82293382],
              [2.87137846, -9.68248579],
              [7.87974764, -6.05485803],
              [8.24018364, -6.09495602],
              [7.39020262, 8.54004355],
          ]
      )
      # truth
      linkage_X_ward = np.array(
          [
              [3.0, 4.0, 0.36265956, 2.0],
              [1.0, 5.0, 1.77045373, 2.0],
              [0.0, 2.0, 2.55760419, 2.0],
              [6.0, 8.0, 9.10208346, 4.0],
              [7.0, 9.0, 24.7784379, 6.0],
          ]
      )
  
      linkage_X_complete = np.array(
          [
              [3.0, 4.0, 0.36265956, 2.0],
              [1.0, 5.0, 1.77045373, 2.0],
              [0.0, 2.0, 2.55760419, 2.0],
              [6.0, 8.0, 6.96742194, 4.0],
              [7.0, 9.0, 18.77445997, 6.0],
          ]
      )
  
      linkage_X_average = np.array(
          [
              [3.0, 4.0, 0.36265956, 2.0],
              [1.0, 5.0, 1.77045373, 2.0],
              [0.0, 2.0, 2.55760419, 2.0],
              [6.0, 8.0, 6.55832839, 4.0],
              [7.0, 9.0, 15.44089605, 6.0],
          ]
      )
  
      n_samples, n_features = np.shape(X)
      connectivity_X = np.ones((n_samples, n_samples))
  
      out_X_unstructured = ward_tree(X, return_distance=True)
      out_X_structured = ward_tree(
          X, connectivity=connectivity_X, return_distance=True
      )
  
      # check that the labels are the same
      assert_array_equal(linkage_X_ward[:, :2], out_X_unstructured[0])
      assert_array_equal(linkage_X_ward[:, :2], out_X_structured[0])
  
      # check that the distances are correct
      assert_array_almost_equal(
          linkage_X_ward[:, 2], out_X_unstructured[4]
      )
      assert_array_almost_equal(
          linkage_X_ward[:, 2], out_X_structured[4]
      )
  
      linkage_options = ["complete", "average", "single"]
      X_linkage_truth = [linkage_X_complete, linkage_X_average]
      for linkage, X_truth in zip(linkage_options, X_linkage_truth):
          out_X_unstructured = linkage_tree(
              X, return_distance=True, linkage=linkage
          )
          out_X_structured = linkage_tree(
              X,
              connectivity=connectivity_X,
              linkage=linkage,
              return_distance=True,
          )
  
          # check that the labels are the same
          assert_array_equal(X_truth[:, :2], out_X_unstructured[0])
          assert_array_equal(X_truth[:, :2], out_X_structured[0])
  
          # check that the distances are correct
          assert_array_almost_equal(
              X_truth[:, 2], out_X_unstructured[4]
          )
          assert_array_almost_equal(X_truth[:, 2], out_X_structured[4])
  
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 18 for calling a global function with example usage: test_ward_linkage_tree_return_distance(global_random_seed) without return types