code: def test_output_dtypes(in_tmp_path):
      shape = (4, 2, 3)
      rng = np.random.RandomState(19441217)  # IN-S BD
      data = rng.normal(4, 20, size=shape)
      aff = np.diag([2.2, 3.3, 4.1, 1])
      cmap = vox2mni(aff)
      img = Image(data, cmap)
      fname_root = "my_file"
      for ext in "img", "nii":
          out_fname = fname_root + "." + ext
          # Default is for data to come from data dtype
          save_image(img, out_fname)
          img_back = load_image(out_fname)
          hdr = img_back.metadata["header"]
          assert_dt_no_end_equal(hdr.get_data_dtype(), np.float64)
          del img_back  # lets window reuse the file
          # All these types are OK for both output formats
          for out_dt in "i2", "i4", np.int16, "<f4", ">f8":
              # Specified output dtype
              save_image(img, out_fname, out_dt)
              img_back = load_image(out_fname)
              hdr = img_back.metadata["header"]
              assert_dt_no_end_equal(hdr.get_data_dtype(), out_dt)
              del img_back  # windows file reuse
              # Output comes from data by default
              data_typed = data.astype(out_dt)
              img_again = Image(data_typed, cmap)
              save_image(img_again, out_fname)
              img_back = load_image(out_fname)
              hdr = img_back.metadata["header"]
              assert_dt_no_end_equal(hdr.get_data_dtype(), out_dt)
              del img_back
              # Even if header specifies otherwise
              in_hdr = Nifti1Header()
              in_hdr.set_data_dtype(np.dtype("c8"))
              img_more = Image(
                  data_typed, cmap, metadata={"header": in_hdr}
              )
              save_image(img_more, out_fname)
              img_back = load_image(out_fname)
              hdr = img_back.metadata["header"]
              assert_dt_no_end_equal(hdr.get_data_dtype(), out_dt)
              del img_back
              # But can come from header if specified
              save_image(img_more, out_fname, dtype_from="header")
              img_back = load_image(out_fname)
              hdr = img_back.metadata["header"]
              assert_dt_no_end_equal(hdr.get_data_dtype(), "c8")
              del img_back
      # u2 only OK for nifti
      save_image(img, "my_file.nii", "u2")
      img_back = load_image("my_file.nii")
      hdr = img_back.metadata["header"]
      assert_dt_no_end_equal(hdr.get_data_dtype(), "u2")
      # Check analyze can't save u2 datatype
      pytest.raises(
          HeaderDataError, save_image, img, "my_file.img", "u2"
      )
      del img_back
  
imports: from os.path import dirname
  from os.path import join
  import nibabel
  import numpy
  import pytest
  from nibabel import Nifti1Header
  from nibabel.filebasedimages import ImageFileError
  from nibabel.spatialimages import HeaderDataError
  from nibabel.tests.test_round_trip import big_bad_ulp
  from nipy.core.api import AffineTransform
  from nipy.core.api import Image, vox2mni
  from nipy.testing import anatfile, assert_almost_equal, assert_array_almost_equal, assert_array_equal, funcfile
  from nipy.testing.decorators import if_templates
  from nipy.utils import DataError, templates
  from ..api import as_image, load_image, save_image
label_desc: Use image node_id 8 for calling a global function with example usage: test_output_dtypes(in_tmp_path) without return types