class_docstr: Class that inherits from both `torch.Tensor` and `MetaObj`, adding support for metadata.
  
  Metadata is stored in the form of a dictionary. Nested, an affine matrix will be
  stored. This should be in the form of `torch.Tensor`.
  
  Behavior should be the same as `torch.Tensor` aside from the extended
  meta functionality.
  
  Copying of information:
  
      * For `c = a + b`, then auxiliary data (e.g., metadata) will be copied from the
        first instance of `MetaTensor` if `a.is_batch` is False
        (For batched data, the metadata will be shallow copied for efficiency purposes).
  
  Example:
      .. code-block:: python
  
          import torch
          from monai.data import MetaTensor
  
          t = torch.tensor([1,2,3])
          affine = torch.as_tensor([[2,0,0,0],
                                    [0,2,0,0],
                                    [0,0,2,0],
                                    [0,0,0,1]], dtype=torch.float64)
          meta = {"some": "info"}
          m = MetaTensor(t, affine=affine, meta=meta)
          m2 = m + m
          assert isinstance(m2, MetaTensor)
          assert m2.meta["some"] == "info"
          assert torch.all(m2.affine == affine)
  
  Notes:
      - Requires pytorch 1.9 or newer for full compatibility.
      - Older versions of pytorch (<=1.8), `torch.jit.trace(net, im)` may
        not work if `im` is of type `MetaTensor`. This can be resolved with
        `torch.jit.trace(net, im.as_tensor())`.
      - For pytorch < 1.8, sharing `MetaTensor` instances across processes may not be supported.
      - For pytorch < 1.9, next(iter(meta_tensor)) returns a torch.Tensor.
        see: https://github.com/pytorch/pytorch/issues/54457
      - A warning will be raised if in the constructor `affine` is not `None` and
        `meta` already contains the key `affine`.
      - You can query whether the `MetaTensor` is a batch with the `is_batch` attribute.
      - With a batch of data, `batch[0]` will return the 0th image
        with the 0th metadata. When the batch dimension is non-singleton, e.g.,
        `batch[:, 0]`, `batch[..., -1]` and `batch[1:3]`, then all (or a subset in the
        last example) of the metadata will be returned, and `is_batch` will return `True`.
      - When creating a batch with this class, use `monai.data.DataLoader` as opposed
        to `torch.utils.data.DataLoader`, as this will take care of collating the
        metadata properly.
class_docstr_tok: ["Class","that","inherits","from","both","`","torch.Tensor","`","and","`","MetaObj","`",",","adding","support","for","metadata",".","Metadata","is","stored","in","the","form","of","a","dictionary",".","Nested",",","an","affine","matrix","will","be","stored",".","This","should","be","in","the","form","of","`","torch.Tensor","`",".","Behavior","should","be","the","same","as","`","torch.Tensor","`","aside","from","the","extended","meta","functionality",".","Copying","of","information",":","*","For","`","c","=","a","+","b","`",",","then","auxiliary","data","(","e.g.",",","metadata",")","will","be","copied","from","the","first","instance","of","`","MetaTensor","`","if","`","a.is_batch","`","is","False","(","For","batched","data",",","the","metadata","will","be","shallow","copied","for","efficiency","purposes",")",".","Example",":","..","code-block",":",":","python","import","torch","from","monai.data","import","MetaTensor","t","=","torch.tensor","(","[","1,2,3","]",")","affine","=","torch.as_tensor","(","[","[","2,0,0,0","]",",","[","0,2,0,0","]",",","[","0,0,2,0","]",",","[","0,0,0,1","]","]",",","dtype=torch.float64",")","meta","=","{","``","some","''",":","``","info","''","}","m","=","MetaTensor","(","t",",","affine=affine",",","meta=meta",")","m2","=","m","+","m","assert","isinstance","(","m2",",","MetaTensor",")","assert","m2.meta","[","``","some","''","]","==","``","info","''","assert","torch.all","(","m2.affine","==","affine",")","Notes",":","-","Requires","pytorch","1.9","or","newer","for","full","compatibility",".","-","Older","versions","of","pytorch","(","<","=1.8",")",",","`","torch.jit.trace","(","net",",","im",")","`","may","not","work","if","`","im","`","is","of","type","`","MetaTensor","`",".","This","can","be","resolved","with","`","torch.jit.trace","(","net",",","im.as_tensor","(",")",")","`",".","-","For","pytorch","<","1.8",",","sharing","`","MetaTensor","`","instances","across","processes","may","not","be","supported",".","-","For","pytorch","<","1.9",",","next","(","iter","(","meta_tensor",")",")","returns","a","torch.Tensor",".","see",":","https",":","\/\/github.com\/pytorch\/pytorch\/issues\/54457","-","A","warning","will","be","raised","if","in","the","constructor","`","affine","`","is","not","`","None","`","and","`","meta","`","already","contains","the","key","`","affine","`",".","-","You","can","query","whether","the","`","MetaTensor","`","is","a","batch","with","the","`","is_batch","`","attribute",".","-","With","a","batch","of","data",",","`","batch","[","0","]","`","will","return","the","0th","image","with","the","0th","metadata",".","When","the","batch","dimension","is","non-singleton",",","e.g.",",","`","batch","[",":",",","0","]","`",",","`","batch","[","...",",","-1","]","`","and","`","batch","[","1:3","]","`",",","then","all","(","or","a","subset","in","the","last","example",")","of","the","metadata","will","be","returned",",","and","`","is_batch","`","will","return","`","True","`",".","-","When","creating","a","batch","with","this","class",",","use","`","monai.data.DataLoader","`","as","opposed","to","`","torch.utils.data.DataLoader","`",",","as","this","will","take","care","of","collating","the","metadata","properly","."]
docstr: Args:
      x: initial array for the MetaTensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.
      affine: optional 4x4 array.
      meta: dictionary of metadata.
      applied_operations: list of previously applied operations on the MetaTensor,
          the list is typically maintained by `monai.transforms.TraceableTransform`.
          See also: :py:class:`monai.transforms.TraceableTransform`
      _args: additional args (currently not in use in this constructor).
      _kwargs: additional kwargs (currently not in use in this constructor).
  
  Note:
      If a `meta` dictionary is given, use it. Else, if `meta` exists in the input tensor `x`, use it.
      Else, use the default value. Similar for the affine, except this could come from
      four places, priority: `affine`, `meta["affine"]`, `x.affine`, `get_default_affine`.
docstr_tok: ["Args",":","x",":","initial","array","for","the","MetaTensor",".","Can","be","a","list",",","tuple",",","NumPy","ndarray",",","scalar",",","and","other","types",".","affine",":","optional","4x4","array",".","meta",":","dictionary","of","metadata",".","applied_operations",":","list","of","previously","applied","operations","on","the","MetaTensor",",","the","list","is","typically","maintained","by","`","monai.transforms.TraceableTransform","`",".","See","also",":",":","py",":","class",":","`","monai.transforms.TraceableTransform","`","_args",":","additional","args","(","currently","not","in","use","in","this","constructor",")",".","_kwargs",":","additional","kwargs","(","currently","not","in","use","in","this","constructor",")",".","Note",":","If","a","`","meta","`","dictionary","is","given",",","use","it",".","Else",",","if","`","meta","`","exists","in","the","input","tensor","`","x","`",",","use","it",".","Else",",","use","the","default","value",".","Similar","for","the","affine",",","except","this","could","come","from","four","places",",","priority",":","`","affine","`",",","`","meta","[","``","affine","''","]","`",",","`","x.affine","`",",","`","get_default_affine","`","."]
code: def __init__(
      self,
      x,
      affine: torch.Tensor | None = None,
      meta: dict | None = None,
      applied_operations: list | None = None,
      *_args,
      **_kwargs,
  ) -> None:
      """
      Args:
          x: initial array for the MetaTensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.
          affine: optional 4x4 array.
          meta: dictionary of metadata.
          applied_operations: list of previously applied operations on the MetaTensor,
              the list is typically maintained by `monai.transforms.TraceableTransform`.
              See also: :py:class:`monai.transforms.TraceableTransform`
          _args: additional args (currently not in use in this constructor).
          _kwargs: additional kwargs (currently not in use in this constructor).
  
      Note:
          If a `meta` dictionary is given, use it. Else, if `meta` exists in the input tensor `x`, use it.
          Else, use the default value. Similar for the affine, except this could come from
          four places, priority: `affine`, `meta["affine"]`, `x.affine`, `get_default_affine`.
  
      """
      super().__init__()
      # set meta
      if meta is not None:
          self.meta = meta
      elif isinstance(x, MetaObj):
          self.__dict__ = deepcopy(x.__dict__)
      # set the affine
      if affine is not None:
          if MetaKeys.AFFINE in self.meta:
              warnings.warn(
                  "Setting affine, but the applied meta contains an affine. This will be overwritten."
              )
          self.affine = affine
      elif MetaKeys.AFFINE in self.meta:
          # by using the setter function, we ensure it is converted to torch.Tensor if not already
          self.affine = self.meta[MetaKeys.AFFINE]
      else:
          self.affine = self.get_default_affine()
      # applied_operations
      if applied_operations is not None:
          self.applied_operations = applied_operations
      else:
          self.applied_operations = (
              MetaObj.get_default_applied_operations()
          )
  
      # if we are creating a new MetaTensor, then deep copy attributes
      if isinstance(x, torch.Tensor) and not isinstance(x, MetaTensor):
          self.copy_meta_from(self)
  
      if MetaKeys.SPACE not in self.meta:
          self.meta[MetaKeys.SPACE] = SpaceKeys.RAS
  
imports: from __future__ import annotations
  import functools
  import warnings
  from copy import deepcopy
  from typing import Any, Sequence
  import numpy
  import torch
  import monai
  from monai.config.type_definitions import NdarrayTensor
  from monai.data.meta_obj import MetaObj, get_track_meta
  from monai.data.utils import affine_to_spacing, decollate_batch, list_data_collate, remove_extra_metadata
  from monai.utils import look_up_option
  from monai.utils.enums import LazyAttr, MetaKeys, PostFix, SpaceKeys
  from monai.utils.type_conversion import convert_data_type, convert_to_dst_type, convert_to_numpy, convert_to_tensor
label_desc: Use image node_id 2 to create a new MetaTensor object from inherited base classes: MetaObj, torch with example: obj = MetaTensor(x, affine, meta, applied_operations)