diff --git a/py/fastspecfit/linetable.py b/py/fastspecfit/linetable.py index 856b09a1..8191ee55 100644 --- a/py/fastspecfit/linetable.py +++ b/py/fastspecfit/linetable.py @@ -25,7 +25,7 @@ def __init__(self, emlines_file=None): if not os.path.isfile(emlines_file): errmsg = f'Emission lines parameter file {emlines_file} does not exist.' log.critical(errmsg) - raise IOError(errmsg) + raise FileNotFoundError(errmsg) self.file = emlines_file diff --git a/py/fastspecfit/test/conftest.py b/py/fastspecfit/test/conftest.py new file mode 100644 index 00000000..c1118e7e --- /dev/null +++ b/py/fastspecfit/test/conftest.py @@ -0,0 +1,40 @@ +""" +fastspecfit.test.conftest +========================= + +""" +import os +import pytest +from urllib.request import urlretrieve + +@pytest.fixture(scope='session') +def template_version(): + yield '2.0.0' + + +@pytest.fixture(scope='session') +def outdir(tmp_path_factory): + outdir = tmp_path_factory.mktemp('data') + yield outdir + + +@pytest.fixture(scope='session') +def templatedir(outdir, template_version): + templatedir = outdir / template_version + templatedir.mkdir() + yield templatedir + + +@pytest.fixture(scope='session') +def templates(templatedir, template_version): + templates_file = f'ftemplates-chabrier-{template_version}.fits' + templates = os.path.join(templatedir, templates_file) + + url = f"https://data.desi.lbl.gov/public/external/templates/fastspecfit/2.0.0/{templates_file}" + if not os.path.isfile(templates): + urlretrieve(url, templates) + yield templates + + # Optional cleanup + if os.path.isfile(templates): + os.remove(templates) diff --git a/py/fastspecfit/test/test_fastspecfit.py b/py/fastspecfit/test/test_fastspecfit.py index 47c423a1..00b92553 100644 --- a/py/fastspecfit/test/test_fastspecfit.py +++ b/py/fastspecfit/test/test_fastspecfit.py @@ -19,81 +19,77 @@ import pytest import numpy as np from urllib.request import urlretrieve -from importlib import resources - - -class TestFastspec(unittest.TestCase): - """Test fastspecfit.fastspecfit.fastspec""" - @classmethod - def setUpClass(cls): - os.environ['DESI_SPECTRO_REDUX'] = str(resources.files('fastspecfit').joinpath('test/data')) - cls.specproddir = resources.files('fastspecfit').joinpath('test/data') - cls.mapdir = resources.files('fastspecfit').joinpath('test/data') - cls.fphotodir = resources.files('fastspecfit').joinpath('test/data') - cls.redrockfile = resources.files('fastspecfit').joinpath('test/data/redrock-4-80613-thru20210324.fits') - - cls.templates = '/Users/ioannis/work/desi/users/ioannis/fastspecfit/templates/2.0.0/ftemplates-chabrier-2.0.0.fits' - cls.outdir = tempfile.mkdtemp() - #cls.templates = os.path.join(cls.outdir, 'ftemplates-chabrier-2.0.0.fits') - #if os.path.isfile(cls.templates): - # os.remove(cls.templates) - ##url = "https://portal.nersc.gov/project/cosmo/temp/ioannis/tmp/ftemplates-chabrier-2.0.0.fits" - #url = "https://data.desi.lbl.gov/public/external/templates/fastspecfit/2.0.0/ftemplates-chabrier-2.0.0.fits" - #urlretrieve(url, cls.templates) - - cls.fastspec_outfile = os.path.join(cls.outdir, 'fastspec.fits') - cls.fastphot_outfile = os.path.join(cls.outdir, 'fastphot.fits') - - def setUp(self): - pass - - @classmethod - def tearDownClass(cls): - pass - - - #def test_ContinuumTools(self): - # """Test the ContinuumTools class.""" - # from fastspecfit.continuum import ContinuumTools - # CTools = ContinuumTools() - # - # # expected attributes - # self.assertTrue(CTools.imf in ['salpeter', 'chabrier', 'kroupa']) - - - #@unittest.SkipTest - def test_fastphot(self): - """Test fastphot.""" - import fitsio - from fastspecfit.fastspecfit import fastphot, parse - - cmd = f'fastphot {self.redrockfile} -o {self.fastphot_outfile} --mapdir {self.mapdir} ' + \ - f'--fphotodir {self.fphotodir} --specproddir {self.specproddir} --templates {self.templates}' - args = parse(options=cmd.split()[1:]) - fastphot(args=args) - - self.assertTrue(os.path.exists(self.fastphot_outfile)) - - fits = fitsio.FITS(self.fastphot_outfile) - for hdu in fits: - if hdu.has_data(): # skip zeroth extension - self.assertTrue(hdu.get_extname() in ['METADATA', 'SPECPHOT']) - - - #@unittest.SkipTest - def test_fastspec(self): - """Test fastspec.""" - import fitsio - from fastspecfit.fastspecfit import fastspec, parse - - cmd = f'fastspec {self.redrockfile} -o {self.fastspec_outfile} --mapdir {self.mapdir} ' + \ - f'--fphotodir {self.fphotodir} --specproddir {self.specproddir} --templates {self.templates}' - args = parse(options=cmd.split()[1:]) - fastspec(args=args) - - self.assertTrue(os.path.exists(self.fastspec_outfile)) - - fits = fitsio.FITS(self.fastspec_outfile) - for hdu in fits: - if hdu.has_data(): # skip zeroth extension - self.assertTrue(hdu.get_extname() in ['METADATA', 'SPECPHOT', 'FASTSPEC', 'MODELS']) + + +def setUpClass(cls): + os.environ['DESI_SPECTRO_REDUX'] = str(resources.files('fastspecfit').joinpath('test/data')) + + #cls.templates = '/Users/ioannis/work/desi/users/ioannis/fastspecfit/templates/2.0.0/ftemplates-chabrier-2.0.0.fits' + #cls.outdir = tempfile.mkdtemp() + #cls.templates = os.path.join(cls.outdir, 'ftemplates-chabrier-2.0.0.fits') + #if os.path.isfile(cls.templates): + # os.remove(cls.templates) + ##url = "https://portal.nersc.gov/project/cosmo/temp/ioannis/tmp/ftemplates-chabrier-2.0.0.fits" + #url = "https://data.desi.lbl.gov/public/external/templates/fastspecfit/2.0.0/ftemplates-chabrier-2.0.0.fits" + #urlretrieve(url, cls.templates) + + cls.fastspec_outfile = os.path.join(cls.outdir, 'fastspec.fits') + cls.fastphot_outfile = os.path.join(cls.outdir, 'fastphot.fits') + + +@pytest.fixture +def filenames(outdir): + from importlib import resources + + specproddir = resources.files('fastspecfit').joinpath('test/data') + mapdir = resources.files('fastspecfit').joinpath('test/data') + fphotodir = resources.files('fastspecfit').joinpath('test/data') + redrockfile = resources.files('fastspecfit').joinpath('test/data/redrock-4-80613-thru20210324.fits') + fastspec_outfile = os.path.join(outdir, 'fastspec.fits') + fastphot_outfile = os.path.join(outdir, 'fastphot.fits') + + filenames = {'specproddir': specproddir, 'mapdir': mapdir, 'fphotodir': fphotodir, + 'redrockfile': redrockfile, 'fastspec_outfile': fastspec_outfile, + 'fastphot_outfile': fastphot_outfile, } + + yield filenames + + +def test_fastphot(filenames, templates): + """Test fastphot.""" + import fitsio + from fastspecfit.fastspecfit import fastphot, parse + + outfile = filenames["fastphot_outfile"] + + cmd = f'fastphot {filenames["redrockfile"]} -o {outfile} ' + \ + f'--mapdir {filenames["mapdir"]} --fphotodir {filenames["fphotodir"]} ' + \ + f'--specproddir {filenames["specproddir"]} --templates {templates}' + + args = parse(options=cmd.split()[1:]) + fastphot(args=args) + + assert(os.path.exists(outfile)) + + #fits = fitsio.FITS(outfile) + #for hdu in fits: + # if hdu.has_data(): # skip zeroth extension + # assert(hdu.get_extname() in ['METADATA', 'SPECPHOT']) + + +#def test_fastspec(self): +# """Test fastspec.""" +# import fitsio +# from fastspecfit.fastspecfit import fastspec, parse +# +# cmd = f'fastspec {self.redrockfile} -o {self.fastspec_outfile} --mapdir {self.mapdir} ' + \ +# f'--fphotodir {self.fphotodir} --specproddir {self.specproddir} --templates {self.templates}' +# args = parse(options=cmd.split()[1:]) +# fastspec(args=args) +# +# self.assertTrue(os.path.exists(self.fastspec_outfile)) +# +# fits = fitsio.FITS(self.fastspec_outfile) +# for hdu in fits: +# if hdu.has_data(): # skip zeroth extension +# self.assertTrue(hdu.get_extname() in ['METADATA', 'SPECPHOT', 'FASTSPEC', 'MODELS']) diff --git a/py/fastspecfit/test/test_linetable.py b/py/fastspecfit/test/test_linetable.py index 9a636db8..9c89b9ef 100644 --- a/py/fastspecfit/test/test_linetable.py +++ b/py/fastspecfit/test/test_linetable.py @@ -6,6 +6,7 @@ import os import pytest + def test_linetable(): import astropy from fastspecfit.linetable import LineTable @@ -13,5 +14,5 @@ def test_linetable(): assert(os.path.isfile(emline_table.file)) assert(type(emline_table.table) == astropy.table.Table) - with pytest.raises(IOError): + with pytest.raises(FileNotFoundError): LineTable(emlines_file='doesnotexist.ecsv') diff --git a/py/fastspecfit/test/test_templates.py b/py/fastspecfit/test/test_templates.py index 5eb913c1..fb82fb36 100644 --- a/py/fastspecfit/test/test_templates.py +++ b/py/fastspecfit/test/test_templates.py @@ -4,43 +4,9 @@ """ import os -import pytest -import numpy as np -from urllib.request import urlretrieve -@pytest.fixture(scope='session') -def template_version(): - yield '2.0.0' - - -@pytest.fixture(scope='session') -def outdir(tmp_path_factory): - outdir = tmp_path_factory.mktemp('data') - yield outdir - - -@pytest.fixture(scope='session') -def templatedir(outdir, template_version): - templatedir = outdir / template_version - templatedir.mkdir() - yield templatedir - - -@pytest.fixture(scope='session') -def templates(templatedir, template_version): - templates_file = f'ftemplates-chabrier-{template_version}.fits' - templates = os.path.join(templatedir, templates_file) - - url = f"https://data.desi.lbl.gov/public/external/templates/fastspecfit/2.0.0/{templates_file}" - if not os.path.isfile(templates): - urlretrieve(url, templates) - yield templates - - # Optional cleanup - if os.path.isfile(templates): - os.remove(templates) - +# See conftest.py for fixtures used by multiple unit tests. def test_templates_nofilename(outdir, template_version, templates): from fastspecfit.templates import Templates