Skip to content

MAINT Use scipy.sparse.isspmatrix_* #26420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2023

Conversation

jjerphan
Copy link
Member

@jjerphan jjerphan commented May 23, 2023

Reference Issues/PRs

Towards resolving #26418.

What does this implement/fix? Explain your changes.

Contrary to what names suggest, scipy.sparse.isspmatrix_* not only check for sparse matrices' instances but also sparse arrays'.

This PR proposes replacing all occurrences of checks using isinstance(X, *_matrix) by checks calling to scipy.sparse.isspmatrix_*.

Any other comments?

Later, when $n$-dimensional arrays are implemented (with $n > 2$) in SciPy, we will need to add extra checks on $n$.

cc @ivirshup

Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
@jjerphan jjerphan changed the title Use dedicated checks from scipy.sparse MAINT Use scipy.sparse.isspmatrix_* May 23, 2023
@jjerphan
Copy link
Member Author

@rossbar: FYI, this is the PR that intentionally uses the current behavior of scipy.sparse.isspmatrix as mentioned IRL.

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

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

Looks good. It's a bit unfortunate that the name is isspmatrix.

@jjerphan
Copy link
Member Author

Based on IRL discussions with @rossbar, similar issparray_* functions might be introduced in the future and the isspmatrix_* functios might be deprecated or might have their behavior change in the future.

@rossbar
Copy link
Contributor

rossbar commented May 24, 2023

Based on IRL discussions with @rossbar, similar issparray_* functions might be introduced in the future and the isspmatrix_* functios might be deprecated or might have their behavior change in the future.

There's still discussion to be had, but I would propose to deprecate the isspmatrix_<format> exactly for the reason that it's not API that should be duplicated for arrays.

Instead of e.g. isspmatrix_csc(a), I would argue for the following:

  • In the case where you don't care that a is a sparse matrix or sparse array (i.e. you can support both), then I would write this as: issparse(a) and a.format == "csc"
  • In the case where you want to make sure you support only matrix semantics (which you wouldn't want to do for new code, but might be necessary to ensure compatibility), then: isinstance(a, spmatrix) and a.format == "csc"

The worst case is when you want to ensure that the input is a sparse array and not a sparse matrix. For this case I think the only current way to do it is with three independent checks: one for sparsity, one to ensure it's not a matrix, and a final one for the format:

>>> issparse(a) and not isinstance(a, spmatrix) and a.format == "csc"

@jjerphan
Copy link
Member Author

Let's wait for scipy/scipy#18528 to be merged before pursuing.

@rossbar
Copy link
Contributor

rossbar commented May 29, 2023

Another update here - after scipy/scipy#18531, isinstance(..., csr_matrix) now (or will, as of scipy 1.11) return True for csr_matrix and not csr_array.

@jjerphan
Copy link
Member Author

Yes, this PR needs to be updated to use scipy.sparse.issparse and format instead.

@jjerphan
Copy link
Member Author

I would be in favor of merging this PR for uniformity and addressing the conversion in another PR. What do you think?

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

LGTM

@thomasjpfan thomasjpfan merged commit 41b0bd8 into scikit-learn:main May 30, 2023
@jjerphan jjerphan deleted the maint/sparse-array-checks branch May 30, 2023 17:48
REDVM pushed a commit to REDVM/scikit-learn that referenced this pull request Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants