Commit deba0f1
committed
fix(MetaTensor): astype with torch dtype now returns MetaTensor preserving metadata
When calling MetaTensor.astype() with a torch dtype (e.g. torch.int32),
the result was a plain torch.Tensor, silently losing all metadata (affine,
spacing, applied transforms, etc.). The root cause was that out_type was
hardcoded to torch.Tensor instead of the actual type of self.
Fix by using type(self) as out_type when a torch dtype is requested, so
that convert_data_type() receives output_type=MetaTensor, sets
track_meta=True, and preserves metadata through the dtype cast.
The analyzer module already annotated the result of astype(torch.int16)
as MetaTensor, relying on this contract.
Updated test to assert the result is an instance of MetaTensor and that
the metadata key is preserved after the cast.
Closes #8202
Signed-off-by: Oleksandr Sanin <alexaaander.sanin@gmail.com>1 parent ef2acfb commit deba0f1
2 files changed
Lines changed: 9 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
442 | 442 | | |
443 | 443 | | |
444 | 444 | | |
445 | | - | |
| 445 | + | |
| 446 | + | |
446 | 447 | | |
447 | 448 | | |
448 | 449 | | |
| |||
453 | 454 | | |
454 | 455 | | |
455 | 456 | | |
456 | | - | |
| 457 | + | |
457 | 458 | | |
458 | 459 | | |
459 | 460 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
434 | 434 | | |
435 | 435 | | |
436 | 436 | | |
437 | | - | |
438 | | - | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
439 | 443 | | |
440 | 444 | | |
441 | 445 | | |
| |||
0 commit comments