def test_update_source_position(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) ag.generateMaps() ag.freeSources('name == "2AGLJ2021+4029"', "pos", False) ag.mle() self.assertRaises(ValueError, ag.updateSourcePosition, "2AGLJ2021+4029", useMulti=False) changed = ag.updateSourcePosition("2AGLJ2021+4029", useMulti=False, glon=78.2375, glat=2.12298) self.assertEqual(False, changed) ag.freeSources('name == "2AGLJ2021+4029"', "pos", True) ag.mle() changed = ag.updateSourcePosition("2AGLJ2021+4029", useMulti=True) self.assertEqual(False, changed) ag.destroy()
def test_lc(self): test_out_dir = self.set_outputfolder("test_lc") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.setOptions(energybins=[[100, 300]], fovbinnumber=1) # to reduce the computational time ag.freeSources(lambda name: name == self.VELA, "flux", True) lightCurveData = ag.lightCurveMLE(self.VELA, tmin=433860000, tmax=433880000, timetype="TT", binsize=20000) self.assertEqual(True, os.path.isfile(lightCurveData)) with open(lightCurveData, "r") as lcd: lines = lcd.readlines() # print("readlines: ", lines) self.assertEqual(True,len(lines) == 1+1) # 1 header + 2 temporal bins lightCurveData = ag.lightCurveMLE(self.VELA, tmin=433900000, tmax=433940000, timetype="TT", binsize=20000) self.assertEqual(True, os.path.isfile(lightCurveData)) with open(lightCurveData, "r") as lcd: lines = lcd.readlines() # print("readlines: ", lines) self.assertEqual(True, len(lines) == 1+2) # 1 header + 3 temporal bins lightCurvePlot = ag.displayLightCurve("mle", saveImage=True) self.assertEqual(True, os.path.isfile(lightCurvePlot)) ag.destroy()
def test_write_sources_on_file(self): test_out_dir = self.set_outputfolder("test_write_sources_on_file") ag = AGAnalysis(self.agilepyConf) sources_subset = ag.loadSourcesFromCatalog("2AGL", rangeDist=(0, 30)) self.assertRaises(SourceModelFormatNotSupported, ag.writeSourcesOnFile, "regfile", "notsupportedformat", None) #reg regfile = ag.writeSourcesOnFile("regfile", "reg", sources_subset) with open(regfile) as f: linesNum = sum(1 for line in f) assert 1 == linesNum """ ag.generateMaps() ag.mle() regfile = ag.writeSourcesOnFile("regfile", "reg", sources_subset) with open(regfile) as f: linesNum = sum(1 for line in f) self.assertEqual(len(sources_subset)+1, linesNum) """ ag.generateMaps() ag.freeSources(lambda name: name == self.VELA, "pos", True) ag.mle() regfile = ag.writeSourcesOnFile("regfile", "reg", sources_subset) with open(regfile) as f: linesNum = sum(1 for line in f) self.assertEqual(len(sources_subset)+1, linesNum)
def test_source_dist_updated_after_mle(self): test_out_dir = self.set_outputfolder("test_source_dist_updated_after_mle") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) maplistFilePath = ag.generateMaps() ag.freeSources(lambda name: name == self.VELA, "pos", True) ag.freeSources(lambda name: name == self.VELA, "flux", True) source = ag.selectSources(lambda name: name == self.VELA).pop() print(source) dist_before = source.get("dist")["value"] ag.mle(maplistFilePath) source = ag.selectSources(lambda name: name == self.VELA).pop() print(source) dist_after = source.get("dist")["value"] multiDist_after = source.get("multiDist")["value"] assert dist_before == dist_after assert multiDist_after != dist_after ag.destroy()
def test_print_source(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) maplistFilePath = ag.generateMaps() ag.freeSources('name == "2AGLJ2021+3654"', "pos", True) ag.mle(maplistFilePath) for s in ag.sourcesLibrary.sources: print(s) self.assertEqual(True, True) ag.destroy()
def test_lc(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) ag.setOptions(glon=78.2375, glat=2.12298) ag.setOptions(tmin=456400000.000000, tmax=456500000.000000, timetype="TT") ag.freeSources('name == "2AGLJ2021+4029"', "flux", True) lightCurveData = ag.lightCurve("2AGLJ2021+4029", binsize=20000) print(lightCurveData) self.assertEqual(True, os.path.isfile(lightCurveData))
def test_fixed_parameters(self): test_out_dir = self.set_outputfolder("test_fixed_parameters") ag = AGAnalysis(self.agilepyConf) ag.setOptions( energybins=[[100, 1000]], tmin=434279000, tmax=434289532, timetype="TT") sources = ag.loadSourcesFromCatalog("2AGL", rangeDist=(0, 25)) assert len(sources) == 9 source = ag.selectSources('name == "2AGLJ0835-4514"').pop() flux0 = source.spectrum.getVal("flux") sources = ag.freeSources( 'name == "2AGLJ0835-4514"', "flux", False, show=True) _ = ag.generateMaps() _ = ag.mle() flux1 = source.get("multiFlux")["value"] assert flux0 == flux1 ag.destroy()
def test_analysis_pipeline(self): test_out_dir = self.set_outputfolder("test_analysis_pipeline") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.setOptions(tmin = 433857532, tmax = 434289532, timetype = "TT", glon = 263.55, glat = -2.78) maplistFilePath = ag.generateMaps() self.assertEqual(True, os.path.isfile(maplistFilePath)) sources = ag.loadSourcesFromCatalog("2AGL", rangeDist = (0, 21)) sources = ag.freeSources('name == "2AGLJ0835-4514"', "flux", True, show=True) products_1 = ag.mle() for p in products_1: self.assertEqual(True, os.path.isfile(p)) products_2 = ag.mle() for p in products_2: self.assertEqual(True, os.path.isfile(p)) ag.setOptions(tmin = 433857532, tmax = 433907532, timetype = "TT", glon = 263.55, glat = -2.78) maplistfile = ag.generateMaps() products_3 = ag.mle() for p in products_3: self.assertEqual(True, os.path.isfile(p)) ag.destroy()
def test_multi_update_free_parameters(self): test_out_dir = self.set_outputfolder("test_multi_update_free_parameters") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.generateMaps() sources = ag.selectSources(lambda name: name == self.VELA) source = sources.pop() #index2 = source.spectrum.get("index2") flux = source.spectrum.getVal("flux") #cutoffEnergy = source.spectrum.get("cutoffEnergy") ag.freeSources(lambda name: name == self.VELA, "pos", True) # ag.freeSources(lambda name: name == self.VELA, "index2", True) ag.freeSources(lambda name: name == self.VELA, "flux", True) # ag.freeSources(lambda name: name == self.VELA, "cutoffEnergy", True) print(source) ag.mle() print(source) assert source.multiAnalysis.getVal("multiFlux") != source.spectrum.getVal("flux") assert flux == source.spectrum.getVal("flux") ag.mle() assert source.multiAnalysis.getVal("multiFlux") != source.spectrum.getVal("flux") assert flux != source.spectrum.getVal("flux") print(source) #todo: validation test ag.destroy()
def test_generic_column(self): test_out_dir = self.set_outputfolder("g_col") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.setOptions(energybins=[[100, 300]], fovbinnumber=1) ag.freeSources(lambda name: name == self.VELA, "flux", True) lightCurveData = ag.lightCurveMLE( self.VELA, tmin=433860000, tmax=433880000, timetype="TT", binsize=20000) #filename = ag.displayGenericColumns( # lightCurveData, columns=["l_peak"], um=["test_um"], saveImage=True) filename = ag.displayGenericColumns( lightCurveData, columns=["l_peak", "counts"], um=["test_um", "counts"], saveImage=True) self.assertEqual(True, os.path.isfile(filename)) ag.destroy()
def test_source_flux_updated_after_mle(self): test_out_dir = self.set_outputfolder("test_source_flux_updated_after_mle") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) maplistFilePath = ag.generateMaps() ag.freeSources(lambda name: name == self.VELA, "flux", True) source_1 = ag.selectSources(lambda name: name == self.VELA).pop() flux_1 = source_1.get("flux")["value"] ag.mle(maplistFilePath) source_2 = ag.selectSources(lambda name: name == self.VELA).pop() flux_2 = source_2.get("multiFlux")["value"] self.assertNotEqual(flux_1, flux_2) ag.destroy()