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: Resample ``self.dataobj`` if needed.  This method assumes ``self.data_obj`` is a 'channel-last' ndarray.
  
  Args:
      meta_dict: a metadata dictionary for affine, original affine and spatial shape information.
          Optional keys are ``"spatial_shape"``, ``"affine"``, ``"original_affine"``.
      resample: if ``True``, the data will be resampled to the original affine (specified in ``meta_dict``).
      options: keyword arguments passed to ``self.resample_if_needed``,
          currently support ``mode``, ``padding_mode``, ``align_corners``, and ``dtype``,
          defaulting to ``bilinear``, ``border``, ``False``, and ``np.float64`` respectively.
docstr_tok: ["Resample","``","self.dataobj","``","if","needed",".","This","method","assumes","``","self.data_obj","``","is","a","'channel-last","'","ndarray",".","Args",":","meta_dict",":","a","metadata","dictionary","for","affine",",","original","affine","and","spatial","shape","information",".","Optional","keys","are","``","``","spatial_shape","''","``",",","``","``","affine","''","``",",","``","``","original_affine","''","``",".","resample",":","if","``","True","``",",","the","data","will","be","resampled","to","the","original","affine","(","specified","in","``","meta_dict","``",")",".","options",":","keyword","arguments","passed","to","``","self.resample_if_needed","``",",","currently","support","``","mode","``",",","``","padding_mode","``",",","``","align_corners","``",",","and","``","dtype","``",",","defaulting","to","``","bilinear","``",",","``","border","``",",","``","False","``",",","and","``","np.float64","``","respectively","."]
code: def set_metadata(
      self,
      meta_dict: Mapping | None = None,
      resample: bool = True,
      **options,
  ):
      """
      Resample ``self.dataobj`` if needed.  This method assumes ``self.data_obj`` is a 'channel-last' ndarray.
  
      Args:
          meta_dict: a metadata dictionary for affine, original affine and spatial shape information.
              Optional keys are ``"spatial_shape"``, ``"affine"``, ``"original_affine"``.
          resample: if ``True``, the data will be resampled to the original affine (specified in ``meta_dict``).
          options: keyword arguments passed to ``self.resample_if_needed``,
              currently support ``mode``, ``padding_mode``, ``align_corners``, and ``dtype``,
              defaulting to ``bilinear``, ``border``, ``False``, and ``np.float64`` respectively.
      """
      original_affine, affine, spatial_shape = self.get_meta_info(
          meta_dict
      )
      if self.output_dtype is None and hasattr(
          self.data_obj, "dtype"
      ):  # pylint: disable=E0203
          self.output_dtype = self.data_obj.dtype  # type: ignore
      self.data_obj, self.affine = self.resample_if_needed(
          data_array=cast(NdarrayOrTensor, self.data_obj),
          affine=affine,
          target_affine=original_affine if resample else None,
          output_spatial_shape=spatial_shape if resample else None,
          mode=options.pop("mode", GridSampleMode.BILINEAR),
          padding_mode=options.pop(
              "padding_mode", GridSamplePadMode.BORDER
          ),
          align_corners=options.pop("align_corners", False),
          dtype=options.pop("dtype", np.float64),
      )
  
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 3 for calling the ITKWriter obj's underlying member method code with example usage: obj.set_metadata(meta_dict, resample) without return types