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

MAINT: sparse: Simplify sputils.isintlike #7864

Merged
merged 5 commits into from
Oct 1, 2017

Conversation

perimosocordiae
Copy link
Member

This works around gh-7860 by not calling np.isscalar, as suggested by @eric-wieser here.

Some objects that fail sputils.isscalarlike will pass the np.ndim check (i.e. object), but these are handled by the try-except check, which should be cheap for anything where np.ndim(x) == 0.

This works around scipygh-7860 by not calling np.isscalar,
as suggested by @eric-wieser.

Some objects that fail sputils.isscalarlike will pass the
np.ndim check (i.e. object), but these are handled by the
try-except check, which should be cheap for anything where
np.ndim(x) == 0.
@pv
Copy link
Member

pv commented Sep 15, 2017

Should this actually use operator.index?
Ditto for the rest of the indexing machinery.

@eric-wieser
Copy link
Contributor

Yes, but that would be a compatibility break. Perhaps

try:
    operator.index(x)
except TypeError:
    if _old_intlike(x):
        print_a_scary_warning()
        return True
    else:
        return False
else:
    return True

Of course, really the error should propagate too

@pv
Copy link
Member

pv commented Sep 15, 2017

yeah, for 1.0.0 we probably want the backward compatible solution, we can iterate more in 1.1.0

@rgommers rgommers added maintenance Items related to regular maintenance tasks scipy.sparse labels Sep 16, 2017
@perimosocordiae
Copy link
Member Author

perimosocordiae commented Sep 16, 2017

I added the suggested deprecation warning, for the case where operator.index(x) fails but int(x) == x.

return False
if loose_int:
warnings.warn("Inexact indices to sparse matrices are deprecated "
"as of scipy 1.0")
Copy link
Contributor

Choose a reason for hiding this comment

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

Ought to be a DeprecationWarning

@perimosocordiae
Copy link
Member Author

@eric-wieser I thought I was forgetting something with that warning...

I also added a filter to the unit test so pytest doesn't fail when the warning is thrown.

@@ -189,12 +190,20 @@ def isintlike(x):
"""Is x appropriate as an index into a sparse matrix? Returns True
if it can be cast safely to a machine int.
"""
if not isscalarlike(x):
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably worth commenting that this is strictly an optimization - this is already covered implicitly by the other branches

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a comment to that effect.

@@ -189,12 +190,20 @@ def isintlike(x):
"""Is x appropriate as an index into a sparse matrix? Returns True
if it can be cast safely to a machine int.
"""
if not isscalarlike(x):
if np.ndim(x) != 0:
Copy link
Member

@pv pv Sep 30, 2017

Choose a reason for hiding this comment

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

Why is this before the call to operator.index?
EDIT: ok, I see now.

return False
if loose_int:
warnings.warn("Inexact indices into sparse matrices are deprecated",
DeprecationWarning)
Copy link
Member

@pv pv Sep 30, 2017

Choose a reason for hiding this comment

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

Shouldn't the DeprecationWarning be raised unconditionally if operator.index fails?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, the warning should only appear for places where isintlike(x) has the "wrong" value. If the old conversion gives the same result as operator.index, there's nothing to warn about - the False already gets turned into an error down the line,

@pv pv merged commit 3a4240b into scipy:master Oct 1, 2017
@pv pv added this to the 1.1.0 milestone Oct 1, 2017
@perimosocordiae perimosocordiae deleted the isscalarlike-ndim branch October 1, 2017 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Items related to regular maintenance tasks scipy.sparse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants