def testInitialization(self): """ Test that all of the member variables of BandpassDict are set to the correct value upon construction. """ for nBp in range(3,10,1): nameList, bpList = self.getListOfBandpasses(nBp) testDict = BandpassDict(bpList, nameList) self.assertEqual(len(testDict), nBp) for controlName, testName in zip(nameList, testDict): self.assertEqual(controlName, testName) for controlName, testName in zip(nameList, testDict.keys()): self.assertEqual(controlName, testName) for name, bp in zip(nameList, bpList): numpy.testing.assert_array_almost_equal(bp.wavelen, testDict[name].wavelen, 19) numpy.testing.assert_array_almost_equal(bp.sb, testDict[name].sb, 19) for bpControl, bpTest in zip(bpList, testDict.values()): numpy.testing.assert_array_almost_equal(bpControl.wavelen, bpTest.wavelen, 19) numpy.testing.assert_array_almost_equal(bpControl.sb, bpTest.sb, 19)
def testInitialization(self): """ Test that all of the member variables of BandpassDict are set to the correct value upon construction. """ for nBp in range(3,10,1): nameList, bpList = self.getListOfBandpasses(nBp) testDict = BandpassDict(bpList, nameList) self.assertEqual(len(testDict), nBp) for controlName, testName in zip(nameList, testDict): self.assertEqual(controlName, testName) for controlName, testName in zip(nameList, testDict.keys()): self.assertEqual(controlName, testName) for name, bp in zip(nameList, bpList): numpy.testing.assert_array_almost_equal(bp.wavelen, testDict[name].wavelen, 10) numpy.testing.assert_array_almost_equal(bp.sb, testDict[name].sb, 10) for bpControl, bpTest in zip(bpList, testDict.values()): numpy.testing.assert_array_almost_equal(bpControl.wavelen, bpTest.wavelen, 10) numpy.testing.assert_array_almost_equal(bpControl.sb, bpTest.sb, 10)
def testExceptions(self): """ Test that the correct exceptions are thrown by BandpassDict """ nameList, bpList = self.getListOfBandpasses(4) dummyNameList = copy.deepcopy(nameList) dummyNameList[1] = dummyNameList[0] with self.assertRaises(RuntimeError) as context: testDict = BandpassDict(bpList, dummyNameList) self.assertTrue('occurs twice' in context.exception.message) testDict = BandpassDict(bpList, nameList) with self.assertRaises(AttributeError) as context: testDict.phiArray = None with self.assertRaises(AttributeError) as context: testDict.wavelenStep = 0.9 with self.assertRaises(AttributeError) as context: testDict.wavelenMatch = numpy.arange(10.0,100.0,1.0)
def testWavelenMatch(self): """ Test that when you load bandpasses sampled over different wavelength grids, they all get sampled to the same wavelength grid. """ dwavList = numpy.arange(5.0,25.0,5.0) bpList = [] bpNameList = [] for ix, dwav in enumerate(dwavList): name = 'bp_%d' % ix wavelen = numpy.arange(10.0, 1500.0, dwav) sb = numpy.exp(-0.5*(numpy.power((wavelen-100.0*ix)/100.0,2))) bp = Bandpass(wavelen=wavelen, sb=sb) bpList.append(bp) bpNameList.append(name) # First make sure that we have created distinct wavelength grids for ix in range(len(bpList)): for iy in range(ix+1,len(bpList)): self.assertTrue(len(bpList[ix].wavelen)!=len(bpList[iy].wavelen)) testDict = BandpassDict(bpList, bpNameList) # Now make sure that the wavelength grids in the dict were resampled, but that # the original wavelength grids were not changed for ix in range(len(bpList)): numpy.testing.assert_array_almost_equal(testDict.values()[ix].wavelen, testDict.wavelenMatch, 19) if ix!=0: self.assertTrue(len(testDict.wavelenMatch)!=len(bpList[ix].wavelen))
def _get_sed_mags_and_cosmology(catalog_name): """ Returns 3 numpy arrays: sed_names, sed_mag_list, sed_mag_norm and a dictionary for cosmology sed_names is 1d str array, with length = number of SED files in the library sed_mag_list is MxN float array, with M = number of SED files in the library, and N = number of top hat filters in the catalog sed_mag_norm is 1d float array, with length = number of SED files in the library cosmology = {'H0': H0, 'Om0': Om0} """ gc = GCRCatalogs.load_catalog(catalog_name, config_overwrite=dict(md5=False)) cosmo = dict(H0=gc.cosmology.H0.value, Om0=gc.cosmology.Om0) bp_params_raw = {'disk': set(), 'bulge': set()} for q in gc.list_all_quantities(): m = _gcr_sed_re.match(q) if m: wav0, width, tag = m.groups() bp_params_raw[tag].add((int(wav0), int(width))) assert bp_params_raw['disk'] == bp_params_raw[ 'bulge'], 'SEDs for disk and bulge do not match' assert bp_params_raw['disk'], 'No SED found' bp_params_sorted = sorted(bp_params_raw['disk'], key=lambda p: p[0]) # SED labels in GCR specify the band pass in angstrom, but CatSim uses nm # Hence the conversion factor 0.1 in the code below wav_min = bp_params_sorted[0][0] * 0.1 wav_max = max((0.1 * (wav0 + width) for wav0, width in bp_params_sorted)) wav_grid = np.arange(wav_min, wav_max, 0.1) bp_name_list = list() bp_list = list() for wav0, width in bp_params_sorted: sb_grid = ((wav_grid >= wav0 * 0.1) & (wav_grid <= (wav0 + width) * 0.1)).astype(float) bp_list.append(Bandpass(wavelen=wav_grid, sb=sb_grid)) bp_name_list.append('%d_%d' % (wav0, width)) bandpass_dict = BandpassDict(bp_list, bp_name_list) sed_names = list() sed_mag_list = list() sed_mag_norm = list() imsim_bp = Bandpass() imsim_bp.imsimBandpass() for sed_file_name in os.listdir(_galaxy_sed_dir): spec = Sed() spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name)) sed_names.append(sed_file_name) sed_mag_list.append(tuple(bandpass_dict.magListForSed(spec))) sed_mag_norm.append(spec.calcMag(imsim_bp)) return np.array(sed_names), np.array(sed_mag_list), np.array( sed_mag_norm), cosmo
def testManyMagSystems(self): """ Test that the SSM photometry mixin can simultaneously calculate magnitudes in multiple bandpass systems """ catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'compoundSsmPhotCat.txt') cat = Compound_SSM_photCat(self.photDB) cat.write_catalog(catName) dtype = np.dtype([('id', np.int), ('lsst_u', np.float), ('lsst_g', np.float), ('lsst_r', np.float), ('lsst_i', np.float), ('lsst_z', np.float), ('lsst_y', np.float), ('cartoon_u', np.float), ('cartoon_g', np.float), ('cartoon_r', np.float), ('cartoon_i', np.float), ('cartoon_z', np.float)]) testData = np.genfromtxt(catName, dtype=dtype, delimiter=',') self.assertGreater(len(testData), 0) controlData = np.genfromtxt(self.dbFile, dtype=self.dtype) self.assertGreater(len(controlData), 0) LSSTbandpasses = BandpassDict.loadTotalBandpassesFromFiles() bandpassDir = os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData') cartoonBandpasses = BandpassDict.loadTotalBandpassesFromFiles( ['u', 'g', 'r', 'i', 'z'], bandpassDir=bandpassDir, bandpassRoot='test_bandpass_') controlSedList = SedList(controlData['sedFilename'], controlData['magNorm'], wavelenMatch=LSSTbandpasses.wavelenMatch) controlLsstMags = LSSTbandpasses.magListForSedList(controlSedList) controlCartoonMags = cartoonBandpasses.magListForSedList( controlSedList) for ii in range(len(controlLsstMags)): for jj, bpName in enumerate( ['lsst_u', 'lsst_g', 'lsst_r', 'lsst_i', 'lsst_z', 'lsst_y']): self.assertAlmostEqual(controlLsstMags[ii][jj], testData[bpName][ii], 10) for jj, bpName in enumerate([ 'cartoon_u', 'cartoon_g', 'cartoon_r', 'cartoon_i', 'cartoon_z' ]): self.assertAlmostEqual(controlCartoonMags[ii][jj], testData[bpName][ii], 10) if os.path.exists(catName): os.unlink(catName)
def testFluxListForSedList(self): """ Test that fluxListForSedList calculates the correct fluxes """ nBandpasses = 7 bpNameList, bpList = self.getListOfBandpasses(nBandpasses) testBpDict = BandpassDict(bpList, bpNameList) nSed = 20 sedNameList = self.getListOfSedNames(nSed) magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList = self.rng.random_sample(nSed)*5.0 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 # first, test on an SedList without a wavelenMatch testSedList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList) fluxList = testBpDict.fluxListForSedList(testSedList) self.assertEqual(fluxList.shape[0], nSed) self.assertEqual(fluxList.shape[1], nBandpasses) for ix, sedObj in enumerate(testSedList): dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), flambda=copy.deepcopy(sedObj.flambda)) for iy, bp in enumerate(testBpDict): flux = dummySed.calcFlux(bpList[iy]) self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2) # now use wavelenMatch testSedList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList, wavelenMatch=testBpDict.wavelenMatch) fluxList = testBpDict.fluxListForSedList(testSedList) self.assertEqual(fluxList.shape[0], nSed) self.assertEqual(fluxList.shape[1], nBandpasses) for ix, sedObj in enumerate(testSedList): dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), flambda=copy.deepcopy(sedObj.flambda)) for iy, bp in enumerate(testBpDict): flux = dummySed.calcFlux(bpList[iy]) self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2)
def testMagArrayForSedList(self): """ Test that magArrayForSedList calculates the correct magnitude """ nBandpasses = 7 bpNameList, bpList = self.getListOfBandpasses(nBandpasses) testBpDict = BandpassDict(bpList, bpNameList) nSed = 20 sedNameList = self.getListOfSedNames(nSed) magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 redshiftList = self.rng.random_sample(nSed)*5.0 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 # first, test on an SedList without a wavelenMatch testSedList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList) magArray = testBpDict.magArrayForSedList(testSedList) for ix, sedObj in enumerate(testSedList): dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), flambda=copy.deepcopy(sedObj.flambda)) for iy, bp in enumerate(bpNameList): mag = dummySed.calcMag(bpList[iy]) self.assertAlmostEqual(mag, magArray[bp][ix], 2) # now use wavelenMatch testSedList = SedList(sedNameList, magNormList, fileDir=self.sedDir, internalAvList=internalAvList, redshiftList=redshiftList, galacticAvList=galacticAvList, wavelenMatch=testBpDict.wavelenMatch) magArray = testBpDict.magArrayForSedList(testSedList) for ix, sedObj in enumerate(testSedList): dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), flambda=copy.deepcopy(sedObj.flambda)) for iy, bp in enumerate(bpNameList): mag = dummySed.calcMag(bpList[iy]) self.assertAlmostEqual(mag, magArray[bp][ix], 2)
def _create_sed_library_mags(wav_min, wav_width): """ Calculate the magnitudes of the SEDs in sims_sed_library dir in the tophat filters specified by wav_min, wav_width Parameters ---------- wav_min is a numpy array of the minimum wavelengths of the tophat filters (in nm) wav_width is a numpy array of the widths of the tophat filters (in nm) Returns ------- sed_names is an array containing the names of the SED files sed_mag_list is MxN float array, with M = number of SED files in the library, and N = number of top hat filters in the catalog sed_mag_norm is 1d float array, with length = number of SED files in the library """ wav_max = max((wav0 + width for wav0, width in zip(wav_min, wav_width))) wav_grid = np.arange(wav_min.min(), wav_max, 0.1) bp_name_list = list() bp_list = list() for wav0, width in zip(wav_min, wav_width): sb_grid = ((wav_grid >= wav0) & (wav_grid <= (wav0 + width))).astype(float) bp_list.append(Bandpass(wavelen=wav_grid, sb=sb_grid)) bp_name_list.append('%d_%d' % (wav0, width)) bandpass_dict = BandpassDict(bp_list, bp_name_list) sed_names = list() sed_mag_list = list() sed_mag_norm = list() imsim_bp = Bandpass() imsim_bp.imsimBandpass() for sed_file_name in os.listdir(_galaxy_sed_dir): spec = Sed() spec.readSED_flambda(os.path.join(_galaxy_sed_dir, sed_file_name)) sed_names.append(defaultSpecMap[sed_file_name]) sed_mag_list.append(tuple(bandpass_dict.magListForSed(spec))) sed_mag_norm.append(spec.calcMag(imsim_bp)) return np.array(sed_names), np.array(sed_mag_list), np.array(sed_mag_norm)
def setUp(self): """ setup before tests """ throughputsDir = getPackageDir('throughputs') self.approxBandPassDir = os.path.join(throughputsDir, 'approximate_baseline') self.refBandPassDir = os.path.join(throughputsDir, 'baseline') self.refBandPassDict = BandpassDict.loadTotalBandpassesFromFiles() self.approxBandPassDict = \ BandpassDict.loadTotalBandpassesFromFiles(bandpassDir=self.approxBandPassDir) self.errorMsg = "The failure of this test indicates that the" " approximate bandpasses in the lsst throughputs directory do not" "sync up with the baseline bandpasses is throughputs. This may require running" " the script : throughputs.approximate_baseline/approximateBandpasses.py"
def testLoadTotalBandpassesFromFiles(self): """ Test that the class method loadTotalBandpassesFromFiles produces the expected result """ bandpassDir = os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData') bandpassNames = ['g', 'r', 'u'] bandpassRoot = 'test_bandpass_' bandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames=bandpassNames, bandpassDir=bandpassDir, bandpassRoot = bandpassRoot) controlBandpassList = [] for bpn in bandpassNames: dummyBp = Bandpass() dummyBp.readThroughput(os.path.join(bandpassDir,bandpassRoot+bpn+'.dat')) controlBandpassList.append(dummyBp) wMin = controlBandpassList[0].wavelen[0] wMax = controlBandpassList[0].wavelen[-1] wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0] for bp in controlBandpassList: bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax, wavelen_step=wStep) for test, control in zip(bandpassDict.values(), controlBandpassList): numpy.testing.assert_array_almost_equal(test.wavelen, control.wavelen, 19) numpy.testing.assert_array_almost_equal(test.sb, control.sb, 19)
def calculate_mags(sed_name, mag_norm, out_dict): """ Parameters ---------- sed_name is a numpy array of SED names mag_norm is a numpy array of magNorms out_dict is a multiprocessing.Manager.dict() that will store the magnitudes calculated by this process. """ i_process = mp.current_process().pid bp_dir = getPackageDir('throughputs') bp_dir = os.path.join(bp_dir, 'imsim', 'goal') bp_dict = BandpassDict.loadTotalBandpassesFromFiles(bandpassDir=bp_dir) sed_dir = getPackageDir('sims_sed_library') out_mags = np.zeros((len(sed_name), 6), dtype=float) for i_star, (s_name, m_norm) in enumerate(zip(sed_name, mag_norm)): spec = Sed() spec.readSED_flambda(os.path.join(sed_dir, defaultSpecMap[s_name])) fnorm = getImsimFluxNorm(spec, m_norm) spec.multiplyFluxNorm(fnorm) mags = bp_dict.magListForSed(spec) out_mags[i_star] = mags out_dict[i_process] = out_mags
def fromThroughputs(cls): """ instantiate class from the LSST throughputs in the throughputs directory """ totalbpdict, hwbpdict = BandpassDict.loadBandpassesFromFiles() return cls(hwBandpassDict=hwbpdict)
def testLSSTmags(self): """ Test that PhotometrySSM properly calculates LSST magnitudes """ catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'lsstSsmPhotCat.txt') cat = LSST_SSM_photCat(self.photDB) cat.write_catalog(catName) dtype = np.dtype([('id', np.int), ('u', np.float), ('g', np.float), ('r', np.float), ('i', np.float), ('z', np.float), ('y', np.float)]) testData = np.genfromtxt(catName, dtype=dtype, delimiter=',') self.assertGreater(len(testData), 0) controlData = np.genfromtxt(self.dbFile, dtype=self.dtype) self.assertGreater(len(controlData), 0) LSSTbandpasses = BandpassDict.loadTotalBandpassesFromFiles() controlSedList = SedList(controlData['sedFilename'], controlData['magNorm'], wavelenMatch=LSSTbandpasses.wavelenMatch) controlMags = LSSTbandpasses.magListForSedList(controlSedList) for ii in range(len(controlMags)): for jj, bpName in enumerate(['u', 'g', 'r', 'i', 'z', 'y']): self.assertAlmostEqual(controlMags[ii][jj], testData[bpName][ii], 10) if os.path.exists(catName): os.unlink(catName)
def _find_objects_on_chip(self, object_lines, chip_name): num_lines = len(object_lines) ra_phosim = np.zeros(num_lines, dtype=float) dec_phosim = np.zeros(num_lines, dtype=float) mag_norm = 55. * np.ones(num_lines, dtype=float) for i, line in enumerate(object_lines): if not line.startswith('object'): raise RuntimeError('Trying to process non-object entry from ' 'the instance catalog.') tokens = line.split() ra_phosim[i] = float(tokens[2]) dec_phosim[i] = float(tokens[3]) mag_norm[i] = float(tokens[4]) ra_appGeo, dec_appGeo \ = PhoSimAstrometryBase._appGeoFromPhoSim(np.radians(ra_phosim), np.radians(dec_phosim), self.obs_md) ra_obs_rad, dec_obs_rad \ = _observedFromAppGeo(ra_appGeo, dec_appGeo, obs_metadata=self.obs_md, includeRefraction=True) x_pupil, y_pupil = _pupilCoordsFromObserved(ra_obs_rad, dec_obs_rad, self.obs_md) on_chip_dict = _chip_downselect(mag_norm, x_pupil, y_pupil, self.logger, [chip_name]) index = on_chip_dict[chip_name] self.object_lines = [] for i in index[0]: self.object_lines.append(object_lines[i]) self.x_pupil = list(x_pupil[index]) self.y_pupil = list(y_pupil[index]) self.bp_dict = BandpassDict.loadTotalBandpassesFromFiles()
def get_quiescent_lsst_magnitudes(self): if not hasattr(self, 'lsstBandpassDict'): self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() return self._quiescentMagnitudeGetter(self.lsstBandpassDict, self.get_quiescent_lsst_magnitudes._colnames)
def _fluxes(sed_name, mag_norm, redshift): """ Find the fluxes for a galaxy component Parameters ---------- sed_name is an SED file name mag_norm is a float redshift is a float Returns ------- array of fluxes in ugrizy order """ if not hasattr(_fluxes, '_bp_dict'): bp_dir = getPackageDir('throughputs') bp_dir = os.path.join(bp_dir, 'imsim', 'goal') _fluxes._bp_dict = BandpassDict.loadTotalBandpassesFromFiles( bandpassDir=bp_dir) _fluxes._sed_dir = getPackageDir('sims_sed_library') spec = Sed() full_sed_name = os.path.join(_fluxes._sed_dir, sed_name) if not os.path.isfile(full_sed_name): full_sed_name = os.path.join(_fluxes._sed_dir, defaultSpecMap[sed_name]) spec.readSED_flambda(full_sed_name) fnorm = getImsimFluxNorm(spec, mag_norm) spec.multiplyFluxNorm(fnorm) spec.redshiftSED(redshift, dimming=True) return _fluxes._bp_dict.fluxListForSed(spec)
def testLSSTmags(self): """ Test that PhotometrySSM properly calculates LSST magnitudes """ catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'lsstSsmPhotCat.txt') cat=LSST_SSM_photCat(self.photDB) cat.write_catalog(catName) dtype = np.dtype([('id', np.int), ('u', np.float), ('g', np.float), ('r', np.float), ('i', np.float), ('z', np.float), ('y', np.float)]) testData = np.genfromtxt(catName, dtype=dtype, delimiter=',') self.assertGreater(len(testData), 0) controlData = np.genfromtxt(self.dbFile, dtype=self.dtype) self.assertGreater(len(controlData), 0) LSSTbandpasses = BandpassDict.loadTotalBandpassesFromFiles() controlSedList = SedList(controlData['sedFilename'], controlData['magNorm'], wavelenMatch=LSSTbandpasses.wavelenMatch) controlMags = LSSTbandpasses.magListForSedList(controlSedList) for ii in range(len(controlMags)): for jj, bpName in enumerate(['u', 'g', 'r', 'i', 'z', 'y']): self.assertAlmostEqual(controlMags[ii][jj], testData[bpName][ii], 10) if os.path.exists(catName): os.unlink(catName)
def testLSSTmags(self): """ Test that PhotometrySSM properly calculates LSST magnitudes """ cat = LSST_SSM_photCat(self.photDB) dtype = np.dtype([('id', np.int), ('u', np.float), ('g', np.float), ('r', np.float), ('i', np.float), ('z', np.float), ('y', np.float)]) with lsst.utils.tests.getTempFilePath('.txt') as catName: cat.write_catalog(catName) testData = np.genfromtxt(catName, dtype=dtype, delimiter=',') self.assertGreater(len(testData), 0) controlData = np.genfromtxt(self.dbFile, dtype=self.dtype) self.assertGreater(len(controlData), 0) LSSTbandpasses = BandpassDict.loadTotalBandpassesFromFiles() controlSedList = SedList(controlData['sedFilename'], controlData['magNorm'], wavelenMatch=LSSTbandpasses.wavelenMatch, fileDir=getPackageDir('sims_sed_library'), specMap=defaultSpecMap) controlMags = LSSTbandpasses.magListForSedList(controlSedList) for ii in range(len(controlMags)): for jj, bpName in enumerate(['u', 'g', 'r', 'i', 'z', 'y']): self.assertAlmostEqual(controlMags[ii][jj], testData[bpName][ii], 10)
def get_test_disk_mags(self): if not hasattr(self, 'testBandpassDict'): self.testBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() return self._magnitudeGetter('disk', self.testBandpassDict, self.get_test_disk_mags._colnames)
def get_lsst_magnitudes(self): """ getter for LSST magnitudes of solar system objects """ if not hasattr(self, 'lsstBandpassDict'): self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() return self._quiescentMagnitudeGetter(self.lsstBandpassDict, self.get_lsst_magnitudes._colnames)
def get_test_agn_mags(self): if not hasattr(self, 'testBandpassDict'): self.testBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() mag = self._quiescentMagnitudeGetter('agn', self.testBandpassDict, self.get_test_agn_mags._colnames) mag += self._variabilityGetter(self.get_test_agn_mags._colnames) return mag
def testLoadBandpassesFromFiles(self): """ Test that running the classmethod loadBandpassesFromFiles produces expected result """ fileDir = os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData') bandpassNames = ['g', 'z', 'i'] bandpassRoot = 'test_bandpass_' componentList = ['toy_mirror.dat'] atmo = os.path.join(fileDir, 'toy_atmo.dat') bandpassDict, hardwareDict = BandpassDict.loadBandpassesFromFiles(bandpassNames=bandpassNames, filedir=fileDir, bandpassRoot=bandpassRoot, componentList=componentList, atmoTransmission=atmo) controlBandpassList = [] controlHardwareList = [] for bpn in bandpassNames: componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'), os.path.join(fileDir, 'toy_mirror.dat')] dummyBp = Bandpass() dummyBp.readThroughputList(componentList) controlHardwareList.append(dummyBp) componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'), os.path.join(fileDir, 'toy_mirror.dat'), os.path.join(fileDir, 'toy_atmo.dat')] dummyBp = Bandpass() dummyBp.readThroughputList(componentList) controlBandpassList.append(dummyBp) wMin = controlBandpassList[0].wavelen[0] wMax = controlBandpassList[0].wavelen[-1] wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0] for bp, hh in zip(controlBandpassList, controlHardwareList): bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax, wavelen_step=wStep) hh.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax, wavelen_step=wStep) for test, control in zip(bandpassDict.values(), controlBandpassList): np.testing.assert_array_almost_equal(test.wavelen, control.wavelen, 19) np.testing.assert_array_almost_equal(test.sb, control.sb, 19) for test, control in zip(hardwareDict.values(), controlHardwareList): np.testing.assert_array_almost_equal(test.wavelen, control.wavelen, 19) np.testing.assert_array_almost_equal(test.sb, control.sb, 19)
def get_test_mags(self): if not hasattr(self, 'variabilitybandpassDict'): self.variabilityBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() self._loadSedList(self.variabilityBandpassDict.wavelenMatch) if not hasattr(self, '_sedList'): return numpy.ones((6,0)) return self._magnitudeGetter(self.variabilityBandpassDict, self.get_test_mags._colnames)
def testManyMagSystems(self): """ Test that the SSM photometry mixin can simultaneously calculate magnitudes in multiple bandpass systems """ catName = os.path.join(getPackageDir('sims_catUtils'), 'tests', 'scratchSpace', 'compoundSsmPhotCat.txt') cat=Compound_SSM_photCat(self.photDB) cat.write_catalog(catName) dtype = np.dtype([('id', np.int), ('lsst_u', np.float), ('lsst_g', np.float), ('lsst_r', np.float), ('lsst_i', np.float), ('lsst_z', np.float), ('lsst_y', np.float), ('cartoon_u', np.float), ('cartoon_g', np.float), ('cartoon_r', np.float), ('cartoon_i', np.float), ('cartoon_z', np.float)]) testData = np.genfromtxt(catName, dtype=dtype, delimiter=',') self.assertGreater(len(testData), 0) controlData = np.genfromtxt(self.dbFile, dtype=self.dtype) self.assertGreater(len(controlData), 0) LSSTbandpasses = BandpassDict.loadTotalBandpassesFromFiles() cartoonBandpasses = BandpassDict.loadTotalBandpassesFromFiles( ['u', 'g', 'r', 'i', 'z'], bandpassDir = os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData'), bandpassRoot = 'test_bandpass_' ) controlSedList = SedList(controlData['sedFilename'], controlData['magNorm'], wavelenMatch=LSSTbandpasses.wavelenMatch) controlLsstMags = LSSTbandpasses.magListForSedList(controlSedList) controlCartoonMags = cartoonBandpasses.magListForSedList(controlSedList) for ii in range(len(controlLsstMags)): for jj, bpName in enumerate(['lsst_u', 'lsst_g', 'lsst_r', 'lsst_i', 'lsst_z', 'lsst_y']): self.assertAlmostEqual(controlLsstMags[ii][jj], testData[bpName][ii], 10) for jj, bpName in enumerate(['cartoon_u', 'cartoon_g', 'cartoon_r', 'cartoon_i', 'cartoon_z']): self.assertAlmostEqual(controlCartoonMags[ii][jj], testData[bpName][ii], 10) if os.path.exists(catName): os.unlink(catName)
def fromTwinklesData(cls, tableName, objectTypeID=42, dbHostName=None, idCol='snid', columns=('snid', 'redshift', 'snra', 'sndec', 't0', 'x0', 'x1', 'c'), idSequence=None): """ Simplified classmethod to construct this class from the Twinkles Run 1 perspective. Parameters ---------- tableName : string, mandatory case insensitive string name of table on database to connect to for model parameters of astrophysical objects idCol : string, optional, defaults to 'snid' column name of Index on the table columns : tuple of strings, optional, defaults to values for SN tuple of strings that completely specify the truth values for idSequence : sequence of one dimension, optional, defaults to None sequence of unique ids in the catsim universe indexing the astrophysical objects in the database. dbHostName : string, optional, defaults to None force the class to use this hostname. If not provided, the class will set this to localhost, which is the desired hostname when using an ssh tunnel. This parameter is useful when working from whitelisted computers. Returns ------ An instance of the class RefLightCurve class where the other parameters have been defaulted to sensible values for Twinkles Run1 Analysis. Examples -------- """ data_dir = os.path.join(os.environ['MONITOR_DIR'], 'data') opsimCsv = os.path.join(data_dir, 'SelectedKrakenVisits.csv') opsimdf = pd.read_csv(opsimCsv, index_col='obsHistID') observations = opsimdf[['expMJD', 'filter', 'fiveSigmaDepth']].copy() del opsimdf # Obtain the tuple of total, HardWare bandPassDict and keep the total lsstBP = BandpassDict.loadBandpassesFromFiles()[0] cls = RefLightCurves(tableName=tableName, objectTypeID=objectTypeID, idCol=idCol, dbHostName=dbHostName, columns=columns, observations=observations, bandPassDict=lsstBP, idSequence=idSequence) return cls
def testMatchToRestFrame(self): """Test that Galaxies with no effects added into catalog mags are matched correctly.""" rng = np.random.RandomState(42) galPhot = BandpassDict.loadTotalBandpassesFromFiles() imSimBand = Bandpass() imSimBand.imsimBandpass() testMatching = selectGalaxySED(galDir=self.testSpecDir) testSEDList = testMatching.loadBC03() testSEDNames = [] testMags = [] testMagNormList = [] magNormStep = 1 for testSED in testSEDList: getSEDMags = Sed() testSEDNames.append(testSED.name) getSEDMags.setSED(wavelen=testSED.wavelen, flambda=testSED.flambda) testMagNorm = np.round(rng.uniform(20.0, 22.0), magNormStep) testMagNormList.append(testMagNorm) fluxNorm = getSEDMags.calcFluxNorm(testMagNorm, imSimBand) getSEDMags.multiplyFluxNorm(fluxNorm) testMags.append(galPhot.magListForSed(getSEDMags)) # Also testing to make sure passing in non-default bandpasses works # Substitute in nan values to simulate incomplete data. testMags[0][1] = np.nan testMags[0][2] = np.nan testMags[0][4] = np.nan testMags[1][1] = np.nan testMatchingResults = testMatching.matchToRestFrame( testSEDList, testMags, bandpassDict=galPhot) self.assertEqual(None, testMatchingResults[0][0]) self.assertEqual(testSEDNames[1:], testMatchingResults[0][1:]) self.assertEqual(None, testMatchingResults[1][0]) np.testing.assert_almost_equal(testMagNormList[1:], testMatchingResults[1][1:], decimal=magNormStep) # Test Match Errors errMags = np.array( (testMags[2], testMags[2], testMags[2], testMags[2])) errMags[1, 1] += 1. # Total MSE will be 2/(5 colors) = 0.4 errMags[2, 0:2] = np.nan errMags[2, 3] += 1. # Total MSE will be 2/(3 colors) = 0.667 errMags[3, :] = None errSED = testSEDList[2] testMatchingResultsErrors = testMatching.matchToRestFrame( [errSED], errMags, bandpassDict=galPhot) np.testing.assert_almost_equal(np.array((0.0, 0.4, 2. / 3.)), testMatchingResultsErrors[2][0:3], decimal=3) self.assertEqual(None, testMatchingResultsErrors[2][3])
def testMagListForSed(self): """ Test that magListForSed calculates the correct magnitude """ wavelen = numpy.arange(10.0,2000.0,1.0) flux = (wavelen*2.0-5.0)*1.0e-6 spectrum = Sed(wavelen=wavelen, flambda=flux) for nBp in range(3, 10, 1): nameList, bpList = self.getListOfBandpasses(nBp) testDict = BandpassDict(bpList, nameList) self.assertFalse(len(testDict.values()[0].wavelen)==len(spectrum.wavelen)) magList = testDict.magListForSed(spectrum) for ix, (name, bp, magTest) in enumerate(zip(nameList, bpList, magList)): magControl = spectrum.calcMag(bp) self.assertAlmostEqual(magTest, magControl, 5)
def testFluxDictForSed(self): """ Test that fluxDictForSed calculates the correct fluxes """ wavelen = numpy.arange(10.0,2000.0,1.0) flux = (wavelen*2.0-5.0)*1.0e-6 spectrum = Sed(wavelen=wavelen, flambda=flux) for nBp in range(3, 10, 1): nameList, bpList = self.getListOfBandpasses(nBp) testDict = BandpassDict(bpList, nameList) self.assertFalse(len(testDict.values()[0].wavelen)==len(spectrum.wavelen)) fluxDict = testDict.fluxDictForSed(spectrum) for ix, (name, bp) in enumerate(zip(nameList, bpList)): fluxControl = spectrum.calcFlux(bp) self.assertAlmostEqual(fluxDict[name]/fluxControl, 1.0, 2)
def _parallel_fitting(mag_array, redshift, redshift_true, H0, Om0, wav_min, wav_width, lsst_mag_array, out_dict, tag): pid = os.getpid() (sed_names, mag_norms, av_arr, rv_arr) = sed_from_galacticus_mags(mag_array, redshift, redshift_true, H0, Om0, wav_min, wav_width, lsst_mag_array) tot_bp_dict = BandpassDict.loadTotalBandpassesFromFiles() sed_dir = getPackageDir('sims_sed_library') lsst_fit_fluxes = np.zeros((6, len(sed_names)), dtype=float) t_start = time.time() ccm_w = None restframe_seds = {} imsim_bp = Bandpass() imsim_bp.imsimBandpass() n04_ln10 = -0.4 * np.log(10) for ii in range(len(sed_names)): av_val = av_arr[ii] rv_val = rv_arr[ii] sed_tag = '%s_%.3f_%.3f' % (sed_names[ii], av_val, rv_val) if sed_tag not in restframe_seds: rest_sed = Sed() rest_sed.readSED_flambda(os.path.join(sed_dir, sed_names[ii])) mag = rest_sed.calcMag(imsim_bp) if ccm_w is None or not np.array_equal(rest_sed.wavelen, ccm_w): ccm_w = np.copy(rest_sed.wavelen) ax, bx = rest_sed.setupCCM_ab() rest_sed.addDust(ax, bx, A_v=av_val, R_v=rv_val) restframe_seds[sed_tag] = (rest_sed, mag) for i_bp, bp in enumerate('ugrizy'): m_norm = mag_norms[i_bp][ii] if m_norm > 0.0 and not np.isfinite(m_norm): continue spec = Sed(wavelen=restframe_seds[sed_tag][0].wavelen, flambda=restframe_seds[sed_tag][0].flambda) fnorm = np.exp(n04_ln10 * (m_norm - restframe_seds[sed_tag][1])) try: assert np.isfinite(fnorm) assert fnorm > 0.0 except AssertionError: print('\n\nmagnorm %e\n\n' % (m_norm)) raise spec.multiplyFluxNorm(fnorm) spec.redshiftSED(redshift[ii], dimming=True) ff = spec.calcFlux(tot_bp_dict[bp]) lsst_fit_fluxes[i_bp][ii] = ff out_dict[tag] = (sed_names, mag_norms, av_arr, rv_arr, lsst_fit_fluxes)
def testSignalToNoise(self): """ Test that calcSNR_m5 and calcSNR_sed give similar results """ defaults = LSSTdefaults() photParams = PhotometricParameters() totalDict, hardwareDict = BandpassDict.loadBandpassesFromFiles() skySED = Sed() skySED.readSED_flambda( os.path.join(lsst.utils.getPackageDir('throughputs'), 'baseline', 'darksky.dat')) m5 = [] for filt in totalDict: m5.append( calcM5(skySED, totalDict[filt], hardwareDict[filt], photParams, seeing=defaults.seeing(filt))) sedDir = lsst.utils.getPackageDir('sims_sed_library') sedDir = os.path.join(sedDir, 'starSED', 'kurucz') fileNameList = os.listdir(sedDir) numpy.random.seed(42) offset = numpy.random.random_sample(len(fileNameList)) * 2.0 for ix, name in enumerate(fileNameList): if ix > 100: break spectrum = Sed() spectrum.readSED_flambda(os.path.join(sedDir, name)) ff = spectrum.calcFluxNorm(m5[2] - offset[ix], totalDict.values()[2]) spectrum.multiplyFluxNorm(ff) magList = [] controlList = [] magList = [] for filt in totalDict: controlList.append( calcSNR_sed(spectrum, totalDict[filt], skySED, hardwareDict[filt], photParams, defaults.seeing(filt))) magList.append(spectrum.calcMag(totalDict[filt])) testList, gammaList = calcSNR_m5(numpy.array(magList), numpy.array(totalDict.values()), numpy.array(m5), photParams) for tt, cc in zip(controlList, testList): msg = '%e != %e ' % (tt, cc) self.assertTrue(numpy.abs(tt / cc - 1.0) < 0.001, msg=msg)
def _make_gs_interpreters(self, seed, sensor_list, file_id): """ Create a separate GalSimInterpreter for each sensor so that they can be run in parallel and maintain separate checkpoint files. Also extract GsObjectLists from gs_obj_dict for only the sensors in sensor_list so that the memory in the underlying InstCatTrimmer object in gs_obj_dict can be recovered. """ bp_dict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames=self.obs_md.bandpass) disable_sky_model = self.config['sky_model']['disable_sky_model'] noise_and_background \ = make_sky_model(self.obs_md, self.phot_params, seed=seed, apply_sensor_model=self.apply_sensor_model, disable_sky_model=disable_sky_model) self.gs_interpreters = dict() for det in self.camera_wrapper.camera: det_type = det.getType() det_name = det.getName() if sensor_list is not None and det_name not in sensor_list: continue if det_type in (DetectorType.WAVEFRONT, DetectorType.GUIDER): continue gs_det = make_galsim_detector(self.camera_wrapper, det_name, self.phot_params, self.obs_md) self.gs_interpreters[det_name] \ = make_gs_interpreter(self.obs_md, [gs_det], bp_dict, noise_and_background, epoch=2000.0, seed=seed, apply_sensor_model=self.apply_sensor_model, bf_strength=self.config['ccd']['bf_strength']) self.gs_interpreters[det_name].sky_bg_per_pixel \ = noise_and_background.sky_counts(det_name) self.gs_interpreters[det_name].setPSF(PSF=self.psf) if self.apply_sensor_model: add_treering_info(self.gs_interpreters[det_name].detectors) if file_id is not None: self.gs_interpreters[det_name].checkpoint_file \ = self.checkpoint_file(file_id, det_name) self.gs_interpreters[det_name].nobj_checkpoint \ = self.config['checkpointing']['nobj'] self.gs_interpreters[det_name]\ .restore_checkpoint(self.camera_wrapper, self.phot_params, self.obs_md) if self.create_centroid_file: self.gs_interpreters[det_name].centroid_base_name = \ os.path.join(self.outdir, self.config['persistence']['centroid_prefix'])
def __init__(self, population, model, pointings, rng, maxObsHistID, pointingColumnDict, pruneWithRadius): self.model = model self.population = population self._pointings = pointings self._rng = rng self.maxObsHistID = maxObsHistID self.pointingColumnDict = pointingColumnDict self.pruneWithRadius = pruneWithRadius self.bandPasses = BandpassDict.loadTotalBandpassesFromFiles()
def get_magnitudes(self): """ Example photometry getter for alternative (i.e. non-LSST) bandpasses """ if not hasattr(self, 'cartoonBandpassDict'): bandpassNames = ['u','g','r','i','z'] bandpassDir = os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData') self.cartoonBandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames,bandpassDir = bandpassDir, bandpassRoot = 'test_bandpass_') return self._quiescentMagnitudeGetter(self.cartoonBandpassDict, self.get_magnitudes._colnames)
def testAlternateBandpassesStars(self): """ This will test our ability to do photometry using non-LSST bandpasses. It will first calculate the magnitudes using the getters in cartoonPhotometryStars. It will then load the alternate bandpass files 'by hand' and re-calculate the magnitudes and make sure that the magnitude values agree. This is guarding against the possibility that some default value did not change and the code actually ended up loading the LSST bandpasses. """ obs_metadata_pointed = ObservationMetaData( mjd=2013.23, boundType="circle", unrefractedRA=200.0, unrefractedDec=-30.0, boundLength=1.0 ) bandpassDir = os.path.join(lsst.utils.getPackageDir("sims_photUtils"), "tests", "cartoonSedTestData") cartoon_dict = BandpassDict.loadTotalBandpassesFromFiles( ["u", "g", "r", "i", "z"], bandpassDir=bandpassDir, bandpassRoot="test_bandpass_" ) testBandPasses = {} keys = ["u", "g", "r", "i", "z"] bplist = [] for kk in keys: testBandPasses[kk] = Bandpass() testBandPasses[kk].readThroughput(os.path.join(bandpassDir, "test_bandpass_%s.dat" % kk)) bplist.append(testBandPasses[kk]) sedObj = Sed() phiArray, waveLenStep = sedObj.setupPhiArray(bplist) sedFileName = os.path.join(lsst.utils.getPackageDir("sims_sed_library"), "starSED", "kurucz") sedFileName = os.path.join(sedFileName, "km20_5750.fits_g40_5790.gz") ss = Sed() ss.readSED_flambda(sedFileName) controlBandpass = Bandpass() controlBandpass.imsimBandpass() ff = ss.calcFluxNorm(22.0, controlBandpass) ss.multiplyFluxNorm(ff) testMags = cartoon_dict.magListForSed(ss) ss.resampleSED(wavelen_match=bplist[0].wavelen) ss.flambdaTofnu() mags = -2.5 * numpy.log10(numpy.sum(phiArray * ss.fnu, axis=1) * waveLenStep) - ss.zp self.assertTrue(len(mags) == len(testMags)) self.assertTrue(len(mags) > 0) for j in range(len(mags)): self.assertAlmostEqual(mags[j], testMags[j], 10)
def get_cartoon_mags(self): if not hasattr(self, 'cartoonBandpassDict'): bandpassDir = os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData') self.cartoonBandpassDict = \ BandpassDict.loadTotalBandpassesFromFiles(bandpassNames = ['u', 'g', 'r', 'i', 'z'], bandpassDir = bandpassDir, bandpassRoot = 'test_bandpass_') return self._quiescentMagnitudeGetter(self.cartoonBandpassDict, self.get_cartoon_mags._colnames)
def __init__(self, idSequence, opsim_csv=None, db_config=None): if opsim_csv is None: opsim_csv = os.path.join(lsst.utils.getPackageDir('monitor'), 'data', 'SelectedKrakenVisits.csv') df = pd.read_csv(opsim_csv, index_col='obsHistID') opsim_df = df[['expMJD', 'filter', 'fiveSigmaDepth']] lsstBP = BandpassDict.loadBandpassesFromFiles()[0] self.reflc = RefLightCurves(idSequence=idSequence, tableName='TwinkSN', bandPassDict=lsstBP, observations=opsim_df)
def setUpClass(cls): cls.scratchDir = os.path.join(getPackageDir('sims_GalSimInterface'), 'tests', 'scratchSpace') cls.obs = ObservationMetaData(pointingRA=122.0, pointingDec=-29.1, mjd=57381.2, rotSkyPos=43.2) cls.camera = camTestUtils.CameraWrapper().camera cls.dbFileName = os.path.join(cls.scratchDir, 'allowed_chips_test_db.txt') if os.path.exists(cls.dbFileName): os.unlink(cls.dbFileName) cls.controlSed = Sed() cls.controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'), 'flatSED','sed_flat.txt.gz')) cls.magNorm = 18.1 imsim = Bandpass() imsim.imsimBandpass() ff = cls.controlSed.calcFluxNorm(cls.magNorm, imsim) cls.controlSed.multiplyFluxNorm(ff) a_x, b_x = cls.controlSed.setupCCMab() cls.controlSed.addCCMDust(a_x, b_x, A_v=0.1, R_v=3.1) bpd = BandpassDict.loadTotalBandpassesFromFiles() pp = PhotometricParameters() cls.controlADU = cls.controlSed.calcADU(bpd['u'], pp) cls.countSigma = np.sqrt(cls.controlADU/pp.gain) cls.x_pix = 50 cls.y_pix = 50 x_list = [] y_list = [] name_list = [] for dd in cls.camera: x_list.append(cls.x_pix) y_list.append(cls.y_pix) name_list.append(dd.getName()) x_list = np.array(x_list) y_list = np.array(y_list) ra_list, dec_list = raDecFromPixelCoords(x_list, y_list, name_list, camera=cls.camera, obs_metadata=cls.obs, epoch=2000.0) dra_list = 3600.0*(ra_list-cls.obs.pointingRA) ddec_list = 3600.0*(dec_list-cls.obs.pointingDec) create_text_catalog(cls.obs, cls.dbFileName, dra_list, ddec_list, mag_norm=[cls.magNorm]*len(dra_list)) cls.db = allowedChipsFileDBObj(cls.dbFileName, runtable='test')
def testMatchToRestFrame(self): """Test that Galaxies with no effects added into catalog mags are matched correctly.""" np.random.seed(42) galPhot = BandpassDict.loadTotalBandpassesFromFiles() imSimBand = Bandpass() imSimBand.imsimBandpass() testMatching = selectGalaxySED(galDir = self.testSpecDir) testSEDList = testMatching.loadBC03() testSEDNames = [] testMags = [] testMagNormList = [] magNormStep = 1 for testSED in testSEDList: getSEDMags = Sed() testSEDNames.append(testSED.name) getSEDMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) testMagNorm = np.round(np.random.uniform(20.0,22.0),magNormStep) testMagNormList.append(testMagNorm) fluxNorm = getSEDMags.calcFluxNorm(testMagNorm, imSimBand) getSEDMags.multiplyFluxNorm(fluxNorm) testMags.append(galPhot.magListForSed(getSEDMags)) #Also testing to make sure passing in non-default bandpasses works #Substitute in nan values to simulate incomplete data. testMags[0][1] = np.nan testMags[0][2] = np.nan testMags[0][4] = np.nan testMags[1][1] = np.nan testMatchingResults = testMatching.matchToRestFrame(testSEDList, testMags, bandpassDict = galPhot) self.assertEqual(None, testMatchingResults[0][0]) self.assertEqual(testSEDNames[1:], testMatchingResults[0][1:]) self.assertEqual(None, testMatchingResults[1][0]) np.testing.assert_almost_equal(testMagNormList[1:], testMatchingResults[1][1:], decimal = magNormStep) #Test Match Errors errMags = np.array((testMags[2], testMags[2], testMags[2], testMags[2])) errMags[1,1] += 1. #Total MSE will be 2/(5 colors) = 0.4 errMags[2, 0:2] = np.nan errMags[2, 3] += 1. #Total MSE will be 2/(3 colors) = 0.667 errMags[3, :] = None errSED = testSEDList[2] testMatchingResultsErrors = testMatching.matchToRestFrame([errSED], errMags, bandpassDict = galPhot) np.testing.assert_almost_equal(np.array((0.0, 0.4, 2./3.)), testMatchingResultsErrors[2][0:3], decimal = 3) self.assertEqual(None, testMatchingResultsErrors[2][3])
def setUpClass(cls): cls.scratchDir = tempfile.mkdtemp(dir=ROOT, prefix='allowedChipsTest-') cls.obs = ObservationMetaData(pointingRA=122.0, pointingDec=-29.1, mjd=57381.2, rotSkyPos=43.2, bandpassName='r') cls.camera = camTestUtils.CameraWrapper().camera cls.dbFileName = os.path.join(cls.scratchDir, 'allowed_chips_test_db.txt') if os.path.exists(cls.dbFileName): os.unlink(cls.dbFileName) cls.controlSed = Sed() cls.controlSed.readSED_flambda(os.path.join(getPackageDir('sims_sed_library'), 'flatSED', 'sed_flat.txt.gz')) cls.magNorm = 18.1 imsim = Bandpass() imsim.imsimBandpass() ff = cls.controlSed.calcFluxNorm(cls.magNorm, imsim) cls.controlSed.multiplyFluxNorm(ff) a_x, b_x = cls.controlSed.setupCCMab() cls.controlSed.addCCMDust(a_x, b_x, A_v=0.1, R_v=3.1) bpd = BandpassDict.loadTotalBandpassesFromFiles() pp = PhotometricParameters() cls.controlADU = cls.controlSed.calcADU(bpd['u'], pp) cls.countSigma = np.sqrt(cls.controlADU/pp.gain) cls.x_pix = 50 cls.y_pix = 50 x_list = [] y_list = [] name_list = [] for dd in cls.camera: x_list.append(cls.x_pix) y_list.append(cls.y_pix) name_list.append(dd.getName()) x_list = np.array(x_list) y_list = np.array(y_list) ra_list, dec_list = raDecFromPixelCoords(x_list, y_list, name_list, camera=cls.camera, obs_metadata=cls.obs, epoch=2000.0) dra_list = 3600.0*(ra_list-cls.obs.pointingRA) ddec_list = 3600.0*(dec_list-cls.obs.pointingDec) create_text_catalog(cls.obs, cls.dbFileName, dra_list, ddec_list, mag_norm=[cls.magNorm]*len(dra_list)) cls.db = allowedChipsFileDBObj(cls.dbFileName, runtable='test')
def testPhiArray(self): """ Test that the phi array is correctly calculated by BandpassDict upon construction. """ for nBp in range(3, 10, 1): nameList, bpList = self.getListOfBandpasses(nBp) testDict = BandpassDict(bpList, nameList) dummySed = Sed() controlPhi, controlWavelenStep = dummySed.setupPhiArray(bpList) numpy.testing.assert_array_almost_equal(controlPhi, testDict.phiArray, 19) self.assertAlmostEqual(controlWavelenStep, testDict.wavelenStep, 10)
def testUncertaintyExceptions(self): """ Test that calcSNR_m5 raises exceptions when it needs to """ totalDict, hardwareDict = BandpassDict.loadBandpassesFromFiles() magnitudes = numpy.array([22.0, 23.0, 24.0, 25.0, 26.0, 27.0]) shortMagnitudes = numpy.array([22.0]) photParams = PhotometricParameters() shortGamma = numpy.array([1.0, 1.0]) self.assertRaises(RuntimeError, calcSNR_m5, magnitudes, totalDict.values(), shortMagnitudes, photParams) self.assertRaises(RuntimeError, calcSNR_m5, shortMagnitudes, totalDict.values(), magnitudes, photParams) self.assertRaises( RuntimeError, calcSNR_m5, magnitudes, totalDict.values(), magnitudes, photParams, gamma=shortGamma ) snr, gg = calcSNR_m5(magnitudes, totalDict.values(), magnitudes, photParams)
def get_lsst_agn_mags(self): """ Getter for AGN magnitudes in the LSST bandpasses """ # load a BandpassDict of LSST bandpasses, if not done already if not hasattr(self, 'lsstBandpassDict'): self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() # actually calculate the magnitudes mag = self._quiescentMagnitudeGetter('agn', self.lsstBandpassDict, self.get_lsst_agn_mags._colnames) mag += self._variabilityGetter(self.get_lsst_agn_mags._colnames) return mag
def testSignalToNoise(self): """ Test that calcSNR_m5 and calcSNR_sed give similar results """ defaults = LSSTdefaults() photParams = PhotometricParameters() totalDict, hardwareDict = BandpassDict.loadBandpassesFromFiles() skySED = Sed() skySED.readSED_flambda(os.path.join(lsst.utils.getPackageDir("throughputs"), "baseline", "darksky.dat")) m5 = [] for filt in totalDict: m5.append(calcM5(skySED, totalDict[filt], hardwareDict[filt], photParams, seeing=defaults.seeing(filt))) sedDir = lsst.utils.getPackageDir("sims_sed_library") sedDir = os.path.join(sedDir, "starSED", "kurucz") fileNameList = os.listdir(sedDir) numpy.random.seed(42) offset = numpy.random.random_sample(len(fileNameList)) * 2.0 for ix, name in enumerate(fileNameList): if ix > 100: break spectrum = Sed() spectrum.readSED_flambda(os.path.join(sedDir, name)) ff = spectrum.calcFluxNorm(m5[2] - offset[ix], totalDict.values()[2]) spectrum.multiplyFluxNorm(ff) magList = [] controlList = [] magList = [] for filt in totalDict: controlList.append( calcSNR_sed( spectrum, totalDict[filt], skySED, hardwareDict[filt], photParams, defaults.seeing(filt) ) ) magList.append(spectrum.calcMag(totalDict[filt])) testList, gammaList = calcSNR_m5( numpy.array(magList), numpy.array(totalDict.values()), numpy.array(m5), photParams ) for tt, cc in zip(controlList, testList): msg = "%e != %e " % (tt, cc) self.assertTrue(numpy.abs(tt / cc - 1.0) < 0.001, msg=msg)
def testSystematicUncertainty(self): """ Test that systematic uncertainty is added correctly. """ sigmaSys = 0.002 m5 = [23.5, 24.3, 22.1, 20.0, 19.5, 21.7] photParams = PhotometricParameters(sigmaSys=sigmaSys) bandpassDict = BandpassDict.loadTotalBandpassesFromFiles() obs_metadata = ObservationMetaData(unrefractedRA=23.0, unrefractedDec=45.0, m5=m5, bandpassName=self.bandpasses) magnitudes = bandpassDict.magListForSed(self.starSED) skySeds = [] for i in range(len(self.bandpasses)): skyDummy = Sed() skyDummy.readSED_flambda(os.path.join(lsst.utils.getPackageDir("throughputs"), "baseline", "darksky.dat")) normalizedSkyDummy = setM5( obs_metadata.m5[self.bandpasses[i]], skyDummy, self.totalBandpasses[i], self.hardwareBandpasses[i], seeing=LSSTdefaults().seeing(self.bandpasses[i]), photParams=PhotometricParameters(), ) skySeds.append(normalizedSkyDummy) for i in range(len(self.bandpasses)): snr = calcSNR_sed( self.starSED, self.totalBandpasses[i], skySeds[i], self.hardwareBandpasses[i], seeing=LSSTdefaults().seeing(self.bandpasses[i]), photParams=PhotometricParameters(), ) testSNR, gamma = calcSNR_m5( numpy.array([magnitudes[i]]), [self.totalBandpasses[i]], numpy.array([m5[i]]), photParams=PhotometricParameters(sigmaSys=0.0), ) self.assertAlmostEqual(snr, testSNR[0], 10, msg="failed on calcSNR_m5 test %e != %e " % (snr, testSNR[0])) control = numpy.sqrt(numpy.power(magErrorFromSNR(testSNR), 2) + numpy.power(sigmaSys, 2))
def get_magnitudes(self): """ Example photometry getter for alternative (i.e. non-LSST) bandpasses """ idNames = self.column_by_name('id') columnNames = [name for name in self.get_magnitudes._colnames] bandpassNames = ['u','g','r','i','z'] bandpassDir = os.path.join(getPackageDir('sims_photUtils'), 'tests', 'cartoonSedTestData') if not hasattr(self, 'cartoonBandpassDict'): self.cartoonBandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames,bandpassDir = bandpassDir, bandpassRoot = 'test_bandpass_') output = self._quiescentMagnitudeGetter(self.cartoonBandpassDict, self.get_magnitudes._colnames) ############################################################################# #Everything below this comment exists solely for the purposes of the unit test #if you need to write a customized getter for photometry that uses non-LSST #bandpasses, you only need to emulate the code above this comment. magNormList = self.column_by_name('magNorm') sedNames = self.column_by_name('sedFilename') av = self.column_by_name('galacticAv') #the two variables below will allow us to get at the SED and magnitude #data from within the unit test class, so that we can be sure #that the mixin loaded the correct bandpasses sublist = SedList(sedNames, magNormList, galacticAvList=av, fileDir=getPackageDir('sims_sed_library'), specMap=defaultSpecMap) for ss in sublist: self.sedMasterList.append(ss) if len(output) > 0: for i in range(len(output[0])): subList = [] for j in range(len(output)): subList.append(output[j][i]) self.magnitudeMasterList.append(subList) return output
def get_sdss_magnitudes(self): """ An example getter for stellar magnitudes in SDSS bands """ # Load a BandpassDict of SDSS bandpasses, if not done already if not hasattr(self, 'sdssBandpassDict'): bandpassNames = ['u','g','r','i','z'] bandpassDir = os.path.join(getPackageDir('throughputs'),'sdss') bandpassRoot = 'sdss_' self.sdssBandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames, bandpassRoot = bandpassRoot, bandpassDir = bandpassDir) # Actually calculate the magnitudes return self._magnitudeGetter(self.sdssBandpassDict, self.get_sdss_magnitudes._colnames)