-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtests.py
More file actions
423 lines (330 loc) · 12.8 KB
/
tests.py
File metadata and controls
423 lines (330 loc) · 12.8 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# -*- coding: utf-8 -*-
import os
import sys
import numpy as np
import pytest
from numpy.testing import assert_allclose
from fsps import StellarPopulation, filters
skip_slow_tests = pytest.mark.skipif(
(sys.platform.startswith("win") or sys.platform.startswith("darwin"))
and "CI" in os.environ,
reason="Slow tests only run on Linux CI",
)
@pytest.fixture(scope="module")
def pop_and_params():
pop = StellarPopulation(zcontinuous=1)
params = dict([(k, pop.params[k]) for k in pop.params.all_params])
return pop, params
def _reset_default_params(pop, params):
pop._zcontinuous = 1
for k in pop.params.all_params:
pop.params[k] = params[k]
@skip_slow_tests
def test_isochrones(pop_and_params):
"""Just test that `isochrones()` method runs"""
# recomputes SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["imf_type"] = 0
pop.isochrones()
@skip_slow_tests
def test_imf3(pop_and_params):
"""Make sure that changing the (upper) imf changes the parameter dirtiness
and also the SSP spectrum"""
# recomputes SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["imf_type"] = 2
pop.params["imf3"] = 2.3
w, model1 = pop.get_spectrum(tage=0.2)
# check that changing the IMF does something
pop.params["imf3"] = 8.3
assert pop.params.dirtiness == 2
w, model2 = pop.get_spectrum(tage=0.2)
assert not np.allclose(model1 / model2 - 1.0, 0.0)
# Do we *really* need to do this second check?
pop.params["imf3"] = 2.3
assert pop.params.dirtiness == 2
w, model1b = pop.get_spectrum(tage=0.2)
assert pop.params.dirtiness == 0
assert_allclose(model1 / model1b - 1.0, 0.0)
def test_param_checks(pop_and_params):
# recomputes SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["sfh"] = 1
pop.params["tage"] = 2
pop.params["sf_start"] = 0.5
# this should never raise an error:
w, s = pop.get_spectrum(tage=pop.params["tage"])
# this used to raise an assertion error in the setter:
pop.params["sf_start"] = pop.params["tage"] + 0.1
# this also used to raise an assertion error in the setter:
pop.params["imf_type"] = 8
# fix the invalid IMF but leave the invalid sf_start > tage
pop.params["imf_type"] = 1
with pytest.raises(AssertionError):
w, s = pop.get_spectrum(tage=pop.params["tage"])
pop.params["tage"] = 1.0
pop.params["sf_start"] = 0.1
w, s = pop.get_spectrum(tage=pop.params["tage"])
def test_smooth_lsf(pop_and_params):
# recomputes SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
tmax = 1.0
wave_lsf = np.arange(4000, 7000.0, 10)
x = (wave_lsf - 5500) / 1500.0
# a quadratic lsf dependence that goes from ~50 to ~100 km/s
sigma_lsf = 50 * (1.0 + 0.4 * x + 0.6 * x**2)
w, spec = pop.get_spectrum(tage=tmax)
pop.params["smooth_lsf"] = True
assert pop.params.dirtiness == 2
pop.set_lsf(wave_lsf, sigma_lsf)
w, smspec = pop.get_spectrum(tage=tmax)
hi = w > 7100
sm = (w < 7000) & (w > 3000)
assert np.allclose(spec[hi] / smspec[hi] - 1.0, 0.0)
assert not np.allclose(spec[sm] / smspec[sm] - 1.0, 0.0)
pop.set_lsf(wave_lsf, sigma_lsf * 2)
assert pop.params.dirtiness == 2
@skip_slow_tests
def test_tabular(pop_and_params):
"""Test that you get the right shape spectral arrays for tabular SFHs, that
the parameter dirtiness is appropriately managed for changing tabular SFH,
and that multi-metallicity SFH work."""
# uses default SSPs, but makes them for every metallicity
pop, params = pop_and_params
_reset_default_params(pop, params)
import os
fn = os.path.join(os.environ["SPS_HOME"], "data/sfh.dat")
age, sfr, z = np.genfromtxt(fn, unpack=True, skip_header=0)
# Mono-metallicity
pop.params["sfh"] = 3
pop.set_tabular_sfh(age, sfr)
w, spec = pop.get_spectrum(tage=0)
pop.set_tabular_sfh(age, sfr)
assert pop.params.dirty
w, spec = pop.get_spectrum(tage=0)
assert spec.shape[0] == len(pop.ssp_ages)
assert pop.params["sfh"] == 3
w, spec_last = pop.get_spectrum(tage=-99)
assert spec_last.ndim == 1
w, spec = pop.get_spectrum(tage=age.max())
assert np.allclose(spec / spec_last - 1.0, 0.0)
pop.params["logzsol"] = -1
w, spec_lowz = pop.get_spectrum(tage=age.max())
assert not np.allclose(spec / spec_lowz - 1.0, 0.0)
# test the formed mass for single age
assert np.allclose(np.trapezoid(sfr, age) * 1e9, pop.formed_mass)
# Multi-metallicity
pop._zcontinuous = 3
pop.set_tabular_sfh(age, sfr, z)
w, spec_multiz = pop.get_spectrum(tage=age.max())
assert not np.allclose(spec_lowz / spec_multiz - 1.0, 0.0)
pop._zcontinuous = 1
pop.set_tabular_sfh(age, sfr)
# get mass weighted metallicity
mbin = np.gradient(age) * sfr
mwz = (z * mbin).sum() / mbin.sum()
pop.params["logzsol"] = np.log10(mwz / pop.solar_metallicity)
w, spec_onez = pop.get_spectrum(tage=age.max())
assert not np.allclose(spec_onez / spec_multiz - 1.0, 0.0)
def test_get_mags(pop_and_params):
"""Basic test for supplying filter names to get_mags"""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
fuv1 = pop.get_mags(bands=["galex_fuv"])[:, 0]
mags = pop.get_mags()
fuv2 = mags[:, 61] # this should be galex_FUV
fuv3 = mags[:, 62] # this should *not* be galex_FUV
assert np.all(fuv1 == fuv2)
assert np.all(fuv1 != fuv3)
def test_ssp(pop_and_params):
"""Test that you get a sensible wavelength array, and that you get a
sensible V-band magnitude for a 1-Gyr SSP"""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["sfh"] = 0
wave, spec = pop.get_spectrum(tage=1, peraa=True)
assert (wave[0] > 0) & (wave[0] < wave[-1]) & (wave[0] < 912.0)
assert (wave[-1] > 1e6) & (wave[-1] < 1e10)
Mv = 4.62 # AB absolute magnitude for a Zsol 1Gyr old SSP
# This also tests get_mags
mag = pop.get_mags(tage=1, bands=["v"])
assert np.all(abs(mag - Mv) < 1.0)
assert np.all((pop.stellar_mass < 1.0) & (pop.stellar_mass > 0))
assert pop.params.dirtiness == 0
def test_libraries(pop_and_params):
"""This does not require or build clean SSPs"""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
ilib, splib, dlib = pop.libraries
assert ilib == pop.isoc_library
assert splib == pop.spec_library
assert dlib == pop.duste_library
def test_filters():
"""Test all the filters got transmission data loaded."""
# uses default SSPs
flist = filters.list_filters()
# force trans cache to load
filters.FILTERS[flist[0]]._load_transmission_cache()
for f in flist:
assert f in filters.TRANS_CACHE, "transmission not loaded for {}".format(f)
def test_csp_dirtiness(pop_and_params):
"""Make sure that changing CSP parameters increases dirtiness to 1"""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["sfh"] = 1
pop.params["tau"] = 1.0
wave, spec = pop.get_spectrum(tage=1.0)
assert pop.params.dirtiness == 0
pop.params["tau"] = 3.0
assert pop.params.dirtiness == 1
def test_redshift(pop_and_params):
"""Test redshifting, make sure that
1. redshifting does not persist in cached arrays
2. specifying redshift via get_mags keyword or param key are consistent
"""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["sfh"] = 0
pop.params["zred"] = 0.0
pop.params["add_igm_absorption"] = False
v1 = pop.get_mags(redshift=1.0, tage=1.0, bands=["v"])
v2 = pop.get_mags(redshift=1.0, tage=1.0, bands=["v"])
assert np.all(v1 == v2)
pop.params["zred"] = 1.0
v3 = pop.get_mags(redshift=None, tage=1.0, bands=["v"])
v4 = pop.get_mags(redshift=None, tage=1.0, bands=["v"])
v5 = pop.get_mags(redshift=0.0, tage=1.0, bands=["v"])
assert np.all(v3 == v4)
assert np.all(v3 == v1)
assert np.all(v5 != v4)
def test_dust3(pop_and_params):
"""Make sure nebular lines are actually added."""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["sfh"] = 4
pop.params["dust_type"] = 4
pop.params["tau"] = 5.0
pop.params["dust1"] = 0
pop.params["dust2"] = 0.5
# make sure dust3 affects the population when there are old stars
pop.params["dust3"] = 0.0
mag1 = pop.get_mags(tage=1.0, bands=["u", "b"])
pop.params["dust3"] = 1.0
mag2 = pop.get_mags(tage=1.0, bands=["u", "b"])
assert np.all(mag2 > mag1)
# make sure the dust3 isn't affecting young populations
pop.params["dust_tesc"] = 8
pop.params["dust3"] = 0.0
mag3 = pop.get_mags(tage=0.05, bands=["u", "b"])
pop.params["dust3"] = 1.0
mag4 = pop.get_mags(tage=0.05, bands=["u", "b"])
assert_allclose(mag3, mag4)
def test_nebemlineinspec(pop_and_params):
"""Make sure nebular lines are actually added."""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["sfh"] = 4
pop.params["tau"] = 5.0
pop.params["add_neb_emission"] = True
pop.params["nebemlineinspec"] = False
wave, spec_neboff = pop.get_spectrum(tage=1.0)
pop.params["nebemlineinspec"] = True
wave, spec_nebon = pop.get_spectrum(tage=1.0)
assert (spec_nebon - spec_neboff).sum() > 0
assert np.all(np.isfinite(pop.emline_luminosity))
assert np.all(np.isfinite(pop.emline_wavelengths))
ha_idx = (wave > 6556) & (wave < 6573)
assert (spec_nebon - spec_neboff)[ha_idx].sum() > 0
def test_mformed(pop_and_params):
"""Make sure formed mass integrates to 1 for parameteric SFH"""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
pop.params["sfh"] = 1
pop.params["const"] = 0.5
w, s = pop.get_spectrum(tage=0)
assert pop.formed_mass[-1] == 1
assert pop.formed_mass[50] < 1.0
assert pop.formed_mass[50] > 0.0
w, s = pop.get_spectrum(tage=0)
assert pop.formed_mass[-1] == 1.0
def test_light_ages(pop_and_params):
"""Make sure light weighting works, and gives sensible answers for the
light-weighted age in the FUV and mass-weighted age stored in
`stellar_mass`"""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
tmax = 5.0
pop.params["sfh"] = 1
pop.params["const"] = 0.5
w, spec = pop.get_spectrum(tage=tmax)
mstar = pop.stellar_mass
lbol = pop.log_lbol
pop.params["compute_light_ages"] = True
w, light_age = pop.get_spectrum(tage=tmax)
assert np.all(np.abs(np.log10(spec / light_age)) > 1)
# make sure fuv really from young stars
fuv = (w > 1220) & (w < 2000)
assert (light_age[fuv]).max() < 0.1
assert (light_age[fuv]).max() > 1e-5
assert pop.log_lbol != lbol
assert pop.stellar_mass != mstar
assert pop.stellar_mass < tmax
# luminosity weighted age always less than mass-weighted age
# assert pop.log_lbol < pop.stellar_mass
def test_smoothspec(pop_and_params):
# FIXME: This is not very stringent
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
wave, spec = pop.get_spectrum(tage=1, peraa=True)
spec2 = pop.smoothspec(wave, spec, 160.0, minw=1e3, maxw=1e4)
assert (spec - spec2 == 0.0).sum() > 0
@skip_slow_tests
def test_ssp_weights(pop_and_params):
"""Check that weights dotted into ssp is the same as the returned spectrum
when there's no dust or emission lines and zcontinuous=0"""
# uses default SSPs
pop, params = pop_and_params
_reset_default_params(pop, params)
import os
fn = os.path.join(os.environ["SPS_HOME"], "data/sfh.dat")
age, sfr, z = np.genfromtxt(fn, unpack=True, skip_header=0)
pop.params["sfh"] = 3
pop.set_tabular_sfh(age, sfr)
zind = -3
pop.params["logzsol"] = np.log10(pop.zlegend[zind] / pop.solar_metallicity)
wave, spec = pop.get_spectrum(tage=age.max())
mstar = pop.stellar_mass
wght = pop._ssp_weights
ssp, smass, slbol = pop._all_ssp_spec()
assert np.allclose((smass[:, zind] * wght[:, 0]).sum(), mstar)
# Requires scipy
# def test_sfr_avg():
# _reset_default_params()
# pop.params['sfh'] = 1.0
# pop.params['const'] = 0.5
# w, spec = pop.get_spectrum(tage=0)
# sfr6 = pop.sfr_avg(dt=1e-3)
# dsfr = np.log10(pop.sfr/pop.sfr6)
# good = pop.ssp_age > 6
# assert np.all(np.abs(dsfr[good]) < 1e-2)
# def test_imf3_multiprocessing():
# from multiprocessing import Pool
# pool = Pool()
# thetas = np.linspace(2.3, 8.3, 4)
# single = map(_get_model, thetas)
# multi = pool.map(_get_model, thetas)
# assert_allclose(single, multi)