def test_vrad(self): """Testing component vs spectrum with some vrad shift.""" # create spectrum wave = np.linspace(4500, 9300, 3500) spec = Spectrum(flux=np.random.rand(3500), wave=wave) # create component cmp = StarComponent(spec) # redshift original spectrum spec.redshift(100.) # create params and interpolate spec_cmp = cmp(v=100., sig=0.) # test for equal assert np.array_equal(spec.wave, spec_cmp.wave) assert np.array_equal(spec.flux, spec_cmp.flux)
def test_load(self): """Test loading a filter.""" # load it via Filter f = Filter('V', path=data_filename('filters')) # load it as Spectrum s = Spectrum.load(data_filename('filters/V.txt')) # should be identical assert np.array_equal(f.wave, s.wave) assert np.array_equal(f.throughput, s.flux)
def test_squared(self): # filename for spectrum filename = data_filename('spectra/ngc6397id000010554jd2456865p5826f000.fits') # load spectrum and sigma spec = Spectrum.load(filename) sigma = fits.getdata(filename, 'SIGMA') # create weight weight = WeightFromSigma(squared=True) # compare diff = weight(spec, filename) - 1./sigma**2 assert np.max(np.abs(diff)) < 1e-5
def test_restframe(self): """Testing component vs spectrum.""" # create spectrum wave = np.linspace(4500, 9300, 3500) spec = Spectrum(flux=np.random.rand(3500), wave=wave) # create component cmp = StarComponent(spec) # create params and interpolate spec_cmp = cmp(v=0., sig=0.) # test for equal assert np.array_equal(spec.wave, spec_cmp.wave) assert np.array_equal(spec.flux, spec_cmp.flux)
def test_default(self): # filename for spectrum filename = data_filename( 'spectra/ngc6397id000010554jd2456865p5826f000.fits') # load spectrum and sigma spec = Spectrum.load(filename) # create weight weight = WeightRanges(ranges=[(4841., 4881., 10.)], initial=1.) w = weight(spec, filename) # first pixel should be one assert w[0] == 1. # get pixel at 4861AA and test it i = spec.index_of_wave(4861) assert w[i] == 10.
def test_apply(self): """Test applying a filter.""" # load it via Filter f = Filter('V', path=data_filename('filters')) # load spectrum, fix flux (which is given in 1e20erg/s/cm/cm2) and resample filter s = Spectrum.load( data_filename('spectra/ngc6397id000010554jd2456865p5826f000.fits')) s /= 1e20 f.resample(spec=s, inplace=True) # calculate V mag as ST and VEGAMAG st = f.stmag(s) vega = f.vegamag(s) # F606W mag is 13.17, V mag should be close to 13.4 assert abs(st - 13.4) < 0.1 assert abs(vega - 13.4) < 0.1
def test_resample(self): """Test resampling a filter.""" # load it via Filter f = Filter('V', path=data_filename('filters')) # integrate it int1 = np.trapz(f.throughput, f.wave) # load spectrum and resample filter s = Spectrum.load( data_filename('spectra/ngc6397id000010554jd2456865p5826f000.fits')) f.resample(spec=s, inplace=True) # integrate it again int2 = np.trapz(f.throughput, f.wave) # integral should not have changed significantly assert abs(int1 - int2) < 1
def test_truediv(self, spectrum): """Tests __mul__ method. Checking, whether we can divide a spectrum by a number, an array, and another spectrum.""" # divide by number result = spectrum / 10. assert np.array_equal(result, spectrum.flux / 10.) # divide by array div = np.arange(1., 10., 1.) result = spectrum / div assert np.array_equal(result, spectrum.flux / div) # divide by spectrum other = Spectrum(flux=div, wave=spectrum.wave, wave_mode=spectrum.wave_mode) result = spectrum / other assert np.array_equal(result, spectrum.flux / other.flux)
def test_add(self, spectrum): """Tests __add__ method. Checking, whether we can add a spectrum to a number, an array, and another spectrum.""" # add number result = spectrum * 10. assert np.array_equal(result, spectrum.flux * 10.) # add array div = np.arange(1., 10., 1.) result = spectrum + div assert np.array_equal(result, spectrum.flux + div) # add spectrum other = Spectrum(flux=div, wave=spectrum.wave, wave_mode=spectrum.wave_mode) result = spectrum + other assert np.array_equal(result, spectrum.flux + other.flux)
def test_mul(self, spectrum): """Tests __mul__ method. Checking, whether we can multiply a spectrum with a number, an array, and another spectrum.""" # multiply with number result = spectrum * 10. assert np.array_equal(result, spectrum.flux * 10.) # multiply with array div = np.arange(1., 10., 1.) result = spectrum * div assert np.array_equal(result, spectrum.flux * div) # multiply with spectrum other = Spectrum(flux=div, wave=spectrum.wave, wave_mode=spectrum.wave_mode) result = spectrum * other assert np.array_equal(result, spectrum.flux * other.flux)
def spec_grid_1d(): # create some random spectra wave = np.linspace(4500, 9300, 3500) specs = { (5500., ): Spectrum(flux=np.random.rand(3500), wave=wave), (5600., ): Spectrum(flux=np.random.rand(3500), wave=wave), (5700., ): Spectrum(flux=np.random.rand(3500), wave=wave), (5800., ): Spectrum(flux=np.random.rand(3500), wave=wave), (5900., ): Spectrum(flux=np.random.rand(3500), wave=wave), (6000., ): Spectrum(flux=np.random.rand(3500), wave=wave) } # define axis ax = GridAxis(name='Teff', values=[5500, 5600, 5700, 5800, 5900, 6000]) # return new grid return ValuesGrid([ax], specs)
def _fit_func(self, params: Parameters) -> np.ndarray: """Fit function for LM optimization Args: params: Parameters to evaluate. Returns: Weights scaled residuals between spec and model. """ try: # evaluate params self._model = self._get_model(params) # calc residuals return (self._model.flux[self._valid] - self._spec.flux[self._valid]) * self._weight[self._valid] except (KeyError, pd.core.indexing.IndexingError, ValueError): # could not interpolate self.log.exception('Could not interpolate model.') self._model = Spectrum(spec=self._spec) self._model.flux[:] = 0 return np.ones((len(self._spec.flux[self._valid]))) * 1e100
def spectrum(): wave = np.arange(1., 10., 1.) flux = np.arange(1001., 1010., 1.) yield Spectrum(flux=flux, wave=wave, wave_mode=Spectrum.Mode.LAMBDA)
def spectrum(): wave = np.arange(1, 10, 1) flux = np.random.uniform(0, 1, len(wave)) yield Spectrum(flux=flux, wave=wave, wave_mode=Spectrum.Mode.LAMBDA)