Update a stack of applied/pending transforms metadata of ``data``. Args: data: dictionary of data or `MetaTensor`. key: if data is a dictionary, data[key] will be modified. sp_size: the expected output spatial size when the transform is applied.
(
cls,
data,
key: Hashable = None,
sp_size=None,
affine=None,
extra_info: dict | None = None,
orig_size: tuple | None = None,
transform_info=None,
lazy=False,
)
| 168 | |
| 169 | @classmethod |
| 170 | def track_transform_meta( |
| 171 | cls, |
| 172 | data, |
| 173 | key: Hashable = None, |
| 174 | sp_size=None, |
| 175 | affine=None, |
| 176 | extra_info: dict | None = None, |
| 177 | orig_size: tuple | None = None, |
| 178 | transform_info=None, |
| 179 | lazy=False, |
| 180 | ): |
| 181 | """ |
| 182 | Update a stack of applied/pending transforms metadata of ``data``. |
| 183 | |
| 184 | Args: |
| 185 | data: dictionary of data or `MetaTensor`. |
| 186 | key: if data is a dictionary, data[key] will be modified. |
| 187 | sp_size: the expected output spatial size when the transform is applied. |
| 188 | it can be tensor or numpy, but will be converted to a list of integers. |
| 189 | affine: the affine representation of the (spatial) transform in the image space. |
| 190 | When the transform is applied, meta_tensor.affine will be updated to ``meta_tensor.affine @ affine``. |
| 191 | extra_info: if desired, any extra information pertaining to the applied |
| 192 | transform can be stored in this dictionary. These are often needed for |
| 193 | computing the inverse transformation. |
| 194 | orig_size: sometimes during the inverse it is useful to know what the size |
| 195 | of the original image was, in which case it can be supplied here. |
| 196 | transform_info: info from self.get_transform_info(). |
| 197 | lazy: whether to push the transform to pending_operations or applied_operations. |
| 198 | |
| 199 | Returns: |
| 200 | |
| 201 | For backward compatibility, if ``data`` is a dictionary, it returns the dictionary with |
| 202 | updated ``data[key]``. Otherwise, this function returns a MetaObj with updated transform metadata. |
| 203 | """ |
| 204 | data_t = data[key] if key is not None else data # compatible with the dict data representation |
| 205 | out_obj = MetaObj() |
| 206 | # after deprecating metadict, we should always convert data_t to metatensor here |
| 207 | if isinstance(data_t, MetaTensor): |
| 208 | out_obj.copy_meta_from(data_t, keys=out_obj.__dict__.keys()) |
| 209 | |
| 210 | if lazy and (not get_track_meta()): |
| 211 | warnings.warn("metadata is not tracked, please call 'set_track_meta(True)' if doing lazy evaluation.") |
| 212 | |
| 213 | if not lazy and affine is not None and isinstance(data_t, MetaTensor): |
| 214 | # not lazy evaluation, directly update the metatensor affine (don't push to the stack) |
| 215 | orig_affine = data_t.peek_pending_affine() |
| 216 | orig_affine = convert_to_dst_type(orig_affine, affine, dtype=torch.float64)[0] |
| 217 | try: |
| 218 | affine = orig_affine @ to_affine_nd(len(orig_affine) - 1, affine, dtype=torch.float64) |
| 219 | except RuntimeError as e: |
| 220 | if orig_affine.ndim > 2: |
| 221 | if data_t.is_batch: |
| 222 | msg = "Transform applied to batched tensor, should be applied to instances only" |
| 223 | else: |
| 224 | msg = "Mismatch affine matrix, ensured that the batch dimension is not included in the calculation." |
| 225 | raise RuntimeError(msg) from e |
| 226 | else: |
| 227 | raise |
no test coverage detected