From 1153c36bad762b8a40bac703536fe3e8b5f15880 Mon Sep 17 00:00:00 2001 From: Sherelyn Alejandro <80668767+SherelynA@users.noreply.github.com> Date: Mon, 11 Nov 2024 23:01:24 -0500 Subject: [PATCH 1/9] Fixing absolute flux calculation which was missing a squared. --- sedkit/spectrum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sedkit/spectrum.py b/sedkit/spectrum.py index 6b0421e..91d5296 100644 --- a/sedkit/spectrum.py +++ b/sedkit/spectrum.py @@ -528,7 +528,7 @@ def flux_calibrate(self, distance, target_distance=10 * q.pc, flux_units=None): if self.unc is None: unc = None else: - term1 = (self.spectrum[2] * distance[0] / target_distance).to(flux_units) + term1 = (self.spectrum[2] * (distance[0] / target_distance) ** 2).to(flux_units) term2 = (2 * self.spectrum[1] * (distance[1] * distance[0] / target_distance**2)).to(flux_units) unc = np.sqrt(term1**2 + term2**2) From 7575e1a2305cbc3ea4dc9e113079686b0069ac8c Mon Sep 17 00:00:00 2001 From: Sherelyn Alejandro <80668767+SherelynA@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:51:38 -0500 Subject: [PATCH 2/9] Adding tests for flux calibration. --- sedkit/tests/test_spectrum.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sedkit/tests/test_spectrum.py b/sedkit/tests/test_spectrum.py index 0e3f1d3..8ab47a0 100644 --- a/sedkit/tests/test_spectrum.py +++ b/sedkit/tests/test_spectrum.py @@ -145,6 +145,16 @@ def test_integrate(self): self.assertAlmostEqual(fbol[0].value, 4000, places=1) self.assertNotEqual(str(fbol[1].value), 'nan') + def test_flux_calibrate(self): + """ Test that flux calibrate is working properly""" + # 1 parsec Distance + close_distance = 1 * q.pc + abs_sed = self.flat1.flux_calibrate(close_distance) + + # 15 parsec Distance + far_distance = 15 * q.pc + abs_sed = self.flat1.flux_calibrate(far_distance) + def test_interpolate(self): """Test interpolate method""" spec1 = self.flat1 From cbdf5a6197d059dc096e4147d07eb5ecdba754e6 Mon Sep 17 00:00:00 2001 From: Sherelyn Alejandro <80668767+SherelynA@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:31:34 -0500 Subject: [PATCH 3/9] Adding tests for flux calibration but no check yet --- sedkit/tests/test_spectrum.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sedkit/tests/test_spectrum.py b/sedkit/tests/test_spectrum.py index 8ab47a0..d735282 100644 --- a/sedkit/tests/test_spectrum.py +++ b/sedkit/tests/test_spectrum.py @@ -147,13 +147,13 @@ def test_integrate(self): def test_flux_calibrate(self): """ Test that flux calibrate is working properly""" - # 1 parsec Distance - close_distance = 1 * q.pc - abs_sed = self.flat1.flux_calibrate(close_distance) + # 1 parsec Distance with unc + close_distance = 1 * q.pc, 0.05 * q.pc + abs_sed = sp.Spectrum.flux_calibrate(self.flat1, close_distance) - # 15 parsec Distance - far_distance = 15 * q.pc - abs_sed = self.flat1.flux_calibrate(far_distance) + # 15 parsec Distance with unc + far_distance = 15 * q.pc, 5 * q.pc + abs_sed = sp.Spectrum.flux_calibrate(self.flat1, far_distance) def test_interpolate(self): """Test interpolate method""" From e6b99c9639f2152263d3cf4a6fa427f0b27f9d0b Mon Sep 17 00:00:00 2001 From: kelle Date: Fri, 15 Nov 2024 12:14:36 +0100 Subject: [PATCH 4/9] flux calibrate tests --- tests/test_spectrum.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_spectrum.py b/tests/test_spectrum.py index df50dc4..3347843 100644 --- a/tests/test_spectrum.py +++ b/tests/test_spectrum.py @@ -153,11 +153,15 @@ def test_flux_calibrate(self): """ Test that flux calibrate is working properly""" # 1 parsec Distance with unc close_distance = 1 * q.pc, 0.05 * q.pc - abs_sed = sp.Spectrum.flux_calibrate(self.flat1, close_distance) + abs_sed_close = sp.Spectrum.flux_calibrate(self.flat1, close_distance) + assert np.isclose(np.mean(abs_sed_close.flux),0.01, rtol=0.005) + assert np.isclose(np.mean(abs_sed_close.unc),0.001, rtol=0.005) # 15 parsec Distance with unc - far_distance = 15 * q.pc, 5 * q.pc - abs_sed = sp.Spectrum.flux_calibrate(self.flat1, far_distance) + far_distance = 15 * q.pc, 2 * q.pc + abs_sed_far = sp.Spectrum.flux_calibrate(self.flat1, far_distance) + assert np.isclose(np.mean(abs_sed_far.flux), 2.25, rtol=0.005) + assert np.isclose(np.mean(abs_sed_far.unc), 0.6, rtol=0.005) def test_interpolate(self): """Test interpolate method""" From aa0bec108addf1a9e1b90423fbc6a06487ff48c6 Mon Sep 17 00:00:00 2001 From: kelle Date: Fri, 15 Nov 2024 14:21:43 +0100 Subject: [PATCH 5/9] add more tests --- tests/test_spectrum.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/test_spectrum.py b/tests/test_spectrum.py index 3347843..9416d4d 100644 --- a/tests/test_spectrum.py +++ b/tests/test_spectrum.py @@ -240,7 +240,7 @@ def test_norm_to_spec(self): s2 = self.flat2 # Normalize 1 to 2 and check that they are close - s3 = s1.norm_to_spec(s2, plot=True) + s3 = s1.norm_to_spec(s2, plot=False) self.assertAlmostEqual(np.nanmean(s2.flux), np.nanmean(s3.flux), places=4) self.assertNotEqual(s2.size, s3.size) self.assertEqual(s1.size, s3.size) @@ -250,24 +250,23 @@ def test_trim(self): # Test include s1 = copy.copy(self.flat1) trimmed = s1.trim(include=[(0.8 * q.um, 2 * q.um)], concat=False) - # self.assertTrue(len(trimmed) == 1) - # self.assertNotEqual(self.flat1.size, trimmed[0].size) + assert len(trimmed[0].wave) == 115 # Test exclude s1 = copy.copy(self.flat1) trimmed = s1.trim(exclude=[(0.8 * q.um, 3 * q.um)], concat=False) - # self.assertNotEqual(self.flat1.size, trimmed[0].size) + assert len(trimmed[0].wave) == 115 # Test split s1 = copy.copy(self.flat1) trimmed = s1.trim(exclude=[(0.8 * q.um, 0.9 * q.um)], concat=False) - # self.assertTrue(len(trimmed) == 2) - # self.assertNotEqual(self.flat1.size, trimmed[0].size) + assert len(trimmed[0].wave) == 115 + # Test concat s1 = copy.copy(self.flat1) trimmed = s1.trim(exclude=[(0.8 * q.um, 0.9 * q.um)], concat=True) - # self.assertNotEqual(self.flat1.size, trimmed.size) + assert len(trimmed.wave) == 173 class TestFileSpectrum(unittest.TestCase): @@ -281,10 +280,12 @@ def setUp(self): def test_fits(self): """Test that a fits file can be loaded""" spec = sp.FileSpectrum(self.fitsfile, wave_units='um', flux_units='erg/s/cm2/AA') + assert spec def test_txt(self): """Test that a txt file can be loaded""" spec = sp.FileSpectrum(self.txtfile, wave_units='um', flux_units='erg/s/cm2/AA') + assert spec class TestVega(unittest.TestCase): From 7a77b5dc6d22d2eb0c70e0bda8144152c3cfc099 Mon Sep 17 00:00:00 2001 From: kelle Date: Fri, 15 Nov 2024 14:21:52 +0100 Subject: [PATCH 6/9] fix warning --- sedkit/spectrum.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sedkit/spectrum.py b/sedkit/spectrum.py index fb3f726..1749e56 100644 --- a/sedkit/spectrum.py +++ b/sedkit/spectrum.py @@ -23,7 +23,7 @@ import numpy as np from pandas import DataFrame from svo_filters import Filter - +import warnings from . import mcmc as mc from . import utilities as u @@ -1494,7 +1494,11 @@ def __init__(self, file, wave_units=None, flux_units=None, ext=0, # Sanity check for wave_units if data[0].min() > 100 and wave_units == q.um: - print("WARNING: Your wavelength range ({} - {}) looks like Angstroms. Are you sure it's {}?".format(data[0].min(), data[0].max(), wave_units)) + msg = ( + f"Your wavelength range ({data[0].min()} - {data[0].max()})" + f"looks like Angstroms. Are you sure it's {wave_units}?" + ) + warnings.warn(msg) # Apply units wave = data[0] * wave_units From c3c4180204f23689ec8842ec7756b9c8a851576c Mon Sep 17 00:00:00 2001 From: kelle Date: Fri, 15 Nov 2024 14:29:25 +0100 Subject: [PATCH 7/9] units change --- tests/test_spectrum.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_spectrum.py b/tests/test_spectrum.py index 9416d4d..a0b51d3 100644 --- a/tests/test_spectrum.py +++ b/tests/test_spectrum.py @@ -279,12 +279,12 @@ def setUp(self): def test_fits(self): """Test that a fits file can be loaded""" - spec = sp.FileSpectrum(self.fitsfile, wave_units='um', flux_units='erg/s/cm2/AA') + spec = sp.FileSpectrum(self.fitsfile, wave_units='um', flux_units='erg s-1 cm-2 AA-1') assert spec def test_txt(self): """Test that a txt file can be loaded""" - spec = sp.FileSpectrum(self.txtfile, wave_units='um', flux_units='erg/s/cm2/AA') + spec = sp.FileSpectrum(self.txtfile, wave_units='um', flux_units='erg s-1 cm-2 AA-1') assert spec From 598c1f22e584664b8f8591c0e9949af4c7612000 Mon Sep 17 00:00:00 2001 From: kelle Date: Fri, 15 Nov 2024 16:25:42 +0100 Subject: [PATCH 8/9] plot=False --- tests/test_sed.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_sed.py b/tests/test_sed.py index 4f19ae3..668acf8 100644 --- a/tests/test_sed.py +++ b/tests/test_sed.py @@ -217,10 +217,10 @@ def test_edit_spectrum(self): s.add_spectrum(self.spec1) # Smooth it - s.edit_spectrum(0, smooth={'beta': 5}, plot=True) + s.edit_spectrum(0, smooth={'beta': 5}, plot=False) # Unsmooth it - s.edit_spectrum(0, restore=True) + s.edit_spectrum(0, restore=True, plot=False) # Bad beta self.assertRaises((ValueError, TypeError), s.edit_spectrum, idx=0, smooth={'beta': 'foo'}) From 88b9b546dd8d8fc8038b61f9d86b7593449299c0 Mon Sep 17 00:00:00 2001 From: kelle Date: Fri, 15 Nov 2024 16:26:10 +0100 Subject: [PATCH 9/9] un-add tests --- tests/test_spectrum.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tests/test_spectrum.py b/tests/test_spectrum.py index a0b51d3..ca0f8a8 100644 --- a/tests/test_spectrum.py +++ b/tests/test_spectrum.py @@ -253,20 +253,16 @@ def test_trim(self): assert len(trimmed[0].wave) == 115 # Test exclude - s1 = copy.copy(self.flat1) - trimmed = s1.trim(exclude=[(0.8 * q.um, 3 * q.um)], concat=False) - assert len(trimmed[0].wave) == 115 + trimmed2 = s1.trim(exclude=[(0.8 * q.um, 3 * q.um)], concat=False) # Test split - s1 = copy.copy(self.flat1) - trimmed = s1.trim(exclude=[(0.8 * q.um, 0.9 * q.um)], concat=False) - assert len(trimmed[0].wave) == 115 + trimmed3 = s1.trim(exclude=[(0.8 * q.um, 0.9 * q.um)], concat=False) + # assert len(trimmed3[0].wave) == 115 # Test concat - s1 = copy.copy(self.flat1) - trimmed = s1.trim(exclude=[(0.8 * q.um, 0.9 * q.um)], concat=True) - assert len(trimmed.wave) == 173 + trimmed4 = s1.trim(exclude=[(0.8 * q.um, 0.9 * q.um)], concat=True) + # assert len(trimmed4.wave) == 173 class TestFileSpectrum(unittest.TestCase):