def to_data(self, obj): coords = SpectralCoordinates(obj.spectral_axis) data = Data(coords=coords) data['flux'] = obj.flux data.get_component('flux').units = str(obj.unit) data.meta.update(obj.meta) return data
def test_to_spectrum1d_with_spectral_coordinates(): coords = SpectralCoordinates([1, 4, 10] * u.micron) data = Data(label='spectrum1d', coords=coords) data.add_component(Component(np.array([3, 4, 5]), units='Jy'), 'x') assert_allclose(data.coords.pixel2world([0, 0.5, 1, 1.5, 2]), [[1, 2.5, 4, 7, 10]]) spec = data.get_object(Spectrum1D, attribute=data.id['x']) assert_quantity_allclose(spec.spectral_axis, [1, 4, 10] * u.micron) assert_quantity_allclose(spec.flux, [3, 4, 5] * u.Jy)
def to_data(self, obj): coords = SpectralCoordinates(obj.spectral_axis) data = Data(coords=coords) data['flux'] = obj.flux data[ 'uncertainty'] = obj.uncertainty.array if obj.uncertainty is not None else np.ones( obj.flux.shape) data['mask'] = obj.mask if hasattr( obj, 'mask') and obj.mask is not None else np.zeros( obj.flux.shape).astype(bool) data.get_component('flux').units = str(obj.unit) data.get_component('uncertainty').units = str(obj.unit) data.meta.update(obj.meta) return data
def to_data(self, obj): coords = SpectralCoordinates(obj.spectral_axis) data = Data(coords=coords) data['flux'] = obj.flux data.get_component('flux').units = str(obj.unit) # Include uncertainties if they exist if obj.uncertainty is not None: data['uncertainty'] = obj.uncertainty.quantity data.get_component('uncertainty').units = str(obj.uncertainty.unit) data.meta.update( {'uncertainty_type': obj.uncertainty.uncertainty_type}) # Include mask if it exists if obj.mask is not None: data['mask'] = obj.mask data.meta.update(obj.meta) return data
def to_data(self, obj): # Glue expects spectral axis first for cubes (opposite of specutils). # Swap the spectral axis to first here. to_object doesn't need this because # Spectrum1D does it automatically on initialization. if len(obj.flux.shape) == 3: data = Data(coords=obj.wcs.swapaxes(-1, 0)) data['flux'] = np.swapaxes(obj.flux, -1, 0) data.get_component('flux').units = str(obj.unit) else: if obj.flux.ndim == 1 and obj.wcs.world_n_dim == 1 and isinstance( obj.wcs, GWCS): data = Data(coords=SpectralCoordinates(obj.spectral_axis)) elif obj.flux.ndim == 2 and obj.wcs.world_n_dim == 1: data = Data(coords=PaddedSpectrumWCS(obj.wcs)) else: data = Data(coords=obj.wcs) data['flux'] = obj.flux data.get_component('flux').units = str(obj.unit) # Include uncertainties if they exist if obj.uncertainty is not None: if len(obj.flux.shape) == 3: data['uncertainty'] = np.swapaxes(obj.uncertainty.quantity, -1, 0) else: data['uncertainty'] = obj.uncertainty.quantity data.get_component('uncertainty').units = str(obj.uncertainty.unit) data.meta.update( {'uncertainty_type': obj.uncertainty.uncertainty_type}) # Include mask if it exists if obj.mask is not None: if len(obj.flux.shape) == 3: data['mask'] = np.swapaxes(obj.mask, -1, 0) else: data['mask'] = obj.mask data.meta.update(obj.meta) return data
def test_to_spectrum1d_default_attribute(): coords = SpectralCoordinates([1, 4, 10] * u.micron) data = Data(label='spectrum1d', coords=coords) with pytest.raises(ValueError) as exc: data.get_object(Spectrum1D) assert exc.value.args[0] == 'Data object has no attributes.' data.add_component(Component(np.array([3, 4, 5]), units='Jy'), 'x') spec = data.get_object(Spectrum1D) assert_quantity_allclose(spec.flux, [3, 4, 5] * u.Jy) data.add_component(Component(np.array([3, 4, 5]), units='Jy'), 'y') with pytest.raises(ValueError) as exc: data.get_object(Spectrum1D) assert exc.value.args[0] == ('Data object has more than one attribute, so ' 'you will need to specify which one to use as ' 'the flux for the spectrum using the attribute= ' 'keyword argument.')