code: def test_metric_minimum_average_direct_flip():
      feature = dipysfeature.IdentityFeature()
  
      class MinimumAverageDirectFlipMetric(dipysmetric.Metric):
          def __init__(self, feature):
              super(MinimumAverageDirectFlipMetric, self).__init__(
                  feature=feature
              )
  
          @property
          def is_order_invariant(self):
              return True  # Ordering is handled in the distance computation
  
          def are_compatible(self, shape1, shape2):
              return shape1[0] == shape2[0]
  
          def dist(self, v1, v2):
              def average_euclidean(x, y):
                  return np.mean(norm(x - y, axis=1))
  
              dist_direct = average_euclidean(v1, v2)
              dist_flipped = average_euclidean(v1, v2[::-1])
              return min(dist_direct, dist_flipped)
  
      for metric in [
          MinimumAverageDirectFlipMetric(feature),
          dipysmetric.MinimumAverageDirectFlipMetric(feature),
      ]:
          # Test special cases of the MDF distance.
          assert_equal(metric.dist(s, s), 0.0)
          assert_equal(metric.dist(s, s[::-1]), 0.0)
  
          # Translation
          offset = np.array([0.8, 1.3, 5], dtype=dtype)
          assert_almost_equal(
              metric.dist(s, s + offset), norm(offset), 5
          )
  
          # Scaling
          M_scaling = np.diag([1.2, 2.8, 3]).astype(dtype)
          s_mean = np.mean(s, axis=0)
          s_zero_mean = s - s_mean
          s_scaled = np.dot(M_scaling, s_zero_mean.T).T + s_mean
          d = np.mean(
              norm((np.diag(M_scaling) - 1) * s_zero_mean, axis=1)
          )
          assert_almost_equal(metric.dist(s, s_scaled), d, 5)
  
          # Rotation
          from dipy.core.geometry import rodrigues_axis_rotation
  
          rot_axis = np.array([1, 2, 3], dtype=dtype)
          M_rotation = rodrigues_axis_rotation(rot_axis, 60.0).astype(
              dtype
          )
          s_mean = np.mean(s, axis=0)
          s_zero_mean = s - s_mean
          s_rotated = np.dot(M_rotation, s_zero_mean.T).T + s_mean
  
          opposite = norm(
              np.cross(rot_axis, s_zero_mean), axis=1
          ) / norm(rot_axis)
          distances = np.sqrt(
              2 * opposite**2 * (1 - np.cos(60.0 * np.pi / 180.0))
          ).astype(dtype)
          d = np.mean(distances)
          assert_almost_equal(metric.dist(s, s_rotated), d, 5)
  
          # 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
              same_nb_points = f1.shape[0] == f2.shape[0]
              assert_equal(
                  metric.are_compatible(f1.shape, f2.shape),
                  same_nb_points,
              )
  
              # 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_equal(distance, 0.0)
  
                  assert_almost_equal(
                      distance, dipysmetric.dist(metric, s1, s2)
                  )
                  assert_almost_equal(distance, dipymetric.mdf(s1, s2))
                  assert_greater_equal(distance, 0.0)
  
          # This metric type is order invariant
          assert_true(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_true(
                      np.allclose(metric.dist(f1, f2_flip), distance)
                  )
                  assert_true(
                      np.allclose(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 2 for calling a global function with example usage: test_metric_minimum_average_direct_flip() and returns: True, unknown, min, np