Debian Codegraph @

tests/test_extinction.py

raw symbols SCIP
1 #!/usr/bin/env py.test
2
3 import os
4
5 import numpy as np
6 from numpy.testing import assert_allclose
7
8 import extinction
9
10 # Resolve test data relative to this file so the suite can be run from any
11 # working directory, including against an installed wheel.
12 TESTDATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
13 'testdata')
14
15
16 def test_ccm89():
17 # NOTE: Test is only to precision of 0.016 because there is a discrepancy
18 # of 0.014 for the B band wavelength of unknown origin (and up to 0.002 in
19 # other bands).
20 #
21 # Note that a and b can be obtained with:
22 # b = ccm89(wave, 0.)
23 # a = ccm89(wave, 1.) - b
24 #
25 # These differ from the values tablulated in the original paper.
26 # Could be due to floating point errors in the original paper?
27 #
28 # U, B, V, R, I, J, H, K band effective wavelengths from CCM '89 table 3
29
30 x_inv_microns = np.array([2.78, 2.27, 1.82, 1.43, 1.11, 0.80, 0.63, 0.46])
31 wave = 1.e4 / x_inv_microns
32
33 # A(lambda)/A(V) for R_V = 3.1 from Table 3 of CCM '89
34 ref_values = np.array([1.569, 1.337, 1.000, 0.751, 0.479, 0.282, 0.190,
35 0.114])
36
37 assert_allclose(extinction.ccm89(wave, 1.0, 3.1), ref_values,
38 rtol=0.016, atol=0.)
39
40
41 def test_odonnell94():
42 # NOTE: The tabulated values go to 0.001, but the test is only for matching
43 # at the 0.005 level, because there is currently a discrepancy up to 0.0047
44 # of unknown origin.
45 #
46 # Tests od94() at Rv = 3.1 against the widely used values tabulated in
47 # Schlegel, Finkbeiner and Davis (1998)
48 # http://adsabs.harvard.edu/abs/1998ApJ...500..525S
49 #
50 # This is tested by evaluating the extinction curve at a (given)
51 # effective wavelength, since these effective wavelengths:
52 # "... represent(s) that wavelength on the extinction curve
53 # with the same extinction as the full passband."
54 #
55 # The test does not include UKIRT L' (which, at 3.8 microns) is
56 # beyond the range of wavelengths allowed by the function
57 # or the APM b_J filter which is defined in a non-standard way.
58 #
59 # The SFD98 tabulated values go to 1e-3, so we should be able to match at
60 # that level.
61
62 wave = np.array([3372., 4404., 5428., 6509., 8090.,
63 3683., 4393., 5519., 6602., 8046.,
64 12660., 16732., 22152.,
65 5244., 6707., 7985., 9055.,
66 6993.,
67 3502., 4676., 4127.,
68 4861., 5479.,
69 3546., 4925., 6335., 7799., 9294.,
70 3047., 4711., 5498.,
71 6042., 7068., 8066.,
72 4814., 6571., 8183.])
73
74 ref_values = np.array([1.664, 1.321, 1.015, 0.819, 0.594,
75 1.521, 1.324, 0.992, 0.807, 0.601,
76 0.276, 0.176, 0.112,
77 1.065, 0.793, 0.610, 0.472,
78 0.755,
79 1.602, 1.240, 1.394,
80 1.182, 1.004,
81 1.579, 1.161, 0.843, 0.639, 0.453,
82 1.791, 1.229, 0.996,
83 0.885, 0.746, 0.597,
84 1.197, 0.811, 0.580])
85
86 assert_allclose(extinction.odonnell94(wave, 1.0, 3.1), ref_values,
87 rtol=0.0051, atol=0.)
88
89
90 def test_fitzpatrick99_knots():
91 """Test that knots match values in Fitzpatrick (1999) Table 3 for
92 fitzpatrick99 function (with R_V = 3.1)"""
93
94 wave = np.array([np.inf, 26500., 12200., 6000., 5470., 4670., 4110.,
95 2700., 2600.])
96 x = np.array([0.0, 0.377, 0.820, 1.667, 1.828, 2.141, 2.433, 3.704,
97 3.846])
98
99 # A(lambda) values for E(B-V) = 1 or A_V = 3.1
100 ref_values = np.array([0.0, 0.265, 0.829, 2.688, 3.055, 3.806, 4.315,
101 6.265, 6.591])
102
103 assert_allclose(extinction.fitzpatrick99(wave, 3.1, unit='aa'), ref_values,
104 rtol=0., atol=0.001)
105
106 # atol = 0.002 because the input values are less precise (e.g., 0.377
107 # rather than 1.e4 / 26500.)
108 assert_allclose(extinction.fitzpatrick99(x, 3.1, unit='invum'), ref_values,
109 rtol=0., atol=0.002)
110
111
112 def test_fitzpatrick99_rv_knots():
113 """Test that Fitzpatrick function has the right R_V dependence at
114 the knots."""
115
116 wave = np.array([26500., 12200., 6000., 5470., 4670., 4110.])
117
118 # "the IR points at 1/lambda < 1 invum are simply scaled by R/3.1"
119 # "the optical points are vertically offset by an amount R - 3.1 , with
120 # slight corrections made to preserve the normalization"
121
122 for r in [1., 2., 3., 4., 5., 6.]:
123 # `expected` gives expected A(lambda) for E(B-V) = 1 (A_V = R)
124 expected = [r / 3.1 * 0.265, # Table 3 scaled by R/3.1
125 r / 3.1 * 0.829, # Table 3 scaled by R/3.1
126 -0.426 + 1.0044 * r, # Table 4
127 -0.050 + 1.0016 * r, # Table 4
128 0.701 + 1.0016 * r, # Table 4
129 1.208 + 1.0032 * r - 0.00033 * r**2] # Table 4
130
131 result = extinction.Fitzpatrick99(r)(wave, r)
132
133 # Note that we don't expect an exact match because, as noted in the
134 # code, "the optical spline points are not taken from F99 table 4,
135 # but rather updated versions from E. Fitzpatrick (matching the IDL
136 # astrolib routine FM_UNRED).
137 assert_allclose(result, expected, rtol=0.003)
138
139
140 def test_fitzpatrick99_idl():
141 """Test that result matches implementation in IDL procedure FM_UNRED"""
142
143 for r_v in (2.3, 3.1, 4.0, 5.3):
144 fname = os.path.join(TESTDATA_DIR, 'fm_unred_{:3.1f}.dat'.format(r_v))
145 wave, a_lambda_ref = np.loadtxt(fname, unpack=True)
146 a_lambda = extinction.Fitzpatrick99(r_v)(wave, 1.0)
147 assert_allclose(a_lambda, a_lambda_ref, rtol=0.00005)
148
149
150 def test_fitzpatrick99_r_v():
151 """Test that we can access the `r_v` attribute."""
152
153 f = extinction.Fitzpatrick99()
154 assert f.r_v == 3.1
155 f = extinction.Fitzpatrick99(2.1)
156 assert f.r_v == 2.1
157
158
159 def test_fitzpatrick99_func():
160 """Check passing R_V."""
161
162 wave = np.array([2000., 30000.])
163 for r_v in (3.1, 4.0):
164 assert np.all(extinction.Fitzpatrick99(r_v)(wave, 1.0) ==
165 extinction.fitzpatrick99(wave, 1.0, r_v))
166
167
168 def test_fm07():
169 wave = np.arange(3000., 9000., 1000)
170
171 # from running the code; we're just checking that results don't change!
172 ref_values = [1.84192286, 1.42645161, 1.13842341, 0.88843179, 0.69226384,
173 0.54709373]
174 assert_allclose(extinction.fm07(wave, 1.), ref_values)
175
176
177 def test_calzetti00():
178 """Test calzetti against another translation of the same base code"""
179
180 wave = np.array([2000., 4000., 8000.])
181 flux = np.ones(3)
182
183 new_flux = extinction.apply(extinction.calzetti00(wave, -1., 3.1), flux)
184
185 # derived using Julia version of IDL calz_unred
186 ref_values = np.array([10.5288, 3.88153, 1.61769])
187
188 assert_allclose(new_flux, ref_values, atol=0.0001)