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)
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_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_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_mle(self): test_out_dir = self.set_outputfolder("test_mle") ag = AGAnalysis(self.agilepyConf) ag.setOptions(tmin = 433857532, tmax = 433858532, timetype = "TT", glon = 263.55, glat = -2.78) self.assertRaises(MaplistIsNone, ag.mle) ag.generateMaps() self.assertRaises(SourcesLibraryIsEmpty, ag.mle)
def test_generate_maps(self): test_out_dir = self.set_outputfolder("test_generate_maps") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.setOptions(tmin=433857532, tmax=433858532, timetype="TT") outDir = ag.getOption("outdir") maplistFilePath0 = ag.generateMaps() self.assert_maplistfile_lines_number(maplistFilePath0, 4) outDir0 = Path(outDir).joinpath("maps", "0") self.assertEqual(True, outDir0.is_dir()) self.assert_generated_maps_number(outDir0, 16) self.assert_generated_maps_exist(ag.parseMaplistFile(maplistFilePath0)) # second generation (same parameters) maplistFilePath1 = ag.generateMaps() self.assert_maplistfile_lines_number(maplistFilePath1, 4) outDir1 = Path(outDir).joinpath("maps", "1") self.assertEqual(True, outDir1.is_dir()) self.assert_generated_maps_number(outDir1, 16) self.assert_generated_maps_exist(ag.parseMaplistFile(maplistFilePath1)) # third generation with different time interval ag.setOptions(tmin=433957532, tmax=433957632, timetype="TT") maplistFilePath2 = ag.generateMaps() self.assert_maplistfile_lines_number(maplistFilePath2, 4) outDir2 = Path(outDir).joinpath("maps", "2") self.assertEqual(True, outDir2.is_dir()) self.assert_generated_maps_number(outDir2, 16) self.assert_generated_maps_exist(ag.parseMaplistFile(maplistFilePath2)) # fourth generation with different glon and glat ag.setOptions(glon=265, glat=-3, timetype="TT") maplistFilePath3 = ag.generateMaps() self.assert_maplistfile_lines_number(maplistFilePath3, 4) outDir3 = Path(outDir).joinpath("maps", "3") self.assertEqual(True, outDir3.is_dir()) self.assert_generated_maps_number(outDir3, 16) self.assert_generated_maps_exist(ag.parseMaplistFile(maplistFilePath3)) # fifth generation with different fovbinnumber, and energy range ag.setOptions(fovbinnumber=1, energybins=[[100,300]]) maplistFilePath4 = ag.generateMaps() self.assert_maplistfile_lines_number(maplistFilePath4, 1) outDir3 = Path(outDir).joinpath("maps", "4") self.assertEqual(True, outDir3.is_dir()) self.assert_generated_maps_number(outDir3, 4) self.assert_generated_maps_exist(ag.parseMaplistFile(maplistFilePath4)) ag.destroy()
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_gal_iso(self): test_out_dir = self.set_outputfolder("test_update_gal_iso") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.setOptions(tmin=433857532, tmax=433858532, timetype="TT") outDir = Path(ag.getOption("outdir")) ag.config.setOptions(galcoeff=[0.6, 0.8, 0.6, 0.8]) ag.config.setOptions(isocoeff=[10, 15, 10, 15]) galcoeffs = ag.config.getOptionValue("galcoeff") isocoeffs = ag.config.getOptionValue("isocoeff") _ = ag.generateMaps() matrix = ag.parseMaplistFile() for idx, row in enumerate(matrix): self.assertEqual(str(galcoeffs[idx]), row[4]) self.assertEqual(str(isocoeffs[idx]), row[5]) ag.config.setOptions(galcoeff=[0,0,0,0]) ag.config.setOptions(isocoeff=[0,0,0,0]) galcoeffs = ag.config.getOptionValue("galcoeff") isocoeffs = ag.config.getOptionValue("isocoeff") matrix = ag.parseMaplistFile() for idx, row in enumerate(matrix): self.assertEqual(str(galcoeffs[idx]), row[4]) self.assertEqual(str(isocoeffs[idx]), row[5])
def test_parse_maplistfile(self): test_out_dir = self.set_outputfolder("test_parse_maplistfile") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.setOptions(energybins=[[100,300],[300,1000]], fovbinnumber=2) maplistFilePath = ag.generateMaps() maplistRows1 = ag.parseMaplistFile() maplistRows2 = ag.parseMaplistFile(maplistFilePath) self.assertEqual(4, len(maplistRows1)) self.assertEqual(4, len(maplistRows2)) for i in range(4): for j in range(3): self.assertEqual(maplistRows1[i][j], maplistRows2[i][j]) for i in range(4): for j in range(3): self.assertEqual(True, os.path.isfile(maplistRows1[i][j])) 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_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_analysis_pipeline(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) maplistFilePath = ag.generateMaps() self.assertEqual(True, os.path.isfile(maplistFilePath)) products_1 = ag.mle(maplistFilePath) for p in products_1: self.assertEqual(True, os.path.isfile(p)) products_2 = ag.mle(maplistFilePath) for p in products_2: self.assertEqual(True, os.path.isfile(p)) 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_display_sky_maps_singlemode(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) _ = ag.generateMaps() maps = ag.displayCtsSkyMaps(saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) maps = ag.displayExpSkyMaps(saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) maps = ag.displayGasSkyMaps(saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) ag.destroy()
def test_generate_maps(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) outDir = ag.getOption("outdir") maplistFilePath = ag.generateMaps() self.assertEqual(True, os.path.isfile(maplistFilePath)) maps = os.listdir(Path(outDir).joinpath("maps")) self.assertEqual(16, len(maps)) lines = None with open(maplistFilePath) as mfp: lines = mfp.readlines() self.assertEqual(4, len(lines)) 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_saving_sky_maps_singlemode(self): test_out_dir = self.set_outputfolder("test_saving_sky_maps_singlemode") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) _ = ag.generateMaps() maps = ag.displayCtsSkyMaps(saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) maps = ag.displayExpSkyMaps(saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) maps = ag.displayGasSkyMaps(saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) ag.destroy()
def test_saving_sky_maps(self): test_out_dir = self.set_outputfolder("test_saving_sky_maps") ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt) ag.setOptions(tmin = 433857532, tmax = 433857732, timetype = "TT") _ = ag.generateMaps() maps = ag.displayCtsSkyMaps(singleMode=False, saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) maps = ag.displayExpSkyMaps(singleMode=False, saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) maps = ag.displayGasSkyMaps(singleMode=False, saveImage=True) for m in maps: self.assertEqual(True, os.path.isfile(m)) 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()
def test_update_gal_iso(self): ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath) outDir = Path(ag.getOption("outdir")) ag.config.setOptions(galcoeff=[0.6, 0.8, 0.6, 0.8]) ag.config.setOptions(isocoeff=[10, 15, 10, 15]) galcoeffs = ag.config.getOptionValue("galcoeff") isocoeffs = ag.config.getOptionValue("isocoeff") maplistFilePath = ag.generateMaps() matrix = ag.parseMaplistFile() for idx, row in enumerate(matrix): self.assertEqual(str(galcoeffs[idx]), row[4]) self.assertEqual(str(isocoeffs[idx]), row[5]) if outDir.joinpath("maps").exists() and outDir.joinpath( "maps").is_dir(): shutil.rmtree(outDir.joinpath("maps")) outDir.joinpath("maps").mkdir(parents=False, exist_ok=True) ag.config.setOptions(galcoeff=[0, 0, 0, 0]) ag.config.setOptions(isocoeff=[0, 0, 0, 0]) galcoeffs = ag.config.getOptionValue("galcoeff") isocoeffs = ag.config.getOptionValue("isocoeff") matrix = ag.parseMaplistFile() for idx, row in enumerate(matrix): self.assertEqual(str(galcoeffs[idx]), row[4]) self.assertEqual(str(isocoeffs[idx]), row[5]) ag.destroy()