Skip to content
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

Serialization of Spectrum1D object (How to make Spectrum1D pickle-able?) #1085

Open
MichalSteiner opened this issue Sep 27, 2023 · 0 comments

Comments

@MichalSteiner
Copy link

Hello everyone,

I struggle with serialization (through the pickle or dill library) of the sp.Spectrum1D object. In particular, the default wcs is not pickle-able. I use serialization to both save the data as pickle file and for multiprocessing of several functions, so this functionality would be helpful, especially since I actually don't really care about the sp.Spectrum1D.wcs attribute.

I know it's possible to bypass this by temporarily removing the wcs with spectrum.wcs = None, or using the high-level API WCS class. However, the first solution is a patchwork solution that doesn't solve the fundamental issue (the default wcs is re-added at some point in my pipeline). I think it is possible to setup the high-level WCS for spectral data, but I am unsure how to do it properly. Right now, I am using the spam CTYPE of WCS to do this, but this resets the spectrum.spectral_axis to pixel array in several functions, which is something I would like to avoid.

Is there a workaround that would fix this issue altogether? I think the easiest way is to create the WCS correctly, but unfortunately, the WCS documentation is not super helpful for the spectral data. As far as I gathered, since my spectral_axis is not linear, I should use the tabulated option, based on this paper, Greisen et al. (2006), section 6. Is there some convenience function in specutils that creates high-level WCS given a spectral_axis?

Minimal example:

import dill # Pickle library
import numpy as np
import specutils as sp
# Mock spectrum
spectrum = sp.Spectrum1D(spectral_axis = np.arange(10)*u.AA,
                              flux = np.arange(10)*u.dimensionless_unscaled
                              )
print('Pickleable :' , dill.pickles(spectrum))

# Remove wcs
spectrum.wcs = None
print('Pickleable :' , dill.pickles(spectrum))

# Mock spectrum with high-level API WCS of ctype = 'SPAM'
wcs = WCS(naxis=1)
wcs.wcs.ctype = ['SPAM']
spectrum = sp.Spectrum1D(spectral_axis = np.arange(10)*u.AA,
                         flux = np.arange(10)*u.dimensionless_unscaled,
                         wcs = wcs
                         )
print('Pickleable :' , dill.pickles(spectrum))



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant