Skip to content

ndarray-like base class #4096

Description

@embray

Although I don't recall exactly what the reasons were, I believe for good reasons the Time class does not subclass numpy.ndarray. Nevertheless, array-like Time objects behave a lot like ndarray in that indexing works as one would expect with an ndarray, and it implements all of the ndarray methods.

The SkyCoord class also does not subclass ndarray for good reasons, but supports similar indexing operations for arrays of coordinates. Unlike Time, it does not support ndarray-like methods. But I feel like it would be good if, so long as we're offering array-like special objects, we should strive to emulate the ndarray interface as consistently as possible with those objects (as we also do with Quantity, which of course is an ndarray subclass). I think not all such methods and attributes are always applicable, but I think this should be a general goal at least. It would be consistent and represent the least astonishment to users.

That's why I'm suggesting a base class that provides stubs for many applicable ndarray attributes and methods. Time and SkyCoord (and any other applicable classes--I'm thinking Model as well) can use this class as a mixin and implement only those interfaces that make sense for the class. To an extent this can be made pretty automatic too--Time has a generic method that knows how to represent a Time as a couple of bare ndarrays, apply the ndarray methods directly, and then reconstitute the result as a Time. So most ndarray methods can be implemented just by implementing a general method(s) for to-from ndarray.

One thing Time is currently missing, which we should also have, is some equivalent of np.concatenate that works with non-ndarrays. Not sure yet exactly how I see that working--maybe as a classmethod.

Activity

  1. mhvk commented on Aug 28, 2015

    @mhvk
    Contributor

    @embray - yes, I think this is a very good idea. As you say, in Time, it turned out that all that was really needed was a way to apply the method to the underlying jd1 and jd1 ndarrays, and then recombining them in a new Time instance. I think it should be quite easy to do the same for SkyCoord.

  2. taldcroft commented on Sep 1, 2015

    @taldcroft
    Member

    I guess my question is how extensive the list of stubs would be. Specifically I'm thinking of SkyCoord where there is no concept of ordering so that all the min / max / sort-based methods would not be implemented.

    I can see this being a useful way to provide all the shape-changing methods where (at least in the Time example) it's clear that there is a pattern that could be factored out.

  3. mhvk commented on Sep 1, 2015

    @mhvk
    Contributor

    It would seem a generalized method that does shape-changing and different views (__getitem__) would make sense as a start. Those are also the ones in Time that do little more than call self._replicate(method, *args, **kwargs), so exactly the ones for which it makes sense to factor it out.

  4. embray commented on Sep 1, 2015

    @embray
    MemberAuthor

    I mean, one could come up with an ordering for SkyCoord, right? It just wouldn't be that useful.

  5. taldcroft commented on Sep 2, 2015

    @taldcroft
    Member

    I don't think it would be a good thing to add methods that we know are not useful.

  6. mhvk commented on Sep 3, 2015

    @mhvk
    Contributor

    See #4123 for an initial trial for just the indexing/shape-changing methods.

  7. mhvk commented on Nov 2, 2015

    @mhvk
    Contributor

    Done for Time -- for SkyCoord, see #4123

  8. mhvk commented on Nov 18, 2016

    @mhvk
    Contributor

    Closing since we now do have ShapedLikeNDArray and it seems to work well!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions