glue-viz / glue-astronomy

Plugin to add astronomy-specific functionality to glue
https://glue-astronomy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
11 stars 12 forks source link

Uncertainties failing to round-trip properly from Spectrum1D #24

Closed rosteen closed 3 years ago

rosteen commented 3 years ago

Using the latest version throws an error when retrieving a Spectrum1D in jdaviz (the file is https://data.sdss.org/sas/dr14/sdss/spectro/redux/26/spectra/0751/spec-0751-52251-0160.fits):

---------------------------------------------------------------------------
UnitConversionError                       Traceback (most recent call last)
~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/nddata/nduncertainty.py in parent_nddata(self, value)
    232                 try:
--> 233                     unit_from_data.to(self.unit)
    234                 except UnitConversionError:

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/units/quantity.py in to(self, unit, equivalencies)
    688         unit = Unit(unit)
--> 689         return self._new_view(self._to_value(unit, equivalencies), unit)
    690 

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/units/quantity.py in _to_value(self, unit, equivalencies)
    659             equivalencies = self._equivalencies
--> 660         return self.unit.to(unit, self.view(np.ndarray),
    661                             equivalencies=equivalencies)

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/units/core.py in to(self, other, value, equivalencies)
    986         else:
--> 987             return self._get_converter(other, equivalencies=equivalencies)(value)
    988 

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/units/core.py in _get_converter(self, other, equivalencies)
    917 
--> 918             raise exc
    919 

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/units/core.py in _get_converter(self, other, equivalencies)
    902         try:
--> 903             return self._apply_equivalencies(
    904                 self, other, self._normalize_equivalencies(equivalencies))

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/units/core.py in _apply_equivalencies(self, unit, other, equivalencies)
    885 
--> 886         raise UnitConversionError(
    887             "{} and {} are not convertible".format(

UnitConversionError: 'Angstrom2 cm4 s2 / erg2' and 'erg / (Angstrom cm2 s)' (spectral flux density wav) are not convertible

During handling of the above exception, another exception occurred:

UnitConversionError                       Traceback (most recent call last)
<ipython-input-2-a4b364c1fa48> in <module>
      1 from astropy.utils.data import download_file
      2 fn = download_file('https://data.sdss.org/sas/dr14/sdss/spectro/redux/26/spectra/0751/spec-0751-52251-0160.fits', cache=True)
----> 3 specviz.load_spectrum(fn, "myfile", format="SDSS-III/IV spec")

~/projects/jdaviz/jdaviz/configs/specviz/helper.py in load_spectrum(self, data, data_label, format, show_in_viewer)
     67         self.app.add_data(data, data_label)
     68         if show_in_viewer:
---> 69             self.app.add_data_to_viewer("spectrum-viewer", data_label)
     70 
     71     def get_spectra(self, data_label=None):

~/projects/jdaviz/jdaviz/app.py in add_data_to_viewer(self, viewer_reference, data_label, clear_other_data)
    563         if data_id is not None:
    564             data_ids.append(data_id)
--> 565             self._update_selected_data_items(viewer_item['id'], data_ids)
    566         else:
    567             raise ValueError(

~/projects/jdaviz/jdaviz/app.py in _update_selected_data_items(self, viewer_id, selected_items)
    833                                               viewer_id=viewer_id,
    834                                               sender=self)
--> 835             self.hub.broadcast(add_data_message)
    836 
    837         # Remove any deselected data objects from viewer

~/projects/glue/glue/core/hub.py in broadcast(self, message)
    213             logging.getLogger(__name__).info("Broadcasting %s", message)
    214             for subscriber, handler in self._find_handlers(message):
--> 215                 handler(message)
    216 
    217     def __getstate__(self):

~/projects/jdaviz/jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py in _on_viewer_data_changed(self, msg)
     72         viewer = self.app.get_viewer('spectrum-viewer')
     73 
---> 74         self._viewer_data = self.app.get_data_from_viewer('spectrum-viewer')
     75 
     76         self.dc_items = [layer_state.layer.label

~/projects/jdaviz/jdaviz/app.py in get_data_from_viewer(self, viewer_reference, data_label, cls, include_subsets)
    372 
    373                     if cls is not None:
--> 374                         layer_data = layer_data.get_object(cls=cls,
    375                                                            statistic=statistic)
    376                     # If the shape of the data is 2d, then use CCDData as the

~/projects/glue/glue/core/data.py in get_object(self, cls, **kwargs)
    287         handler, _ = data_translator.get_handler_for(cls)
    288 
--> 289         return handler.to_object(self, **kwargs)
    290 
    291     @property

~/projects/glue-astronomy/glue_astronomy/translators/spectrum1d.py in to_object(self, data_or_subset, attribute, statistic)
    145             [attribute] if not hasattr(attribute, '__len__') else attribute)
    146 
--> 147         return Spectrum1D(**data_kwargs, **kwargs)

~/projects/specutils/specutils/spectra/spectrum1d.py in __init__(self, flux, spectral_axis, wcs, velocity_convention, rest_value, redshift, radial_velocity, bin_specification, **kwargs)
    187             wcs = gwcs_from_array(np.arange(size) * u.Unit(""))
    188 
--> 189         super(Spectrum1D, self).__init__(
    190             data=flux.value if isinstance(flux, u.Quantity) else flux,
    191             wcs=wcs, **kwargs)

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/nddata/nddata.py in __init__(self, data, uncertainty, mask, wcs, meta, unit, copy)
    232         self._unit = unit
    233         # Call the setter for uncertainty to further check the uncertainty
--> 234         self.uncertainty = uncertainty
    235 
    236     def __str__(self):

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/nddata/nddata.py in uncertainty(self, value)
    324                 # to be saved as weakref but that's done by NDUncertainty
    325                 # setter).
--> 326                 value.parent_nddata = self
    327         self._uncertainty = value

~/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/astropy/nddata/nduncertainty.py in parent_nddata(self, value)
    233                     unit_from_data.to(self.unit)
    234                 except UnitConversionError:
--> 235                     raise UnitConversionError("Unit {} of uncertainty "
    236                                               "incompatible with unit {} of "
    237                                               "data".format(self.unit,

UnitConversionError: Unit 1e-17 erg / (Angstrom cm2 s) of uncertainty incompatible with unit 1e-17 erg / (Angstrom cm2 s) of data