Skip to content

Remove guarded imports of scipy#6596

Merged
rapids-bot[bot] merged 2 commits intorapidsai:branch-25.06from
jcrist:remove-has-scipy
Apr 29, 2025
Merged

Remove guarded imports of scipy#6596
rapids-bot[bot] merged 2 commits intorapidsai:branch-25.06from
jcrist:remove-has-scipy

Conversation

@jcrist
Copy link
Copy Markdown
Member

@jcrist jcrist commented Apr 28, 2025

SciPy is currently a required dependency of cuml (and has been for a while). With the intent to make sklearn a required dependency (which also requires scipy), it seems unlikely we'll ever step back from this requirement.

This PR removes all the remaining guarded imports of scipy, along with the has_scipy function. There were plenty of places where we didn't guard the import, so doing this improves consistency and deletes some unneeded branching. In the 2 places where we were using has_scipy to also check the version I've inlined the checks which is also more readable IMO.

SciPy is currently a required dependency of cuml (and has been for a
while). With the intent to make sklearn a required dependency (which
also requires scipy), it seems unlikely we'll ever step back from this
requirement.

This PR removes all the remaining guarded imports of scipy, along with
the `has_scipy` function. There were plenty of places where we didn't
guard the import, so doing this improves consistency and deletes some
unneeded branching. In the 2 places where we were using `has_scipy` to
also check the version I've inlined the checks which is also more
readable IMO.
@jcrist jcrist requested review from a team as code owners April 28, 2025 15:56
@github-actions github-actions Bot added the Cython / Python Cython or Python issue label Apr 28, 2025
@jcrist jcrist self-assigned this Apr 28, 2025
@jcrist jcrist added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels Apr 28, 2025
Comment on lines -38 to +34
is_scipy_sparse = has_scipy() and scipy.sparse.issparse(X)
return cupyx.scipy.sparse.issparse(X) or is_scipy_sparse
return scipy.sparse.issparse(X) or cupyx.scipy.sparse.issparse(X)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to worry about the order in which the parts of the or statement are evaluated? I think Python short circuits logical statements like this, aka it stops once the value is known. I can't work out in my head if the old version relied on this behaviour to avoid errors? Maybe also far too obscure a thing to worry about?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

@betatim betatim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

One question for my education but I think I answered it myself from the rest of the diff.

@betatim
Copy link
Copy Markdown
Member

betatim commented Apr 29, 2025

/merge

@rapids-bot rapids-bot Bot merged commit 03fd64e into rapidsai:branch-25.06 Apr 29, 2025
78 checks passed
@jcrist jcrist deleted the remove-has-scipy branch April 29, 2025 16:56
rapids-bot Bot pushed a commit that referenced this pull request Apr 29, 2025
This PR started because I noticed most of the functions in `import_utils.py` were effectively dead code. It spiraled out a bit from there to remove _almost all_ of `import_utils.py` in favor of:

- Using `pytest.importorskip` to handle conditional imports in tests. This is the proper `pytest` pattern to do this, and is both easier to get correct and uses fewer lines of code.
- Localized import checks (see `hdbscan.pyx`). Keeping the import check local to the module is easier to read IMO, and also helps avoid dead code accumulating in a global utils file.
- No import checks. Things like `cupy` are required dependencies and don't need to be gated at all.

The remaining functions will be removed in other PRs:

- `has_dask` will go away once `dask` is made optional (see #5934)
- `has_scipy` is removed in #6596
- `has_sklearn` will go away once `sklearn` is made a required dependency

Authors:
  - Jim Crist-Harif (https://github.com/jcrist)

Approvers:
  - Simon Adorf (https://github.com/csadorf)

URL: #6599
Ofek-Haim pushed a commit to Ofek-Haim/cuml that referenced this pull request May 13, 2025
SciPy is currently a required dependency of cuml (and has been for a while). With the intent to make sklearn a required dependency (which also requires scipy), it seems unlikely we'll ever step back from this requirement.

This PR removes all the remaining guarded imports of scipy, along with the `has_scipy` function. There were plenty of places where we didn't guard the import, so doing this improves consistency and deletes some unneeded branching. In the 2 places where we were using `has_scipy` to also check the version I've inlined the checks which is also more readable IMO.

Authors:
  - Jim Crist-Harif (https://github.com/jcrist)

Approvers:
  - Gil Forsyth (https://github.com/gforsyth)
  - Tim Head (https://github.com/betatim)

URL: rapidsai#6596
Ofek-Haim pushed a commit to Ofek-Haim/cuml that referenced this pull request May 13, 2025
This PR started because I noticed most of the functions in `import_utils.py` were effectively dead code. It spiraled out a bit from there to remove _almost all_ of `import_utils.py` in favor of:

- Using `pytest.importorskip` to handle conditional imports in tests. This is the proper `pytest` pattern to do this, and is both easier to get correct and uses fewer lines of code.
- Localized import checks (see `hdbscan.pyx`). Keeping the import check local to the module is easier to read IMO, and also helps avoid dead code accumulating in a global utils file.
- No import checks. Things like `cupy` are required dependencies and don't need to be gated at all.

The remaining functions will be removed in other PRs:

- `has_dask` will go away once `dask` is made optional (see rapidsai#5934)
- `has_scipy` is removed in rapidsai#6596
- `has_sklearn` will go away once `sklearn` is made a required dependency

Authors:
  - Jim Crist-Harif (https://github.com/jcrist)

Approvers:
  - Simon Adorf (https://github.com/csadorf)

URL: rapidsai#6599
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Cython / Python Cython or Python issue improvement Improvement / enhancement to an existing function non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants