def test_plot_no_mask(self): flux = numpy.random.randn(1000) ivar = (1. / (flux / 100))**2 sp = Spectrum( flux=flux, wavelength=numpy.arange(1000), ivar=ivar, ) sp.plot()
def test_plot_no_std(self): mask = numpy.zeros(1000, dtype=numpy.int) mask[50:100] = 2**10 mask[500:600] = 2**4 sp = Spectrum( flux=numpy.random.randn(1000), wavelength=numpy.arange(1000), mask=mask, pixmask_flag='MANGA_DRP3PIXMASK', ) sp.plot()
def _get_spectrum(self, name): """Returns an `.Spectrum`.""" model = self.datamodel.spectra[name] spec_data = self._get_extension_data(name) if spec_data is None: raise MarvinError('cannot find data for this extension. ' 'Maybe it is not loaded into the DB.') spectrum = Spectrum(spec_data, wavelength=np.array(self._wavelength), std=self._get_extension_data(name, 'std'), unit=model.unit) return spectrum
def spectrum(): """Produces a simple 1D array for datacube testing.""" flux = numpy.arange(1, 1001, dtype=numpy.float32) ivar = (1. / (flux / 100))**2 mask = numpy.zeros(flux.shape, dtype=numpy.int) wave = numpy.arange(1, 1001) mask[50:100] = 2**10 mask[500:600] = 2**4 scale = 1e-3 datacube = Spectrum(flux, wave, ivar=ivar, mask=mask, scale=scale, unit=u.erg / u.s / (u.cm ** 2) / u.Angstrom / spaxel_unit, pixmask_flag='MANGA_DRP3PIXMASK') yield datacube
def _get_spaxel_quantities(self, x, y, spaxel=None): """Returns a dictionary of spaxel quantities.""" modelcube_quantities = FuzzyDict({}) if self.data_origin == 'db': session = marvin.marvindb.session dapdb = marvin.marvindb.dapdb if self.data_origin == 'file' or self.data_origin == 'db': _db_row = None for dm in self.datamodel: data = {'value': None, 'ivar': None, 'mask': None} for key in data: if key == 'ivar' and not dm.has_ivar(): continue if key == 'mask' and not dm.has_mask(): continue if self.data_origin == 'file': extname = dm.fits_extension(None if key == 'value' else key) data[key] = self.data[extname].data[:, y, x] elif self.data_origin == 'db': colname = dm.db_column(None if key == 'value' else key) if not _db_row: _db_row = session.query(dapdb.ModelSpaxel).filter( dapdb.ModelSpaxel.modelcube_pk == self.data.pk, dapdb.ModelSpaxel.x == x, dapdb.ModelSpaxel.y == y).use_cache( self.cache_region).one() data[key] = np.array(getattr(_db_row, colname)) quantity = Spectrum(data['value'], ivar=data['ivar'], mask=data['mask'], wavelength=self._wavelength, unit=dm.unit, pixmask_flag=dm.pixmask_flag) if spaxel: quantity._init_bin(spaxel=spaxel, parent=self, datamodel=dm) modelcube_quantities[dm.full()] = quantity if self.data_origin == 'api': params = {'release': self._release} url = marvin.config.urlmap['api']['getModelCubeQuantitiesSpaxel'][ 'url'] try: response = self._toolInteraction( url.format(name=self.plateifu, x=x, y=y, bintype=self.bintype.name, template=self.template.name, params=params)) except Exception as ee: raise MarvinError( 'found a problem when checking if remote modelcube ' 'exists: {0}'.format(str(ee))) data = response.getData() for dm in self.datamodel: quantity = Spectrum(data[dm.name]['value'], ivar=data[dm.name]['ivar'], mask=data[dm.name]['mask'], wavelength=data['wavelength'], unit=dm.unit, pixmask_flag=dm.pixmask_flag) if spaxel: quantity._init_bin(spaxel=spaxel, parent=self, datamodel=dm) modelcube_quantities[dm.full()] = quantity return modelcube_quantities
def _get_spaxel_quantities(self, x, y, spaxel=None): """Returns a dictionary of spaxel quantities.""" cube_quantities = FuzzyDict({}) if self.data_origin == 'db': session = marvin.marvindb.session datadb = marvin.marvindb.datadb if self.data_origin == 'file' or self.data_origin == 'db': # Stores a dictionary of (table, row) _db_rows = {} for dm in self.datamodel.datacubes + self.datamodel.spectra: data = {'value': None, 'ivar': None, 'mask': None, 'std': None} for key in data: if key == 'ivar': if dm in self.datamodel.spectra or not dm.has_ivar(): continue if key == 'mask': if dm in self.datamodel.spectra or not dm.has_mask(): continue if key == 'std': if dm in self.datamodel.datacubes or not dm.has_std(): continue if self.data_origin == 'file': extname = dm.fits_extension(None if key == 'value' else key) if dm in self.datamodel.datacubes: data[key] = self.data[extname].data[:, y, x] else: data[key] = self.data[extname].data elif self.data_origin == 'db': colname = dm.db_column(None if key == 'value' else key) if dm in self.datamodel.datacubes: if 'datacubes' not in _db_rows: _db_rows['datacubes'] = session.query( datadb.Spaxel).filter( datadb.Spaxel.cube == self.data, datadb.Spaxel.x == x, datadb.Spaxel.y == y).one() spaxel_data = getattr(_db_rows['datacubes'], colname) else: if 'spectra' not in _db_rows: _db_rows['spectra'] = session.query( datadb.Cube).filter( datadb.Cube.pk == self.data.pk).one() spaxel_data = getattr(_db_rows['spectra'], colname, None) # In case the column was empty in the DB. At some point # this can be removed. if spaxel_data is None: warnings.warn( 'cannot find {!r} data for {!r}. ' 'Maybe the data is not in the DB.'.format( colname, self.plateifu), MarvinUserWarning) cube_quantities[dm.name] = None continue data[key] = np.array(spaxel_data) cube_quantities[dm.name] = Spectrum( data['value'], ivar=data['ivar'], mask=data['mask'], std=data['std'], wavelength=self._wavelength, unit=dm.unit, pixmask_flag=dm.pixmask_flag) if self.data_origin == 'api': params = {'release': self._release} url = marvin.config.urlmap['api']['getCubeQuantitiesSpaxel']['url'] try: response = self._toolInteraction( url.format(name=self.plateifu, x=x, y=y, params=params)) except Exception as ee: raise MarvinError( 'found a problem when checking if remote cube ' 'exists: {0}'.format(str(ee))) data = response.getData() for dm in self.datamodel.datacubes + self.datamodel.spectra: if data[dm.name]['value'] is None: warnings.warn( 'cannot find {!r} data for {!r}. ' 'Maybe the data is not in the DB.'.format( dm.name, self.plateifu), MarvinUserWarning) cube_quantities[dm.name] = None continue cube_quantities[dm.name] = Spectrum( data[dm.name]['value'], ivar=data[dm.name]['ivar'], mask=data[dm.name]['mask'], wavelength=data['wavelength'], unit=dm.unit, pixmask_flag=dm.pixmask_flag) return cube_quantities
def test_plot_no_std_no_mask(self): sp = Spectrum(numpy.random.randn(1000), wavelength=numpy.arange(1000)) sp.plot()