def test_get_ml_parameters_wrong_wavelength(data): model = specML.get_model(data=data) flux = data.y.sample(1).values[0] wavelength = np.linspace(1, 2, len(flux)) s = Spectroscopy(wavelength, flux, Wavelength.AA) with pytest.raises(ValueError): s.getMLparams(model)
def test_simple(): wavelength = np.linspace(4000, 9000, 100) flux = np.abs(np.random.random(100)) s = Spectroscopy(wavelength, flux) assert isinstance(s.wavelength, np.ndarray) assert isinstance(s.flux, np.ndarray) assert isinstance(s, Spectroscopy) assert repr(s) == 'Nothing calculated yet. Use "Spectroscopy.getAll()"' s.getAll() assert 'Teff' in repr(s) assert hasattr(s, 'Teff') assert hasattr(s, 'logg') assert hasattr(s, 'feh') assert hasattr(s, 'vmicro') assert hasattr(s, 'vmacro') assert hasattr(s, 'vsini') assert isinstance(s.Teff, int) assert isinstance(s.logg, float) assert isinstance(s.feh, float) assert isinstance(s.vmicro, float) assert isinstance(s.vmacro, float) assert isinstance(s.vsini, float)
def test_range_optical(w0, w1, expected): wavelength = [w0, w1] flux = [1, 2] s = Spectroscopy(wavelength, flux) s.getRange() assert s.range == expected
def test_normalization(data): wavelength = data.get_wavelength() flux = data.y.sample(1).values[0] + 2 s = Spectroscopy(wavelength, flux) s.normalize() assert isinstance(s, Spectroscopy) assert np.all(s.wavelength == wavelength) assert np.any(s.flux != flux)
def test_interpolate_flow(data): model = specML.get_model(data=data) wavelength = data.get_wavelength() wavelength = np.append(wavelength[::2], wavelength[-1]) flux = data.y.sample(1).values[0] flux = np.append(flux[::2], flux[-1]) s = Spectroscopy(wavelength, flux, Wavelength.AA) p = s.getMLparams(model, interpolate=True) assert len(p) == 6
def test_get_ml_parameters(data): model = specML.get_model(data=data) wavelength = data.get_wavelength() flux = data.y.sample(1).values[0] s = Spectroscopy(wavelength, flux, Wavelength.AA) p = s.getMLparams(model) assert len(p) == 6 assert hasattr(s, 'parameters') assert hasattr(s, 'Teff') assert hasattr(s, 'logg') assert hasattr(s, 'feh') assert hasattr(s, 'vmicro') assert hasattr(s, 'vmacro') assert hasattr(s, 'vsini') assert hasattr(s, 'MLmodel')
def test_wavelength_unit(w0, unit, expected): wavelength = [w0, 1000] flux = [1, 2] s = Spectroscopy(wavelength, flux, unit) if unit != Wavelength.ICM: assert s.wavelength[0] == expected else: assert s.wavelength[-1] == expected assert (s.flux == np.array([2, 1])).all() # Bonus test
def test_vmacro(logg): s = Spectroscopy([1, 2], [1, 2]) s.Teff = 5777 s.feh = 0.00 s.logg = logg vmicro = s._get_vmacro() assert isinstance(vmicro, float) assert vmicro >= 0
def test_different_length(wavelength, flux): with pytest.raises(ValueError): Spectroscopy(wavelength, flux)
def test_wavelength_not_sorted_icm(): with pytest.raises(ValueError): Spectroscopy([1, 2], [1, 2], Wavelength.ICM)
def test_wavelength_not_sorted(): with pytest.raises(ValueError): Spectroscopy([2, 1], [42, 21])
def test_flux_zero(): s = Spectroscopy([1], [0]) assert isinstance(s, Spectroscopy)
def test_empty(): with pytest.raises(ValueError): Spectroscopy([], [])
def test_negative_values(wavelength, flux): with pytest.raises(ValueError): Spectroscopy(wavelength, flux)
class Star: name: str spectral: Optional[SpectralType] = None stage: Optional[Stage] = None colourInformation: bool = False seismicInformation: bool = False spectroscopicInformation: bool = False calibrationInformation: bool = False isochroneInformation: bool = False def __post_init__(self): self.stage = getStage(self.spectral) if self.stage is None else self.stage def __add__(self, other) -> None: raise ValueError('Can not add together two stars') def __repr__(self) -> str: info = '#' * 30 + '\n' info += f'## Star: {self.name}\n' if self.stage is not None: info += f'## Evolutionary stage: {self.stage.value}\n' if self.spectral is not None: info += f'## Spectral type: {self.spectral.value["name"]}\n' if self.colourInformation: info += f'# \n# Colour - Teff={self.colour.Teff}K\n' info += f'# Colour - logg={self.colour.logg}dex\n' info += f'# Colour - [Fe/H]={self.colour.feh}dex\n' if self.seismicInformation: info += f'# \n# Seismology - density={self.seismic.density}g/cm3\n' info += f'# Seismology - logg={self.seismic.logg}dex\n' info += f'# Seismology - mass={self.seismic.mass}Msun\n' info += f'# Seismology - radius={self.seismic.radius}Rsun\n' if self.spectroscopicInformation: info += f'# \n# Spectroscopy - Teff={self.spectroscopic.Teff}K\n' info += f'# Spectroscopy - logg={self.spectroscopic.logg}dex\n' info += f'# Spectroscopy - [Fe/H]={self.spectroscopic.feh}dex\n' info += f'# Spectroscopy - vmicro={self.spectroscopic.vmicro}km/s\n' info += f'# Spectroscopy - vmacro={self.spectroscopic.vmacro}km/s\n' info += f'# Spectroscopy - vsini={self.spectroscopic.vsini}km/s\n' if self.calibrationInformation: info += f'# \n# Calibration - mass={self.calibration.mass}Msun\n' info += f'# Calibration - radius={self.calibration.radius}Rsun\n' if self.isochroneInformation: info += f'# \n# Isochrone - mass={self.isochrone.mass}({self.isochrone.masserr})Msun\n' info += f'# Isochrone - radius={self.isochrone.radius}({self.isochrone.radiuserr})Rsun\n' info += f'# Isochrone - age={self.isochrone.age}({self.isochrone.ageerr})Gyr\n' info += '#' * 30 + '\n' return info def getColourInformation(self, method: str='Ramirez05', *args, **kwargs): print(f'Calculating colour information from method based on: {method}') self.colour = Colour(method, *args, **kwargs) self.colour.getAll() self.colourInformation = True print('Values can be reached through: Star.colour.') def getSeismicInformation(self, vmax: float, deltav: float, Teff: int) -> None: self.seismic = Seismology(vmax, deltav, Teff) self.seismic.getAll() self.seismicInformation = True print('Values can be reached through: Star.seismic.') def getSpectroscopicInformation(self, wavelength: listLikeType, flux: listLikeType) -> None: self.spectroscopic = Spectroscopy(wavelength, flux) self.spectroscopic.getAll() self.spectroscopicInformation = True print('Values can be reached through: Star.spectroscopic.') def getCalibration(self, Teff: Optional[int]=None, logg: Optional[float]=None, feh: Optional[float]=None) -> None: val_pystar.check_calibration_input(self.spectroscopicInformation, Teff, logg, feh) Teff = self.spectroscopic.Teff if Teff is None else Teff logg = self.spectroscopic.logg if logg is None else logg feh = self.spectroscopic.feh if feh is None else feh self.calibration = Calibrations(Teff, logg, feh) self.calibration.getAll() self.calibrationInformation = True print('Values can be reached through: Star.calibration.') def getIsochrone(self, Teff: Optional[int]=None, logg: Optional[float]=None, feh: Optional[float]=None) -> None: val_pystar.check_isochrone_input(self.spectroscopicInformation, Teff, logg, feh) Teff = self.spectroscopic.Teff if Teff is None else Teff logg = self.spectroscopic.logg if logg is None else logg feh = self.spectroscopic.feh if feh is None else feh self.isochrone = Isochrone((Teff, 50), (logg, 0.05), (feh, 0.05)) self.isochrone.getAll() self.isochroneInformation = True print('Values can be reached through: Star.isochrone')
def test_interpolate(): s = Spectroscopy([1, 2], [0.88, 0.93]) wavelength = np.asarray([1, 1.5, 2]) s._interpolate(wavelength) assert len(s.flux) == len(wavelength) assert (s.flux == np.array([0.88, 0.905, 0.93])).all()
def test_get_ml_parameters_wrong_length(data): model = specML.get_model(data=data) s = Spectroscopy([1, 2], [1, 2], Wavelength.AA) with pytest.raises(ValueError): s.getMLparams(model)
def getSpectroscopicInformation(self, wavelength: listLikeType, flux: listLikeType) -> None: self.spectroscopic = Spectroscopy(wavelength, flux) self.spectroscopic.getAll() self.spectroscopicInformation = True print('Values can be reached through: Star.spectroscopic.')
def test_outside_range(): wavelength = [1, 2] flux = [1, 0.99] s = Spectroscopy(wavelength, flux) with pytest.raises(ValueError): s.getAll()