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

Data translation tests #60

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions jdaviz/tests/test_data_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import numpy as np

from astropy import units as u
from astropy.io import fits
from astropy.units import Quantity
from astropy.units.quantity import allclose

from glue.config import data_translator
from glue.core import Data, DataCollection

from specutils import Spectrum1D


@data_translator(Spectrum1D)
class Spectrum1DHandler:
def to_data(self, obj):
data = Data()
data['spectral_axis'] = obj.spectral_axis
data['flux'] = obj.data
data.get_component('flux').units = str(obj.flux.unit)
data.meta['s1d'] = obj
return data

def to_object(self, data):
try:
result = data.meta['s1d']
except KeyError:
result = data.label
return result


def test_translation():
input_flux = Quantity(np.array([0.2, 0.3, 2.2, 0.3]), u.Jy)
input_spaxis = Quantity(np.array([1, 2, 3, 4]), u.micron)
spec = Spectrum1D(input_flux, spectral_axis=input_spaxis)

dc = DataCollection()

# Translate Spectrum1D -> Data
dc['spec'] = spec

assert len(dc) == 1

assert(isinstance(dc.data[0], Data))
spectrum = dc.data[0].meta['s1d']
assert(isinstance(spectrum, Spectrum1D))

# we could check more of the internals.
allclose(spectrum.flux, input_flux, atol=1e-5*u.Jy)
allclose(spectrum.spectral_axis, input_spaxis, atol=1e-5*u.micron)

# Translate back Data -> Spectrum1D
spectrum = dc['spec'].get_object()

assert(isinstance(spectrum, Spectrum1D))

# we could check more of the internals.
allclose(spectrum.flux, input_flux, atol=1e-5*u.Jy)
allclose(spectrum.spectral_axis, input_spaxis, atol=1e-5*u.micron)


def test_translation_real_data():

# The description on issue JDAT-136 reads:
#
# "Doing the operation over a spectrum object that's been read
# into glue-jupyter by converting back into a spectrum, doing
# the specutils, and then putting it back into the data collection."
#
# Taking it literally, we should expect that something like this
# would be possible:
#
# >>> data = Data('https://dr14.sdss.org/optical/spectrum/view/...
# >>> spec = data.get_object(cls=Spectrum1D)
# >>> assert(isinstance(spec, Spectrum1D))
#
# That is not possible because the internal reader in Data doesn't
# have access to a data translator.
#
# Instead, we repeat here more or less the same test as above.

f = fits.open('https://dr14.sdss.org/optical/spectrum/view/data/format=fits/spec=lite?plateid=1323&mjd=52797&fiberid=12')
specdata = f[1].data
f.close()

flux_unit = u.Unit('erg cm-2 s-1 AA-1')
spaxis_unit = u.Unit('Angstrom')

lamb = 10 ** specdata['loglam'] * spaxis_unit
flux = specdata['flux'] * 10 ** -17 * flux_unit
spec = Spectrum1D(spectral_axis=lamb, flux=flux)

data = Data(spec)

spectrum = data.get_object(cls=Spectrum1D)
assert(isinstance(spectrum, Spectrum1D))

allclose(spectrum.flux, flux, atol=1e-5*flux_unit)
allclose(spectrum.spectral_axis, lamb, atol=1e-5*spaxis_unit)
130 changes: 130 additions & 0 deletions notebooks/test_data_translation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from specutils import Spectrum1D\n",
"from astropy import units as u\n",
"from glue.config import data_translator\n",
"from glue.core import Data, DataCollection"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"@data_translator(Spectrum1D)\n",
"class Spectrum1DHandler:\n",
" def to_data(self, obj):\n",
" data = Data()\n",
" data['spectral_axis'] = obj.spectral_axis\n",
" data['flux'] = obj.data\n",
" data.get_component('flux').units = str(spec.flux.unit)\n",
" data.meta['s1d'] = obj\n",
" return data\n",
"\n",
" def to_object(self, data):\n",
" return data.meta['s1d']"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"spec = Spectrum1D([0.2, 0.3, 2.2, 0.3] * u.Jy, spectral_axis=[1, 2, 3, 4] * u.micron)\n",
"dc = DataCollection()\n",
"dc['spec'] = spec "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DataCollection (1 data set)\n",
"\t 0: spec\n"
]
}
],
"source": [
"print(dc)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data Set: spec\n",
"Number of dimensions: 1\n",
"Shape: 4\n",
"Main components:\n",
" - spectral_axis\n",
" - flux [Jy]\n",
"Coordinate components:\n",
" - Pixel Axis 0 [x]\n"
]
}
],
"source": [
"print(dc['spec'])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Spectrum1D(flux=<Quantity [0.2, 0.3, 2.2, 0.3] Jy>, spectral_axis=<Quantity [1., 2., 3., 4.] micron>)>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dc['spec'].get_object()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}