Weird interaction between Astropy and the Pickle module
See original GitHub issueDescription
Using the astropy si
function on an astropy quantity
object returns the wrong units after having used the pickle module to read in a dictionary with an astropy quantity
object.
Expected behavior
b = 50*u.GHz
print(b.si)
>>>> <Quantity 5.e+10 1 / s>
Actual behavior
b = 50*u.GHz
print(b.si)
>>>> <Quantity 1.35135135 Ci>
Steps to Reproduce
- Dump a dictionary containing an astropy object to a pickle file
import pickle
import astropy.units as u
a = {'test': 1*u.GHz }
with open('dictionary.pkl', 'wb') as f:
pickle.dump(a, f)
- In a new file use the
si
function on an astropy object after having loaded the pickled dictionary
import pickle
import astropy.units as u
with open('dictionary.pkl', 'rb') as f:
a = pickle.load(f)
b = 50*u.GHz
print(b.si)
>>>> <Quantity 1.35135135 Ci>
This only seems to be an issue when the pickled object contains an astropy quantity. Additionally, if you dump and load in the same file, the si
function works as intended. It appears as if the loading of an astropy object through pickle alters the state of astropy in some way?
I have tried to use the astropy.io.misc functions fnpickle
and fnunpickle
but to no help (I believe these are simply wrappers of the pickle dump and load functions?).
System Details
macOS-10.16-x86_64-i386-64bit Python 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 18:42:56) [Clang 10.0.1 ] Numpy 1.19.4 astropy 4.2.1 Scipy 1.5.3 Matplotlib 3.3.3
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (6 by maintainers)
Top GitHub Comments
OK, fixed! Thanks @MetinSa for reporting (must have been a strange one to find!) and thanks @pllim for the good sleuthing that really helped to narrow it down - another nice collaboration!
Yes, confirmed! What a weird error. The sorting was a very good clue, and it turns out that after the pickle load, the “score” is 0 for all options, because, somehow, the following is no longer true:
i.e., the “s” unit stored in u.si.bases is now different from that on the module – which should never happen…
EDIT: Narrowing it down to the loading