Skip to content
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

BUG: sparse: raise error for array classes, document/test old behavior #20490

Merged
merged 9 commits into from May 7, 2024

Conversation

ilan-gold
Copy link
Contributor

@ilan-gold ilan-gold commented Apr 16, 2024

Reference issue

Closes #20128

What does this implement/fix?

We raise an error on all array classes and ensure that matrices continue to return 1x1 objects.

Additional information

The way I did this was to make sparray the first parent in the super classes and add an __init__ method. Not super familiar with the codebase, but presumably since nothing broke, this is an ok thing to do?

@ilan-gold ilan-gold changed the title (feat): raise error for array classes, document/test old behavior (fix): raise error for array classes, document/test old behavior Apr 16, 2024
@lucascolley lucascolley changed the title (fix): raise error for array classes, document/test old behavior BUG: sparse: raise error for array classes, document/test old behavior Apr 16, 2024
@lucascolley lucascolley added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Apr 16, 2024
@tylerjereddy tylerjereddy added this to the 1.14.0 milestone Apr 17, 2024
Copy link
Contributor

@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

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

The diff looks pretty clean to me, and it seems that CJ gave a +1 to the idea of errors in these cases in the matching issue.

There are a few related linter complaints you can see in the CI.

I should let CJ or another sparse regular take a look before it goes in though, but looks pretty clean at first glance.

Comment on lines 1510 to 1516
def __init__(self, *args, **kwargs):
if np.isscalar(args[0]):
raise ValueError(
"scipy sparse array classes do not \
support instantiation from a scalar"
)
super().__init__(*args, **kwargs)
Copy link
Contributor

@dschult dschult Apr 19, 2024

Choose a reason for hiding this comment

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

The __init__ methods are pretty spread out across classes already. If it spreads more it may become hard to tell which init methods gets called during the init process. So, I think in the long run this check might be better placed inside _sputils.check_shape. But that's just my suggestion. I'm ok with other choices.

@perimosocordiae should probably decide.

Copy link
Contributor

@dschult dschult left a comment

Choose a reason for hiding this comment

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

Many of the CI failures are due to linting of strings extending across \newline boundaries. The following comments should help remove those complaints.

scipy/sparse/_base.py Outdated Show resolved Hide resolved
scipy/sparse/_bsr.py Outdated Show resolved Hide resolved
scipy/sparse/tests/test_construct.py Outdated Show resolved Hide resolved
Comment on lines 98 to 99
f'mismatching blocksize={blocksize}'
f'vs {self.data.shape[1:]}'
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want a space either at the end of the first string or at the start of the second string. I typically put it at the start of the second string but that's personal preference. If the space isn't in either spot the {blocksize}' output runs into the vs`.

self._shape = None
if self.__class__.__name__ == '_spbase':
raise ValueError("This class is not intended"
" to be instantiated directly.")
if isinstance(self, sparray) and np.isscalar(arg1):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implementing the suggestion from our call means that _spbase now needs to check the first argument which resulted in this change. Not sure if the lack of arguments in _spbase was a choice or not, but maybe there's a way around this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm... Good point about lack of arguments being a choice or not.
It looks like setting maxprint actually doesn't work for all the subclasses that override __init__. So currently we can't choose a non-default maxprint value for most of the formats.

That could be fixed by adding the maxprint parameter to __init__ methods throughout. Or by adding **kwargs parameters that get passed through to the superclass __init__. The second option is harder to read for someone trying to figure out what all the options are.

And this could be fixed in a separate PR. Let me know if you prefer that and I'll open a separate issue.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the maxprint functionality has been inaccessible for a while, unfortunately. We should clean that up in a later PR.

self._shape = None
if self.__class__.__name__ == '_spbase':
raise ValueError("This class is not intended"
" to be instantiated directly.")
if isinstance(self, sparray) and np.isscalar(arg1):
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the maxprint functionality has been inaccessible for a while, unfortunately. We should clean that up in a later PR.

@perimosocordiae
Copy link
Member

This PR looks good from here. Once the CI completes, let's merge it.

@perimosocordiae perimosocordiae merged commit 1457f69 into scipy:main May 7, 2024
30 checks passed
@perimosocordiae
Copy link
Member

Merged, thanks @ilan-gold for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: csr_array(int()) errors
5 participants