MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / ToMetaTensord

Class ToMetaTensord

monai/transforms/meta_utility/dictionary.py:89–120  ·  view source on GitHub ↗

Dictionary-based transform to convert a dictionary to MetaTensor. If input is `{"a": torch.Tensor, "a_meta_dict": dict, "b": ...}`, then output will have the form `{"a": MetaTensor, "b": MetaTensor}`.

Source from the content-addressed store, hash-verified

87
88
89class ToMetaTensord(MapTransform, InvertibleTransform):
90 """
91 Dictionary-based transform to convert a dictionary to MetaTensor.
92
93 If input is `{"a": torch.Tensor, "a_meta_dict": dict, "b": ...}`, then output will
94 have the form `{"a": MetaTensor, "b": MetaTensor}`.
95 """
96
97 backend = [TransformBackends.TORCH, TransformBackends.NUMPY, TransformBackends.CUPY]
98
99 def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]:
100 d = dict(data)
101 for key in self.key_iterator(d):
102 self.push_transform(d, key)
103 im = d[key]
104 meta = d.pop(PostFix.meta(key), None)
105 transforms = d.pop(PostFix.transforms(key), None)
106 im = MetaTensor(im, meta=meta, applied_operations=transforms) # type: ignore
107 d[key] = im
108 return d
109
110 def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]:
111 d = dict(data)
112 for key in self.key_iterator(d):
113 # check transform
114 _ = self.get_most_recent_transform(d, key)
115 # do the inverse
116 im: MetaTensor = d[key] # type: ignore
117 d.update(im.as_dict(key))
118 # Remove the applied transform
119 self.pop_transform(d, key)
120 return d
121
122
123FromMetaTensorD = FromMetaTensorDict = FromMetaTensord

Callers 4

test_transformsMethod · 0.90
_cmpMethod · 0.90
test_inverseMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_transformsMethod · 0.72
_cmpMethod · 0.72
test_inverseMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…