Skip to content

Make unwrap a ufunc #9959

Closed
Closed

Description

@mcdevitts

This would allow unwrap to work on subclasses of ndarrays. deg2rad, rad2deg (etc.) are all ufuncs, so it's not readily apparent why unwrap is not a ufunc.

Activity

  1. eric-wieser commented on Nov 4, 2017

    @eric-wieser
    Member

    Is there any need for it to be a ufunc? Or do you just want it to work on subclasses?

  2. njsmith commented on Nov 4, 2017

    @njsmith
    Member
  3. mcdevitts commented on Nov 5, 2017

    @mcdevitts
    Author

    My potentially poorly informed understanding was that it needs to be a ufunc for it to work in subclasses. Ignoring that point, from a user standpoint unwrap is used commonly with angle, deg2rad, and rad2deg, so it's rather odd that unwrap would not behave in a similar manner. Granted, this is entirely from an outsider's point of view.

    When I first started using MaskedArray's I hit this issue. Everything was working for taking the angle of a complex dataset up until I had to unwrap it.

  4. eric-wieser commented on Nov 5, 2017

    @eric-wieser
    Member

    @njsmith: unwrap can be modeled as a ufunc.accumulate though, with an inner loop something like

    def unwrap_inner(a, b):
        return a + ieeeremainder(b - a, pi)
    

    I don't understand the meaning of the discont argument though

  5. mhvk commented on Nov 5, 2017

    @mhvk
    Contributor

    It does seem this can be made to work on subclasses by replacing asarray with asanyarray, and replacing the copyto stanzas with settings corresponding slices (as copyto is not subclass-safe; that might in fact be a different function that could do with using a "selection ufunc"!)

  6. njsmith commented on Nov 5, 2017

    @njsmith
    Member
  7. mhvk commented on Nov 5, 2017

    @mhvk
    Contributor

    I couldn't agree more that __array_ufunc__ is better; just not sure that is a particularly easy way here (accumulate might work, but what is the actual ufunc, then? the gufunc is more logical, but needs my axis PR to be able to be a proper replacement).

  8. charris commented on Nov 5, 2017

    @charris
    Member

    just not sure that is a particularly easy way here

    The current ufunc setup is pretty constraining with all the various methods. We should take another look at the design at some point.

  9. mhvk commented on Nov 5, 2017

    @mhvk
    Contributor

    I think a gufunc is a good option, though. The only issue is that it needs an axis argument -- though we could wing that by using the current wrapper for that.

  10. eric-wieser commented on Nov 5, 2017

    @eric-wieser
    Member

    It would be relatively straightforward to change unwrap to not downcast to ndarray, and use __setitem__ instead of copy to, which would fix most subclass use.

    You're still not going to get meaningful results for masked arrays with that though - it's not even clear to me what the correct meaning is.

  11. mcdevitts commented on Feb 19, 2018

    @mcdevitts
    Author

    Masked arrays were just an example of how unwrap would not preserve the subclassing. I'm not expecting unwrap to work for masked arrays without modification. More, unwrap, just happens to be a very common operation with working with phasors over frequency, and all the other functions preserve the subclass.

    Is this fix going to be incorporated? I hope it will.

  12. mattip commented on Jun 13, 2018

    @mattip
    Member

    The only issue is that it needs an axis argument

    That is now merged, should we make unwrap a gufunc? It would seem to fit the template of a ufunc more than a generic array operation since it has a well defined inner loop

  13. mhvk commented on Jun 13, 2018

    @mhvk
    Contributor

    That now is definitely possible!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions