-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathconftest.py
131 lines (103 loc) · 3.99 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# This file is used to configure the behavior of pytest
import numpy as np
import pytest
from astropy import units as u
from astropy.io import fits
from astropy.nddata import CCDData, NDData, VarianceUncertainty
from astropy.utils.data import get_pkg_data_filename
from specutils import Spectrum1D, SpectralAxis
try:
from pytest_astropy_header.display import PYTEST_HEADER_MODULES, TESTED_VERSIONS
ASTROPY_HEADER = True
except ImportError:
ASTROPY_HEADER = False
# Test image is comprised of 30 rows with 10 columns each. Row content
# is row index itself. This makes it easy to predict what should be the
# value extracted from a region centered at any arbitrary Y position.
def _mk_test_data(imgtype, nrows=30, ncols=10):
image_ones = np.ones(shape=(nrows, ncols))
image = image_ones.copy()
for j in range(nrows):
image[j, ::] *= j
if imgtype == "raw":
pass # no extra processing
elif imgtype == "ccddata":
image = CCDData(image, unit=u.Jy)
else: # spectrum
flux = image * u.DN
uncert = VarianceUncertainty(image_ones)
if imgtype == "spec_no_axis":
image = Spectrum1D(flux, uncertainty=uncert)
else: # "spec"
image = Spectrum1D(flux, spectral_axis=np.arange(ncols) * u.um, uncertainty=uncert)
return image
@pytest.fixture
def mk_test_img_raw():
return _mk_test_data("raw")
@pytest.fixture
def mk_test_img():
return _mk_test_data("ccddata")
@pytest.fixture
def mk_test_spec_no_spectral_axis():
return _mk_test_data("spec_no_axis")
@pytest.fixture
def mk_test_spec_with_spectral_axis():
return _mk_test_data("spec")
# Test data file already transposed like this:
# fn = download_file('https://stsci.box.com/shared/static/exnkul627fcuhy5akf2gswytud5tazmw.fits', cache=True) # noqa: E501
# img = fits.getdata(fn).T
@pytest.fixture
def all_images():
np.random.seed(7)
filename = get_pkg_data_filename(
"data/transposed_det_image_seq5_MIRIMAGE_P750Lexp1_s2d.fits", package="specreduce.tests")
img = fits.getdata(filename)
flux = img * (u.MJy / u.sr)
sax = SpectralAxis(np.linspace(14.377, 3.677, flux.shape[-1]) * u.um)
unc = VarianceUncertainty(np.random.rand(*flux.shape))
all_images = {}
all_images['arr'] = img
all_images['s1d'] = Spectrum1D(flux, spectral_axis=sax, uncertainty=unc)
all_images['s1d_pix'] = Spectrum1D(flux, uncertainty=unc)
all_images['ccd'] = CCDData(img, uncertainty=unc, unit=flux.unit)
all_images['ndd'] = NDData(img, uncertainty=unc, unit=flux.unit)
all_images['qnt'] = img * flux.unit
return all_images
@pytest.fixture
def spec1d():
np.random.seed(7)
flux = np.random.random(50)*u.Jy
sa = np.arange(0, 50)*u.pix
spec = Spectrum1D(flux, spectral_axis=sa)
return spec
@pytest.fixture
def spec1d_with_emission_line():
np.random.seed(7)
sa = np.arange(0, 200)*u.pix
flux = (np.random.randn(200) +
10*np.exp(-0.01*((sa.value-130)**2)) +
sa.value/100) * u.Jy
spec = Spectrum1D(flux, spectral_axis=sa)
return spec
@pytest.fixture
def spec1d_with_absorption_line():
np.random.seed(7)
sa = np.arange(0, 200)*u.pix
flux = (np.random.randn(200) -
10*np.exp(-0.01*((sa.value-130)**2)) +
sa.value/100) * u.Jy
spec = Spectrum1D(flux, spectral_axis=sa)
return spec
def pytest_configure(config):
if ASTROPY_HEADER:
config.option.astropy_header = True
# Customize the following lines to add/remove entries from the list of
# packages for which version numbers are displayed when running the tests.
PYTEST_HEADER_MODULES.pop('Pandas', None)
PYTEST_HEADER_MODULES.pop('h5py', None)
PYTEST_HEADER_MODULES['astropy'] = 'astropy'
PYTEST_HEADER_MODULES['specutils'] = 'specutils'
PYTEST_HEADER_MODULES['photutils'] = 'photutils'
PYTEST_HEADER_MODULES['synphot'] = 'synphot'
from specreduce import __version__
TESTED_VERSIONS["specreduce"] = __version__