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

Adding support for uncertainty extraction to NDDataArray #86

Merged
merged 6 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 27 additions & 2 deletions glue_astronomy/translators/nddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from glue.core import Data, Subset
from glue.core.coordinates import Coordinates

from .spectrum1d import SpectralCoordinates


def _get_attribute(attribute, data):
if isinstance(attribute, str):
Expand Down Expand Up @@ -53,6 +55,10 @@ def to_data(self, obj):
data = Data(coords=obj.wcs)
data['data'] = obj.data
data.get_component('data').units = str(obj.unit)
if obj.uncertainty is not None:
data['uncertainty'] = getattr(
obj.uncertainty, 'array', obj.uncertainty
)
data.meta.update(obj.meta)
return data

Expand All @@ -70,20 +76,39 @@ def to_object(self, data_or_subset, attribute=None):

data, subset_state = _get_data_and_subset_state(data_or_subset)

if isinstance(data.coords, WCS) or isinstance(data.coords, BaseHighLevelWCS):
if isinstance(data.coords, (WCS, BaseHighLevelWCS, SpectralCoordinates)):
wcs = data.coords
elif type(data.coords) is Coordinates or data.coords is None:
wcs = None
else:
raise TypeError('data.coords should be an instance of Coordinates or WCS')

component_labels = [d.label for d in data.component_ids()]
if attribute is None:
for desired_label in ('data', 'flux'):
if desired_label in component_labels:
attribute = desired_label
break

attribute = _get_attribute(attribute, data)
component = data.get_component(attribute)
values = data.get_data(attribute)
values, mask = _get_value_and_mask(subset_state, data, values)

if 'uncertainty' in component_labels:
uncertainty = StdDevUncertainty(
data.get_component('uncertainty').data
)
else:
uncertainty = None

result = NDDataArray(
values, unit=component.units, mask=mask, wcs=wcs, meta=data.meta
values,
unit=component.units,
mask=mask,
wcs=wcs,
meta=data.meta,
uncertainty=uncertainty
)

return result
Expand Down
16 changes: 16 additions & 0 deletions glue_astronomy/translators/tests/test_nddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ def test_from_nddata(cls, kwargs, data_attr):
assert image_new.unit is u.Jy


def test_nddata_uncertainty():
data = [[2, 3], [4, 5]] * u.Jy
uncertainty = StdDevUncertainty(np.ones_like(data))
spec = NDDataArray(
data=data,
uncertainty=uncertainty
)
data_collection = DataCollection()
data_collection['data'] = spec

data = data_collection['data']
spec_new = data.get_object(NDDataArray)
assert isinstance(spec_new.uncertainty, StdDevUncertainty)
assert_equal(spec_new.uncertainty.array, uncertainty.array)
bmorris3 marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize('with_wcs', (False, True))
def test_from_ccddata(with_wcs):

Expand Down