Skip to content

Commit 3f0c768

Browse files
Fix Incompatible types in assignment issue (#7950)
Fixes #7947 ### Description Add type ignore in several redefinitions cases ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 12d00ce commit 3f0c768

File tree

6 files changed

+10
-6
lines changed

6 files changed

+10
-6
lines changed

monai/auto3dseg/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ def __call__(self, data: Mapping[Hashable, MetaTensor]) -> dict[Hashable, MetaTe
470470

471471
unique_label = unique(ndas_label)
472472
if isinstance(ndas_label, (MetaTensor, torch.Tensor)):
473-
unique_label = unique_label.data.cpu().numpy()
473+
unique_label = unique_label.data.cpu().numpy() # type: ignore[assignment]
474474

475475
unique_label = unique_label.astype(np.int16).tolist()
476476

monai/metrics/cumulative_average.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def get_current(self, to_numpy: bool = True) -> NdarrayOrTensor:
6565
if self.val is None:
6666
return 0
6767

68+
val: NdarrayOrTensor
6869
val = self.val.clone()
6970
val[~torch.isfinite(val)] = 0
7071

@@ -96,6 +97,7 @@ def aggregate(self, to_numpy: bool = True) -> NdarrayOrTensor:
9697
dist.all_reduce(sum)
9798
dist.all_reduce(count)
9899

100+
val: NdarrayOrTensor
99101
val = torch.where(count > 0, sum / count, sum)
100102

101103
if to_numpy:

monai/metrics/panoptic_quality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def _get_paired_iou(
274274

275275
return paired_iou, paired_true, paired_pred
276276

277-
pairwise_iou = pairwise_iou.cpu().numpy()
277+
pairwise_iou = pairwise_iou.cpu().numpy() # type: ignore[assignment]
278278
paired_true, paired_pred = linear_sum_assignment(-pairwise_iou)
279279
paired_iou = pairwise_iou[paired_true, paired_pred]
280280
paired_true = torch.as_tensor(list(paired_true[paired_iou > match_iou_threshold] + 1), device=device)

monai/metrics/rocauc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float:
8888

8989
n = len(y)
9090
indices = y_pred.argsort()
91-
y = y[indices].cpu().numpy()
92-
y_pred = y_pred[indices].cpu().numpy()
91+
y = y[indices].cpu().numpy() # type: ignore[assignment]
92+
y_pred = y_pred[indices].cpu().numpy() # type: ignore[assignment]
9393
nneg = auc = tmp_pos = tmp_neg = 0.0
9494

9595
for i in range(n):

monai/transforms/croppad/functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _np_pad(img: NdarrayTensor, pad_width: list[tuple[int, int]], mode: str, **k
4848
warnings.warn(f"Padding: moving img {img.shape} from cuda to cpu for dtype={img.dtype} mode={mode}.")
4949
img_np = img.detach().cpu().numpy()
5050
else:
51-
img_np = img
51+
img_np = np.asarray(img)
5252
mode = convert_pad_mode(dst=img_np, mode=mode).value
5353
if mode == "constant" and "value" in kwargs:
5454
kwargs["constant_values"] = kwargs.pop("value")

monai/visualize/img2tensorboard.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ def plot_2d_or_3d_image(
176176
# as the `d` data has no batch dim, reduce the spatial dim index if positive
177177
frame_dim = frame_dim - 1 if frame_dim > 0 else frame_dim
178178

179-
d: np.ndarray = data_index.detach().cpu().numpy() if isinstance(data_index, torch.Tensor) else data_index
179+
d: np.ndarray = (
180+
data_index.detach().cpu().numpy() if isinstance(data_index, torch.Tensor) else np.asarray(data_index)
181+
)
180182

181183
if d.ndim == 2:
182184
d = rescale_array(d, 0, 1) # type: ignore

0 commit comments

Comments
 (0)