code: def test_write_4d(self):
      with tempfile.TemporaryDirectory() as out_dir:
          image_name = os.path.join(out_dir, "test.nii.gz")
          for p in TEST_NDARRAYS:
              img = p(np.arange(6).reshape((1, 1, 3, 2)))
              writer_obj = NibabelWriter()
              writer_obj.set_data_array(img, channel_dim=-1)
              writer_obj.set_metadata(
                  {
                      "affine": np.diag([1.4, 1, 1, 1]),
                      "original_affine": np.diag([1, 1.4, 1, 1]),
                  }
              )
              writer_obj.write(image_name, verbose=True)
              out = nib.load(image_name)
              np.testing.assert_allclose(
                  out.get_fdata(),
                  [[[[0, 1], [2, 3], [4, 5]]]],
                  atol=1e-4,
                  rtol=1e-4,
              )
              np.testing.assert_allclose(
                  out.affine,
                  np.diag([1, 1.4, 1, 1]),
                  atol=1e-4,
                  rtol=1e-4,
              )
  
              image_name = os.path.join(out_dir, "test1.nii.gz")
              img = p(np.arange(5).reshape((1, 1, 5, 1)))
              writer_obj.set_data_array(
                  img, channel_dim=-1, squeeze_end_dims=False
              )
              writer_obj.set_metadata(
                  {
                      "affine": np.diag([1, 1, 1, 3, 3]),
                      "original_affine": np.diag([1.4, 2.0, 2, 3, 5]),
                  }
              )
              writer_obj.write(image_name, verbose=True)
              out = nib.load(image_name)
              np.testing.assert_allclose(
                  out.get_fdata(),
                  [[[[0], [2], [4]]]],
                  atol=1e-4,
                  rtol=1e-4,
              )
              np.testing.assert_allclose(
                  out.affine,
                  np.diag([1.4, 2, 2, 1]),
                  atol=1e-4,
                  rtol=1e-4,
              )
  
file_path: MONAI/tests/test_nifti_rw.py
imports: from __future__ import annotations
  import os
  import tempfile
  import unittest
  import nibabel
  import numpy
  from parameterized import parameterized
  from monai.data import NibabelWriter
  from monai.transforms import LoadImage, Orientation, Spacing
  from tests.utils import TEST_NDARRAYS, assert_allclose, make_nifti_image
label_desc: Use image node_id 5 for calling the TestNiftiLoadRead obj's underlying member method code with example usage: obj.test_write_4d() without return types