Skip to content

MAINT: make np.ma.apply_along_axis consistent with np.apply_along_axis #8511

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

Closed
wants to merge 2 commits into from

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Jan 21, 2017

This transfers the fixes made in #8441 to np.ma.apply_along_axes.

This comes with a behaviour change - previously, the ma version would not fall afoul of #8352.

@homu
Copy link
Contributor

homu commented Feb 18, 2017

☔ The latest upstream changes (presumably #8614) made this pull request unmergeable. Please resolve the merge conflicts.

@homu
Copy link
Contributor

homu commented Feb 21, 2017

☔ The latest upstream changes (presumably #8584) made this pull request unmergeable. Please resolve the merge conflicts.

This changes the behavior somewhat, but not in ways related to masked arrays
It would previously try to find a suitable dtype for all values, now it just uses
the first dtype.
return result
def wrapped_func(a, *args, **kwargs):
res = func1d(a, *args, **kwargs)
return np.asanyarray(res).view(masked_array)
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I note that the original only returned a masked array when arr had the _mask attribute.

Copy link
Contributor

Choose a reason for hiding this comment

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

To me, it seems to make sense to always return a MaskedArray, since that is more or less the point of having this different function. However, we should not presuppose the output type, i.e., if the output is already a MaskedArray subclass, there is no reason to change it. So, would this be covered by just returning [np.ma.]asanyarray(res)?

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 think you're right, this should be np.ma.asanyarray

@charris
Copy link
Member

charris commented Jun 9, 2017

This is certainly a vast simplification of the original, which seems unduly complicated. However, the results may differ from the original in more than just #8352, which worries me.

@charris
Copy link
Member

charris commented Jun 9, 2017

If this goes in there should also be a mention in the Compatibility section of the release notes.

@charris
Copy link
Member

charris commented Jun 9, 2017

@ahaldane @mhvk, thoughts?

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

I like this, even if it may somewhat change the type of array returned. Though masked subclasses should not be converted.

return result
def wrapped_func(a, *args, **kwargs):
res = func1d(a, *args, **kwargs)
return np.asanyarray(res).view(masked_array)
Copy link
Contributor

Choose a reason for hiding this comment

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

To me, it seems to make sense to always return a MaskedArray, since that is more or less the point of having this different function. However, we should not presuppose the output type, i.e., if the output is already a MaskedArray subclass, there is no reason to change it. So, would this be covered by just returning [np.ma.]asanyarray(res)?

@mhvk
Copy link
Contributor

mhvk commented May 29, 2018

I think here there again was only a trivial fix to be made. Want to do it? It certainly remains a nice cleanup.

p.s. By the way, I was just trying to find your comment about the signature you'd like for gufuncs where the output shape depends on the inputs...

@eric-wieser
Copy link
Member Author

@mhvk: Done

@mhvk
Copy link
Contributor

mhvk commented Jun 14, 2018

The circle-ci failure seems strange; has the setup changed? If so, a rebase would solve it, which also allows to squash the two commits. Anyway, all looks OK otherwise.

@charris
Copy link
Member

charris commented Sep 25, 2018

Let's give this a shot. Eric, could you add a bit to the released notes and squash the commits. Maybe rebase in the process.

@eric-wieser
Copy link
Member Author

My worry is that we've steered people towards np.ma.apply_along_axis to work around #8352, meaning we really ought to provide them with a different workaround before merging this.

@charris
Copy link
Member

charris commented Dec 17, 2020

@eric-wieser What do you want to do with this?

@eric-wieser
Copy link
Member Author

I'll try to revisit this one soon to at least solve the conflicts. I think to actually fix the cutting strings problem we need a way to request a specific dtype, which is something that is impossible to do without creating a new function as apply_along_axis already forwards all **kwargs.

One option would be np.along_axis(f, *args, **kwargs)(arr, axis=..., dtype=..., out=...); of course, this is starting to look a lot like vectorize.

Another quick fix might just be to provide np.apply_along_axis.object, and point users to that function when they have the string cutting issue.

Base automatically changed from master to main March 4, 2021 02:03
@charris charris added the 52 - Inactive Pending author response label Apr 6, 2022
@charris charris closed this Apr 6, 2022
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