code: def test_reversible_gradient(plot=False):
      def loglike(x):
          x, y = x.transpose()
          return -0.5 * (x**2 + ((y - 0.5) / 0.2) ** 2)
  
      def transform(u):
          return u
  
      Lmin = -0.5
  
      for i in [84] + list(range(1, 100)):
          print("setting seed = %d" % i)
          np.random.seed(i)
          points = np.random.uniform(size=(10000, 2))
          L = loglike(points)
          mask = L > Lmin
          points = points[mask, :][:100, :]
          active_u = points
          active_values = L[mask][:100]
  
          transformLayer = AffineLayer(wrapped_dims=[])
          transformLayer.optimize(points, points)
          region = MLFriends(points, transformLayer)
          (
              region.maxradiussq,
              region.enlarge,
          ) = region.compute_enlargement(nbootstraps=30)
          region.create_ellipsoid()
          nclusters = transformLayer.nclusters
          assert nclusters == 1
          assert np.allclose(
              region.unormed, region.transformLayer.transform(points)
          ), "transform should be reproducible"
          assert region.inside(
              points
          ).all(), "live points should lie near live points"
  
          if i == 84:
              v = np.array([0.03477044, -0.01977415])
              reflpoint = np.array([0.09304075, 0.29114574])
          elif i == 4:
              v = np.array([0.03949306, -0.00634806])
              reflpoint = np.array([0.9934771, 0.55358031])
  
          else:
              v = np.random.normal(size=2)
              v /= (v**2).sum() ** 0.5
              v *= 0.04
              j = np.random.randint(len(active_u))
              reflpoint = np.random.normal(active_u[j, :], 0.04)
              if (
                  not (reflpoint < 1).all()
                  and not (reflpoint > 0).all()
              ):
                  continue
  
          bpts = region.transformLayer.transform(reflpoint).reshape(
              (1, -1)
          )
          tt = get_sphere_tangents(region.unormed, bpts)
          t = (
              region.transformLayer.untransform(
                  tt * 1e-3 + region.unormed
              )
              - region.u
          )
          # compute new vector
          normal = t / norm(t, axis=1).reshape((-1, 1))
          print("reflecting at  ", reflpoint, "with direction", v)
          mask_forward1, angles, anglesnew = get_reflection_angles(
              normal, v
          )
          if mask_forward1.any():
              j = np.argmin(
                  ((region.unormed[mask_forward1, :] - bpts) ** 2).sum(
                      axis=1
                  )
              )
              k = np.arange(len(normal))[mask_forward1][j]
              angles_used = angles[k]
              normal_used = normal[k, :]
              print("chose normal", normal_used, k)
              # chosen_point = region.u[k,:]
              vnew = -(v - 2 * angles_used * normal_used)
              assert vnew.shape == v.shape
  
              (
                  mask_forward2,
                  angles2,
                  anglesnew2,
              ) = get_reflection_angles(normal, vnew)
              # j2 = np.argmin(((region.unormed[mask_forward2,:] - bpts)**2).sum(axis=1))
              # chosen_point2 = region.u[mask_forward2,:][0,:]
              # assert j2 == j, (j2, j)
              assert mask_forward2[k]
              # assert_allclose(chosen_point, chosen_point2)
  
              # for m, a, b, m2, a2, b2 in zip(mask_forward1, angles, anglesnew, mask_forward2, angles2, anglesnew2):
              #    if m != m2:
              #        print('  ', m, a, b, m2, a2, b2)
  
              # print("using normal", normal)
              # print("changed v from", v, "to", vnew)
  
              # angles2 = -(normal * (vnew / norm(vnew))).sum(axis=1)
              # mask_forward2 = angles < 0
              if plot:
                  plt.figure(figsize=(5, 5))
                  plt.title("%d" % mask_forward1.sum())
                  plt.plot(
                      (reflpoint + v)[0],
                      (reflpoint + v)[1],
                      "^",
                      color="orange",
                  )
                  plt.plot(
                      (reflpoint + vnew)[:, 0],
                      (reflpoint + vnew)[:, 1],
                      "^ ",
                      color="lime",
                  )
                  plt.plot(reflpoint[0], reflpoint[1], "^ ", color="r")
                  plt.plot(
                      region.u[:, 0],
                      region.u[:, 1],
                      "x ",
                      ms=2,
                      color="k",
                  )
                  plt.plot(
                      region.u[mask_forward1, 0],
                      region.u[mask_forward1, 1],
                      "o ",
                      ms=6,
                      mfc="None",
                      mec="b",
                  )
                  plt.plot(
                      region.u[mask_forward2, 0],
                      region.u[mask_forward2, 1],
                      "s ",
                      ms=8,
                      mfc="None",
                      mec="g",
                  )
                  plt.xlim(0, 1)
                  plt.ylim(0, 1)
                  plt.savefig(
                      "test_flatnuts_reversible_gradient_%d.png" % i,
                      bbox_inches="tight",
                  )
                  plt.close()
              assert mask_forward1[k] == mask_forward2[k], (
                  mask_forward1[k],
                  mask_forward2[k],
              )
  
              print("reflecting at  ", reflpoint, "with direction", v)
              # make that step, then try to go back
              j = np.arange(len(normal))[mask_forward1][0]
              normal = normal[j, :]
              angles = (normal * (v / norm(v))).sum()
              v2 = v - 2 * angle(normal, v) * normal
  
              print("reflecting with", normal, "new direction", v2)
  
              # newpoint = reflpoint + v2
              # angles2 = (normal * (v2 / norm(v2))).sum()
              v3 = v2 - 2 * angle(normal, v2) * normal
  
              print("re-reflecting gives direction", v3)
              assert_allclose(v3, v)
  
              print()
              print("FORWARD:", v, reflpoint)
              samplingpath = SamplingPath(
                  reflpoint - v, v, active_values[0]
              )
              contourpath = ContourSamplingPath(samplingpath, region)
              normal = contourpath.gradient(reflpoint)
              if normal is not None:
                  assert normal.shape == v.shape, (
                      normal.shape,
                      v.shape,
                  )
  
                  print("BACKWARD:", v, reflpoint)
                  v2 = -(v - 2 * angle(normal, v) * normal)
                  normal2 = contourpath.gradient(reflpoint)
                  assert_allclose(normal, normal2)
                  normal2 = normal
                  v3 = -(v2 - 2 * angle(normal2, v2) * normal2)
                  assert_allclose(v3, v)
  
file_path: ultranest/tests/test_samplingpath.py
imports: import numpy
  import matplotlib.pyplot
  from ultranest.mlfriends import AffineLayer, MLFriends
  from ultranest.samplingpath import SamplingPath, ContourSamplingPath
  from ultranest.samplingpath import box_line_intersection, nearest_box_intersection_line, linear_steps_with_reflection, angle, get_sphere_tangents, norm
  from numpy.testing import assert_allclose
label_desc: Use image node_id 10 for calling a global function with example usage: test_reversible_gradient(plot) and returns: unknown, u