Coverage for tests/testParametrizedLightCurveMixin.py : 6%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import unittest
2import tempfile
3import gzip
4import os
5import numpy as np
7import lsst.utils.tests
9from lsst.sims.catUtils.mixins import ParametrizedLightCurveMixin
10from lsst.sims.catUtils.mixins import VariabilityStars
11from lsst.sims.catalogs.definitions import InstanceCatalog
12from lsst.sims.catalogs.db import fileDBObject
13from lsst.sims.utils import ObservationMetaData
14from lsst.sims.utils.CodeUtilities import sims_clean_up
17def setup_module(module):
18 lsst.utils.tests.init()
21class ParametrizedLightCurve_testCase(unittest.TestCase):
23 def test_calc_dflux(self):
24 """
25 Test the method that calculates the flux of
26 parametrized light curves by generating a fake light
27 curve library with known parameters, calculating
28 the fluxes, and comparing to the expected results.
29 """
30 lc_temp_file_name = tempfile.mktemp(prefix='test_calc_dflux_lc',
31 suffix='.gz')
33 rng = np.random.RandomState(7124)
34 n_c_1 = 10
35 a1_list = rng.random_sample(n_c_1)*5.0
36 b1_list = (rng.random_sample(n_c_1)-0.5)*2.0
37 c1_list = (rng.random_sample(n_c_1)-0.5)*0.1
38 omega1_list = rng.random_sample(n_c_1)*20.0
39 tau1_list = rng.random_sample(n_c_1)*100.0
40 median1 = 100.0
42 n_c_2 = 15
43 a2_list = rng.random_sample(n_c_2)*5.0
44 b2_list = (rng.random_sample(n_c_2)-0.5)*2.0
45 c2_list = (rng.random_sample(n_c_2)-0.5)*0.1
46 omega2_list = rng.random_sample(n_c_2)*20.0
47 tau2_list = rng.random_sample(n_c_2)*100.0
48 median2 = 200.0
50 with gzip.open(lc_temp_file_name, 'w') as out_file:
51 out_file.write(b'# a header\n')
52 out_file.write(b'kplr990000000_lc.txt 100 1.0e+02 %d ' % n_c_1)
53 for i_c in range(n_c_1):
54 out_file.write(b'%e ' % (1.0/(i_c+1)))
55 out_file.write(b'%e ' % median1)
56 for i_c in range(n_c_1):
57 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
58 (a1_list[i_c], b1_list[i_c], c1_list[i_c],
59 omega1_list[i_c], tau1_list[i_c]))
60 out_file.write(b'\n')
62 out_file.write(b'kplr990000001_lc.txt 100 1.0e+02 %d ' % n_c_2)
63 for i_c in range(n_c_2):
64 out_file.write(b'%e ' % (1.0/(i_c+1)))
65 out_file.write(b'%e ' % median2)
66 for i_c in range(n_c_2):
67 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
68 (a2_list[i_c], b2_list[i_c], c2_list[i_c],
69 omega2_list[i_c], tau2_list[i_c]))
70 out_file.write(b'\n')
72 expmjd = rng.random_sample(100)*200.0
73 kp = ParametrizedLightCurveMixin()
74 kp.load_parametrized_light_curves(lc_temp_file_name)
76 q_flux, d_flux = kp._calc_dflux(990000000, expmjd)
77 self.assertAlmostEqual(q_flux, median1+c1_list.sum(), 10)
79 true_flux = np.zeros(len(expmjd))
80 for i_c in range(n_c_1):
81 arg = omega1_list[i_c]*(expmjd-tau1_list[i_c])
82 true_flux += a1_list[i_c]*np.cos(arg)
83 true_flux += b1_list[i_c]*np.sin(arg)
84 self.assertEqual(len(d_flux), len(true_flux))
85 np.testing.assert_allclose(d_flux, true_flux, rtol=0.0, atol=1.0e-10)
87 q_flux, d_flux = kp._calc_dflux(990000001, expmjd)
88 self.assertAlmostEqual(q_flux, median2+c2_list.sum(), 10)
90 true_flux = np.zeros(len(expmjd))
91 for i_c in range(n_c_2):
92 arg = omega2_list[i_c]*(expmjd-tau2_list[i_c])
93 true_flux += a2_list[i_c]*np.cos(arg)
94 true_flux += b2_list[i_c]*np.sin(arg)
95 self.assertEqual(len(d_flux), len(true_flux))
96 np.testing.assert_allclose(d_flux, true_flux, rtol=0.0, atol=1.0e-10)
98 sims_clean_up()
99 if os.path.exists(lc_temp_file_name):
100 os.unlink(lc_temp_file_name)
102 def test_applyParametrizedLightCurve_singleExpmjd(self):
103 """
104 test applyParametrizedLightCurve on a single expmjd value
105 by creating a dummy light curve file with known
106 parameters, generating magnitudes, and comparing to
107 the expected outputs.
109 We will use _calc_dflux() to calculate the known truth,
110 since that method was tested in test_calc_dflux()
111 """
113 lc_temp_file_name = tempfile.mktemp(prefix='test_applyParametrizedLightCurve_singleexpmjd',
114 suffix='.gz')
116 rng = np.random.RandomState(5245)
117 n_c_1 = 10
118 a1_list = rng.random_sample(n_c_1)*5.0
119 b1_list = (rng.random_sample(n_c_1)-0.5)*2.0
120 c1_list = (rng.random_sample(n_c_1)-0.5)*0.1
121 omega1_list = rng.random_sample(n_c_1)*20.0
122 tau1_list = rng.random_sample(n_c_1)*100.0
123 median1 = 100.0
125 n_c_2 = 15
126 a2_list = rng.random_sample(n_c_2)*5.0
127 b2_list = (rng.random_sample(n_c_2)-0.5)*2.0
128 c2_list = (rng.random_sample(n_c_2)-0.5)*0.1
129 omega2_list = rng.random_sample(n_c_2)*20.0
130 tau2_list = rng.random_sample(n_c_2)*100.0
131 median2 = 200.0
133 with gzip.open(lc_temp_file_name, 'w') as out_file:
134 out_file.write(b'# a header\n')
135 out_file.write(b'kplr999000000_lc.txt 100 1.0e+02 %d ' % n_c_1)
136 for i_c in range(n_c_1):
137 out_file.write(b'%e ' % (1.0/(i_c+1)))
138 out_file.write(b'%e ' % median1)
139 for i_c in range(n_c_1):
140 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
141 (a1_list[i_c], b1_list[i_c], c1_list[i_c],
142 omega1_list[i_c], tau1_list[i_c]))
143 out_file.write(b'\n')
145 out_file.write(b'kplr999000001_lc.txt 100 1.0e+02 %d ' % n_c_2)
146 for i_c in range(n_c_2):
147 out_file.write(b'%e ' % (1.0/(i_c+1)))
148 out_file.write(b'%e ' % median2)
149 for i_c in range(n_c_2):
150 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
151 (a2_list[i_c], b2_list[i_c], c2_list[i_c],
152 omega2_list[i_c], tau2_list[i_c]))
153 out_file.write(b'\n')
155 params = {}
156 params['lc'] = np.array([999000001, 999000000, None, 999000001])
157 params['t0'] = np.array([223.1, 1781.45, None, 32.0])
159 kp = ParametrizedLightCurveMixin()
160 kp.load_parametrized_light_curves(lc_temp_file_name)
162 # first test that passing in an empty set of params
163 # results in an empty numpy array (so that the 'dry
164 # run' of catalog generation does not fail)
165 d_mag_out = kp.applyParametrizedLightCurve([],{},1.0)
166 np.testing.assert_array_equal(d_mag_out,
167 np.array([[],[],[],[],[],[]]))
169 expmjd = 59580.0
170 d_mag_out = kp.applyParametrizedLightCurve([], params, expmjd)
171 self.assertEqual(d_mag_out.shape, (6, 4))
173 for i_obj in range(4):
174 if i_obj == 2:
175 for i_filter in range(6):
176 self.assertEqual(d_mag_out[i_filter][i_obj], 0.0)
177 else:
178 q_flux, d_flux = kp._calc_dflux(params['lc'][i_obj],
179 expmjd-params['t0'][i_obj])
181 d_mag_truth = -2.5*np.log10(1.0+d_flux/q_flux)
182 self.assertFalse(np.isnan(d_mag_truth))
183 for i_filter in range(6):
184 self.assertAlmostEqual(d_mag_out[i_filter][i_obj]/d_mag_truth, 1.0, 12)
186 sims_clean_up()
187 if os.path.exists(lc_temp_file_name):
188 os.unlink(lc_temp_file_name)
190 def test_applyParametrizedLightCurve_singleExpmjd_as_array(self):
191 """
192 test applyParametrizedLightCurve on an aray of expmjd values
193 that only contains one value by creating a dummy light curve
194 file with known parameters, generating magnitudes, and comparing
195 to the expected outputs.
197 We will use _calc_dflux() to calculate the known truth,
198 since that method was tested in test_calc_dflux()
199 """
201 lc_temp_file_name = tempfile.mktemp(prefix='test_applyParametrizedLightCurve_singleexpmjd',
202 suffix='.gz')
204 rng = np.random.RandomState(5245)
205 n_c_1 = 10
206 a1_list = rng.random_sample(n_c_1)*5.0
207 b1_list = (rng.random_sample(n_c_1)-0.5)*2.0
208 c1_list = (rng.random_sample(n_c_1)-0.5)*0.1
209 omega1_list = rng.random_sample(n_c_1)*20.0
210 tau1_list = rng.random_sample(n_c_1)*100.0
211 median1 = 100.0
213 n_c_2 = 15
214 a2_list = rng.random_sample(n_c_2)*5.0
215 b2_list = (rng.random_sample(n_c_2)-0.5)*2.0
216 c2_list = (rng.random_sample(n_c_2)-0.5)*0.1
217 omega2_list = rng.random_sample(n_c_2)*20.0
218 tau2_list = rng.random_sample(n_c_2)*100.0
219 median2 = 200.0
221 with gzip.open(lc_temp_file_name, 'w') as out_file:
222 out_file.write(b'# a header\n')
223 out_file.write(b'kplr999000000_lc.txt 100 1.0e+02 %d ' % n_c_1)
224 for i_c in range(n_c_1):
225 out_file.write(b'%e ' % (1.0/(i_c+1)))
226 out_file.write(b'%e ' % median1)
227 for i_c in range(n_c_1):
228 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
229 (a1_list[i_c], b1_list[i_c], c1_list[i_c],
230 omega1_list[i_c], tau1_list[i_c]))
231 out_file.write(b'\n')
233 out_file.write(b'kplr999000001_lc.txt 100 1.0e+02 %d ' % n_c_2)
234 for i_c in range(n_c_2):
235 out_file.write(b'%e ' % (1.0/(i_c+1)))
236 out_file.write(b'%e ' % median2)
237 for i_c in range(n_c_2):
238 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
239 (a2_list[i_c], b2_list[i_c], c2_list[i_c],
240 omega2_list[i_c], tau2_list[i_c]))
241 out_file.write(b'\n')
243 params = {}
244 params['lc'] = np.array([999000001, 999000000, None, 999000001])
245 params['t0'] = np.array([223.1, 1781.45, None, 32.0])
247 kp = ParametrizedLightCurveMixin()
248 kp.load_parametrized_light_curves(lc_temp_file_name)
250 # first test that passing in an empty set of params
251 # results in an empty numpy array (so that the 'dry
252 # run' of catalog generation does not fail)
253 d_mag_out = kp.applyParametrizedLightCurve([],{},1.0)
254 np.testing.assert_array_equal(d_mag_out,
255 np.array([[],[],[],[],[],[]]))
257 expmjd = np.array([59580.0])
258 d_mag_out = kp.applyParametrizedLightCurve([], params, expmjd)
259 self.assertEqual(d_mag_out.shape, (6, 4, 1))
261 for i_obj in range(4):
262 if i_obj == 2:
263 for i_filter in range(6):
264 self.assertEqual(d_mag_out[i_filter][i_obj], 0.0)
265 else:
266 q_flux, d_flux = kp._calc_dflux(params['lc'][i_obj],
267 expmjd-params['t0'][i_obj])
269 d_mag_truth = -2.5*np.log10(1.0+d_flux/q_flux)
270 self.assertFalse(np.isnan(d_mag_truth))
271 for i_filter in range(6):
272 self.assertEqual(len(d_mag_out[i_filter][i_obj]), 1)
273 self.assertAlmostEqual(d_mag_out[i_filter][i_obj][0]/d_mag_truth, 1.0, 12)
275 sims_clean_up()
276 if os.path.exists(lc_temp_file_name):
277 os.unlink(lc_temp_file_name)
280 def test_applyParametrizedLightCurve_manyExpmjd(self):
281 """
282 test applyParametrizedLightCurve on an array of expmjd values
283 by creating a dummy light curve file with known
284 parameters, generating magnitudes, and comparing to
285 the expected outputs.
287 We will use _calc_dflux() to calculate the known truth,
288 since that method was tested in test_calc_dflux()
289 """
291 lc_temp_file_name = tempfile.mktemp(prefix='test_applyParametrizedLightCurve_manyexpmjd',
292 suffix='.gz')
294 rng = np.random.RandomState(13291)
295 n_c_1 = 10
296 a1_list = rng.random_sample(n_c_1)*5.0
297 b1_list = (rng.random_sample(n_c_1)-0.5)*2.0
298 c1_list = (rng.random_sample(n_c_1)-0.5)*0.1
299 omega1_list = rng.random_sample(n_c_1)*20.0
300 tau1_list = rng.random_sample(n_c_1)*100.0
301 median1 = 100.0
303 n_c_2 = 15
304 a2_list = rng.random_sample(n_c_2)*5.0
305 b2_list = (rng.random_sample(n_c_2)-0.5)*2.0
306 c2_list = (rng.random_sample(n_c_2)-0.5)*0.1
307 omega2_list = rng.random_sample(n_c_2)*20.0
308 tau2_list = rng.random_sample(n_c_2)*100.0
309 median2 = 200.0
311 with gzip.open(lc_temp_file_name, 'w') as out_file:
312 out_file.write(b'# a header\n')
313 out_file.write(b'kplr999900000_lc.txt 100 1.0e+02 %d ' % n_c_1)
314 for i_c in range(n_c_1):
315 out_file.write(b'%e ' % (1.0/(i_c+1)))
316 out_file.write(b'%e ' % median1)
317 for i_c in range(n_c_1):
318 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
319 (a1_list[i_c], b1_list[i_c], c1_list[i_c],
320 omega1_list[i_c], tau1_list[i_c]))
321 out_file.write(b'\n')
323 out_file.write(b'kplr999900001_lc.txt 100 1.0e+02 %d ' % n_c_2)
324 for i_c in range(n_c_2):
325 out_file.write(b'%e ' % (1.0/(i_c+1)))
326 out_file.write(b'%e ' % median2)
327 for i_c in range(n_c_2):
328 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
329 (a2_list[i_c], b2_list[i_c], c2_list[i_c],
330 omega2_list[i_c], tau2_list[i_c]))
331 out_file.write(b'\n')
333 params = {}
334 params['lc'] = np.array([999900001, 999900000, None, 999900001])
335 params['t0'] = np.array([223.1, 1781.45, None, 32.0])
337 kp = ParametrizedLightCurveMixin()
338 kp.load_parametrized_light_curves(lc_temp_file_name)
340 # first test that passing in an empty set of params
341 # results in an empty numpy array (so that the 'dry
342 # run' of catalog generation does not fail)
343 d_mag_out = kp.applyParametrizedLightCurve([],{},1.0)
344 np.testing.assert_array_equal(d_mag_out,
345 np.array([[],[],[],[],[],[]]))
347 expmjd = rng.random_sample(10)*10000.0 + 59580.0
348 d_mag_out = kp.applyParametrizedLightCurve([], params, expmjd)
349 self.assertEqual(d_mag_out.shape, (6, 4, 10))
351 for i_obj in range(4):
352 if i_obj == 2:
353 for i_filter in range(6):
354 np.testing.assert_array_equal(d_mag_out[i_filter][i_obj],
355 np.zeros(10))
356 else:
357 q_flux, d_flux = kp._calc_dflux(params['lc'][i_obj],
358 expmjd-params['t0'][i_obj])
360 d_mag_truth = -2.5*np.log10(1.0+d_flux/q_flux)
361 nan_vals = np.where(np.isnan(d_mag_truth))
362 self.assertEqual(len(nan_vals[0]), 0)
363 for i_filter in range(6):
364 np.testing.assert_array_equal(d_mag_out[i_filter][i_obj], d_mag_truth)
366 sims_clean_up()
367 if os.path.exists(lc_temp_file_name):
368 os.unlink(lc_temp_file_name)
370 def test_ParametrizedLightCurve_in_catalog(self):
371 """
372 Test the performance of applyParametrizedLightCurve()
373 in the context of an InstanceCatalog
374 """
376 # Create dummy light curve parameters
377 lc_temp_file_name = tempfile.mktemp(prefix='test_ParametrizedLightCurve_in_catalog',
378 suffix='.gz')
380 rng = np.random.RandomState(1621145)
381 n_c_1 = 10
382 a1_list = rng.random_sample(n_c_1)*5.0
383 b1_list = (rng.random_sample(n_c_1)-0.5)*2.0
384 c1_list = (rng.random_sample(n_c_1)-0.5)*0.1
385 omega1_list = rng.random_sample(n_c_1)*20.0
386 tau1_list = rng.random_sample(n_c_1)*100.0
387 median1 = 100.0
389 n_c_2 = 15
390 a2_list = rng.random_sample(n_c_2)*5.0
391 b2_list = (rng.random_sample(n_c_2)-0.5)*2.0
392 c2_list = (rng.random_sample(n_c_2)-0.5)*0.1
393 omega2_list = rng.random_sample(n_c_2)*20.0
394 tau2_list = rng.random_sample(n_c_2)*100.0
395 median2 = 200.0
397 with gzip.open(lc_temp_file_name, 'w') as out_file:
398 out_file.write(b'# a header\n')
399 out_file.write(b'kplr999990000_lc.txt 100 1.0e+02 %d ' % n_c_1)
400 for i_c in range(n_c_1):
401 out_file.write(b'%e ' % (1.0/(i_c+1)))
402 out_file.write(b'%e ' % median1)
403 for i_c in range(n_c_1):
404 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
405 (a1_list[i_c], b1_list[i_c], c1_list[i_c],
406 omega1_list[i_c], tau1_list[i_c]))
407 out_file.write(b'\n')
409 out_file.write(b'kplr999990001_lc.txt 100 1.0e+02 %d ' % n_c_2)
410 for i_c in range(n_c_2):
411 out_file.write(b'%e ' % (1.0/(i_c+1)))
412 out_file.write(b'%e ' % median2)
413 for i_c in range(n_c_2):
414 out_file.write(b'%.15e %.15e %.15e %.15e %.15e ' %
415 (a2_list[i_c], b2_list[i_c], c2_list[i_c],
416 omega2_list[i_c], tau2_list[i_c]))
417 out_file.write(b'\n')
419 # Create dummy database of astrophysical sources
420 db_temp_file_name = tempfile.mktemp(prefix='test_ParametrizedLightCurve_in_catalog_db',
421 suffix='.txt')
423 lc_list = [999990001, None, 999990001, 999990000]
424 t0_list = [1729.1, None, 2345.1, 10.9]
426 with open(db_temp_file_name, 'w') as out_file:
427 out_file.write('# a header\n')
428 for i_obj in range(len(lc_list)):
429 if lc_list[i_obj] is not None:
430 paramStr = '{"m":"kplr", "p":{"lc":%d, "t0":%.3f}}' % (lc_list[i_obj], t0_list[i_obj])
431 else:
432 paramStr = None
433 out_file.write('%d;10.0;20.0;0.01;0.01;%s\n' % (i_obj, paramStr))
435 dtype = np.dtype([('simobjid', int), ('ra', float), ('dec', float),
436 ('ebv', float), ('parallax', float), ('varParamStr', str, 100)])
437 db = fileDBObject(db_temp_file_name, runtable='test', dtype=dtype, delimiter=';',
438 idColKey='simobjid')
440 class ParametrizedVarParamStrCat(InstanceCatalog, VariabilityStars):
441 column_outputs = ['simobjid', 'delta_lsst_u', 'delta_lsst_g', 'delta_lsst_r',
442 'delta_lsst_i', 'delta_lsst_z', 'delta_lsst_y']
443 default_formats = {'f':'%.15g'}
446 obs = ObservationMetaData(mjd=59580.0)
447 cat = ParametrizedVarParamStrCat(db, obs_metadata=obs)
448 cat.load_parametrized_light_curves(lc_temp_file_name)
449 cat_out_name = tempfile.mktemp(prefix='test_ParametrizedLightCurve_in_cat_out',
450 suffix='.txt')
452 cat.write_catalog(cat_out_name)
454 kp = ParametrizedLightCurveMixin()
455 cat_dtype = np.dtype([('simobjid', int), ('du', float), ('dg', float),
456 ('dr', float), ('di', float), ('dz', float),
457 ('dy', float)])
459 cat_data = np.genfromtxt(cat_out_name, dtype=cat_dtype, delimiter=', ')
461 for i_obj in range(len(cat_data)):
462 obj_id = cat_data['simobjid'][i_obj]
463 if lc_list[obj_id] is None:
464 self.assertEqual(cat_data['du'][i_obj], 0.0)
465 self.assertEqual(cat_data['dg'][i_obj], 0.0)
466 self.assertEqual(cat_data['dr'][i_obj], 0.0)
467 self.assertEqual(cat_data['di'][i_obj], 0.0)
468 self.assertEqual(cat_data['dz'][i_obj], 0.0)
469 self.assertEqual(cat_data['dy'][i_obj], 0.0)
470 else:
471 q_flux, d_flux = kp._calc_dflux(lc_list[obj_id], obs.mjd.TAI-t0_list[obj_id])
472 d_mag_true = -2.5*np.log10(1.0+d_flux/q_flux)
473 self.assertGreater(np.abs(d_mag_true), 0.0001)
474 self.assertAlmostEqual(cat_data['du'][i_obj], d_mag_true, 15)
475 self.assertAlmostEqual(cat_data['dg'][i_obj], d_mag_true, 15)
476 self.assertAlmostEqual(cat_data['dr'][i_obj], d_mag_true, 15)
477 self.assertAlmostEqual(cat_data['di'][i_obj], d_mag_true, 15)
478 self.assertAlmostEqual(cat_data['dz'][i_obj], d_mag_true, 15)
479 self.assertAlmostEqual(cat_data['dy'][i_obj], d_mag_true, 15)
481 if os.path.exists(cat_out_name):
482 os.unlink(cat_out_name)
484 if os.path.exists(db_temp_file_name):
485 os.unlink(db_temp_file_name)
487 sims_clean_up()
488 if os.path.exists(lc_temp_file_name):
489 os.unlink(lc_temp_file_name)
492class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
493 pass
495if __name__ == "__main__": 495 ↛ 496line 495 didn't jump to line 496, because the condition on line 495 was never true
496 lsst.utils.tests.init()
497 unittest.main()