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

translator for specreduce Trace objects #72

Merged
merged 8 commits into from
Aug 17, 2022
Merged
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
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
0.4.1 (unreleased)
0.5.0 (unreleased)
------------------

- Updated ``AstropyRegions`` translator to export ``roi.theta`` angle
(supported as of ``glue`` 1.5.0). [#73]

- Added support to import and export specreduce Trace objects. [#72]

0.4.0 (2022-04-07)
------------------

Expand Down
3 changes: 3 additions & 0 deletions docs/translators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ data classes. At this time, the following data classes are supported:
* :class:`~astropy.nddata.CCDData` for spectra (from the `astropy.nddata
<https://docs.astropy.org/en/stable/nddata/>`_ sub-package)

* :class `~specreduce.tracing.Trace` for 2D spectral traces (from the `specreduce
<https://specreduce.readthedocs.io>`_ package)

Working with these classes is described in `Datasets and subsets`_ below. In
addition, this plugin defines ways for glue selections to be translated to
Astropy `regions <https://astropy-regions.readthedocs.io>`_, as described in
Expand Down
1 change: 1 addition & 0 deletions glue_astronomy/translators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import regions # noqa
from . import spectral_cube # noqa
from . import spectrum1d # noqa
from . import trace # noqa
29 changes: 29 additions & 0 deletions glue_astronomy/translators/tests/test_trace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np

from specreduce import tracing
from specreduce.utils.synth_data import make_2dspec_image

from glue.core import Data, DataCollection


def test_trace():

image = make_2dspec_image()
trace = tracing.FlatTrace(image, 5)

data_collection = DataCollection()

data_collection['dc_trace'] = trace
data = data_collection['dc_trace']
assert isinstance(data, Data)
kecnry marked this conversation as resolved.
Show resolved Hide resolved
assert np.all(data['trace'] == trace.trace)

trace_from_data = data.get_object()
assert isinstance(trace_from_data, tracing.FlatTrace)

# now edit the glue data object, this should now map to an ArrayTrace (instead of a FlatTrace)
new_trace = np.ones_like(trace.trace)
data.update_components({data.get_component('trace'): new_trace})
trace_from_data = data.get_object()
assert isinstance(trace_from_data, tracing.ArrayTrace)
assert np.all(trace_from_data.trace == new_trace)
48 changes: 48 additions & 0 deletions glue_astronomy/translators/trace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np

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

from specreduce.tracing import Trace, ArrayTrace


@data_translator(Trace)
class TraceHandler:

def to_data(self, obj):
"""
Convert a specreduce Trace object to a glue Data object

Parameters
----------
obj : `specreduce.tracing.Trace`
The Trace object to convert
"""
data = Data(trace=obj.trace)
if hasattr(obj, 'meta'):
data.meta.update(obj.meta)
data.meta['Trace'] = obj
return data

def to_object(self, data, attribute=None):
"""
Convert a glue Data object to a Trace object.

Parameters
----------
data : `glue.core.data.Data`
The data to convert to a Trace object
attribute : `glue.core.component_id.ComponentID`
The attribute to use for the Trace data
"""

if isinstance(data, Subset):
raise NotImplementedError("Cannot convert subset to Trace object.")
if not isinstance(data.meta.get('Trace'), Trace):
raise TypeError("data is not a valid specreduce Trace object.")

trace = data.meta['Trace']
if not np.all(trace.trace[1] == data['trace']):
trace = ArrayTrace(trace.image, data['trace'])

return trace
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ install_requires =
glue-core>=1.0
regions>=0.4
specutils>=0.7
specreduce>=1.0.0
spectral-cube>=0.6.0

[options.extras_require]
Expand Down