def test_component_unit_header(tmpdir): from astropy import units as u filename = tmpdir.join('test3.fits').strpath data = Data(x=np.arange(6).reshape(2, 3), y=(np.arange(6) * 2).reshape(2, 3), z=(np.arange(6) * 2).reshape(2, 3)) wcs = WCS() data.coords = WCSCoordinates(wcs=wcs) unit1 = data.get_component("x").units = u.m / u.s unit2 = data.get_component("y").units = u.Jy unit3 = data.get_component("z").units = "" fits_writer(filename, data) with fits.open(filename) as hdulist: assert len(hdulist) == 3 bunit = hdulist['x'].header.get('BUNIT') assert u.Unit(bunit) == unit1 bunit = hdulist['y'].header.get('BUNIT') assert u.Unit(bunit) == unit2 bunit = hdulist['z'].header.get('BUNIT') assert bunit == unit3
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) data.meta.update(obj.meta) return data
def to_data(self, obj): if obj.wcs is None: coords = None else: coords = WCSCoordinates(wcs=obj.wcs) data = Data(coords=coords) data['data'] = obj.data data.get_component('data').units = str(obj.unit) data.meta.update(obj.meta) return data
def vue_collapse(self, *args, **kwargs): # Collapsing over the spectral axis. Cut out the desired spectral # region. Defaults to the entire spectrum. spec_min = float(self.spectral_min) * u.Unit(self.spectral_unit) spec_max = float(self.spectral_max) * u.Unit(self.spectral_unit) with warnings.catch_warnings(): warnings.filterwarnings('ignore', message='No observer defined on WCS') spec = spectral_slab(self._selected_cube, spec_min, spec_max) # Spatial-spatial image only. collapsed_spec = spec.collapse(self.selected_func.lower(), axis=-1).T # Quantity data = Data() data['flux'] = collapsed_spec.value data.get_component('flux').units = str(collapsed_spec.unit) self._label_counter += 1 label = f"Collapsed {self._label_counter} {self._selected_data.label}" self.data_collection[label] = data # Link the new dataset pixel-wise to the original dataset. In general # direct pixel to pixel links are the most efficient and should be # used in cases like this where we know there is a 1-to-1 mapping of # pixel coordinates. # Spatial-spatial image only. pix_id_1 = self._selected_data.pixel_component_ids[ 0] # Pixel Axis 0 [z] pix_id_1c = self.data_collection[label].pixel_component_ids[ 0] # Pixel Axis 0 [y] pix_id_2 = self._selected_data.pixel_component_ids[ 1] # Pixel Axis 1 [y] pix_id_2c = self.data_collection[label].pixel_component_ids[ 1] # Pixel Axis 1 [x] self.data_collection.add_link( [LinkSame(pix_id_1, pix_id_1c), LinkSame(pix_id_2, pix_id_2c)]) snackbar_message = SnackbarMessage( f"Data set '{self._selected_data.label}' collapsed successfully.", color="success", sender=self) self.hub.broadcast(snackbar_message) # Spatial-spatial image only. if self.selected_viewer != 'None': # replace the contents in the selected viewer with the results from this plugin self.app.add_data_to_viewer(self.viewer_to_id.get( self.selected_viewer), label, clear_other_data=True)
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 vue_collapse(self, *args, **kwargs): try: spec = self._selected_data.get_object(cls=SpectralCube) except AttributeError: snackbar_message = SnackbarMessage( "Unable to perform collapse over selected data.", color="error", sender=self) self.hub.broadcast(snackbar_message) return # If collapsing over the spectral axis, cut out the desired spectral # region. Defaults to the entire spectrum. if self.selected_axis == 0: spec_min = float(self.spectral_min) * u.Unit(self.spectral_unit) spec_max = float(self.spectral_max) * u.Unit(self.spectral_unit) spec = spec.spectral_slab(spec_min, spec_max) collapsed_spec = getattr( spec, self.selected_func.lower())(axis=self.selected_axis) data = Data(coords=collapsed_spec.wcs) data['flux'] = collapsed_spec.filled_data[...] data.get_component('flux').units = str(collapsed_spec.unit) data.meta.update(collapsed_spec.meta) self._label_counter += 1 label = f"Collapsed {self._label_counter} {self._selected_data.label}" self.data_collection[label] = data # Link the new dataset pixel-wise to the original dataset. In general # direct pixel to pixel links are the most efficient and should be # used in cases like this where we know there is a 1-to-1 mapping of # pixel coordinates. Here which axes are linked to which depends on # the selected axis. (i1, i2), (i1c, i2c) = AXES_MAPPING[self.selected_axis] pix_id_1 = self._selected_data.pixel_component_ids[i1] pix_id_1c = self.data_collection[label].pixel_component_ids[i1c] pix_id_2 = self._selected_data.pixel_component_ids[i2] pix_id_2c = self.data_collection[label].pixel_component_ids[i2c] self.data_collection.add_link(LinkSame(pix_id_1, pix_id_1c)) self.data_collection.add_link(LinkSame(pix_id_2, pix_id_2c)) snackbar_message = SnackbarMessage( f"Data set '{self._selected_data.label}' collapsed successfully.", color="success", sender=self) self.hub.broadcast(snackbar_message)
def load_data(self, data_filenames): """ Load the data based on the extensions defined in the matching YAML file. THen create the datacube and return it. :param data_filename: :return: """ print("in load data") label = None data = None for data_filename in data_filenames.split(','): hdulist = fits.open(data_filename) print(hdulist) if not label: label = "{}: {}".format(self._name, splitext(basename(data_filename))[0]) data = Data(label=label) # this attribute is used to indicate to the cubeviz layout that # this is a cubeviz-specific data component. data.meta[CUBEVIZ_LAYOUT] = self._name data_coords_set = False for ii, hdu in enumerate(hdulist): if 'NAXIS' in hdu.header and hdu.header['NAXIS'] == 3: # Set the coords based on the first 3D HDU if not data_coords_set: data.coords = coordinates_from_header(hdu.header) data_coords_set = True component_name = str(ii) if 'EXTNAME' in hdu.header: component_name = hdu.header['EXTNAME'] # The data must be floating point as spectralcube is expecting floating point data data.add_component(component=hdu.data.astype(np.float), label=component_name) if 'BUNIT' in hdu.header: c = data.get_component(component_name) c.units = self.get_units(hdu.header) # For the purposes of exporting, we keep a reference to the original HDUList object data._cubeviz_hdulist = hdulist return data
def vue_collapse(self, *args, **kwargs): try: spec = self._selected_data.get_object(cls=SpectralCube) except AttributeError: snackbar_message = SnackbarMessage( f"Unable to perform collapse over selected data.", color="error", sender=self) self.hub.broadcast(snackbar_message) return collapsed_spec = getattr(spec, self.selected_func.lower())( axis=self.selected_axis) data = Data(coords=collapsed_spec.wcs) data['flux'] = collapsed_spec.filled_data[...] data.get_component('flux').units = str(collapsed_spec.unit) data.meta.update(collapsed_spec.meta) label = f"Collapsed {self._selected_data.label}" self.data_collection[label] = data # Link the new dataset pixel-wise to the original dataset. In general # direct pixel to pixel links are the most efficient and should be # used in cases like this where we know there is a 1-to-1 mapping of # pixel coordinates. Here which axes are linked to which depends on # the selected axis. (i1, i2), (i1c, i2c) = AXES_MAPPING[self.selected_axis] self.data_collection.add_link(LinkSame(self._selected_data.pixel_component_ids[i1], self.data_collection[label].pixel_component_ids[i1c])) self.data_collection.add_link(LinkSame(self._selected_data.pixel_component_ids[i2], self.data_collection[label].pixel_component_ids[i2c])) snackbar_message = SnackbarMessage( f"Data set '{self._selected_data.label}' collapsed successfully.", color="success", sender=self) self.hub.broadcast(snackbar_message)
def load_data(self, data_filenames): """ Load the data based on the extensions defined in the matching YAML file. THen create the datacube and return it. :param data_filename: :return: """ label = None data = None ifucube = IFUCube() for data_filename in data_filenames.split(','): hdulist = ifucube.open(data_filename, fix=self._check_ifu_valid) # Good in this case means the file has 3D data and can be loaded by SpectralCube.read if self._check_ifu_valid and not ifucube.get_good(): # Popup takes precedence and accepting continues operation and canceling closes the program self.popup_ui.ifucube_log.setText(ifucube.get_log_output()) self.popup_ui.setModal(True) self.popup_ui.show() self.popup_ui.button_accept.clicked.connect(self._accept_button_click) self.popup_ui.button_cancel.clicked.connect(self._reject_button_click) if not label: label = "{}: {}".format(self._name, splitext(basename(data_filename))[0]) data = Data(label=label) # this attribute is used to indicate to the cubeviz layout that # this is a cubeviz-specific data component. data.meta[CUBEVIZ_LAYOUT] = self._name data_coords_set = False for ii, hdu in enumerate(hdulist): if 'NAXIS' in hdu.header and hdu.header['NAXIS'] == 3: # Set the coords based on the first 3D HDU if not data_coords_set: data.coords = coordinates_from_header(hdu.header) data_coords_set = True component_name = str(ii) if 'EXTNAME' in hdu.header: component_name = hdu.header['EXTNAME'] # The data must be floating point as spectralcube is expecting floating point data data.add_component(component=hdu.data.astype(np.float), label=component_name) if 'BUNIT' in hdu.header: c = data.get_component(component_name) c.units = self.get_units(hdu.header) else: # Creates a unique component name component_name = str(ii) data.add_component(component=hdu.data.astype(np.float), label=component_name) # For the purposes of exporting, we keep a reference to the original HDUList object data._cubeviz_hdulist = hdulist return data
def to_data(self, obj): data = Data(coords=obj.wcs) data['data'] = obj.data data.get_component('data').units = str(obj.unit) data.meta.update(obj.meta) return data
def to_data(self, obj): data = Data(coords=obj.wcs) data['flux'] = obj.filled_data[...] data.get_component('flux').units = str(obj.unit) data.meta.update(obj.meta) return data
def load_data(self, data_filenames): """ Load the data based on the extensions defined in the matching YAML file. THen create the datacube and return it. :param data_filename: :return: """ label = None data = None ifucube = IFUCube() for data_filename in data_filenames.split(','): hdulist = ifucube.open(data_filename, fix=self._check_ifu_valid) # Good in this case means the file has 3D data and can be loaded by SpectralCube.read if self._check_ifu_valid and not ifucube.get_good(): # Popup takes precedence and accepting continues operation and canceling closes the program self.popup_ui.ifucube_log.setText(ifucube.get_log_output()) self.popup_ui.setModal(True) self.popup_ui.show() self.popup_ui.button_accept.clicked.connect( self._accept_button_click) self.popup_ui.button_cancel.clicked.connect( self._reject_button_click) if not label: label = "{}: {}".format(self._name, splitext(basename(data_filename))[0]) data = Data(label=label) # this attribute is used to indicate to the cubeviz layout that # this is a cubeviz-specific data component. data.meta[CUBEVIZ_LAYOUT] = self._name data_coords_set = False for ii, hdu in enumerate(hdulist): if 'NAXIS' in hdu.header and hdu.header['NAXIS'] == 3: # Set the coords based on the first 3D HDU if not data_coords_set: data.coords = coordinates_from_header(hdu.header) data_coords_set = True component_name = str(ii) if 'EXTNAME' in hdu.header: component_name = hdu.header['EXTNAME'] # The data must be floating point as spectralcube is expecting floating point data data.add_component(component=hdu.data.astype(np.float), label=component_name) if 'BUNIT' in hdu.header: c = data.get_component(component_name) c.units = self.get_units(hdu.header) else: # Creates a unique component name component_name = str(ii) data.add_component(component=hdu.data.astype(np.float), label=component_name) # For the purposes of exporting, we keep a reference to the original HDUList object data._cubeviz_hdulist = hdulist return data