示例#1
0
    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()
示例#2
0
    def test_setDQ(self):

        test_out_dir = self.set_outputfolder("test_setDQ")


        ag = AGAnalysis(self.agilepyConf)

        # dq out of range
        self.assertRaises(ConfigurationsNotValidError, ag.setOptions, dq=42)

        # dq = 0
        ag.setOptions(dq=0, albedorad=101, fovradmax=102)
        self.assertEqual(ag.getOption("albedorad"), 101)
        self.assertEqual(ag.getOption("fovradmax"), 102)

        # dq == 5 
        ag.setOptions(dq=5)
        self.assertEqual(ag.getOption("albedorad"), 100)
        self.assertEqual(ag.getOption("fovradmax"), 50)
        
        # Try to change albedorad or fovradmax with dq == 5
        self.assertRaises(CannotSetNotUpdatableOptionError,
                          ag.setOptions, albedorad=42, fovradmax=42)

        ag.destroy()
示例#3
0
    def test_extract_light_curve_data(self):

        test_out_dir = self.set_outputfolder("test_extract_light_curve_data")


        ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt)

        sourceFile = Path(self.currentDirPath).joinpath("data/testcase_2AGLJ0835-4514.source")

        lcdata = ag._extractLightCurveDataFromSourceFile(str(sourceFile))


        lcdataKeys = ["sqrt(ts)", "flux","flux_err", "flux_ul","gal","gal_error","iso","iso_error",\
            "l_peak","b_peak","dist_peak","l","b","r","ell_dist","a","b","phi","exp","ExpRatio","counts",\
            "counts_err","Index","Index_Err","Par2","Par2_Err","Par3","Par3_Err","Erglog","Erglog_Err","Erglog_UL",\
            "time_start_tt","time_end_tt","Fix","index","ULConfidenceLevel","SrcLocConfLevel","start_l","start_b",\
            "start_flux","typefun","par2","par3","galmode2","isomode2","isomode2fit","edpcor","fluxcor",\
            "integratortype","expratioEval","expratio_minthr","expratio_maxthr","expratio_size",\
            "emin", "emax", "fovmin", "fovmax", "albedo", "binsize", "expstep", "phasecode", "fit_cts", \
            "fit_fitstatus0", "fit_fcn0", "fit_edm0", "fit_nvpar0", "fit_nparx0", "fit_iter0", \
            "fit_fitstatus1", "fit_fcn1", "fit_edm1", "fit_nvpar1", "fit_nparx1", "fit_iter1", "fit_Likelihood1"]

        self.assertEqual(len(lcdataKeys), len(lcdata))
        
        for key in lcdataKeys:
            self.assertEqual(True, key in lcdata)

        ag.destroy()
    def test_extract_light_curve_data(self):

        ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPathcalcBkg)

        sourceFile = Path(
            self.currentDirPath).joinpath("data/testcase0.source")

        lcdata = ag._extractLightCurveDataFromSourceFile(str(sourceFile))

        print(lcdata)
示例#5
0
    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_delete_output_directory(self):

        ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPath)

        outDir = Path(ag.getOption("outdir"))

        self.assertEqual(True, outDir.exists() and outDir.is_dir())

        ag.deleteAnalysisDir()

        self.assertEqual(False, outDir.exists())
示例#7
0
    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)
示例#8
0
    def test_get_analysis_dir(self):

        test_out_dir = self.set_outputfolder("test_get_analysis_dir")


        ag = AGAnalysis(self.agilepyConf)

        outdir = ag.getAnalysisDir()

        outdir = Path(outdir)

        self.assertTrue(outdir.is_dir(), outdir.exists())
示例#9
0
    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])
示例#10
0
    def test_delete_output_directory(self):

        test_out_dir = self.set_outputfolder("test_delete_output_directory")


        ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt)

        outDir = Path(ag.getOption("outdir"))

        self.assertEqual(True, outDir.exists() and outDir.is_dir())

        ag.deleteAnalysisDir()

        self.assertEqual(False, outDir.exists())
示例#11
0
    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_calc_bkg(self):

        ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPathcalcBkg)

        ag.setOptions(tmin=456461778.0,
                      tmax=456537945.0,
                      timetype="TT",
                      galcoeff=[-1, -1, -1, -1],
                      isocoeff=[10, 12, 10, 12])
        """
        galBkg, isoBkg, maplistfile = ag.calcBkg('CYGX3', pastTimeWindow=0)
        print("\ngalBkg:",galBkg)
        print("isoBkg:",isoBkg)

        galBkg, isoBkg, maplistfile = ag.calcBkg('CYGX3', galcoeff=[-1,-1,-1,-1], pastTimeWindow=0)
        print("\ngalBkg:",galBkg)
        print("isoBkg:",isoBkg)


        galBkg, isoBkg, maplistfile = ag.calcBkg('CYGX3', galcoeff=[0,0,0,0], pastTimeWindow=0)
        print("\ngalBkg:",galBkg)
        print("isoBkg:",isoBkg)
        """

        galBkg, isoBkg, maplistfile = ag.calcBkg('CYGX3',
                                                 galcoeff=[0.8, 0.6, 0.8, 0.6],
                                                 pastTimeWindow=0)
        print("\ngalBkg:", galBkg)
        print("isoBkg:", isoBkg)

        ag.destroy()
示例#13
0
    def test_calc_bkg(self):

        test_out_dir = self.set_outputfolder("test_calc_bkg")


        ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt)

        ag.setOptions(  timetype="TT",
                        galcoeff=[-1, -1, -1, -1],
                        isocoeff=[10, 12, 10, 12]
                     )

        """
        galBkg, isoBkg, maplistfile = ag.calcBkg('CYGX3', pastTimeWindow=0)
        print("\ngalBkg:",galBkg)
        print("isoBkg:",isoBkg)

        galBkg, isoBkg, maplistfile = ag.calcBkg('CYGX3', galcoeff=[-1,-1,-1,-1], pastTimeWindow=0)
        print("\ngalBkg:",galBkg)
        print("isoBkg:",isoBkg)


        galBkg, isoBkg, maplistfile = ag.calcBkg('CYGX3', galcoeff=[0,0,0,0], pastTimeWindow=0)
        print("\ngalBkg:",galBkg)
        print("isoBkg:",isoBkg)
        """

        galCoeff, isoCoeff, maplistfile = ag.calcBkg(self.VELA, galcoeff=[0.8, 0.6, 0.8, 0.6], pastTimeWindow=0)
       
        self.assertEqual([10, 12, 10, 12], isoCoeff)
        self.assertEqual([0.8, 0.6, 0.8, 0.6], galCoeff)

        ag.destroy()
示例#14
0
    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()
示例#15
0
    def test_fix_exponent(self):

        test_out_dir = self.set_outputfolder("test_fix_exponent")


        ag = AGAnalysis(self.agilepyConf, self.sourcesConfTxt)

        self.assertEqual('894.587e-08', ag._fixToNegativeExponent(8.94587e-06, fixedExponent=-8))
        self.assertEqual('309.757e-08', ag._fixToNegativeExponent(3.09757e-06, fixedExponent=-8))
        self.assertEqual('1623.16e-08', ag._fixToNegativeExponent(1.62316e-05, fixedExponent=-8))
        self.assertEqual('1.524e-08', ag._fixToNegativeExponent(1.524e-8, fixedExponent=-8))
        self.assertEqual('1.524e+18e-08', ag._fixToNegativeExponent(1.524e10, fixedExponent=-8))
        self.assertEqual('0.0', ag._fixToNegativeExponent(0.0, fixedExponent=-8))

        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_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()
示例#19
0
    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()
示例#20
0
    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()
示例#21
0
    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_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_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_fix_exponent(self):

        ag = AGAnalysis(self.agilepyconfPath, self.sourcesconfPathcalcBkg)

        self.assertEqual(
            '894.587e-08',
            ag._fixToNegativeExponent(8.94587e-06, fixedExponent=-8))
        self.assertEqual(
            '309.757e-08',
            ag._fixToNegativeExponent(3.09757e-06, fixedExponent=-8))
        self.assertEqual(
            '1623.16e-08',
            ag._fixToNegativeExponent(1.62316e-05, fixedExponent=-8))
        self.assertEqual('1.524e-08',
                         ag._fixToNegativeExponent(1.524e-8, fixedExponent=-8))
        self.assertEqual('1.524e+18e-08',
                         ag._fixToNegativeExponent(1.524e10, fixedExponent=-8))
        self.assertEqual('0.0', ag._fixToNegativeExponent(0.0,
                                                          fixedExponent=-8))
示例#25
0
    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()
示例#26
0
    def test_setOptionTimeMJD(self):

        test_out_dir = self.set_outputfolder("test_setOptionTimeMJD")

        
        ag = AGAnalysis(self.agilepyConf)

        tmin1 = 58030.0
        tmax1 = 58035.0

        tmintt = AstroUtils.time_mjd_to_agile_seconds(tmin1)
        tmaxtt = AstroUtils.time_mjd_to_agile_seconds(tmax1)

        ag.setOptionTimeMJD(tmin=tmin1, tmax=tmax1)

        tmin2 = ag.getOption("tmin")

        tmax2 = ag.getOption("tmax")

        self.assertEqual(tmintt, tmin2)
        self.assertEqual(tmaxtt, tmax2)

        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()
    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()
示例#29
0
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_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()