Skip to content

Commit 9478c51

Browse files
committed
Fix mypy errors from convert_to_tensor → as_tensor swap
The new `img.as_tensor() if isinstance(img, MetaTensor) else cast(...)` expression narrowed mypy's inference of `img_t` to `Tensor`, so downstream reassignments from `_clip` / `_normalize` / `interp` (which return `NdarrayOrTensor`) failed type-checking. Annotate `img_t` explicitly as `NdarrayOrTensor` and drop the redundant `cast(torch.Tensor, img)` so the variable matches the type used by the rest of each transform's body. Keep the `cast` only on `SavitzkyGolaySmooth.__call__` where `self.img_t` is declared as `torch.Tensor` in `__init__` and used unconditionally as a Tensor afterwards. Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent c575f22 commit 9478c51

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

monai/transforms/intensity/array.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
484484
485485
"""
486486
img = convert_to_tensor(img, track_meta=get_track_meta())
487-
img_t = img.as_tensor() if isinstance(img, MetaTensor) else cast(torch.Tensor, img)
487+
img_t: NdarrayOrTensor = img.as_tensor() if isinstance(img, MetaTensor) else img
488488
ret: NdarrayOrTensor
489489
if self.minv is not None or self.maxv is not None:
490490
if self.channel_wise:
@@ -543,7 +543,7 @@ def __call__(self, img: NdarrayOrTensor, factor=None) -> NdarrayOrTensor:
543543
factor = factor if factor is not None else self.factor
544544

545545
img = convert_to_tensor(img, track_meta=get_track_meta())
546-
img_t = img.as_tensor() if isinstance(img, MetaTensor) else cast(torch.Tensor, img)
546+
img_t: NdarrayOrTensor = img.as_tensor() if isinstance(img, MetaTensor) else img
547547
ret: NdarrayOrTensor
548548
if self.channel_wise:
549549
out = []
@@ -1169,7 +1169,7 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
11691169
Apply the transform to `img`.
11701170
"""
11711171
img = convert_to_tensor(img, track_meta=get_track_meta())
1172-
img_t = img.as_tensor() if isinstance(img, MetaTensor) else cast(torch.Tensor, img)
1172+
img_t: NdarrayOrTensor = img.as_tensor() if isinstance(img, MetaTensor) else img
11731173
if self.channel_wise:
11741174
img_t = torch.stack([self._clip(img=d) for d in img_t]) # type: ignore
11751175
else:
@@ -1434,7 +1434,7 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
14341434
Apply the transform to `img`.
14351435
"""
14361436
img = convert_to_tensor(img, track_meta=get_track_meta())
1437-
img_t = img.as_tensor() if isinstance(img, MetaTensor) else cast(torch.Tensor, img)
1437+
img_t: NdarrayOrTensor = img.as_tensor() if isinstance(img, MetaTensor) else img
14381438
if self.channel_wise:
14391439
img_t = torch.stack([self._normalize(img=d) for d in img_t]) # type: ignore
14401440
else:
@@ -1908,7 +1908,7 @@ def __call__(self, img: NdarrayOrTensor, randomize: bool = True) -> NdarrayOrTen
19081908

19091909
if self.reference_control_points is None or self.floating_control_points is None:
19101910
raise RuntimeError("please call the `randomize()` function first.")
1911-
img_t = img.as_tensor() if isinstance(img, MetaTensor) else cast(torch.Tensor, img)
1911+
img_t: NdarrayOrTensor = img.as_tensor() if isinstance(img, MetaTensor) else img
19121912
img_min, img_max = img_t.min(), img_t.max()
19131913
if img_min == img_max:
19141914
warn(
@@ -1953,7 +1953,7 @@ def __init__(self, alpha: float = 0.1) -> None:
19531953

19541954
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
19551955
img = convert_to_tensor(img, track_meta=get_track_meta())
1956-
img_t = img.as_tensor() if isinstance(img, MetaTensor) else cast(torch.Tensor, img)
1956+
img_t: NdarrayOrTensor = img.as_tensor() if isinstance(img, MetaTensor) else img
19571957
n_dims = len(img_t.shape[1:])
19581958

19591959
# FT
@@ -2605,7 +2605,7 @@ def __call__(self, img: torch.Tensor) -> torch.Tensor:
26052605
img: image to remap.
26062606
"""
26072607
img = convert_to_tensor(img, track_meta=get_track_meta())
2608-
img_ = img.as_tensor() if isinstance(img, MetaTensor) else cast(torch.Tensor, img)
2608+
img_ = img.as_tensor() if isinstance(img, MetaTensor) else img
26092609
# sample noise
26102610
vals_to_sample = torch.unique(img_).tolist()
26112611
noise = torch.from_numpy(self.R.choice(vals_to_sample, len(vals_to_sample) - 1 + self.kernel_size))

0 commit comments

Comments
 (0)