Skip to content

Commit

Permalink
Fix Incompatible types in assignment issue (#7950)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
KumoLiu and pre-commit-ci[bot] authored Jul 25, 2024
1 parent 12d00ce commit 3f0c768
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion monai/auto3dseg/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def __call__(self, data: Mapping[Hashable, MetaTensor]) -> dict[Hashable, MetaTe

unique_label = unique(ndas_label)
if isinstance(ndas_label, (MetaTensor, torch.Tensor)):
unique_label = unique_label.data.cpu().numpy()
unique_label = unique_label.data.cpu().numpy() # type: ignore[assignment]

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

Expand Down
2 changes: 2 additions & 0 deletions monai/metrics/cumulative_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def get_current(self, to_numpy: bool = True) -> NdarrayOrTensor:
if self.val is None:
return 0

val: NdarrayOrTensor
val = self.val.clone()
val[~torch.isfinite(val)] = 0

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

val: NdarrayOrTensor
val = torch.where(count > 0, sum / count, sum)

if to_numpy:
Expand Down
2 changes: 1 addition & 1 deletion monai/metrics/panoptic_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _get_paired_iou(

return paired_iou, paired_true, paired_pred

pairwise_iou = pairwise_iou.cpu().numpy()
pairwise_iou = pairwise_iou.cpu().numpy() # type: ignore[assignment]
paired_true, paired_pred = linear_sum_assignment(-pairwise_iou)
paired_iou = pairwise_iou[paired_true, paired_pred]
paired_true = torch.as_tensor(list(paired_true[paired_iou > match_iou_threshold] + 1), device=device)
Expand Down
4 changes: 2 additions & 2 deletions monai/metrics/rocauc.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float:

n = len(y)
indices = y_pred.argsort()
y = y[indices].cpu().numpy()
y_pred = y_pred[indices].cpu().numpy()
y = y[indices].cpu().numpy() # type: ignore[assignment]
y_pred = y_pred[indices].cpu().numpy() # type: ignore[assignment]
nneg = auc = tmp_pos = tmp_neg = 0.0

for i in range(n):
Expand Down
2 changes: 1 addition & 1 deletion monai/transforms/croppad/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _np_pad(img: NdarrayTensor, pad_width: list[tuple[int, int]], mode: str, **k
warnings.warn(f"Padding: moving img {img.shape} from cuda to cpu for dtype={img.dtype} mode={mode}.")
img_np = img.detach().cpu().numpy()
else:
img_np = img
img_np = np.asarray(img)
mode = convert_pad_mode(dst=img_np, mode=mode).value
if mode == "constant" and "value" in kwargs:
kwargs["constant_values"] = kwargs.pop("value")
Expand Down
4 changes: 3 additions & 1 deletion monai/visualize/img2tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def plot_2d_or_3d_image(
# as the `d` data has no batch dim, reduce the spatial dim index if positive
frame_dim = frame_dim - 1 if frame_dim > 0 else frame_dim

d: np.ndarray = data_index.detach().cpu().numpy() if isinstance(data_index, torch.Tensor) else data_index
d: np.ndarray = (
data_index.detach().cpu().numpy() if isinstance(data_index, torch.Tensor) else np.asarray(data_index)
)

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

0 comments on commit 3f0c768

Please sign in to comment.