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,
(
self,
x,
affine: torch.Tensor | None = None,
meta: dict | None = None,
applied_operations: list | None = None,
*_args,
**_kwargs,
)
| 117 | return torch.as_tensor(x, *args, **_kwargs).as_subclass(cls) |
| 118 | |
| 119 | def __init__( |
| 120 | self, |
| 121 | x, |
| 122 | affine: torch.Tensor | None = None, |
| 123 | meta: dict | None = None, |
| 124 | applied_operations: list | None = None, |
| 125 | *_args, |
| 126 | **_kwargs, |
| 127 | ) -> None: |
| 128 | """ |
| 129 | Args: |
| 130 | x: initial array for the MetaTensor. Can be a list, tuple, NumPy ndarray, scalar, and other types. |
| 131 | affine: optional 4x4 array. |
| 132 | meta: dictionary of metadata. |
| 133 | applied_operations: list of previously applied operations on the MetaTensor, |
| 134 | the list is typically maintained by `monai.transforms.TraceableTransform`. |
| 135 | See also: :py:class:`monai.transforms.TraceableTransform` |
| 136 | _args: additional args (currently not in use in this constructor). |
| 137 | _kwargs: additional kwargs (currently not in use in this constructor). |
| 138 | |
| 139 | Note: |
| 140 | If a `meta` dictionary is given, use it. Else, if `meta` exists in the input tensor `x`, use it. |
| 141 | Else, use the default value. Similar for the affine, except this could come from |
| 142 | four places, priority: `affine`, `meta["affine"]`, `x.affine`, `get_default_affine`. |
| 143 | |
| 144 | """ |
| 145 | super().__init__() |
| 146 | # set meta |
| 147 | if meta is not None: |
| 148 | self.meta = meta |
| 149 | elif isinstance(x, MetaObj): |
| 150 | self.__dict__ = deepcopy(x.__dict__) |
| 151 | # set the affine |
| 152 | if affine is not None: |
| 153 | if MetaKeys.AFFINE in self.meta: |
| 154 | warnings.warn("Setting affine, but the applied meta contains an affine. This will be overwritten.") |
| 155 | self.affine = affine |
| 156 | elif MetaKeys.AFFINE in self.meta: |
| 157 | # by using the setter function, we ensure it is converted to torch.Tensor if not already |
| 158 | self.affine = self.meta[MetaKeys.AFFINE] |
| 159 | else: |
| 160 | self.affine = self.get_default_affine() |
| 161 | # applied_operations |
| 162 | if applied_operations is not None: |
| 163 | self.applied_operations = applied_operations |
| 164 | else: |
| 165 | self.applied_operations = MetaObj.get_default_applied_operations() |
| 166 | |
| 167 | # if we are creating a new MetaTensor, then deep copy attributes |
| 168 | if isinstance(x, torch.Tensor) and not isinstance(x, MetaTensor): |
| 169 | self.copy_meta_from(self) |
| 170 | |
| 171 | if MetaKeys.SPACE not in self.meta: |
| 172 | self.meta[MetaKeys.SPACE] = SpaceKeys.RAS # defaulting to the right-anterior-superior space |
| 173 | |
| 174 | @staticmethod |
| 175 | def update_meta(rets: Sequence, func, args, kwargs) -> Sequence: |
nothing calls this directly
no test coverage detected