code: def test_values_from_volume():
      decimal = 4
      data3d = np.arange(2000).reshape(20, 10, 10)
      # Test two cases of 4D data (handled differently)
      # One where the last dimension is length 3:
      data4d_3vec = np.arange(6000).reshape(20, 10, 10, 3)
      # The other where the last dimension is not 3:
      data4d_2vec = np.arange(4000).reshape(20, 10, 10, 2)
      for dt in [np.float32, np.float64]:
          for data in [data3d, data4d_3vec, data4d_2vec]:
              sl1 = [
                  np.array(
                      [[1, 0, 0], [1.5, 0, 0], [2, 0, 0], [2.5, 0, 0]]
                  ).astype(dt),
                  np.array(
                      [[2, 0, 0], [3.1, 0, 0], [3.9, 0, 0], [4.1, 0, 0]]
                  ).astype(dt),
              ]
  
              ans1 = [
                  [
                      data[1, 0, 0],
                      data[1, 0, 0]
                      + (data[2, 0, 0] - data[1, 0, 0]) / 2,
                      data[2, 0, 0],
                      data[2, 0, 0]
                      + (data[3, 0, 0] - data[2, 0, 0]) / 2,
                  ],
                  [
                      data[2, 0, 0],
                      data[3, 0, 0]
                      + (data[4, 0, 0] - data[3, 0, 0]) * 0.1,
                      data[3, 0, 0]
                      + (data[4, 0, 0] - data[3, 0, 0]) * 0.9,
                      data[4, 0, 0]
                      + (data[5, 0, 0] - data[4, 0, 0]) * 0.1,
                  ],
              ]
  
              vv = values_from_volume(data, sl1, np.eye(4))
              npt.assert_almost_equal(vv, ans1, decimal=decimal)
  
              vv = values_from_volume(data, np.array(sl1), np.eye(4))
              npt.assert_almost_equal(vv, ans1, decimal=decimal)
  
              vv = values_from_volume(data, Streamlines(sl1), np.eye(4))
              npt.assert_almost_equal(vv, ans1, decimal=decimal)
  
              affine = np.eye(4)
              affine[:, 3] = [-100, 10, 1, 1]
              x_sl1 = transform_streamlines(sl1, affine)
              x_sl2 = transform_streamlines(sl1, affine)
  
              vv = values_from_volume(data, x_sl1, affine)
              npt.assert_almost_equal(vv, ans1, decimal=decimal)
  
              x_sl1 = transform_streamlines(sl1, affine)
              vv = values_from_volume(data, x_sl1, affine)
  
              npt.assert_almost_equal(vv, ans1, decimal=decimal)
  
              # Test that the streamlines haven't mutated:
              l_sl2 = list(x_sl2)
              npt.assert_equal(x_sl1, l_sl2)
  
              vv = values_from_volume(data, np.array(x_sl1), affine)
              npt.assert_almost_equal(vv, ans1, decimal=decimal)
              npt.assert_equal(np.array(x_sl1), np.array(l_sl2))
  
              # Test for lists of streamlines with different numbers of nodes:
              sl2 = [sl1[0][:-1], sl1[1]]
              ans2 = [ans1[0][:-1], ans1[1]]
              vv = values_from_volume(data, sl2, np.eye(4))
              for ii, v in enumerate(vv):
                  npt.assert_almost_equal(v, ans2[ii], decimal=decimal)
  
      # We raise an error if the streamlines fed don't make sense. In this
      # case, a tuple instead of a list, generator or array
      nonsense_sl = (
          np.array([[1, 0, 0], [1.5, 0, 0], [2, 0, 0], [2.5, 0, 0]]),
          np.array([[2, 0, 0], [3.1, 0, 0], [3.9, 0, 0], [4.1, 0, 0]]),
      )
  
      npt.assert_raises(
          RuntimeError, values_from_volume, data, nonsense_sl, np.eye(4)
      )
  
      # For some use-cases we might have singleton streamlines (with only one
      # node each):
      data3D = np.ones((2, 2, 2))
      streamlines = np.ones((10, 1, 3))
      npt.assert_equal(
          values_from_volume(data3D, streamlines, np.eye(4)).shape,
          (10, 1),
      )
      data4D = np.ones((2, 2, 2, 2))
      streamlines = np.ones((10, 1, 3))
      npt.assert_equal(
          values_from_volume(data4D, streamlines, np.eye(4)).shape,
          (10, 1, 2),
      )
  
imports: import warnings
  import types
  import numpy
  from numpy.linalg import norm
  import numpy.testing
  from dipy.testing.memory import get_type_refcount
  from dipy.testing import assert_arrays_equal
  from dipy.testing.decorators import set_random_number_generator
  from dipy.testing import assert_true
  from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_raises, assert_allclose, assert_almost_equal, assert_equal
  from dipy.tracking.streamlinespeed import compress_streamlines, length, set_number_of_points
  from dipy.tracking.streamline import Streamlines
  from dipy.tracking.streamline import relist_streamlines, unlist_streamlines, center_streamlines, transform_streamlines, select_random_set_of_streamlines, select_by_rois, orient_by_rois, orient_by_streamline, values_from_volume, deform_streamlines, cluster_confidence
label_desc: Use image node_id 22 for calling a global function with example usage: test_values_from_volume() without return types