MCPcopy Index your code
hub / github.com/Project-MONAI/MONAI / MetaTensor

Class MetaTensor

monai/data/meta_tensor.py:52–609  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

50
51
52class MetaTensor(MetaObj, torch.Tensor):
53 """
54 Class that inherits from both `torch.Tensor` and `MetaObj`, adding support for metadata.
55
56 Metadata is stored in the form of a dictionary. Nested, an affine matrix will be
57 stored. This should be in the form of `torch.Tensor`.
58
59 Behavior should be the same as `torch.Tensor` aside from the extended
60 meta functionality.
61
62 Copying of information:
63
64 * For `c = a + b`, then auxiliary data (e.g., metadata) will be copied from the
65 first instance of `MetaTensor` if `a.is_batch` is False
66 (For batched data, the metadata will be shallow copied for efficiency purposes).
67
68 Example:
69 .. code-block:: python
70
71 import torch
72 from monai.data import MetaTensor
73
74 t = torch.tensor([1,2,3])
75 affine = torch.as_tensor([[2,0,0,0],
76 [0,2,0,0],
77 [0,0,2,0],
78 [0,0,0,1]], dtype=torch.float64)
79 meta = {"some": "info"}
80 m = MetaTensor(t, affine=affine, meta=meta)
81 m2 = m + m
82 assert isinstance(m2, MetaTensor)
83 assert m2.meta["some"] == "info"
84 assert torch.all(m2.affine == affine)
85
86 Notes:
87 - Requires pytorch 1.9 or newer for full compatibility.
88 - Older versions of pytorch (<=1.8), `torch.jit.trace(net, im)` may
89 not work if `im` is of type `MetaTensor`. This can be resolved with
90 `torch.jit.trace(net, im.as_tensor())`.
91 - For pytorch < 1.8, sharing `MetaTensor` instances across processes may not be supported.
92 - For pytorch < 1.9, next(iter(meta_tensor)) returns a torch.Tensor.
93 see: https://github.com/pytorch/pytorch/issues/54457
94 - A warning will be raised if in the constructor `affine` is not `None` and
95 `meta` already contains the key `affine`.
96 - You can query whether the `MetaTensor` is a batch with the `is_batch` attribute.
97 - With a batch of data, `batch[0]` will return the 0th image
98 with the 0th metadata. When the batch dimension is non-singleton, e.g.,
99 `batch[:, 0]`, `batch[..., -1]` and `batch[1:3]`, then all (or a subset in the
100 last example) of the metadata will be returned, and `is_batch` will return `True`.
101 - When creating a batch with this class, use `monai.data.DataLoader` as opposed
102 to `torch.utils.data.DataLoader`, as this will take care of collating the
103 metadata properly.
104 """
105
106 @staticmethod
107 def __new__(
108 cls,
109 x,

Callers 15

forwardMethod · 0.90
_transformMethod · 0.90
__call__Method · 0.90
__call__Method · 0.90
__call__Method · 0.90
__call__Method · 0.90
__call__Method · 0.90
__call__Method · 0.90
zoomFunction · 0.90
inverseMethod · 0.90
inverseMethod · 0.90

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…