Skip to content

Commit

Permalink
add new conftest.py file and debug failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moustakas committed Dec 4, 2024
1 parent 4927018 commit 4709b8a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 115 deletions.
2 changes: 1 addition & 1 deletion py/fastspecfit/linetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions py/fastspecfit/test/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
152 changes: 74 additions & 78 deletions py/fastspecfit/test/test_fastspecfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
3 changes: 2 additions & 1 deletion py/fastspecfit/test/test_linetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import os
import pytest


def test_linetable():
import astropy
from fastspecfit.linetable import LineTable
emline_table = LineTable(emlines_file=None)
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')
36 changes: 1 addition & 35 deletions py/fastspecfit/test/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4709b8a

Please sign in to comment.