Skip to content

Commit 5b2d78e

Browse files
authored
Fix Ruff Numpy2 deprecation rules (Project-MONAI#8179)
### Description This addresses expired numpy deprecations that happened with the numpy 2 release. This code is compatible with numpy 1 and 2 to support future enablement of numpy2 for monai. See https://docs.astral.sh/ruff/rules/numpy2-deprecation/ ### 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). --------- Signed-off-by: James Butler <[email protected]>
1 parent 8dc3b4f commit 5b2d78e

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

monai/metrics/fid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _cov(input_data: torch.Tensor, rowvar: bool = True) -> torch.Tensor:
8282

8383
def _sqrtm(input_data: torch.Tensor) -> torch.Tensor:
8484
"""Compute the square root of a matrix."""
85-
scipy_res, _ = scipy.linalg.sqrtm(input_data.detach().cpu().numpy().astype(np.float_), disp=False)
85+
scipy_res, _ = scipy.linalg.sqrtm(input_data.detach().cpu().numpy().astype(np.float64), disp=False)
8686
return torch.from_numpy(scipy_res)
8787

8888

monai/transforms/utils_pytorch_numpy_unification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def moveaxis(x: NdarrayOrTensor, src: int | Sequence[int], dst: int | Sequence[i
8888
def in1d(x, y):
8989
"""`np.in1d` with equivalent implementation for torch."""
9090
if isinstance(x, np.ndarray):
91-
return np.in1d(x, y)
91+
return np.isin(x, y)
9292
return (x[..., None] == torch.tensor(y, device=x.device)).any(-1).view(-1)
9393

9494

pyproject.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ exclude = "monai/bundle/__main__.py"
3939

4040
[tool.ruff]
4141
line-length = 133
42-
lint.ignore = ["F401", "E741"]
42+
target-version = "py39"
43+
44+
[tool.ruff.lint]
45+
select = [
46+
"E", "F", "W", # flake8
47+
"NPY", # NumPy specific rules
48+
]
49+
extend-ignore = [
50+
"E741", # ambiguous variable name
51+
"F401", # unused import
52+
"NPY002", # numpy-legacy-random
53+
]
4354

4455
[tool.pytype]
4556
# Space-separated list of files or directories to exclude.

tests/test_compute_f_beta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_with_nan_values(self):
5959
metric = FBetaScore(get_not_nans=True)
6060
metric(
6161
y_pred=torch.Tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]]),
62-
y=torch.Tensor([[1, 0, 1], [np.NaN, np.NaN, np.NaN], [1, 0, 1]]),
62+
y=torch.Tensor([[1, 0, 1], [np.nan, np.nan, np.nan], [1, 0, 1]]),
6363
)
6464
assert_allclose(metric.aggregate()[0][0], torch.Tensor([0.727273]), atol=1e-6, rtol=1e-6)
6565

0 commit comments

Comments
 (0)