code: def test_concat():
      # Smoke test: concat empty list.
      with pytest.raises(ValueError):
          concat_images([])
  
      # Build combinations of 3D, 4D w/size[3] == 1, and 4D w/size[3] == 3
      all_shapes_5D = (
          (1, 4, 5, 3, 3),
          (7, 3, 1, 4, 5),
          (0, 2, 1, 4, 5),
      )
  
      affine = np.eye(4)
      for dim in range(2, 6):
          all_shapes_ND = tuple(shape[:dim] for shape in all_shapes_5D)
          all_shapes_N1D_unary = tuple(
              shape + (1,) for shape in all_shapes_ND
          )
          all_shapes = all_shapes_ND + all_shapes_N1D_unary
  
          # Loop over all possible combinations of images, in first and
          #   second position.
          for data0_shape in all_shapes:
              data0_numel = np.asarray(data0_shape).prod()
              data0 = np.arange(data0_numel, dtype="int32").reshape(
                  data0_shape
              )
              img0_mem = Nifti1Image(data0, affine)
  
              for data1_shape in all_shapes:
                  data1_numel = np.asarray(data1_shape).prod()
                  data1 = np.arange(data1_numel, dtype="int32").reshape(
                      data1_shape
                  )
                  img1_mem = Nifti1Image(data1, affine)
                  img2_mem = Nifti1Image(
                      data1, affine + 1
                  )  # bad affine
  
                  # Loop over every possible axis, including None (explicit and implied)
                  for axis in list(range(-(dim - 2), (dim - 1))) + [
                      None,
                      "__default__",
                  ]:
                      # Allow testing default vs. passing explicit param
                      if axis == "__default__":
                          np_concat_kwargs = dict(axis=-1)
                          concat_imgs_kwargs = dict()
                          axis = None  # Convert downstream
                      elif axis is None:
                          np_concat_kwargs = dict(axis=-1)
                          concat_imgs_kwargs = dict(axis=axis)
                      else:
                          np_concat_kwargs = dict(axis=axis)
                          concat_imgs_kwargs = dict(axis=axis)
  
                      # Create expected output
                      try:
                          # Error will be thrown if the np.concatenate fails.
                          #   However, when axis=None, the concatenate is possible
                          #   but our efficient logic (where all images are
                          #   3D and the same size) fails, so we also
                          #   have to expect errors for those.
                          if axis is None:  # 3D from here and below
                              all_data = np.concatenate(
                                  [
                                      data0[..., np.newaxis],
                                      data1[..., np.newaxis],
                                  ],
                                  **np_concat_kwargs,
                              )
                          else:  # both 3D, appending on final axis
                              all_data = np.concatenate(
                                  [data0, data1], **np_concat_kwargs
                              )
                          expect_error = False
                      except ValueError:
                          # Shapes are not combinable
                          expect_error = True
  
                      # Check filenames and in-memory images work
                      with InTemporaryDirectory():
                          # Try mem-based, file-based, and mixed
                          imgs = [img0_mem, img1_mem, img2_mem]
                          img_files = [_as_fname(img) for img in imgs]
                          imgs_mixed = [imgs[0], img_files[1], imgs[2]]
                          for img0, img1, img2 in (
                              imgs,
                              img_files,
                              imgs_mixed,
                          ):
                              try:
                                  all_imgs = concat_images(
                                      [img0, img1], **concat_imgs_kwargs
                                  )
                              except ValueError as ve:
                                  assert expect_error, str(ve)
                              else:
                                  assert (
                                      not expect_error
                                  ), "Expected a concatenation error, but got none."
                                  assert_array_equal(
                                      all_imgs.get_fdata(), all_data
                                  )
                                  assert_array_equal(
                                      all_imgs.affine, affine
                                  )
  
                              # check that not-matching affines raise error
                              with pytest.raises(ValueError):
                                  concat_images(
                                      [img0, img2], **concat_imgs_kwargs
                                  )
  
                              # except if check_affines is False
                              try:
                                  all_imgs = concat_images(
                                      [img0, img1], **concat_imgs_kwargs
                                  )
                              except ValueError as ve:
                                  assert expect_error, str(ve)
                              else:
                                  assert (
                                      not expect_error
                                  ), "Expected a concatenation error, but got none."
                                  assert_array_equal(
                                      all_imgs.get_fdata(), all_data
                                  )
                                  assert_array_equal(
                                      all_imgs.affine, affine
                                  )
  
file_path: nibabel/nibabel/tests/test_funcs.py
imports: import numpy
  import pytest
  from numpy.testing import assert_array_equal
  from ..analyze import AnalyzeImage
  from ..funcs import OrientationError, as_closest_canonical, concat_images
  from ..loadsave import save
  from ..nifti1 import Nifti1Image
  from ..tmpdirs import InTemporaryDirectory
label_desc: Use image node_id 2 for calling a global function with example usage: test_concat() without return types