class AGAnalysisUnittesting(unittest.TestCase): def setUp(self): self.currentDirPath = Path(__file__).parent.absolute() self.agilepyconfPath = os.path.join(self.currentDirPath, "conf/agilepyconf.yaml") self.sourcesconfPath = os.path.join(self.currentDirPath, "conf/sourceconf.xml") outDir = Path( os.path.join(os.environ["AGILE"], "agilepy-test-data/unittesting-output/api")) if outDir.exists() and outDir.is_dir(): shutil.rmtree(outDir) self.aga = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) def test_analysis_pipeline(self): maplistFilePath = self.aga.generateMaps() self.assertEqual(True, os.path.isfile(maplistFilePath)) products_1 = self.aga.mle(maplistFilePath) for p in products_1: self.assertEqual(True, os.path.isfile(p)) products_2 = self.aga.mle(maplistFilePath) for p in products_2: self.assertEqual(True, os.path.isfile(p)) def test_source_dist_updated_after_mle(self): maplistFilePath = self.aga.generateMaps() self.aga.mle(maplistFilePath) source_1 = self.aga.selectSources( lambda Name: Name == '2AGLJ2021+4029').pop() dist_1 = source_1.multi.Dist self.aga.setOptions(glon=81, glat=1) self.aga.mle(maplistFilePath) source_2 = self.aga.selectSources( lambda Name: Name == '2AGLJ2021+4029').pop() dist_2 = source_2.multi.Dist self.assertNotEqual(dist_1, dist_2) def test_source_Flux_updated_after_mle(self): maplistFilePath = self.aga.generateMaps() source_1 = self.aga.selectSources( lambda Name: Name == '2AGLJ2021+3654').pop() flux_1 = source_1.getParamValue("Flux") self.aga.mle(maplistFilePath) source_2 = self.aga.selectSources( lambda Name: Name == '2AGLJ2021+3654').pop() flux_2 = source_2.getParamValue("Flux") self.assertNotEqual(flux_1, flux_2)
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_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_update_source_parameter_value(self): test_out_dir = self.set_outputfolder("test_update_source_parameter_value") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) sources = ag.selectSources(lambda name: name == self.VELA) source = sources.pop() self.assertEqual(1.34774, source.spectrum.getVal("index2")) source.set("index2", {"value": 1}) assert 1 == source.spectrum.getVal("index2") self.assertEqual(969.539e-08, source.spectrum.getVal("flux")) source.set("flux", {"value": 1}) assert 1 == source.spectrum.getVal("flux") self.assertEqual(3913.06, source.spectrum.getVal("cutoffEnergy")) source.set("cutoffEnergy", {"value": 1}) assert 1 == source.spectrum.getVal("cutoffEnergy") # self.assertRaises(AttributeError, source.spectrum.get, "index") # self.assertRaises(AttributeError, source.spectrum.set, "index", 10) ag.destroy()
def test_source_Flux_updated_after_mle(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) maplistFilePath = ag.generateMaps() source_1 = ag.selectSources( lambda name: name == '2AGLJ2021+3654').pop() flux_1 = source_1.spectrum.get("flux") ag.mle(maplistFilePath) source_2 = ag.selectSources( lambda name: name == '2AGLJ2021+3654').pop() flux_2 = source_2.multi.get("multiFlux") self.assertNotEqual(flux_1, flux_2) ag.destroy()
def test_source_dist_updated_after_source_position_update(self): test_out_dir = self.set_outputfolder("test_source_dist_updated_after_source_position_update") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.setOptions(tmin = 433857532, tmax = 433857542, timetype = "TT", fovbinnumber=1, energybins=[[100,200]], glon = 263.55, glat = -2.78) source_1 = ag.selectSources(lambda name: name == self.VELA).pop() dist_1 = source_1.spatialModel.get("dist")["value"] ag.updateSourcePosition(self.VELA, glon=264, glat=-3) source_2 = ag.selectSources(lambda name: name == self.VELA).pop() dist_2 = source_2.spatialModel.get("dist")["value"] assert True, dist_1 != dist_2 ag.destroy()
def test_source_dist_updated_after_mle(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) maplistFilePath = ag.generateMaps() ag.mle(maplistFilePath) source_1 = ag.selectSources( lambda name: name == '2AGLJ2021+4029').pop() dist_1 = source_1.multi.get("multiDist") ag.setOptions(glon=81, glat=1) ag.mle(maplistFilePath) source_2 = ag.selectSources( lambda name: name == '2AGLJ2021+4029').pop() dist_2 = source_2.multi.get("multiDist") self.assertNotEqual(dist_1, dist_2) ag.destroy()
def test_print_source(self): test_out_dir = self.set_outputfolder("test_print_source") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) sources = ag.selectSources(lambda name: name == self.VELA) for s in sources: self.assertEqual(True, len(str(s))>370) print(s)
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()
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()