code: def test_save_dtype():
      # Test we can specify the dtype on conversion
      data = np.random.normal(size=(2, 3, 4))
      cmap = vox2mni(np.diag([2.0, 3, 4, 1]))
      for dt_code in (
          "i1",
          "u1",
          "i2",
          "u2",
          "i4",
          "u4",
          "i8",
          "u8",
          "f4",
          "f8",
          "c8",
          "c16",
      ):
          dt = np.dtype(dt_code)
          with warnings.catch_warnings():
              warnings.simplefilter("ignore")
              out_data = data.astype(dt_code)
          img = Image(out_data, cmap)
          ni_img = nipy2nifti(img, data_dtype=dt_code)
          assert get_header(ni_img).get_data_dtype() == dt
          ni_img = nipy2nifti(img, data_dtype=dt)
          assert get_header(ni_img).get_data_dtype() == dt
      # None results in trying to get the code from the input header, then from the
      # data.
      # From data, when there's nothing in the header
      img = Image(data.astype(np.int16), cmap)
      ni_img = nipy2nifti(img, data_dtype=None)
      assert get_header(ni_img).get_data_dtype() == np.dtype(np.int16)
      # From the header
      hdr = nib.Nifti1Header()
      hdr.set_data_dtype(np.int32)
      img = Image(data.astype(np.int16), cmap, metadata={"header": hdr})
      ni_img = nipy2nifti(img, data_dtype=None)
      assert get_header(ni_img).get_data_dtype() == np.dtype(np.int32)
      # Bad dtype
      pytest.raises(TypeError, nipy2nifti, img, data_dtype="foo")
      # Fancy dtype
      data = np.zeros((2, 3, 4), dtype=[("f0", "i2"), ("f1", "f4")])
      img = Image(data, cmap)
      pytest.raises(HeaderDataError, nipy2nifti, img, data_dtype=None)
  
imports: import warnings
  from copy import copy
  import nibabel
  import numpy
  import pytest
  from nibabel.affines import from_matvec
  from nibabel.spatialimages import HeaderDataError
  from numpy.testing import assert_array_equal
  from ...core.api import AffineTransform
  from ...core.api import CoordinateSystem
  from ...core.api import Image
  from ...core.reference.spaces import aligned_csm, mni_csm, scanner_csm, talairach_csm, unknown_csm, unknown_space, vox2mni
  from ...testing import anatfile, funcfile
  from ..files import load
  from ..nibcompat import get_affine, get_header
  from ..nifti_ref import NiftiError, nifti2nipy, nipy2nifti
label_desc: Use image node_id 15 for calling a global function with example usage: test_save_dtype() without return types