code: def test_metric_cosine():
      feature = dipysfeature.VectorOfEndpointsFeature()
  
      class CosineMetric(dipysmetric.Metric):
          def __init__(self, feature):
              super(CosineMetric, self).__init__(feature=feature)
  
          def are_compatible(self, shape1, shape2):
              # Cosine metric works on vectors.
              return shape1 == shape2 and shape1[0] == 1
  
          def dist(self, v1, v2):
              # Check if we have null vectors
              if norm(v1) == 0:
                  return 0.0 if norm(v2) == 0 else 1.0
  
              v1_normed = v1.astype(np.float64) / norm(
                  v1.astype(np.float64)
              )
              v2_normed = v2.astype(np.float64) / norm(
                  v2.astype(np.float64)
              )
              cos_theta = np.dot(v1_normed, v2_normed.T).item()
              # Make sure it's in [-1, 1], i.e. within domain of arccosine
              cos_theta = np.minimum(cos_theta, 1.0)
              cos_theta = np.maximum(cos_theta, -1.0)
              return (
                  np.arccos(cos_theta) / np.pi
              )  # Normalized cosine distance
  
      for metric in [
          CosineMetric(feature),
          dipysmetric.CosineMetric(feature),
      ]:
          # Test special cases of the cosine distance.
          v0 = np.array([[0, 0, 0]], dtype=np.float32)
          v1 = np.array([[1, 2, 3]], dtype=np.float32)
          v2 = np.array([[1, -1.0 / 2, 0]], dtype=np.float32)
          v3 = np.array([[-1, -2, -3]], dtype=np.float32)
  
          assert_equal(metric.dist(v0, v0), 0.0)  # dot-dot
          assert_equal(metric.dist(v0, v1), 1.0)  # dot-line
          assert_equal(metric.dist(v1, v1), 0.0)  # collinear
          assert_equal(metric.dist(v1, v2), 0.5)  # orthogonal
          assert_equal(metric.dist(v1, v3), 1.0)  # opposite
  
          # All possible pairs
          for s1, s2 in itertools.product(*[streamlines] * 2):
              # Extract features since metric doesn't
              # work directly on streamlines
              f1 = metric.feature.extract(s1)
              f2 = metric.feature.extract(s2)
  
              # Test method are_compatible
              are_vectors = f1.shape[0] == 1 and f2.shape[0] == 1
              same_dimension = f1.shape[1] == f2.shape[1]
              assert_equal(
                  metric.are_compatible(f1.shape, f2.shape),
                  are_vectors and same_dimension,
              )
  
              # Test method dist if features are compatible
              if metric.are_compatible(f1.shape, f2.shape):
                  distance = metric.dist(f1, f2)
                  if np.all(f1 == f2):
                      assert_almost_equal(distance, 0.0)
  
                  assert_almost_equal(
                      distance, dipysmetric.dist(metric, s1, s2)
                  )
                  assert_greater_equal(distance, 0.0)
                  assert_less_equal(distance, 1.0)
  
          # This metric type is not order invariant
          assert_false(metric.is_order_invariant)
          # All possible pairs
          for s1, s2 in itertools.product(*[streamlines] * 2):
              f1 = metric.feature.extract(s1)
              f2 = metric.feature.extract(s2)
  
              if not metric.are_compatible(f1.shape, f2.shape):
                  continue
  
              f1_flip = metric.feature.extract(s1[::-1])
              f2_flip = metric.feature.extract(s2[::-1])
  
              distance = metric.dist(f1, f2)
              assert_almost_equal(
                  metric.dist(f1_flip, f2_flip), distance
              )
  
              if not np.all(f1_flip == f2_flip):
                  assert_false(metric.dist(f1, f2_flip) == distance)
                  assert_false(metric.dist(f1_flip, f2) == distance)
  
file_path: dipy/dipy/segment/tests/test_metric.py
imports: import itertools
  import numpy
  from numpy.testing import assert_array_equal, assert_raises, assert_almost_equal, assert_equal
  import dipy.segment.featurespeed
  import dipy.segment.metric
  import dipy.segment.metricspeed
  from dipy.testing import assert_true, assert_false, assert_greater_equal, assert_less_equal
  from dipy.testing.decorators import set_random_number_generator
label_desc: Use image node_id 3 for calling a global function with example usage: test_metric_cosine() and returns: unknown, unknown, unknown