Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Photometry Magnitude Dtype Change #129

Merged
merged 11 commits into from
Nov 15, 2024
21 changes: 10 additions & 11 deletions sedkit/sed.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from bokeh.plotting import figure, show
from bokeh.models import HoverTool, Range1d, ColumnDataSource
from bokeh.palettes import Category10
import itertools
from dustmaps.bayestar import BayestarWebQuery
from svo_filters import svo

Expand Down Expand Up @@ -191,16 +190,16 @@ def __init__(self, name='My Target', verbose=True, method_list=None, substellar=
self.best_fit = {}

# Make empty spectra table
spec_cols = ('name', 'spectrum', 'wave_min', 'wave_max', 'wave_bins', 'resolution', 'history', 'ref')
spec_typs = ('O', 'O', np.float16, np.float16, int, int, 'O', 'O')
self._spectra = at.QTable(names=spec_cols, dtype=spec_typs)
spec_col_names = ('name', 'spectrum', 'wave_min', 'wave_max', 'wave_bins', 'resolution', 'history', 'ref')
spec_dtypes = ('O', 'O', np.float32, np.float32, int, int, 'O', 'O')
self._spectra = at.QTable(names=spec_col_names, dtype=spec_dtypes)
for col in ['wave_min', 'wave_max']:
self._spectra[col].unit = self._wave_units

# Make empty photometry table
self.mag_system = 'Vega'
phot_cols = ('band', 'eff', 'app_magnitude', 'app_magnitude_unc', 'app_flux', 'app_flux_unc', 'abs_magnitude', 'abs_magnitude_unc', 'abs_flux', 'abs_flux_unc', 'bandpass', 'ref')
phot_typs = ('U16', np.float16, np.float16, np.float16, float, float, np.float16, np.float16, float, float, 'O', 'O')
phot_typs = ('U16', np.float32, np.float32, np.float32, float, float, np.float32, np.float32, float, float, 'O', 'O')
self._photometry = at.QTable(names=phot_cols, dtype=phot_typs)
for col in ['app_flux', 'app_flux_unc', 'abs_flux', 'abs_flux_unc']:
self._photometry[col].unit = self._flux_units
Expand Down Expand Up @@ -310,7 +309,7 @@ def add_photometry(self, band, mag, mag_unc=None, system='Vega', ref=None, **kwa
# Make a dict for the new point
mag = round(mag, 3)
mag_unc = mag_unc if np.isnan(mag_unc) else round(mag_unc, 3)
eff = bp.wave_eff.astype(np.float16)
eff = bp.wave_eff.astype(np.float32)
new_photometry = {'band': band, 'eff': eff, 'app_magnitude': mag, 'app_magnitude_unc': mag_unc, 'bandpass': bp, 'ref': ref}

# Add the kwargs
Expand Down Expand Up @@ -361,8 +360,8 @@ def add_photometry_row(self, row, bands=None, rename=None, unc_wildcard='e_*', r
# Add photometry to phot table
for idx, band in enumerate(bands):
if band in row.colnames:
goodmag = isinstance(row[band], (float, np.float16, np.float32))
goodunc = isinstance(row[unc_wildcard.replace('*', band)], (float, np.float16, np.float32))
goodmag = isinstance(row[band], (float, np.float32, np.float32))
goodunc = isinstance(row[unc_wildcard.replace('*', band)], (float, np.float32, np.float32))
if goodmag:
mag = row[band]
unc = row[unc_wildcard.replace('*', band)] if goodunc else np.nan
Expand Down Expand Up @@ -444,8 +443,8 @@ def add_spectrum(self, spectrum, **kwargs):
spec.flux_units = self.flux_units

# Add the spectrum object to the list of spectra
mn = spec.wave_min#.astype(np.float16)
mx = spec.wave_max#.astype(np.float16)
mn = spec.wave_min
mx = spec.wave_max
res = int(((mx - mn) / np.nanmean(np.diff(spec.wave))).value)

# Make sure it's not a duplicate
Expand Down Expand Up @@ -688,7 +687,7 @@ def calculate_synthetic_photometry(self, bandpasses=None):
if mag is not None and not np.isnan(mag):

# Make a dict for the new point
new_photometry = {'band': band, 'eff': bp.wave_eff.astype(np.float16), 'bandpass': bp, 'app_magnitude': mag, 'app_magnitude_unc': mag_unc, 'ref': 'sedkit'}
new_photometry = {'band': band, 'eff': bp.wave_eff.astype(np.float32), 'bandpass': bp, 'app_magnitude': mag, 'app_magnitude_unc': mag_unc, 'ref': 'sedkit'}

# Add it to the table
self._synthetic_photometry.add_row(new_photometry)
Expand Down
40 changes: 26 additions & 14 deletions tests/test_sed.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class TestSED(unittest.TestCase):
"""Tests for the SED class"""

def setUp(self):

# Make Blackbody
Expand All @@ -32,24 +33,34 @@ def setUp(self):

self.sed = sed.SED(verbose=True, foo=123)


def test_add_photometry(self):
"""Test that photometry is added properly"""
s = copy.copy(self.sed)

# Add the photometry
s.add_photometry('2MASS.J', -0.177, 0.206)
s.add_photometry('2MASS.J', 13.123, 0.206)
self.assertEqual(len(s.photometry), 1)
assert s.photometry[0]['band'] == '2MASS.J'
assert s.photometry[0]['app_magnitude'] == np.float32(13.123)
assert s.photometry[0]['app_magnitude_unc'] == np.float32(0.206)

mag, mag_unc = 23.93, 0.3
s.add_photometry('UKIRT/UFTI.J', mag, mag_unc)
assert s.photometry['app_magnitude'][1] == np.float32(23.93)
assert s.photometry['app_magnitude_unc'][1] == np.float32(0.3)

# Now remove it
s.drop_photometry(0)
s.drop_photometry('UKIRT/UFTI.J')
s.drop_photometry('2MASS.J')
self.assertEqual(len(s.photometry), 0)

def test_add_photometry_table(self):
"""Test that photometry is added properly from file"""
s = copy.copy(self.sed)

# Add the photometry
f = str(importlib.resources.files('sedkit')/ 'data/L3_photometry.txt')
f = str(importlib.resources.files('sedkit') / 'data/L3_photometry.txt')
s.add_photometry_table(f)
self.assertEqual(len(s.photometry), 8)

Expand All @@ -73,7 +84,7 @@ def test_add_spectrum(self):
self.assertEqual(len(s.spectra), 1)

# Test new spectrum array
SPEC1 = [self.WAVE1, self.FLUX1, self.FLUX1/100.]
SPEC1 = [self.WAVE1, self.FLUX1, self.FLUX1 / 100.]
s.add_spectrum(SPEC1)
self.assertEqual(len(s.spectra), 2)

Expand All @@ -92,7 +103,7 @@ def test_attributes(self):
s.age = 4 * q.Gyr, 0.1 * q.Gyr, 'reference'
self.assertRaises(TypeError, setattr, s, 'age', 'foo')
self.assertRaises(TypeError, setattr, s, 'age', (4, 0.1))
self.assertRaises(TypeError, setattr, s, 'age', (4*q.Jy, 0.1*q.Jy))
self.assertRaises(TypeError, setattr, s, 'age', (4 * q.Jy, 0.1 * q.Jy))

# Sky coords
s.sky_coords = 1.2345 * q.deg, 1.2345 * q.deg
Expand Down Expand Up @@ -133,7 +144,7 @@ def test_attributes(self):
self.assertRaises(ValueError, setattr, s, 'evo_model', 'foo')

# Flux units
s.flux_units = q.erg/q.s/q.cm**2/q.AA
s.flux_units = q.erg / q.s / q.cm ** 2 / q.AA
self.assertRaises(TypeError, setattr, s, 'flux_units', q.cm)

# Wave units
Expand All @@ -148,9 +159,9 @@ def test_attributes(self):
def test_no_spectra(self):
"""Test that a purely photometric SED can be creted"""
s = copy.copy(self.sed)
s.age = 455*q.Myr, 13*q.Myr
s.radius = 2.362*q.Rsun, 0.02*q.Rjup, 0.02*q.Rjup
s.parallax = 130.23*q.mas, 0.36*q.mas
s.age = 455 * q.Myr, 13 * q.Myr
s.radius = 2.362 * q.Rsun, 0.02 * q.Rjup, 0.02 * q.Rjup
s.parallax = 130.23 * q.mas, 0.36 * q.mas
s.spectral_type = 'A0V'
s.add_photometry('2MASS.J', -0.177, 0.206)
s.add_photometry('2MASS.H', -0.029, 0.146)
Expand All @@ -165,7 +176,7 @@ def test_no_spectra(self):
self.assertIsNotNone(s.fbol)

# Make Wein tail
s.make_wein_tail(teff=2000*q.K)
s.make_wein_tail(teff=2000 * q.K)

# Radius from spectral type
s.results
Expand All @@ -190,9 +201,9 @@ def test_plot(self):
def test_no_photometry(self):
"""Test that a purely photometric SED can be created"""
s = copy.copy(self.sed)
s.age = 455*q.Myr, 13*q.Myr
s.radius = 2.362*q.Rsun, 0.02*q.Rjup,0.02*q.Rjup
s.parallax = 130.23*q.mas, 0.36*q.mas
s.age = 455 * q.Myr, 13 * q.Myr
s.radius = 2.362 * q.Rsun, 0.02 * q.Rjup, 0.02 * q.Rjup
s.parallax = 130.23 * q.mas, 0.36 * q.mas
s.spectral_type = 'A0V'
s.add_spectrum(self.spec1)

Expand Down Expand Up @@ -229,7 +240,7 @@ def test_find_SDSS_spectra(self):
s.sky_coords = SkyCoord('0h8m05.63s +14d50m23.3s', frame='icrs')
s.find_SDSS_spectra(search_radius=20 * q.arcsec)
s.find_SDSS()
s.plot()
# s.plot()

def test_run_methods(self):
"""Test that the method_list argument works"""
Expand Down Expand Up @@ -294,6 +305,7 @@ def test_fit_modelgrid(self):
#
# self.assertTrue(isinstance(s.Teff_bb, (int, float)))


def test_VegaSED():
"""Test the VegaSED class"""
vega = sed.VegaSED()
Expand Down