From 64ea76d83a92b7cf7f13c8f93498d50037c3324c Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Sat, 6 Jul 2024 11:11:57 +0800 Subject: [PATCH] Fix deprecated argument in 'scipy.sparse.linalg.cg' (#7897) Fixes #7896 ### Description - 'scipy.sparse.linalg.cg' keyword argument `tol` is deprecated in favor of `rtol` and will be removed in SciPy v1.14.0. Until then, if set, it will override `rtol`. So update to use `rtol` in `cg`. - Drop python 3.8 test in packaging and premerge-gpu-test. ### Types of changes - [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> --- .github/workflows/pythonapp.yml | 13 +++++++------ docs/requirements.txt | 2 +- monai/data/ultrasound_confidence_map.py | 10 +++++----- monai/optimizers/lr_finder.py | 2 +- requirements-dev.txt | 2 +- requirements.txt | 2 +- setup.cfg | 4 ++-- tests/test_ultrasound_confidence_map_transform.py | 4 ++++ tests/utils.py | 2 +- 9 files changed, 23 insertions(+), 18 deletions(-) diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index d1e77bb567..cd6b6ccede 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -29,10 +29,10 @@ jobs: opt: ["codeformat", "pytype", "mypy"] steps: - uses: actions/checkout@v4 - - name: Set up Python 3.8 + - name: Set up Python 3.9 uses: actions/setup-python@v5 with: - python-version: '3.8' + python-version: '3.9' - name: cache weekly timestamp id: pip-cache run: | @@ -45,6 +45,7 @@ jobs: key: ${{ runner.os }}-pip-${{ steps.pip-cache.outputs.datew }} - name: Install dependencies run: | + find /opt/hostedtoolcache/* -maxdepth 0 ! -name 'Python' -exec rm -rf {} \; python -m pip install --upgrade pip wheel python -m pip install -r requirements-dev.txt - name: Lint and type check @@ -130,10 +131,10 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Set up Python 3.8 + - name: Set up Python 3.9 uses: actions/setup-python@v5 with: - python-version: '3.8' + python-version: '3.9' - name: cache weekly timestamp id: pip-cache run: | @@ -211,10 +212,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Set up Python 3.8 + - name: Set up Python 3.9 uses: actions/setup-python@v5 with: - python-version: '3.8' + python-version: '3.9' - name: cache weekly timestamp id: pip-cache run: | diff --git a/docs/requirements.txt b/docs/requirements.txt index 6caddce666..fe415a07b5 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -6,7 +6,7 @@ itk>=5.2 nibabel parameterized scikit-image>=0.19.0 -scipy>=1.7.1 +scipy>=1.12.0; python_version >= '3.9' tensorboard commonmark==0.9.1 recommonmark==0.6.0 diff --git a/monai/data/ultrasound_confidence_map.py b/monai/data/ultrasound_confidence_map.py index 5c716b62be..865e4a0a0f 100644 --- a/monai/data/ultrasound_confidence_map.py +++ b/monai/data/ultrasound_confidence_map.py @@ -19,10 +19,10 @@ __all__ = ["UltrasoundConfidenceMap"] cv2, _ = optional_import("cv2") -csc_matrix, _ = optional_import("scipy.sparse", "1.7.1", min_version, "csc_matrix") -spsolve, _ = optional_import("scipy.sparse.linalg", "1.7.1", min_version, "spsolve") -cg, _ = optional_import("scipy.sparse.linalg", "1.7.1", min_version, "cg") -hilbert, _ = optional_import("scipy.signal", "1.7.1", min_version, "hilbert") +csc_matrix, _ = optional_import("scipy.sparse", "1.12.0", min_version, "csc_matrix") +spsolve, _ = optional_import("scipy.sparse.linalg", "1.12.0", min_version, "spsolve") +cg, _ = optional_import("scipy.sparse.linalg", "1.12.0", min_version, "cg") +hilbert, _ = optional_import("scipy.signal", "1.12.0", min_version, "hilbert") ruge_stuben_solver, _ = optional_import("pyamg", "5.0.0", min_version, "ruge_stuben_solver") @@ -285,7 +285,7 @@ def _solve_linear_system(self, lap, rhs): lap_sparse = lap.tocsr() ml = ruge_stuben_solver(lap_sparse, coarse_solver="pinv") m = ml.aspreconditioner(cycle="V") - x, _ = cg(lap, rhs, tol=self.cg_tol, maxiter=self.cg_maxiter, M=m) + x, _ = cg(lap, rhs, rtol=self.cg_tol, maxiter=self.cg_maxiter, M=m) else: x = spsolve(lap, rhs) diff --git a/monai/optimizers/lr_finder.py b/monai/optimizers/lr_finder.py index 045135628d..aa2e4567b3 100644 --- a/monai/optimizers/lr_finder.py +++ b/monai/optimizers/lr_finder.py @@ -524,7 +524,7 @@ def plot( # Plot the LR with steepest gradient if steepest_lr: lr_at_steepest_grad, loss_at_steepest_grad = self.get_steepest_gradient(skip_start, skip_end) - if lr_at_steepest_grad is not None: + if lr_at_steepest_grad is not None and loss_at_steepest_grad is not None: ax.scatter( lr_at_steepest_grad, loss_at_steepest_grad, diff --git a/requirements-dev.txt b/requirements-dev.txt index 1bba930273..ced783443e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,7 +2,7 @@ -r requirements-min.txt pytorch-ignite==0.4.11 gdown>=4.7.3 -scipy>=1.7.1 +scipy>=1.12.0; python_version >= '3.9' itk>=5.2 nibabel pillow!=8.3.0 # https://github.com/python-pillow/Pillow/issues/5571 diff --git a/requirements.txt b/requirements.txt index 1d6ae13eec..aae455f58c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ torch>=1.9 -numpy>=1.20,<2.0 +numpy>=1.20,<=1.26.0 diff --git a/setup.cfg b/setup.cfg index 05bf181c70..b2b32066ab 100644 --- a/setup.cfg +++ b/setup.cfg @@ -49,7 +49,7 @@ all = nibabel ninja scikit-image>=0.14.2 - scipy>=1.7.1 + scipy>=1.12.0; python_version >= '3.9' pillow tensorboard gdown>=4.7.3 @@ -92,7 +92,7 @@ ninja = skimage = scikit-image>=0.14.2 scipy = - scipy>=1.7.1 + scipy>=1.12.0; python_version >= '3.9' pillow = pillow!=8.3.0 tensorboard = diff --git a/tests/test_ultrasound_confidence_map_transform.py b/tests/test_ultrasound_confidence_map_transform.py index 87c08b3ac3..1c6b8f7635 100644 --- a/tests/test_ultrasound_confidence_map_transform.py +++ b/tests/test_ultrasound_confidence_map_transform.py @@ -20,8 +20,11 @@ from PIL import Image from monai.transforms import UltrasoundConfidenceMapTransform +from monai.utils import optional_import from tests.utils import assert_allclose +_, has_scipy = optional_import("scipy") + TEST_INPUT = np.array( [ [1, 2, 3, 23, 13, 22, 5, 1, 2, 3], @@ -482,6 +485,7 @@ ) +@unittest.skipUnless(has_scipy, "Requires scipy") class TestUltrasoundConfidenceMapTransform(unittest.TestCase): def setUp(self): diff --git a/tests/utils.py b/tests/utils.py index d1939e590b..77b53cebb8 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -475,7 +475,7 @@ def run_process(self, func, local_rank, args, kwargs, results): if self.verbose: os.environ["NCCL_DEBUG"] = "INFO" os.environ["NCCL_DEBUG_SUBSYS"] = "ALL" - os.environ["NCCL_BLOCKING_WAIT"] = str(1) + os.environ["TORCH_NCCL_BLOCKING_WAIT"] = str(1) os.environ["OMP_NUM_THREADS"] = str(1) os.environ["WORLD_SIZE"] = str(self.nproc_per_node * self.nnodes) os.environ["RANK"] = str(self.nproc_per_node * self.node_rank + local_rank)