def test_get_pointings_v4(self): """ Test that the get_pointings method runs on an OpSim v4 database """ v4_db = os.path.join(getPackageDir('sims_data'), 'OpSimData', 'astro-lsst-01_2014.db') raRange = (78.0, 89.0) decRange = (-74.0, -60.0) bandpass = '******' lc_gen = StellarLightCurveGenerator(self.stellar_db, v4_db) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) self.assertGreater(len(pointings), 1) for group in pointings: for ix, obs in enumerate(group): self.assertAlmostEqual(obs.pointingRA, group[0].pointingRA, 12) self.assertAlmostEqual(obs.pointingDec, group[0].pointingDec, 12) self.assertEqual(obs.bandpass, bandpass) if ix > 0: self.assertGreater(obs.mjd.TAI, group[ix - 1].mjd.TAI)
def test_get_pointings_v4(self): """ Test that the get_pointings method runs on an OpSim v4 database """ v4_db = os.path.join(getPackageDir('sims_data'), 'OpSimData', 'astro-lsst-01_2014.db') raRange = (78.0, 89.0) decRange = (-74.0, -60.0) bandpass = '******' lc_gen = StellarLightCurveGenerator(self.stellar_db, v4_db) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) self.assertGreater(len(pointings), 1) for group in pointings: for ix, obs in enumerate(group): self.assertAlmostEqual(obs.pointingRA, group[0].pointingRA, 12) self.assertAlmostEqual(obs.pointingDec, group[0].pointingDec, 12) self.assertEqual(obs.bandpass, bandpass) if ix > 0: self.assertGreater(obs.mjd.TAI, group[ix-1].mjd.TAI)
def test_fast_stellar_lc_gen(self): bandpass = ('r', 'g') lc_slow = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) lc_slow._lightCurveCatalogClass._mlt_lc_file = self.mlt_lc_file_name ptngs = lc_slow.get_pointings((68.0, 95.0), (-69.0, -55.0), bandpass=bandpass) slow_lc, slow_truth = lc_slow.light_curves_from_pointings(ptngs, chunk_size=10) self.assertEqual(len(slow_truth), self.n_stars) self.assertEqual(len(slow_lc), self.n_stars) lc_fast = FastStellarLightCurveGenerator(self.stellar_db, self.opsimDb) lc_fast._lightCurveCatalogClass._mlt_lc_file = self.mlt_lc_file_name ptngs = lc_fast.get_pointings((68.0, 95.0), (-69.0, -55.0), bandpass=bandpass) fast_lc, fast_truth = lc_fast.light_curves_from_pointings(ptngs, chunk_size=10) self.assertEqual(len(fast_lc), len(slow_lc)) self.assertEqual(len(fast_truth), len(slow_truth)) for obj_id in fast_lc: self.assertEqual(len(fast_lc[obj_id]), len(slow_lc[obj_id])) for bp in fast_lc[obj_id]: self.assertEqual(len(fast_lc[obj_id][bp]), len(slow_lc[obj_id][bp])) for data_key in fast_lc[obj_id][bp]: self.assertEqual(len(slow_lc[obj_id][bp][data_key]), len(fast_lc[obj_id][bp][data_key])) self.assertEqual(slow_lc[obj_id][bp][data_key].shape, slow_lc[obj_id][bp][data_key].shape) self.assertLess(np.abs(fast_lc[obj_id][bp][data_key]-slow_lc[obj_id][bp][data_key]).max(), 1.0e-10) for obj_id in fast_truth: self.assertEqual(fast_truth[obj_id], slow_truth[obj_id])
def test_multiband_light_curves(self): """ Check that multi-band light curves are returned correctly. """ raRange = (78.0, 82.0) decRange = (-69.0, -65.0) bandpass = ('r', 'g') gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = gen.get_pointings(raRange, decRange, bandpass=bandpass) lc_dict, truth_info = gen.light_curves_from_pointings(pointings) self.assertGreater(len(lc_dict), 2) obs_gen = ObservationMetaDataGenerator(database=self.opsimDb, driver='sqlite') control_pointings_r = obs_gen.getObservationMetaData(fieldRA=raRange, fieldDec=decRange, telescopeFilter='r', boundLength=1.75) control_pointings_g = obs_gen.getObservationMetaData(fieldRA=raRange, fieldDec=decRange, telescopeFilter='g', boundLength=1.75) self.assertGreater(len(control_pointings_g), 0) self.assertGreater(len(control_pointings_r), 0) ct = 0 for obs in control_pointings_r: cat = stellarControlCatalog(self.stellar_db, obs_metadata=obs) for star_obj in cat.iter_catalog(): ct += 1 lc = lc_dict[star_obj[0]]['r'] dex = np.argmin(np.abs(lc['mjd']-obs.mjd.TAI)) self.assertLess(np.abs(lc['mjd'][dex]-obs.mjd.TAI), 1.0e-7) self.assertLess(np.abs(lc['mag'][dex]-star_obj[3]), 1.0e-7) self.assertLess(np.abs(lc['error'][dex]-star_obj[4]), 1.0e-7) for obs in control_pointings_g: cat = stellarControlCatalog(self.stellar_db, obs_metadata=obs) for star_obj in cat.iter_catalog(): ct += 1 lc = lc_dict[star_obj[0]]['g'] dex = np.argmin(np.abs(lc['mjd']-obs.mjd.TAI)) self.assertLess(np.abs(lc['mjd'][dex]-obs.mjd.TAI), 1.0e-7) self.assertLess(np.abs(lc['mag'][dex]-star_obj[3]), 1.0e-7) self.assertLess(np.abs(lc['error'][dex]-star_obj[4]), 1.0e-7) # Verify that the same number of objects and observations were found in the # catalogs and the LightCurveGenerator output total_ct = 0 for obj_name in lc_dict: for bandpass in lc_dict[obj_name]: total_ct += len(lc_dict[obj_name][bandpass]['mjd']) self.assertEqual(ct, total_ct)
def test_manual_constraint(self): """ Test that a constraint put in by hand is properly applied """ raRange = (78.0, 89.0) decRange = (-74.0, -60.0) bandpass = '******' lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) (lc_unconstrained, truth_unconstrianed) = lc_gen.light_curves_from_pointings(pointings) (lc_constrained, truth_constrained) = lc_gen.light_curves_from_pointings( pointings, constraint='ebv>0.05') self.assertGreater(len(lc_constrained), 0) self.assertLess(len(lc_constrained), len(lc_unconstrained)) # create catalogs based on all of the pointings in 'pointings'; # verify that the objects in those catalogs appear correctly # in the constrained and unconstrained light curves. class ConstraintCatalogClass(InstanceCatalog): column_outputs = ['uniqueId', 'ebv'] ct_unconstrained = 0 ct_constrained = 0 for field in pointings: for obs in field: cat = ConstraintCatalogClass(self.stellar_db, obs_metadata=obs) for star_obj in cat.iter_catalog(): if star_obj[1] > 0.05: self.assertIn(star_obj[0], lc_constrained) ct_constrained += 1 self.assertIn(star_obj[0], lc_unconstrained) ct_unconstrained += 1 total_ct = 0 for obj_name in lc_unconstrained: for band in lc_unconstrained[obj_name]: total_ct += len(lc_unconstrained[obj_name][band]['mjd']) self.assertEqual(ct_unconstrained, total_ct) total_ct = 0 for obj_name in lc_constrained: for band in lc_constrained[obj_name]: total_ct += len(lc_constrained[obj_name][band]['mjd']) self.assertEqual(ct_constrained, total_ct)
def test_manual_constraint(self): """ Test that a constraint put in by hand is properly applied """ raRange = (78.0, 89.0) decRange = (-74.0, -60.0) bandpass = '******' lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) (lc_unconstrained, truth_unconstrianed) = lc_gen.light_curves_from_pointings(pointings) (lc_constrained, truth_constrained) = lc_gen.light_curves_from_pointings(pointings, constraint = 'ebv>0.05') self.assertGreater(len(lc_constrained), 0) self.assertLess(len(lc_constrained), len(lc_unconstrained)) # create catalogs based on all of the pointings in 'pointings'; # verify that the objects in those catalogs appear correctly # in the constrained and unconstrained light curves. class ConstraintCatalogClass(InstanceCatalog): column_outputs= ['uniqueId', 'ebv'] ct_unconstrained = 0 ct_constrained = 0 for field in pointings: for obs in field: cat = ConstraintCatalogClass(self.stellar_db, obs_metadata=obs) for star_obj in cat.iter_catalog(): if star_obj[1]>0.05: self.assertIn(star_obj[0], lc_constrained) ct_constrained += 1 self.assertIn(star_obj[0], lc_unconstrained) ct_unconstrained += 1 total_ct = 0 for obj_name in lc_unconstrained: for band in lc_unconstrained[obj_name]: total_ct += len(lc_unconstrained[obj_name][band]['mjd']) self.assertEqual(ct_unconstrained, total_ct) total_ct = 0 for obj_name in lc_constrained: for band in lc_constrained[obj_name]: total_ct += len(lc_constrained[obj_name][band]['mjd']) self.assertEqual(ct_constrained, total_ct)
def test_get_pointings_multiband(self): """ Test that the get_pointings method does, in fact, return ObservationMetaData that are grouped appropriately. Test on more than one filter. """ raRange = (78.0, 89.0) decRange = (-74.0, -60.0) bandpass = ('g', 'z') lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) self.assertGreater(len(pointings), 1) ct_g = 0 ct_z = 0 for group in pointings: for ix, obs in enumerate(group): self.assertAlmostEqual(obs.pointingRA, group[0].pointingRA, 12) self.assertAlmostEqual(obs.pointingDec, group[0].pointingDec, 12) if obs.bandpass == 'g': ct_g += 1 elif obs.bandpass == 'z': ct_z += 1 else: raise RuntimeError("Asked for filters (g,z) but got %s" % obs.bandpass) if ix > 0: self.assertGreater(obs.mjd.TAI, group[ix - 1].mjd.TAI) self.assertGreater(ct_g, 0) self.assertGreater(ct_z, 0)
def test_get_pointings(self): """ Test that the get_pointings method does, in fact, return ObservationMetaData that are grouped appropriately. """ raRange = (78.0, 89.0) decRange = (-74.0, -60.0) bandpass = '******' lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) self.assertGreater(len(pointings), 1) for group in pointings: for ix, obs in enumerate(group): self.assertAlmostEqual(obs.pointingRA, group[0].pointingRA, 12) self.assertAlmostEqual(obs.pointingDec, group[0].pointingDec, 12) self.assertEqual(obs.bandpass, bandpass) if ix > 0: self.assertGreater(obs.mjd.TAI, group[ix-1].mjd.TAI)
def test_limited_stellar_light_curves(self): """ Test that we can ask for a limited number of light curves per field of view """ lc_limit = 2 raRange = (78.0, 82.0) decRange = (-69.0, -65.0) bandpass = '******' lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) self.assertEqual(len(pointings), 1) control_light_curves, truth_info = lc_gen.light_curves_from_pointings(pointings) self.assertGreater(len(control_light_curves), 2) test_light_curves, truth_info = lc_gen.light_curves_from_pointings(pointings, lc_per_field=lc_limit) self.assertGreater(len(control_light_curves), len(test_light_curves)) self.assertEqual(len(test_light_curves), lc_limit)
def test_limited_stellar_light_curves(self): """ Test that we can ask for a limited number of light curves per field of view """ lc_limit = 2 raRange = (78.0, 82.0) decRange = (-69.0, -65.0) bandpass = '******' lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) self.assertEqual(len(pointings), 1) control_light_curves, truth_info = lc_gen.light_curves_from_pointings( pointings) self.assertGreater(len(control_light_curves), 2) test_light_curves, truth_info = lc_gen.light_curves_from_pointings( pointings, lc_per_field=lc_limit) self.assertGreater(len(control_light_curves), len(test_light_curves)) self.assertEqual(len(test_light_curves), lc_limit)
def test_get_pointings_multiband(self): """ Test that the get_pointings method does, in fact, return ObservationMetaData that are grouped appropriately. Test on more than one filter. """ raRange = (78.0, 89.0) decRange = (-74.0, -60.0) bandpass = ('g', 'z') lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) self.assertGreater(len(pointings), 1) ct_g = 0 ct_z = 0 for group in pointings: for ix, obs in enumerate(group): self.assertAlmostEqual(obs.pointingRA, group[0].pointingRA, 12) self.assertAlmostEqual(obs.pointingDec, group[0].pointingDec, 12) if obs.bandpass == 'g': ct_g += 1 elif obs.bandpass == 'z': ct_z += 1 else: raise RuntimeError("Asked for filters (g,z) but got %s" % obs.bandpass) if ix > 0: self.assertGreater(obs.mjd.TAI, group[ix-1].mjd.TAI) self.assertGreater(ct_g, 0) self.assertGreater(ct_z, 0)
def test_get_pointings(self): """ Test that the get_pointings method does, in fact, return ObservationMetaData that are grouped appropriately. """ raRange = (78.0, 89.0) decRange = (-74.0, -60.0) bandpass = '******' lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) self.assertGreater(len(pointings), 1) for group in pointings: for ix, obs in enumerate(group): self.assertAlmostEqual(obs.pointingRA, group[0].pointingRA, 12) self.assertAlmostEqual(obs.pointingDec, group[0].pointingDec, 12) self.assertEqual(obs.bandpass, bandpass) if ix > 0: self.assertGreater(obs.mjd.TAI, group[ix - 1].mjd.TAI)
def test_constraint(self): """ Test that the light curve generator correctly ignores objects with varParamStr == None We do this by generating a database of two stars with RRLyrae-like varParamStr and two stars with no varParamStr. We run this database through a LightCurveGenerator and an InstanceCatalog. We verify that the LightCurveGenerator finds 2 stars while the InstanceCatalog finds 4 stars. """ rng = np.random.RandomState(83) raRange = (80.8, 83.8) decRange = (-71.0, -69.0) bandpass = '******' sed_name = "kp01_7500.fits_g40_7600.gz" varparams = { 'varMethodName': 'applyRRly', 'pars': { 'tStartMjd': 30000.0, 'filename': 'rrly_lc/RRab/1096833_per.txt' } } varParamStr = json.dumps(varparams) # create the dummy database db_name = tempfile.mktemp(prefix='stellar_constraint_cata_sqlite-', suffix='.db', dir=ROOT) conn = sqlite3.connect(db_name) c = conn.cursor() c.execute('''CREATE TABLE rrly (id int, ra real, dec real, sedFilename text, magNorm real, varParamStr text, galacticAv real, parallax real, ebv real)''' ) conn.commit() for ix, (rr, dd, mn, px) in \ enumerate(zip(rng.random_sample(4)*(raRange[1]-raRange[0])+raRange[0], rng.random_sample(4)*(decRange[1]-decRange[0])+decRange[0], rng.random_sample(4)*5.0+16.0, rng.random_sample(4)*0.01)): if ix < 2: cmd = '''INSERT INTO rrly VALUES(%d, %e, %e, '%s', %e, '%s', 0.1, 0.032, %e)''' % \ (ix, rr, dd, sed_name, mn, varParamStr, px) else: cmd = '''INSERT INTO rrly VALUES(%d, %e, %e, '%s', %e, NULL, 0.1, 0.032, %e)''' % \ (ix, rr, dd, sed_name, mn, px) c.execute(cmd) conn.commit() # create a CatalogDBObject class to interface with the dummy database class dummyRRLyDBObject(CatalogDBObject): database = db_name host = None driver = 'sqlite' tableid = 'rrly' raColName = 'ra' decColName = 'dec' idColKey = 'id' objid = 'dummyRRLy' skipRegistration = True objectTypeId = 99 columns = [('raJ2000', 'ra*PI()/180.0', float), ('decJ2000', 'dec*PI()/180.0', float)] star_db = dummyRRLyDBObject() # verify that the LightCurveGenerator finds the two variable stars lc_gen = StellarLightCurveGenerator(star_db, self.opsimDb) ptngs = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) lc_dict, truth_dict = lc_gen.light_curves_from_pointings(ptngs) self.assertEqual(len(lc_dict), 2) if os.path.exists(db_name): os.unlink(db_name) # verify that an InstanceCatalog finds all 4 stars obs = ptngs[0][0] cat = stellarControlCatalog(star_db, obs_metadata=obs) with lsst.utils.tests.getTempFilePath('.txt') as dummy_cat_name: cat.write_catalog(dummy_cat_name) with open(dummy_cat_name, 'r') as input_file: lines = input_file.readlines() self.assertEqual(len(lines), 5)
def test_multiband_light_curves(self): """ Check that multi-band light curves are returned correctly. """ raRange = (78.0, 82.0) decRange = (-69.0, -65.0) bandpass = ('r', 'g') gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = gen.get_pointings(raRange, decRange, bandpass=bandpass) lc_dict, truth_info = gen.light_curves_from_pointings(pointings) self.assertGreater(len(lc_dict), 2) obs_gen = ObservationMetaDataGenerator(database=self.opsimDb, driver='sqlite') control_pointings_r = obs_gen.getObservationMetaData( fieldRA=raRange, fieldDec=decRange, telescopeFilter='r', boundLength=1.75) control_pointings_g = obs_gen.getObservationMetaData( fieldRA=raRange, fieldDec=decRange, telescopeFilter='g', boundLength=1.75) self.assertGreater(len(control_pointings_g), 0) self.assertGreater(len(control_pointings_r), 0) ct = 0 for obs in control_pointings_r: cat = stellarControlCatalog(self.stellar_db, obs_metadata=obs) for star_obj in cat.iter_catalog(): ct += 1 lc = lc_dict[star_obj[0]]['r'] dex = np.argmin(np.abs(lc['mjd'] - obs.mjd.TAI)) self.assertLess(np.abs(lc['mjd'][dex] - obs.mjd.TAI), 1.0e-7) self.assertLess(np.abs(lc['mag'][dex] - star_obj[3]), 1.0e-7) self.assertLess(np.abs(lc['error'][dex] - star_obj[4]), 1.0e-7) for obs in control_pointings_g: cat = stellarControlCatalog(self.stellar_db, obs_metadata=obs) for star_obj in cat.iter_catalog(): ct += 1 lc = lc_dict[star_obj[0]]['g'] dex = np.argmin(np.abs(lc['mjd'] - obs.mjd.TAI)) self.assertLess(np.abs(lc['mjd'][dex] - obs.mjd.TAI), 1.0e-7) self.assertLess(np.abs(lc['mag'][dex] - star_obj[3]), 1.0e-7) self.assertLess(np.abs(lc['error'][dex] - star_obj[4]), 1.0e-7) # Verify that the same number of objects and observations were found in the # catalogs and the LightCurveGenerator output total_ct = 0 for obj_name in lc_dict: for bandpass in lc_dict[obj_name]: total_ct += len(lc_dict[obj_name][bandpass]['mjd']) self.assertEqual(ct, total_ct)
def test_date_range(self): """ Run test_stellar_light_curves, this time specifying a range in MJD. """ raRange = (0.0, 110.0) decRange = (-90.0, -50.0) bandpass = '******' mjdRange = (49356.0, 49357.0) lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass, expMJD=mjdRange) test_light_curves, truth_info = lc_gen.light_curves_from_pointings( pointings) self.assertGreater(len(test_light_curves), 2) for unique_id in test_light_curves: # verify that the sources returned all do vary by making sure that the # np.diff run on the magnitudes reutrns something non-zero self.assertGreater( np.abs(np.diff( test_light_curves[unique_id][bandpass]['mag'])).max(), 0.0) self.assertGreater( len(test_light_curves[unique_id][bandpass]['mjd']), 0) self.assertGreater( test_light_curves[unique_id][bandpass]['mjd'].min(), mjdRange[0] - 1.0e-12) self.assertLess( test_light_curves[unique_id][bandpass]['mjd'].max(), mjdRange[1] + 1.0e-12) # Now test that specifying a small chunk_size does not change the output # light curves chunk_light_curves, truth_info = lc_gen.light_curves_from_pointings( pointings, chunk_size=1) self.assertGreater(len(chunk_light_curves), 2) for unique_id in test_light_curves: self.assertEqual( len(test_light_curves[unique_id][bandpass]['mjd']), len(chunk_light_curves[unique_id][bandpass]['mjd'])) np.testing.assert_array_equal( test_light_curves[unique_id][bandpass]['mjd'], chunk_light_curves[unique_id][bandpass]['mjd']) np.testing.assert_array_equal( test_light_curves[unique_id][bandpass]['mag'], chunk_light_curves[unique_id][bandpass]['mag']) np.testing.assert_array_equal( test_light_curves[unique_id][bandpass]['error'], chunk_light_curves[unique_id][bandpass]['error']) # Now find all of the ObservationMetaData that were included in our # light curves, generate InstanceCatalogs from them separately, # and verify that the contents of the InstanceCatalogs agree with # the contents of the light curves. gen = ObservationMetaDataGenerator(database=self.opsimDb, driver='sqlite') obs_list = gen.getObservationMetaData(fieldRA=raRange, fieldDec=decRange, telescopeFilter=bandpass, expMJD=mjdRange, boundLength=1.75) ct = 0 for obs in obs_list: cat = stellarControlCatalog(self.stellar_db, obs_metadata=obs) for star_obj in cat.iter_catalog(): ct += 1 lc = test_light_curves[star_obj[0]][bandpass] dex = np.argmin(np.abs(lc['mjd'] - obs.mjd.TAI)) self.assertLess(np.abs(lc['mjd'][dex] - obs.mjd.TAI), 1.0e-7) self.assertLess(np.abs(lc['mag'][dex] - star_obj[3]), 1.0e-7) self.assertLess(np.abs(lc['error'][dex] - star_obj[4]), 1.0e-7) # Verify that the same number of objects and observations were found in the # catalogs and the LightCurveGenerator output total_ct = 0 for obj_name in test_light_curves: for bandpass in test_light_curves[obj_name]: total_ct += len(test_light_curves[obj_name][bandpass]['mjd']) self.assertEqual(ct, total_ct)
def test_constraint(self): """ Test that the light curve generator correctly ignores objects with varParamStr == None We do this by generating a database of two stars with RRLyrae-like varParamStr and two stars with no varParamStr. We run this database through a LightCurveGenerator and an InstanceCatalog. We verify that the LightCurveGenerator finds 2 stars while the InstanceCatalog finds 4 stars. """ rng = np.random.RandomState(83) raRange = (80.8, 83.8) decRange = (-71.0, -69.0) bandpass = '******' sed_name = "kp01_7500.fits_g40_7600.gz" varparams = {'varMethodName': 'applyRRly', 'pars': {'tStartMjd': 30000.0, 'filename': 'rrly_lc/RRab/1096833_per.txt'}} varParamStr = json.dumps(varparams) # create the dummy database db_name = tempfile.mktemp(prefix='stellar_constraint_cata_sqlite-', suffix='.db', dir=ROOT) conn = sqlite3.connect(db_name) c = conn.cursor() c.execute('''CREATE TABLE rrly (id int, ra real, dec real, sedFilename text, magNorm real, varParamStr text, galacticAv real, parallax real, ebv real)''') conn.commit() for ix, (rr, dd, mn, px) in \ enumerate(zip(rng.random_sample(4)*(raRange[1]-raRange[0])+raRange[0], rng.random_sample(4)*(decRange[1]-decRange[0])+decRange[0], rng.random_sample(4)*5.0+16.0, rng.random_sample(4)*0.01)): if ix < 2: cmd = '''INSERT INTO rrly VALUES(%d, %e, %e, '%s', %e, '%s', 0.1, 0.032, %e)''' % \ (ix, rr, dd, sed_name, mn, varParamStr, px) else: cmd = '''INSERT INTO rrly VALUES(%d, %e, %e, '%s', %e, NULL, 0.1, 0.032, %e)''' % \ (ix, rr, dd, sed_name, mn, px) c.execute(cmd) conn.commit() # create a CatalogDBObject class to interface with the dummy database class dummyRRLyDBObject(CatalogDBObject): database = db_name host = None driver = 'sqlite' tableid = 'rrly' raColName = 'ra' decColName = 'dec' idColKey = 'id' objid = 'dummyRRLy' skipRegistration = True objectTypeId = 99 columns = [('raJ2000', 'ra*PI()/180.0', float), ('decJ2000', 'dec*PI()/180.0', float)] star_db = dummyRRLyDBObject() # verify that the LightCurveGenerator finds the two variable stars lc_gen = StellarLightCurveGenerator(star_db, self.opsimDb) ptngs = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass) lc_dict, truth_dict = lc_gen.light_curves_from_pointings(ptngs) self.assertEqual(len(lc_dict), 2) if os.path.exists(db_name): os.unlink(db_name) # verify that an InstanceCatalog finds all 4 stars obs = ptngs[0][0] cat = stellarControlCatalog(star_db, obs_metadata=obs) with lsst.utils.tests.getTempFilePath('.txt') as dummy_cat_name: cat.write_catalog(dummy_cat_name) with open(dummy_cat_name, 'r') as input_file: lines = input_file.readlines() self.assertEqual(len(lines), 5)
def test_date_range(self): """ Run test_stellar_light_curves, this time specifying a range in MJD. """ raRange = (0.0, 110.0) decRange = (-90.0, -50.0) bandpass = '******' mjdRange = (49356.0, 49357.0) lc_gen = StellarLightCurveGenerator(self.stellar_db, self.opsimDb) pointings = lc_gen.get_pointings(raRange, decRange, bandpass=bandpass, expMJD=mjdRange) test_light_curves, truth_info = lc_gen.light_curves_from_pointings(pointings) self.assertGreater(len(test_light_curves), 2) for unique_id in test_light_curves: # verify that the sources returned all do vary by making sure that the # np.diff run on the magnitudes reutrns something non-zero self.assertGreater(np.abs(np.diff(test_light_curves[unique_id][bandpass]['mag'])).max(), 0.0) self.assertGreater(len(test_light_curves[unique_id][bandpass]['mjd']), 0) self.assertGreater(test_light_curves[unique_id][bandpass]['mjd'].min(), mjdRange[0]-1.0e-12) self.assertLess(test_light_curves[unique_id][bandpass]['mjd'].max(), mjdRange[1]+1.0e-12) # Now test that specifying a small chunk_size does not change the output # light curves chunk_light_curves, truth_info = lc_gen.light_curves_from_pointings(pointings, chunk_size=1) self.assertGreater(len(chunk_light_curves), 2) for unique_id in test_light_curves: self.assertEqual(len(test_light_curves[unique_id][bandpass]['mjd']), len(chunk_light_curves[unique_id][bandpass]['mjd'])) np.testing.assert_array_equal(test_light_curves[unique_id][bandpass]['mjd'], chunk_light_curves[unique_id][bandpass]['mjd']) np.testing.assert_array_equal(test_light_curves[unique_id][bandpass]['mag'], chunk_light_curves[unique_id][bandpass]['mag']) np.testing.assert_array_equal(test_light_curves[unique_id][bandpass]['error'], chunk_light_curves[unique_id][bandpass]['error']) # Now find all of the ObservationMetaData that were included in our # light curves, generate InstanceCatalogs from them separately, # and verify that the contents of the InstanceCatalogs agree with # the contents of the light curves. gen = ObservationMetaDataGenerator(database=self.opsimDb, driver='sqlite') obs_list = gen.getObservationMetaData(fieldRA=raRange, fieldDec=decRange, telescopeFilter=bandpass, expMJD=mjdRange, boundLength=1.75) ct = 0 for obs in obs_list: cat = stellarControlCatalog(self.stellar_db, obs_metadata=obs) for star_obj in cat.iter_catalog(): ct += 1 lc = test_light_curves[star_obj[0]][bandpass] dex = np.argmin(np.abs(lc['mjd']-obs.mjd.TAI)) self.assertLess(np.abs(lc['mjd'][dex]-obs.mjd.TAI), 1.0e-7) self.assertLess(np.abs(lc['mag'][dex]-star_obj[3]), 1.0e-7) self.assertLess(np.abs(lc['error'][dex]-star_obj[4]), 1.0e-7) # Verify that the same number of objects and observations were found in the # catalogs and the LightCurveGenerator output total_ct = 0 for obj_name in test_light_curves: for bandpass in test_light_curves[obj_name]: total_ct += len(test_light_curves[obj_name][bandpass]['mjd']) self.assertEqual(ct, total_ct)