-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix for #1942. Added pickling test class for coordinates as well #1961
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
Conversation
|
It looks like this should be combined with #1947, which doesn't include a fix for |
astropy/coordinates/angles.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think one should use super in cases like these, so that if people make a subclass like class myclass(Longitude, AnotherClass), it will continue to work. So, I would suggest replacing this with something along the lines of
object_state = list(super(Longitude, self).__reduce__())
object_state[2] = (object_state[2], self.wrap_angle)
Note that of course now one does not have to include self._unit any more in the pickle, since the super call goes to Quantity, which already includes it (and which should of course also do a super). In __setstate__, one would then have
super_state, own_state = state
super(Longitude, self).__setstate__(super_state)
self.wrap_angle = own_state
|
@ryanfox - as an alternative to the bits of hassle gone through here: It seems to me that all attributes that one might want to pickle are stored in Example: |
astropy/coordinates/angles.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside: probably best to pickle and restore _wrap_angle (i.e., the internal state); I don't think one wants to call a setter while restoring state.
|
p.s. Looking at the pickle documentation [1], I see that indeed by default the contents of [1] http://docs.python.org/2/library/pickle.html#the-pickle-protocol |
|
@mhvk, I originally implemented it with Also I would have thought using the setter would be better as it goes through |
|
@ryanfox - I think one should try to always use As for the setter, it is perhaps a matter of opinion, but my sense is that the pickler's goal should only be to be able to store and retrieve a given object, it should not do any sanity checks, especially not when unpickling (one really doesn't want to have to deal with an error in unpickling because the internal state was inconsistent when the object was pickled). |
|
Oh, but it would be nice to hear from @mdboom if there might be a "correct" way to pickle |
|
I reworked my changes to utilize About sanity checks: I'll buy you should get exactly the same object back out that you put in. Unless there's some compelling reason I don't know about, I think the internal state will suffice. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The git hub view complains about the lack of a newline here (I have set up emacs to complain about such things; see http://docs.astropy.org/en/latest/development/codeguide_emacs.html)
|
@ryanfox - this looks good. I'm still slightly wondering whether it wouldn't be better to store a dictionary as additional information, perhaps even @mdboom, could you have a look as well? |
|
I've made a PR against this one (ryanfox#1) that implements @mhvk's suggestion to just store the whole dict along with the Numpy-specific informaton in reduce. At least locally, the tests still pass. This means subclasses of Quantity don't have to worry about anything special to support pickling, unless they're doing something funky. |
|
@mdboom - I was indeed thinking along those lines. One possible issue I see for the future is when I would, e.g., subclass from both (Alternatively, just as there is a case for having our own wrapped version of |
|
@mhvk: That all makes sense, particularly the possibility of having a local astropy |
…astropy into mdboom-unit/pickling-quantities
|
I haven't been following this closely so I'm not sure about all the ins and outs. But definitely use |
Fix for #1942. Added pickling test class for coordinates as well
|
@ryanfox - thanks! Looks all OK to me, so I'll merge it. |
CHANGES.rst entry for #1961 (Longitude keeping wrap_angle)
Fix for #1942. Added pickling test class for coordinates as well
CHANGES.rst entry for #1961 (Longitude keeping wrap_angle)
Fix for #1942. Added pickling test class for coordinates as well
CHANGES.rst entry for #1961 (Longitude keeping wrap_angle)
Fix for #1942. Added pickling test class for coordinates as well
CHANGES.rst entry for #1961 (Longitude keeping wrap_angle)
Fix for issue #1942. Per http://www.mail-archive.com/numpy-discussion@scipy.org/msg02446.html,
__reduce__and__setstate__must completely specify the state of the object to be (un)pickled. I overrode those methods to account for the extra attribute in Longitude. I also added a basic test class for pickling/unpickling coordinates.