BUG: sparse: Clean up 1D input handling to sparse array/matrix and add tests#20444
BUG: sparse: Clean up 1D input handling to sparse array/matrix and add tests#20444perimosocordiae merged 4 commits intoscipy:mainfrom
Conversation
|
I think the big question to resolve here is: should spmatrix types continue to support construction from 1d dense inputs? |
|
In the April 15 sparse arrays meeting it was pointed out that np.matrix also constructs 2D matrix from 1D input. So we should leave spmatrix behavior as it is. I've updated this PR to only change behavior for sparray. It raises a ValueError when For @perimosocordiae can you take another look at this one? |
perimosocordiae
left a comment
There was a problem hiding this comment.
These errors will be much nicer for users than what we currently raise. Thanks!
|
This fixes a complaint about |
…d tests (scipy#20444) * test 1d input to sparse. Add FutureWarnings and ValueErrors * remove matrix changes. Let them create 2D * correct imports * rebase on main and update to support 1D CSR input
Fixes #20415
1D input to sparse matrix constructor creates a 2D matrix with one row. This could be thought of as a bug (1D input should not create 2D output) or a feature (1D input saves an extra square bracket pair of typing). But it definitely needs to be changed when moving to a sparse array api that supports 1D sparse arrays.
In addition, the 1D input case is not handled gracefully in 1.13 for
csr_arraybecause the CSR 1D support PR did not get in in time.The 1D input case to 2D output does have a test for sparse matrices, but not for arrays. So new tests are needed as well. The module
test_base.pyeven uses 1D input in 2 places -- this PR changes those to 2D input. The test for the 1D input -> 2D output behavior is left as is. The test for arrays depends on format and in this PR is added to test_common1d.pyupdated to check arrays as well as matrices that the FutureWarning is in place.This PR also adds tests to ensure behavior is determined for 1D input for all formats. It improves the ValueError message for the
csr_arraycase to adjust the reported confusing ValueError message (which will become moot when the csr PR is merged). It adds FutureWarnings for sparse matrix when 1D constructor input occurs in all formats. It adds ValueErrors with better error messages for sparse formats that are not planned to support 1D arrays.The sparse working group should discuss this on Monday to ensure no other similar cases have been missed.
Precommit linting required some changes which are not directly related to the other content in this PR.