class_docstr: Write data and metadata into files on disk using ITK-python.
  
  .. code-block:: python
  
      import numpy as np
      from monai.data import ITKWriter
  
      np_data = np.arange(48).reshape(3, 4, 4)
  
      # write as 3d spatial image no channel
      writer = ITKWriter(output_dtype=np.float32)
      writer.set_data_array(np_data, channel_dim=None)
      # optionally set metadata affine
      writer.set_metadata({"affine": np.eye(4), "original_affine": -1 * np.eye(4)})
      writer.write("test1.nii.gz")
  
      # write as 2d image, channel-first
      writer = ITKWriter(output_dtype=np.uint8)
      writer.set_data_array(np_data, channel_dim=0)
      writer.set_metadata({"spatial_shape": (5, 5)})
      writer.write("test1.png")
class_docstr_tok: ["Write","data","and","metadata","into","files","on","disk","using","ITK-python",".","..","code-block",":",":","python","import","numpy","as","np","from","monai.data","import","ITKWriter","np_data","=","np.arange","(","48",")",".reshape","(","3",",","4",",","4",")","#","write","as","3d","spatial","image","no","channel","writer","=","ITKWriter","(","output_dtype=np.float32",")","writer.set_data_array","(","np_data",",","channel_dim=None",")","#","optionally","set","metadata","affine","writer.set_metadata","(","{","``","affine","''",":","np.eye","(","4",")",",","``","original_affine","''",":","-1","*","np.eye","(","4",")","}",")","writer.write","(","``","test1.nii.gz","''",")","#","write","as","2d","image",",","channel-first","writer","=","ITKWriter","(","output_dtype=np.uint8",")","writer.set_data_array","(","np_data",",","channel_dim=0",")","writer.set_metadata","(","{","``","spatial_shape","''",":","(","5",",","5",")","}",")","writer.write","(","``","test1.png","''",")"]
docstr: Convert ``data_array`` into 'channel-last' numpy ndarray.
  
  Args:
      data_array: input data array with the channel dimension specified by ``channel_dim``.
      channel_dim: channel dimension of the data array. Defaults to 0.
          ``None`` indicates data without any channel dimension.
      squeeze_end_dims: if ``True``, any trailing singleton dimensions will be removed.
      kwargs: keyword arguments passed to ``self.convert_to_channel_last``,
          currently support ``spatial_ndim`` and ``contiguous``, defauting to ``3`` and ``False`` respectively.
docstr_tok: ["Convert","``","data_array","``","into","'channel-last","'","numpy","ndarray",".","Args",":","data_array",":","input","data","array","with","the","channel","dimension","specified","by","``","channel_dim","``",".","channel_dim",":","channel","dimension","of","the","data","array",".","Defaults","to","0",".","``","None","``","indicates","data","without","any","channel","dimension",".","squeeze_end_dims",":","if","``","True","``",",","any","trailing","singleton","dimensions","will","be","removed",".","kwargs",":","keyword","arguments","passed","to","``","self.convert_to_channel_last","``",",","currently","support","``","spatial_ndim","``","and","``","contiguous","``",",","defauting","to","``","3","``","and","``","False","``","respectively","."]
code: def set_data_array(
      self,
      data_array: NdarrayOrTensor,
      channel_dim: int | None = 0,
      squeeze_end_dims: bool = True,
      **kwargs,
  ):
      """
      Convert ``data_array`` into 'channel-last' numpy ndarray.
  
      Args:
          data_array: input data array with the channel dimension specified by ``channel_dim``.
          channel_dim: channel dimension of the data array. Defaults to 0.
              ``None`` indicates data without any channel dimension.
          squeeze_end_dims: if ``True``, any trailing singleton dimensions will be removed.
          kwargs: keyword arguments passed to ``self.convert_to_channel_last``,
              currently support ``spatial_ndim`` and ``contiguous``, defauting to ``3`` and ``False`` respectively.
      """
      n_chns = (
          data_array.shape[channel_dim]
          if channel_dim is not None
          else 0
      )
      self.data_obj = self.convert_to_channel_last(
          data=data_array,
          channel_dim=channel_dim,
          squeeze_end_dims=squeeze_end_dims,
          spatial_ndim=kwargs.pop("spatial_ndim", 3),
          contiguous=kwargs.pop("contiguous", True),
      )
      self.channel_dim = (
          -1
      )  # in most cases, the data is set to channel last
      if squeeze_end_dims and n_chns <= 1:  # num_channel==1 squeezed
          self.channel_dim = None
      if (
          not squeeze_end_dims and n_chns < 1
      ):  # originally no channel and convert_to_channel_last added a channel
          self.channel_dim = None
          self.data_obj = self.data_obj[..., 0]
  
imports: from __future__ import annotations
  from collections.abc import Mapping, Sequence
  from typing import TYPE_CHECKING, Any, cast
  import numpy
  from monai.apps.utils import get_logger
  from monai.config import DtypeLike, NdarrayOrTensor, PathLike
  from monai.data.meta_tensor import MetaTensor
  from monai.data.utils import affine_to_spacing, ensure_tuple, ensure_tuple_rep, orientation_ras_lps, to_affine_nd
  from monai.transforms.spatial.array import Resize, SpatialResample
  from monai.transforms.utils_pytorch_numpy_unification import ascontiguousarray, moveaxis
  from monai.utils import GridSampleMode, GridSamplePadMode, InterpolateMode, MetaKeys, OptionalImportError, SpaceKeys, convert_data_type, convert_to_tensor, get_equivalent_dtype, look_up_option, optional_import, require_pkg
label_desc: Use image node_id 2 for calling the ITKWriter obj's underlying member method code with example usage: obj.set_data_array(data_array, channel_dim, squeeze_end_dims) without return types