Skip to content

TST Extend tests for scipy.sparse.*array in sklearn/utils/_testing.py#27847

Closed
y-vectorfield wants to merge 6 commits intoscikit-learn:mainfrom
y-vectorfield:yv_2311_extends_some_tests_for_scipy_sparse_arrays
Closed

TST Extend tests for scipy.sparse.*array in sklearn/utils/_testing.py#27847
y-vectorfield wants to merge 6 commits intoscikit-learn:mainfrom
y-vectorfield:yv_2311_extends_some_tests_for_scipy_sparse_arrays

Conversation

@y-vectorfield
Copy link
Copy Markdown

Towards #27090.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Nov 26, 2023

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 2dfec12. Link to the linter CI: here

Copy link
Copy Markdown
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

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

Hi @y-vectorfield,

Thank you for this contribution. The tests can't be run because this file only contains test fixtures.

)
elif constructor_name == "sparse_csc":
return sp.sparse.csc_matrix(container, dtype=dtype)
return csr_container(dtype=dtype)
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.

Same suggestions as above but for csc instead of csr.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I returned the original code.

Copy link
Copy Markdown
Member

@jjerphan jjerphan Dec 2, 2023

Choose a reason for hiding this comment

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

Suggested change
return csr_container(dtype=dtype)
return sp.sparse.csc_matrix(container, dtype=dtype)

os.unlink(source_file)


@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
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.

This is a fixture, not a test, so it need not be parametrized.

Suggested change
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I removed this code.

@@ -781,15 +783,15 @@ def _convert_container(
elif constructor_name == "slice":
return slice(container[0], container[1])
elif constructor_name == "sparse_csr":
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.

Suggested change
elif constructor_name == "sparse_csr":
elif constructor_name == "sparse_csr":
# TODO: when we depend on SciPy>=1.8 return `csr_array` instead and change
# the case bellow from `sparse_csr_array` to `sparse_csr_matrix` and make
# it return a `csr_matrix`.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I added your suggestion to the source file.

return slice(container[0], container[1])
elif constructor_name == "sparse_csr":
return sp.sparse.csr_matrix(container, dtype=dtype)
return csr_container(dtype=dtype)
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.

Suggested change
return csr_container(dtype=dtype)
return sp.sparse.csr_matrix(container, dtype=dtype)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I also returned the original code.

)
from sklearn.utils._array_api import _check_array_api_dispatch
from sklearn.utils.fixes import parse_version, sp_version
from sklearn.utils.fixes import CSR_CONTAINERS, parse_version, sp_version
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.

This is not needed anymore.

Suggested change
from sklearn.utils.fixes import CSR_CONTAINERS, parse_version, sp_version
from sklearn.utils.fixes import parse_version, sp_version

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I also removed this module import.

@y-vectorfield
Copy link
Copy Markdown
Author

@jjerphan, thank you very much for your comments.
I did not need to modify this source, by any chance??

@y-vectorfield y-vectorfield force-pushed the yv_2311_extends_some_tests_for_scipy_sparse_arrays branch from 0f4abb3 to 1d45c34 Compare November 26, 2023 13:02
@jjerphan
Copy link
Copy Markdown
Member

I have added a few suggestions in my review above to show what needs to be modified.

Copy link
Copy Markdown
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

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

Ignore this second review: I was confused by your commit, please do not force push after a first review: this makes keeping track of changes harder.

@y-vectorfield
Copy link
Copy Markdown
Author

Ignore this second review: I was confused by your commit, please do not force push after a first review: this makes keeping track of changes harder.

Oh, sorry, I will be careful not to force push in the future.

@y-vectorfield y-vectorfield marked this pull request as ready for review November 26, 2023 21:41
@jjerphan
Copy link
Copy Markdown
Member

Oh, sorry, I will be careful not to force push in the future.

It's fine. :)

I will review the PR once the failures on the CI are fixed. Let me know if you need help.

@y-vectorfield
Copy link
Copy Markdown
Author

Oh, sorry, I will be careful not to force push in the future.

It's fine. :)

I will review the PR once the failures on the CI are fixed. Let me know if you need help.

Hello, @jjerphan, thank you very much for your message.
I would like to teach what I should do.
I returned the original codes and added only comments.

)
elif constructor_name == "sparse_csc":
return sp.sparse.csc_matrix(container, dtype=dtype)
return csr_container(dtype=dtype)
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.

This needs to be changed as suggested above.

Copy link
Copy Markdown
Author

@y-vectorfield y-vectorfield Nov 29, 2023

Choose a reason for hiding this comment

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

Hello, @jjerphan,
Oh, I'm sorry. Do you mean as follows??

current version

return csr_container(dtype=dtype)

modified version(I will add.)

return csc_container(dtype=dtype)

Copy link
Copy Markdown
Author

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

Choose a reason for hiding this comment

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

I meant #27847 (comment) and #27847 (comment) but for CSC.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@jjerphan, I'm sorry for the confusion.
I modified the source.

Copy link
Copy Markdown
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

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

What I meant is shown by those suggestions.

elif constructor_name == "sparse_csr_array":
if sp_version >= parse_version("1.8"):
return sp.sparse.csr_array(container, dtype=dtype)
return sp.sparse.csc_array(container, dtype=dtype)
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.

Suggested change
return sp.sparse.csc_array(container, dtype=dtype)
return sp.sparse.csr_array(container, dtype=dtype)

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.

@y-vectorfield you forgot this one.

Copy link
Copy Markdown
Author

@y-vectorfield y-vectorfield Feb 19, 2024

Choose a reason for hiding this comment

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

@jjerphan ,
Oh, I'm sorry. I reflected your comment.

raise ValueError(
f"sparse_csr_array is only available with scipy>=1.8.0, got {sp_version}"
)
elif constructor_name == "sparse_csc":
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.

Suggested change
elif constructor_name == "sparse_csc":
elif constructor_name == "sparse_csc":
# TODO: when we depend on SciPy>=1.8 return `csc_array` instead and change
# the case bellow from `sparse_csc_array` to `sparse_csc_matrix` and make
# it return a `csc_matrix`.

)
elif constructor_name == "sparse_csc":
return sp.sparse.csc_matrix(container, dtype=dtype)
return csr_container(dtype=dtype)
Copy link
Copy Markdown
Member

@jjerphan jjerphan Dec 2, 2023

Choose a reason for hiding this comment

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

Suggested change
return csr_container(dtype=dtype)
return sp.sparse.csc_matrix(container, dtype=dtype)

@jjerphan
Copy link
Copy Markdown
Member

jjerphan commented Dec 9, 2023

@y-vectorfield: are you still interested in working on this PR?

@y-vectorfield
Copy link
Copy Markdown
Author

y-vectorfield commented Dec 10, 2023

@y-vectorfield: are you still interested in working on this PR?

Hello, @jjerphan, thank you for your message.
Yes, I will work on this PR. Please wait for a moment.

@marenwestermann
Copy link
Copy Markdown
Member

Hi @y-vectorfield would you like to continue working on this PR? Let us know if you need help.

@y-vectorfield
Copy link
Copy Markdown
Author

Hello, @marenwestermann , very sorry. I've been away from this task for a while.
I will submit a PR in the next few days.
Thank you!

@y-vectorfield
Copy link
Copy Markdown
Author

@jjerphan , @marenwestermann ,
Sorry for the late modification.
I reflected on your comments.
If the review is finished, I will rebase this branch.

@y-vectorfield
Copy link
Copy Markdown
Author

@jjerphan , @marenwestermann ,
If there is no problem, please approve this PR.
I will rebase this branch.

@Charlie-XIAO
Copy link
Copy Markdown
Contributor

I didn't get what this PR intends to do. It seems that on main we already have support for returning sparse arrays, added in an earlier PR #27276 when extending tests for utils/tests/test_testing.py.

@jjerphan
Copy link
Copy Markdown
Member

There are still a couple of adaption left for sparse array and sparse matrices.

I do not have time to sort this out, but it should be trivial.

Can someone have a look at the merge conflicts, please?

@Charlie-XIAO
Copy link
Copy Markdown
Contributor

Charlie-XIAO commented Feb 21, 2024

elif "sparse" in constructor_name:
if not sp.sparse.issparse(container):
# For scipy >= 1.13, sparse array constructed from 1d array may be
# 1d or raise an exception. To avoid this, we make sure that the
# input container is 2d. For more details, see
# https://github.com/scipy/scipy/pull/18530#issuecomment-1878005149
container = np.atleast_2d(container)
if "array" in constructor_name and sp_version < parse_version("1.8"):
raise ValueError(
f"{constructor_name} is only available with scipy>=1.8.0, got "
f"{sp_version}"
)
if constructor_name in ("sparse", "sparse_csr"):
# sparse and sparse_csr are equivalent for legacy reasons
return sp.sparse.csr_matrix(container, dtype=dtype)
elif constructor_name == "sparse_csr_array":
return sp.sparse.csr_array(container, dtype=dtype)
elif constructor_name == "sparse_csc":
return sp.sparse.csc_matrix(container, dtype=dtype)
elif constructor_name == "sparse_csc_array":
return sp.sparse.csc_array(container, dtype=dtype)

The problem is that I tried to resolve the merge conflicts but then no relevant changes are left. As above we already have csc and csr arrays and matrices on main, and this PR does not seem to involve anything beyond that. Or should it?

@jjerphan
Copy link
Copy Markdown
Member

OK, so then let's close it.

@jjerphan jjerphan closed this Feb 21, 2024
@y-vectorfield
Copy link
Copy Markdown
Author

@jjerphan , thank you very much for your review.
Unfortunately, the corrections could not be reflected this time.
I will help you the next time.

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