From 6be70b971730399c82b156c8ece86f2bea6c03a1 Mon Sep 17 00:00:00 2001 From: James Butler Date: Wed, 23 Oct 2024 21:24:52 -0400 Subject: [PATCH 1/3] Specify selected rules for ruff lint configuration Signed-off-by: James Butler --- pyproject.toml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index c2ab92a43d..36b5629b8d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,16 @@ exclude = "monai/bundle/__main__.py" [tool.ruff] line-length = 133 -lint.ignore = ["F401", "E741"] +target-version = "py39" + +[tool.ruff.lint] +select = [ + "E", "F", "W", # flake8 +] +extend-ignore = [ + "E741", # ambiguous variable name + "F401", # unused import +] [tool.pytype] # Space-separated list of files or directories to exclude. From bbdbce9aafad1332dcc818864d4d072eefc2876d Mon Sep 17 00:00:00 2001 From: James Butler Date: Wed, 23 Oct 2024 21:26:05 -0400 Subject: [PATCH 2/3] Add ruff numpy rules Signed-off-by: James Butler --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 36b5629b8d..794fd99f9c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,10 +44,13 @@ target-version = "py39" [tool.ruff.lint] select = [ "E", "F", "W", # flake8 + "NPY", # NumPy specific rules ] extend-ignore = [ "E741", # ambiguous variable name "F401", # unused import + "NPY002", # numpy-legacy-random + "NPY201", # numpy2 deprecation ] [tool.pytype] From d38a2009892a9442b44593f66bfa7ec856e423ae Mon Sep 17 00:00:00 2001 From: James Butler Date: Thu, 24 Oct 2024 10:22:25 -0400 Subject: [PATCH 3/3] Fix Ruff Numpy2 deprecation rules See https://docs.astral.sh/ruff/rules/numpy2-deprecation/ Signed-off-by: James Butler --- monai/metrics/fid.py | 2 +- monai/transforms/utils_pytorch_numpy_unification.py | 2 +- pyproject.toml | 1 - tests/test_compute_f_beta.py | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/monai/metrics/fid.py b/monai/metrics/fid.py index d655ac1bee..596f9aef7c 100644 --- a/monai/metrics/fid.py +++ b/monai/metrics/fid.py @@ -82,7 +82,7 @@ def _cov(input_data: torch.Tensor, rowvar: bool = True) -> torch.Tensor: def _sqrtm(input_data: torch.Tensor) -> torch.Tensor: """Compute the square root of a matrix.""" - scipy_res, _ = scipy.linalg.sqrtm(input_data.detach().cpu().numpy().astype(np.float_), disp=False) + scipy_res, _ = scipy.linalg.sqrtm(input_data.detach().cpu().numpy().astype(np.float64), disp=False) return torch.from_numpy(scipy_res) diff --git a/monai/transforms/utils_pytorch_numpy_unification.py b/monai/transforms/utils_pytorch_numpy_unification.py index 98b75cff76..365bd1eab5 100644 --- a/monai/transforms/utils_pytorch_numpy_unification.py +++ b/monai/transforms/utils_pytorch_numpy_unification.py @@ -88,7 +88,7 @@ def moveaxis(x: NdarrayOrTensor, src: int | Sequence[int], dst: int | Sequence[i def in1d(x, y): """`np.in1d` with equivalent implementation for torch.""" if isinstance(x, np.ndarray): - return np.in1d(x, y) + return np.isin(x, y) return (x[..., None] == torch.tensor(y, device=x.device)).any(-1).view(-1) diff --git a/pyproject.toml b/pyproject.toml index 794fd99f9c..9dc9cf619b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,7 +50,6 @@ extend-ignore = [ "E741", # ambiguous variable name "F401", # unused import "NPY002", # numpy-legacy-random - "NPY201", # numpy2 deprecation ] [tool.pytype] diff --git a/tests/test_compute_f_beta.py b/tests/test_compute_f_beta.py index 43ebb6a6d5..be2a7fc176 100644 --- a/tests/test_compute_f_beta.py +++ b/tests/test_compute_f_beta.py @@ -59,7 +59,7 @@ def test_with_nan_values(self): metric = FBetaScore(get_not_nans=True) metric( y_pred=torch.Tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]]), - y=torch.Tensor([[1, 0, 1], [np.NaN, np.NaN, np.NaN], [1, 0, 1]]), + y=torch.Tensor([[1, 0, 1], [np.nan, np.nan, np.nan], [1, 0, 1]]), ) assert_allclose(metric.aggregate()[0][0], torch.Tensor([0.727273]), atol=1e-6, rtol=1e-6)