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: Convert the image to MetaTensor (when meta is not None). If `affine` is in the `meta` dictionary,
  convert that to `torch.Tensor`, too. Remove any superfluous metadata.
  
  Args:
      im: Input image (`np.ndarray` or `torch.Tensor`)
      meta: Metadata dictionary. When it's None, the metadata is not tracked, this method returns a torch.Tensor.
      simple_keys: whether to keep only a simple subset of metadata keys.
      pattern: combined with `sep`, a regular expression used to match and prune keys
          in the metadata (nested dictionary), default to None, no key deletion.
      sep: combined with `pattern`, used to match and delete keys in the metadata (nested dictionary).
          default is ".", see also :py:class:`monai.transforms.DeleteItemsd`.
          e.g. ``pattern=".*_code$", sep=" "`` removes any meta keys that ends with ``"_code"``.
  
  Returns:
      By default, a `MetaTensor` is returned.
      However, if `get_track_meta()` is `False` or meta=None, a `torch.Tensor` is returned.
docstr_tok: ["Convert","the","image","to","MetaTensor","(","when","meta","is","not","None",")",".","If","`","affine","`","is","in","the","`","meta","`","dictionary",",","convert","that","to","`","torch.Tensor","`",",","too",".","Remove","any","superfluous","metadata",".","Args",":","im",":","Input","image","(","`","np.ndarray","`","or","`","torch.Tensor","`",")","meta",":","Metadata","dictionary",".","When","it","'s","None",",","the","metadata","is","not","tracked",",","this","method","returns","a","torch.Tensor",".","simple_keys",":","whether","to","keep","only","a","simple","subset","of","metadata","keys",".","pattern",":","combined","with","`","sep","`",",","a","regular","expression","used","to","match","and","prune","keys","in","the","metadata","(","nested","dictionary",")",",","default","to","None",",","no","key","deletion",".","sep",":","combined","with","`","pattern","`",",","used","to","match","and","delete","keys","in","the","metadata","(","nested","dictionary",")",".","default","is","``",".","``",",","see","also",":","py",":","class",":","`","monai.transforms.DeleteItemsd","`",".","e.g",".","``","pattern=","''",".","*","_code","$","''",",","sep=","''","``","``","removes","any","meta","keys","that","ends","with","``","``","_code","''","``",".","Returns",":","By","default",",","a","`","MetaTensor","`","is","returned",".","However",",","if","`","get_track_meta","(",")","`","is","`","False","`","or","meta=None",",","a","`","torch.Tensor","`","is","returned","."]
code: def ensure_torch_and_prune_meta(
      im: NdarrayTensor,
      meta: dict | None,
      simple_keys: bool = False,
      pattern: str | None = None,
      sep: str = ".",
  ):
      """
      Convert the image to MetaTensor (when meta is not None). If `affine` is in the `meta` dictionary,
      convert that to `torch.Tensor`, too. Remove any superfluous metadata.
  
      Args:
          im: Input image (`np.ndarray` or `torch.Tensor`)
          meta: Metadata dictionary. When it's None, the metadata is not tracked, this method returns a torch.Tensor.
          simple_keys: whether to keep only a simple subset of metadata keys.
          pattern: combined with `sep`, a regular expression used to match and prune keys
              in the metadata (nested dictionary), default to None, no key deletion.
          sep: combined with `pattern`, used to match and delete keys in the metadata (nested dictionary).
              default is ".", see also :py:class:`monai.transforms.DeleteItemsd`.
              e.g. ``pattern=".*_code$", sep=" "`` removes any meta keys that ends with ``"_code"``.
  
      Returns:
          By default, a `MetaTensor` is returned.
          However, if `get_track_meta()` is `False` or meta=None, a `torch.Tensor` is returned.
      """
      img = convert_to_tensor(
          im, track_meta=get_track_meta() and meta is not None
      )  # potentially ascontiguousarray
  
      # if not tracking metadata, return `torch.Tensor`
      if not isinstance(img, MetaTensor):
          return img
  
      if meta is None:
          meta = {}
  
      # remove any superfluous metadata.
      if simple_keys:
          # ensure affine is of type `torch.Tensor`
          if MetaKeys.AFFINE in meta:
              meta[MetaKeys.AFFINE] = convert_to_tensor(
                  meta[MetaKeys.AFFINE]
              )  # bc-breaking
          remove_extra_metadata(meta)  # bc-breaking
  
      if pattern is not None:
          meta = monai.transforms.DeleteItemsd(
              keys=pattern, sep=sep, use_re=True
          )(meta)
  
      # return the `MetaTensor`
      if meta is None:
          meta = {}
      img.meta = meta
      if MetaKeys.AFFINE in meta:
          img.affine = meta[
              MetaKeys.AFFINE
          ]  # this uses the affine property setter
      else:
          img.affine = MetaTensor.get_default_affine()
      return img
  
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 25 for calling the MetaTensor obj's underlying member method code with example usage: obj.ensure_torch_and_prune_meta(im, meta, simple_keys, pattern, sep) and returns: img, img