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: Create an ITK object from ``data_array``. This method assumes a 'channel-last' ``data_array``.
  
  Args:
      data_array: input data array.
      channel_dim: channel dimension of the data array. This is used to create a Vector Image if it is not ``None``.
      affine: affine matrix of the data array. This is used to compute `spacing`, `direction` and `origin`.
      dtype: output data type.
      affine_lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to ``True``.
          Set to ``True`` to be consistent with ``NibabelWriter``,
          otherwise the affine matrix is assumed already in the ITK convention.
          Set to ``None`` to use ``data_array.meta[MetaKeys.SPACE]`` to determine the flag.
      kwargs: keyword arguments. Current `itk.GetImageFromArray` will read ``ttype`` from this dictionary.
  
  see also:
  
      - https://github.com/InsightSoftwareConsortium/ITK/blob/v5.2.1/Wrapping/Generators/Python/itk/support/extras.py#L389
docstr_tok: ["Create","an","ITK","object","from","``","data_array","``",".","This","method","assumes","a","'channel-last","'","``","data_array","``",".","Args",":","data_array",":","input","data","array",".","channel_dim",":","channel","dimension","of","the","data","array",".","This","is","used","to","create","a","Vector","Image","if","it","is","not","``","None","``",".","affine",":","affine","matrix","of","the","data","array",".","This","is","used","to","compute","`","spacing","`",",","`","direction","`","and","`","origin","`",".","dtype",":","output","data","type",".","affine_lps_to_ras",":","whether","to","convert","the","affine","matrix","from","``","LPS","''","to","``","RAS","''",".","Defaults","to","``","True","``",".","Set","to","``","True","``","to","be","consistent","with","``","NibabelWriter","``",",","otherwise","the","affine","matrix","is","assumed","already","in","the","ITK","convention",".","Set","to","``","None","``","to","use","``","data_array.meta","[","MetaKeys.SPACE","]","``","to","determine","the","flag",".","kwargs",":","keyword","arguments",".","Current","`","itk.GetImageFromArray","`","will","read","``","ttype","``","from","this","dictionary",".","see","also",":","-","https",":","\/\/github.com\/InsightSoftwareConsortium\/ITK\/blob\/v5.2.1\/Wrapping\/Generators\/Python\/itk\/support\/extras.py","#","L389"]
code: def create_backend_obj(
      cls,
      data_array: NdarrayOrTensor,
      channel_dim: int | None = 0,
      affine: NdarrayOrTensor | None = None,
      dtype: DtypeLike = np.float32,
      affine_lps_to_ras: bool | None = True,
      **kwargs,
  ):
      """
      Create an ITK object from ``data_array``. This method assumes a 'channel-last' ``data_array``.
  
      Args:
          data_array: input data array.
          channel_dim: channel dimension of the data array. This is used to create a Vector Image if it is not ``None``.
          affine: affine matrix of the data array. This is used to compute `spacing`, `direction` and `origin`.
          dtype: output data type.
          affine_lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to ``True``.
              Set to ``True`` to be consistent with ``NibabelWriter``,
              otherwise the affine matrix is assumed already in the ITK convention.
              Set to ``None`` to use ``data_array.meta[MetaKeys.SPACE]`` to determine the flag.
          kwargs: keyword arguments. Current `itk.GetImageFromArray` will read ``ttype`` from this dictionary.
  
      see also:
  
          - https://github.com/InsightSoftwareConsortium/ITK/blob/v5.2.1/Wrapping/Generators/Python/itk/support/extras.py#L389
  
      """
      if (
          isinstance(data_array, MetaTensor)
          and affine_lps_to_ras is None
      ):
          affine_lps_to_ras = (
              data_array.meta.get(MetaKeys.SPACE, SpaceKeys.LPS)
              != SpaceKeys.LPS
          )  # do the converting from LPS to RAS only if the space type is currently LPS.
      data_array = super().create_backend_obj(data_array)
      _is_vec = channel_dim is not None
      if _is_vec:
          data_array = np.moveaxis(
              data_array, -1, 0
          )  # from channel last to channel first
      data_array = data_array.T.astype(
          get_equivalent_dtype(dtype, np.ndarray), copy=True, order="C"
      )
      itk_obj = itk.GetImageFromArray(
          data_array, is_vector=_is_vec, ttype=kwargs.pop("ttype", None)
      )
  
      d = len(itk.size(itk_obj))
      if affine is None:
          affine = np.eye(d + 1, dtype=np.float64)
      _affine = convert_data_type(affine, np.ndarray)[0]
      if affine_lps_to_ras:
          _affine = orientation_ras_lps(to_affine_nd(d, _affine))
      spacing = affine_to_spacing(_affine, r=d)
      _direction: np.ndarray = np.diag(1 / spacing)
      _direction = _affine[:d, :d] @ _direction
      itk_obj.SetSpacing(spacing.tolist())
      itk_obj.SetOrigin(_affine[:d, -1].tolist())
      itk_obj.SetDirection(itk.GetMatrixFromArray(_direction))
      return itk_obj
  
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 5 for calling the ITKWriter obj's underlying member method code with example usage: obj.create_backend_obj(cls, data_array, channel_dim, affine, dtype, affine_lps_to_ras) and returns: itk_obj