def test_download_data(self, logger, datacoveragepath):
        """
        To run this test --runrest it needed when calling pytest
        """

        testOutputDir = Path(__file__).absolute().parent.joinpath("test_out")

        if testOutputDir.exists():
            rmtree(str(testOutputDir))

        testOutputDir.mkdir(exist_ok=True, parents=True)

        queryEVTPath = testOutputDir.joinpath("EVT.qfile")
        queryLOGPath = testOutputDir.joinpath("LOG.qfile")

        with open(queryEVTPath, "w") as inf:
            inf.write(
                """2020-01-15T00:00:00 2020-01-31T00:00:00\n2020-03-15T00:00:00 2020-03-31T00:00:00"""
            )
        with open(queryLOGPath, "w") as infLOG:
            infLOG.write(
                """2020-01-15T00:00:00 2020-01-31T00:00:00\n2020-03-15T00:00:00 2020-03-31T00:00:00"""
            )

        os.environ["TEST_LOGS_DIR"] = str(testOutputDir)

        configPath = Path(__file__).absolute().parent.joinpath(
            "test_data", "test_download_data_config.yaml")
        config = AgilepyConfig()
        config.loadBaseConfigurations(configPath)
        config.loadConfigurationsForClass("AGAnalysis")

        datacoveragepath = Path(__file__).absolute().parent.joinpath(
            "test_data", "AGILE_test_datacoverage")

        agdataset = AGDataset(logger, datacoveragepath)

        #test download data

        tmin = 57083  # 2015-02-28T00:00:00
        tmax = 57090  # 2015-03-15T00:00:00
        downloaded = agdataset.downloadData(tmin, tmax,
                                            config.getOptionValue("datapath"),
                                            config.getOptionValue("evtfile"),
                                            config.getOptionValue("logfile"))

        assert downloaded == True
        sleep(3)  # this sleep is to avoid too many requests ban
        #test tmax outside data coverage
        tmin = 58051
        tmax = 59582

        with pytest.raises(NoCoverageDataError):
            downloaded = agdataset.downloadData(
                tmin, tmax, config.getOptionValue("datapath"),
                config.getOptionValue("evtfile"),
                config.getOptionValue("logfile"))
class AgilepyConfigUT(unittest.TestCase):
    def setUp(self):
        self.currentDirPath = Path(__file__).parent.absolute()
        self.agilepyconfPath = os.path.join(self.currentDirPath,
                                            "conf/agilepyconf.yaml")

        outDir = Path(
            os.path.join(os.environ["AGILE"],
                         "agilepy-test-data/unittesting-output/config"))

        if outDir.exists() and outDir.is_dir():
            shutil.rmtree(outDir)

    ##### TEST NOT SUPPORTED AFTER REST FEATURE
    """
    def test_validation_tmin_not_in_index(self):

        self.config = AgilepyConfig()
        self.config.loadBaseConfigurations(self.agilepyconfPath)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertRaises(ConfigurationsNotValidError, self.config.setOptions, tmin=40000000, tmax=433957532, timetype="TT")


    
    def test_validation_tmax_not_in_index(self):

        self.config = AgilepyConfig()
        self.config.loadBaseConfigurations(self.agilepyconfPath)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertRaises(ConfigurationsNotValidError, self.config.setOptions,
                          tmin=433900000, tmax=456537946, timetype="TT")
    """

    def test_validation_min_max(self):

        self.config = AgilepyConfig()
        self.config.loadBaseConfigurations(self.agilepyconfPath)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          dq=0,
                          fovradmin=10,
                          fovradmax=0)
        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          emin=10,
                          emax=0)

    def test_set_options(self):

        self.config = AgilepyConfig()
        self.config.loadBaseConfigurations(self.agilepyconfPath)
        self.config.loadConfigurationsForClass("AGAnalysis")

        # float instead of int is ok.
        self.assertEqual(
            None,
            self.config.setOptions(tmin=433857532.,
                                   tmax=435153532.,
                                   timetype="TT"))

        self.assertEqual("TT", self.config.getOptionValue("timetype"))

        self.assertEqual(
            None,
            self.config.setOptions(tmin=58026.5, tmax=58027.5, timetype="MJD"))

        self.assertEqual("MJD", self.config.getOptionValue("timetype"))

        self.assertRaises(CannotSetNotUpdatableOptionError,
                          self.config.setOptions,
                          tmin=58026.5)  # we must pass also timetype

        self.assertRaises(CannotSetNotUpdatableOptionError,
                          self.config.setOptions,
                          verboselvl=2)
        self.assertRaises(CannotSetNotUpdatableOptionError,
                          self.config.setOptions,
                          logfilenameprefix="pippo")

        self.assertRaises(OptionNotFoundInConfigFileError,
                          self.config.setOptions(),
                          pdor="kmer")

        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions(),
                          minimizertype=666)
        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions(),
                          energybins=[1, 2, 3])
        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions(),
                          energybins=666)
        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions(),
                          energybins="100, 300")

        self.assertRaises(CannotSetHiddenOptionError,
                          self.config.setOptions(),
                          lonpole=100)

        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions,
                          galcoeff=0.617196)
        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions,
                          isocoeff=0.617196)

        # len(energybins) = 2
        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          galcoeff=[0.617196])
        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          isocoeff=[0.617196])

        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions,
                          fluxcorrection=3.14)

        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          fluxcorrection=25)

    def test_energybins(self):

        self.config = AgilepyConfig()

        # galcoeff and isocoeff are None
        conf1Path = os.path.join(self.currentDirPath, "conf/conf1.yaml")

        self.config.loadBaseConfigurations(conf1Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertEqual(100, self.config.getOptionValue("energybins")[0][0])
        self.assertEqual(300, self.config.getOptionValue("energybins")[0][1])
        self.assertEqual(300, self.config.getOptionValue("energybins")[1][0])
        self.assertEqual(1000, self.config.getOptionValue("energybins")[1][1])

        self.config.setOptions(energybins=[[200, 400], [400, 1000]])

        self.assertEqual(200, self.config.getOptionValue("energybins")[0][0])
        self.assertEqual(400, self.config.getOptionValue("energybins")[0][1])
        self.assertEqual(400, self.config.getOptionValue("energybins")[1][0])
        self.assertEqual(1000, self.config.getOptionValue("energybins")[1][1])

        # wrong type
        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions,
                          energybins=["[200, 400]", "[400, 1000]"])

        # wrong length
        # self.assertRaises(ConfigurationsNotValidError, self.config.setOptions, energybins=[[200, 400]])

        # galcoeff and isocoeff are None and len(energybins) = 1
        conf3Path = os.path.join(self.currentDirPath, "conf/conf3.yaml")

        self.config.loadBaseConfigurations(conf3Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertEqual(100, self.config.getOptionValue("energybins")[0][0])
        self.assertEqual(300, self.config.getOptionValue("energybins")[0][1])

        self.config.setOptions(energybins=[[200, 400], [400, 1000]])

    def test_bkg_coeff(self):

        self.config = AgilepyConfig()

        # galcoeff and isocoeff are None
        conf1Path = os.path.join(self.currentDirPath, "conf/conf1.yaml")

        self.config.loadBaseConfigurations(conf1Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertEqual(4, len(self.config.getOptionValue("isocoeff")))
        self.assertEqual(4, len(self.config.getOptionValue("galcoeff")))

        for i in range(4):
            self.assertEqual(-1, self.config.getOptionValue("isocoeff")[i])
            self.assertEqual(-1, self.config.getOptionValue("galcoeff")[i])

        # galcoeff isocoeff are lists
        conf1Path = os.path.join(self.currentDirPath, "conf/conf2.yaml")

        self.config.loadBaseConfigurations(conf1Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertEqual(4, len(self.config.getOptionValue("isocoeff")))
        self.assertEqual(4, len(self.config.getOptionValue("galcoeff")))

        self.assertEqual(10, self.config.getOptionValue("isocoeff")[0])
        self.assertEqual(15, self.config.getOptionValue("isocoeff")[1])
        self.assertEqual(0.6, self.config.getOptionValue("galcoeff")[0])
        self.assertEqual(0.8, self.config.getOptionValue("galcoeff")[1])

        # galcoeff and isocoeff are changed at runtime

        self.config.setOptions(isocoeff=[10, 15, 10, 15],
                               galcoeff=[0.6, 0.8, 0.6, 0.8])
        self.assertEqual(10, self.config.getOptionValue("isocoeff")[0])
        self.assertEqual(15, self.config.getOptionValue("isocoeff")[1])
        self.assertEqual(10, self.config.getOptionValue("isocoeff")[2])
        self.assertEqual(15, self.config.getOptionValue("isocoeff")[3])
        self.assertEqual(0.6, self.config.getOptionValue("galcoeff")[0])
        self.assertEqual(0.8, self.config.getOptionValue("galcoeff")[1])
        self.assertEqual(0.6, self.config.getOptionValue("galcoeff")[2])
        self.assertEqual(0.8, self.config.getOptionValue("galcoeff")[3])

        # this should cause an error because len(energybins) == 2
        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          isocoeff=[10])
        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          galcoeff=[0.6])

        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions,
                          isocoeff=None,
                          galcoeff=None)

        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions,
                          isocoeff="Pluto",
                          galcoeff="Pippo")

        # this should create an error!!
        self.assertRaises(ConfigFileOptionTypeError,
                          self.config.setOptions,
                          isocoeff=["Pluto"],
                          galcoeff=["Pippo"])

    def test_complete_configuration(self):

        self.config = AgilepyConfig()

        conf1Path = os.path.join(self.currentDirPath, "conf/conf1.yaml")

        self.config.loadBaseConfigurations(conf1Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertEqual(5.99147, self.config.getOptionValue("loccl"))

        self.config.setOptions(loccl=99)

        self.assertEqual(9.21034, self.config.getOptionValue("loccl"))

        self.assertEqual(4, len(self.config.getOptionValue("isocoeff")))
        self.assertEqual(4, len(self.config.getOptionValue("galcoeff")))

        conf2Path = os.path.join(self.currentDirPath, "conf/conf2.yaml")

        self.config.loadBaseConfigurations(conf2Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertEqual(9.21034, self.config.getOptionValue("loccl"))

    def test_evt_log_files_env_vars(self):

        self.config = AgilepyConfig()

        conf1Path = os.path.join(self.currentDirPath, "conf/conf1.yaml")

        self.config.loadBaseConfigurations(conf1Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertEqual(True, "$"
                         not in self.config.getOptionValue("evtfile"))
        self.assertEqual(True, "$"
                         not in self.config.getOptionValue("logfile"))

        self.config.setOptions(
            evtfile="$AGILE/agilepy-test-data/test_dataset_6.0/EVT/EVT.index",
            logfile="$AGILE/agilepy-test-data/test_dataset_6.0/LOG/LOG.index")

        self.assertEqual(True, "$"
                         not in self.config.getOptionValue("evtfile"))
        self.assertEqual(True, "$"
                         not in self.config.getOptionValue("logfile"))

    def test_datapath_restapi(self):

        self.config = AgilepyConfig()

        conf1Path = os.path.join(self.currentDirPath, "conf/conf1.yaml")

        self.config.loadBaseConfigurations(conf1Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertRaises(CannotSetNotUpdatableOptionError,
                          self.config.setOptions,
                          datapath="/foo/bar")
        self.assertRaises(CannotSetNotUpdatableOptionError,
                          self.config.setOptions,
                          userestapi=True)

    def test_set_evtlog_ifdatapath(self):
        self.config = AgilepyConfig()

        conf1Path = os.path.join(self.currentDirPath, "conf/conf4.yaml")

        dirpath = Path("/tmp/foo/bar")
        dirpath.mkdir(parents=True, exist_ok=True)

        self.config.loadBaseConfigurations(conf1Path)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.assertEqual(
            self.config.getOptionValue("evtfile"),
            Path(self.config.getOptionValue("datapath")).joinpath("EVT.index"))
        self.assertEqual(
            self.config.getOptionValue("logfile"),
            Path(self.config.getOptionValue("datapath")).joinpath("LOG.index"))

        dirpath.rmdir()

    ##### TEST NOT SUPPORTED AFTER REST FEATURE
    """
Exemple #3
0
class SourcesLibraryUT(unittest.TestCase):
    def setUp(self):
        self.currentDirPath = Path(__file__).parent.absolute()

        self.test_logs_dir = Path(self.currentDirPath).joinpath(
            "test_logs", "SourcesLibraryUT")
        self.test_logs_dir.mkdir(parents=True, exist_ok=True)
        os.environ["TEST_LOGS_DIR"] = str(self.test_logs_dir)

        self.agilepyConf = os.path.join(self.currentDirPath,
                                        "conf/agilepyconf.yaml")

        self.config = AgilepyConfig()
        self.config.loadBaseConfigurations(self.agilepyConf)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.logger = AgilepyLogger()
        self.logger.initialize(
            self.config.getConf("output", "outdir"),
            self.config.getConf("output", "logfilenameprefix"),
            self.config.getConf("output", "verboselvl"))

        outDir = Path(
            os.path.join(os.environ["AGILE"],
                         "agilepy-test-data/unittesting-output/core"))

        if outDir.exists() and outDir.is_dir():
            shutil.rmtree(outDir)

        self.sl = SourcesLibrary(self.config, self.logger)

    @staticmethod
    def get_free_params(source):

        return {
            "curvature": source.spectrum.curvature["free"],
            "pivot_energy": source.spectrum.pivotEnergy["free"],
            "index": source.spectrum.index["free"],
            "pos": source.spatialModel.pos["free"],
            "flux": source.spectrum.flux["free"]
        }

    def test_load_file_with_wrong_extension(self):

        xmlsourcesconfPath = os.path.join(self.currentDirPath,
                                          "test_data/sourceconf.wrongext")

        self.assertRaises(SourceModelFormatNotSupported,
                          self.sl.loadSourcesFromFile, xmlsourcesconfPath)

    def test_load_wrong_file(self):

        xmlsourcesconfPath = os.path.join(self.currentDirPath,
                                          "conf/idontexitst.txt")

        self.assertRaises(FileNotFoundError, self.sl.loadSourcesFromFile,
                          xmlsourcesconfPath)

    def test_load_from_catalog(self):

        added = self.sl.loadSourcesFromCatalog("2AGL")
        self.assertEqual(175, len(added))
        self.assertEqual(175, len(self.sl.sources))

        self.assertRaises(FileNotFoundError, self.sl.loadSourcesFromCatalog,
                          "paperino")

    def test_load_catalog_from_catalog_filtering_on_distances(self):

        added = self.sl.loadSourcesFromCatalog("2AGL", rangeDist=(70, 80))

        self.assertEqual(13, len(added))
        self.assertEqual(13, len(self.sl.sources))

        self.sl.sources = []
        added = self.sl.loadSourcesFromCatalog("2AGL", rangeDist=(0, 10))
        self.assertEqual(1, len(added))
        self.assertEqual(1, len(self.sl.sources))

        self.sl.sources = []
        added = self.sl.loadSourcesFromCatalog("2AGL", rangeDist=(0, 50))
        self.assertEqual(30, len(added))
        self.assertEqual(30, len(self.sl.sources))

    def test_load_sources_from_xml_file(self):

        added = self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.xml"))

        self.assertEqual(2, len(added))
        self.assertEqual(2, len(self.sl.sources))

        sources = self.sl.selectSources('name == "2AGLJ2021+4029"')
        self.assertEqual(1, len(sources))
        source = sources.pop()
        self.assertEqual(119.3e-08, source.spectrum.getVal("flux"))
        self.assertEqual(1.75, source.spectrum.getVal("index"))
        self.assertEqual(78.2375, source.spatialModel.getVal("pos")[0])
        self.assertEqual(True, source.spatialModel.getVal("dist") > 0)

        sources = self.sl.selectSources('name == "2AGLJ2021+3654"')
        self.assertEqual(1, len(sources))
        source = sources.pop()
        self.assertEqual(70.89e-08, source.spectrum.getVal("flux"))
        self.assertEqual(1.38, source.spectrum.getVal("index"))
        self.assertEqual(75.2562, source.spatialModel.getVal("pos")[0])
        self.assertEqual(True, source.spatialModel.getVal("dist") > 0)

    def test_load_sources_from_txt_file(self):

        added = self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.txt"))

        self.assertEqual(10, len(added))
        self.assertEqual(10, len(self.sl.sources))

        sources = self.sl.selectSources('name == "2AGLJ1801-2334"')
        self.assertEqual(1, len(sources))
        source = sources.pop()

        self.assertEqual(3.579e-07, source.spectrum.getVal("flux"))
        self.assertEqual(3.37991, source.spectrum.getVal("index"))
        self.assertEqual(6.16978, source.spatialModel.getVal("pos")[0])

        # testing fixflags
        f0 = {
            "curvature": 0,
            "pivot_energy": 0,
            "index": 0,
            "pos": 0,
            "flux": 0
        }  # special case
        f1 = {
            "curvature": 0,
            "pivot_energy": 0,
            "index": 0,
            "pos": 0,
            "flux": 1
        }
        f2 = {
            "curvature": 0,
            "pivot_energy": 0,
            "index": 0,
            "pos": 1,
            "flux": 0
        }
        f3 = {
            "curvature": 0,
            "pivot_energy": 0,
            "index": 0,
            "pos": 1,
            "flux": 1
        }
        f4 = {
            "curvature": 0,
            "pivot_energy": 0,
            "index": 1,
            "pos": 0,
            "flux": 0
        }
        f5 = {
            "curvature": 0,
            "pivot_energy": 0,
            "index": 1,
            "pos": 0,
            "flux": 1
        }

        f7 = {
            "curvature": 0,
            "pivot_energy": 0,
            "index": 1,
            "pos": 1,
            "flux": 1
        }

        f28 = {
            "curvature": 1,
            "pivot_energy": 1,
            "index": 1,
            "pos": 0,
            "flux": 0
        }
        f30 = {
            "curvature": 1,
            "pivot_energy": 1,
            "index": 1,
            "pos": 1,
            "flux": 0
        }
        f32 = {
            "curvature": 0,
            "pivot_energy": 0,
            "index": 0,
            "pos": 2,
            "flux": 0
        }  # special case

        fs = [f0, f1, f2, f3, f4, f5, f7, f28, f30, f32]

        for i in range(len(fs)):
            ff = i
            if ff == 6: ff = 7
            elif ff == 7: ff = 28
            elif ff == 8: ff = 30
            elif ff == 9: ff = 32

            self.assertDictEqual(
                fs[i], SourcesLibraryUT.get_free_params(self.sl.sources[i]))

    def test_source_file_parsing(self):

        sourceFile = os.path.join(self.currentDirPath,
                                  "test_data/testcase_2AGLJ0835-4514.source")

        res = self.sl.parseSourceFile(sourceFile)

        self.assertEqual(True, bool(res))

        self.assertEqual(True, isinstance(res.multiFlux["value"], float))
        self.assertEqual(9.07364e-06, res.multiFlux["value"])
        self.assertEqual(True, isinstance(res.multiSqrtTS["value"], float))
        self.assertEqual(2.17268, res.multiSqrtTS["value"])
        self.assertEqual(None, res.multiDist["value"])

    def test_load_source_from_catalog_without_scaling(self):

        sources = self.sl.loadSourcesFromCatalog(catalogName="2AGL")

        self.assertEqual(175, len(sources))

        self.assertEqual(7.45398e-08, sources[0].spectrum.getVal("flux"))

    def test_load_source_from_catalog_with_scaling(self):

        self.config.setOptions(emin_sources=10, emax_sources=1000)

        sources = self.sl.loadSourcesFromCatalog(catalogName="2AGL")

        self.assertEqual(175, len(sources))

        self.assertEqual(6.940938928095228e-07,
                         sources[0].spectrum.getVal("flux"))

    def test_select_sources_with_selection_string(self):

        self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.xml"))
        self.assertEqual(2, len(self.sl.sources))

        sources = self.sl.selectSources(
            'name == "2AGLJ2021+3654" AND flux > 0')
        self.assertEqual(1, len(sources))

        sourceFile = os.path.join(self.currentDirPath,
                                  "test_data/testcase_2AGLJ2021+3654.source")

        mleAnalysisResults = self.sl.parseSourceFile(sourceFile)

        self.sl.updateSourceWithMLEResults(mleAnalysisResults)

        sources = self.sl.selectSources(
            'name == "2AGLJ2021+3654" AND flux > 0')
        self.assertEqual(1, len(sources))

        sources = self.sl.selectSources('multisqrtts == 10')
        self.assertEqual(1, len(sources))

        sources = self.sl.selectSources('sqrtts == 10')
        self.assertEqual(1, len(sources))

    def test_select_sources_with_selection_lambda(self):

        self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.xml"))

        sources = self.sl.selectSources(lambda name: name == "2AGLJ2021+3654")
        self.assertEqual(1, len(sources))

        sourceFile = os.path.join(self.currentDirPath,
                                  "test_data/testcase_2AGLJ2021+3654.source")

        mleAnalysisResults = self.sl.parseSourceFile(sourceFile)

        self.sl.updateSourceWithMLEResults(mleAnalysisResults)

        sources = self.sl.selectSources(
            lambda name, flux: name == "2AGLJ2021+3654" and flux > 0)
        self.assertEqual(1, len(sources))

    def test_free_sources_with_selection_string(self):

        self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.xml"))
        sourceFile = os.path.join(self.currentDirPath,
                                  "test_data/testcase_2AGLJ2021+3654.source")
        mleAnalysisResults = self.sl.parseSourceFile(sourceFile)
        self.sl.updateSourceWithMLEResults(mleAnalysisResults)

        sources = self.sl.freeSources('name == "2AGLJ2021+3654" AND flux > 0',
                                      "flux", False)

        self.assertEqual(1, len(sources))
        self.assertEqual(True, "flux" not in sources[0].getFreeParams())

        sources = self.sl.freeSources('name == "2AGLJ2021+3654" AND flux > 0',
                                      "flux", True)
        self.assertEqual(True, "flux" in sources[0].getFreeParams())

        sources = self.sl.freeSources('name == "2AGLJ2021+3654" AND flux > 0',
                                      "index", True)
        self.assertEqual(True, "index" in sources[0].getFreeParams())

        sources = self.sl.freeSources('name == "2AGLJ2021+3654" AND flux > 0',
                                      "index", False)
        self.assertEqual(True, "index" not in sources[0].getFreeParams())

    def test_free_sources_with_selection_lambda(self):

        self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.xml"))
        sourceFile = os.path.join(self.currentDirPath,
                                  "test_data/testcase_2AGLJ2021+3654.source")
        mleAnalysisResults = self.sl.parseSourceFile(sourceFile)
        self.sl.updateSourceWithMLEResults(mleAnalysisResults)

        sources = self.sl.freeSources(
            lambda name, flux: name == "2AGLJ2021+3654" and flux > 0, "flux",
            False)
        self.assertEqual(1, len(sources))
        self.assertEqual(True, "flux" not in sources[0].getFreeParams())

        sources = self.sl.freeSources(
            lambda name, flux: name == "2AGLJ2021+3654" and flux > 0, "flux",
            True)
        self.assertEqual(True, "flux" in sources[0].getFreeParams())

        sources = self.sl.freeSources(
            lambda name, flux: name == "2AGLJ2021+3654" and flux > 0, "index",
            True)
        self.assertEqual(True, "index" in sources[0].getFreeParams())

        sources = self.sl.freeSources(
            lambda name, flux: name == "2AGLJ2021+3654" and flux > 0, "index",
            False)
        self.assertEqual(True, "index" not in sources[0].getFreeParams())

    def test_write_to_file_xml(self):

        self.config = AgilepyConfig()

        self.config.loadBaseConfigurations(self.agilepyConf)

        self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.xml"))

        outfileName = "write_to_file_testcase"

        outputFile = Path(self.sl.writeToFile(outfileName, fileformat="xml"))

        self.assertEqual(True, outputFile.exists())

        sourcesxml = parse(outputFile).getroot()

        self.assertEqual(2, len(sourcesxml))

    def test_write_to_file_txt(self):

        self.config = AgilepyConfig()

        self.config.loadBaseConfigurations(self.agilepyConf)

        sourcesFile = os.path.join(
            self.currentDirPath,
            "test_data/sourcesconf_for_write_to_file_txt.txt")

        self.sl.loadSourcesFromFile(sourcesFile)

        outfileName = "write_to_file_testcase"

        outputFile = Path(self.sl.writeToFile(outfileName, fileformat="txt"))

        self.assertEqual(True, outputFile.exists())

        with open(outputFile) as of:
            lines = of.readlines()

        self.assertEqual(
            "1.57017e-07 80.3286 1.12047 2.16619 0 2 _2AGLJ2032+4135 0 0 0 0 0.5 5.0 20 10000 0 100",
            lines[0].strip())
        self.assertEqual(
            "1.69737e-07 79.9247 0.661449 1.99734 0 2 CYGX3 0 0 0 0 0.5 5.0 20 10000 0 100",
            lines[1].strip())
        self.assertEqual(
            "1.19303e-06 78.2375 2.12298 1.75823 3 2 _2AGLJ2021+4029 0 1 3307.63 0 0.5 5.0 20.0 10000.0  0 100",
            lines[2].strip())

    def test_add_source(self):

        self.config = AgilepyConfig()

        self.config.loadBaseConfigurations(self.agilepyConf)

        self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.xml"))

        newSourceDict = {"a": 10}
        self.assertRaises(SourceParamNotFoundError, self.sl.addSource,
                          "newsource", newSourceDict)

        newSourceDict = {
            "glon": 250,
            "glat": 30,
            "spectrumType": "LogPaperone"
        }
        self.assertRaises(SpectrumTypeNotFoundError, self.sl.addSource,
                          "newsource", newSourceDict)

        newSourceDict = {
            "glon": 250,
            "glat": 30,
            "spectrumType": "LogParabola"
        }
        self.assertRaises(SourceParamNotFoundError, self.sl.addSource, "",
                          newSourceDict)
        self.assertRaises(SourceParamNotFoundError, self.sl.addSource, None,
                          newSourceDict)
        self.assertRaises(SourceParamNotFoundError, self.sl.addSource,
                          "newsource", newSourceDict)

        newSourceDict = {
            "glon": 250,
            "glat": 30,
            "spectrumType": "LogParabola",
            "flux": 40,
            "index": 2,
            "pivotEnergy": 4,
            "curvature": 5
        }
        newSourceObj = self.sl.addSource("newsource", newSourceDict)

        self.assertEqual(True, isinstance(newSourceObj, PointSource))

        newSource = self.sl.selectSources('name == "newsource"').pop()

        self.assertEqual(40, newSource.spectrum.getVal("flux"))
        self.assertEqual(5, newSource.spectrum.getVal("curvature"))
        self.assertEqual("newsource", newSource.name)
        self.assertEqual(35.2462913047547,
                         newSource.spatialModel.getVal("dist"))

    def test_convert_catalog_to_xml(self):

        catalogFile = "$AGILE/catalogs/2AGL.multi"

        outfile = self.sl.convertCatalogToXml(catalogFile)

        sourcesxml = parse(outfile).getroot()

        self.assertEqual(175, len(sourcesxml))

        added = self.sl.loadSourcesFromFile(outfile)

        self.assertEqual(175, len(added))

        self.assertEqual(175, len(self.sl.sources))

    def test_backup_restore(self):
        self.config = AgilepyConfig()

        self.config.loadBaseConfigurations(self.agilepyConf)

        self.sl.loadSourcesFromFile(
            os.path.join(self.currentDirPath, "test_data/sources_2.xml"))
        """
        for s in self.sl.getSources():
            print(s)
        """

        self.assertEqual(2, len(self.sl.sources))

        self.sl.backupSL()

        self.sl.deleteSources('name=="2AGLJ2021+4029"')

        self.assertEqual(1, len(self.sl.sources))

        self.sl.deleteSources('name=="2AGLJ2021+3654"')

        self.assertEqual(0, len(self.sl.sources))

        self.sl.restoreSL()

        self.assertEqual(2, len(self.sl.sources))
Exemple #4
0
class SourceModelUT(unittest.TestCase):
    def setUp(self):
        self.currentDirPath = Path(__file__).parent.absolute()

        self.test_logs_dir = Path(self.currentDirPath).joinpath(
            "test_logs", "SourceModelUT")
        self.test_logs_dir.mkdir(parents=True, exist_ok=True)

        os.environ["TEST_LOGS_DIR"] = str(self.test_logs_dir)

        outDir = Path(os.path.join(os.environ["AGILE"])).joinpath(
            "agilepy-test-data/unittesting-output/core")
        if outDir.exists() and outDir.is_dir():
            shutil.rmtree(outDir)

        self.sourcesTxt = os.path.join(self.currentDirPath,
                                       "test_data/sources.txt")
        self.sourcesXml = os.path.join(self.currentDirPath,
                                       "test_data/sources.xml")

        self.agilepyConf = os.path.join(self.currentDirPath,
                                        "conf/agilepyconf.yaml")

        self.config = AgilepyConfig()
        self.config.loadBaseConfigurations(self.agilepyConf)
        self.config.loadConfigurationsForClass("AGAnalysis")

        self.logger = AgilepyLogger()
        self.logger.initialize(
            self.config.getConf("output", "outdir"),
            self.config.getConf("output", "logfilenameprefix"),
            self.config.getConf("output", "verboselvl"))

        self.sl = SourcesLibrary(self.config, self.logger)

    def test_parse_source_XML_format(self):

        xmlRoot = parse(self.sourcesXml).getroot()

        sources = []

        for sourceRoot in xmlRoot:

            source = Source.parseSourceXMLFormat(sourceRoot)

            sources.append(source)

        assert sources[0].get("flux")["value"] == 7.45398e-08
        assert sources[0].get("pos")["value"] == (92.4102, -10.3946)

        assert sources[1].get("flux")["value"] == 41.6072e-08
        assert sources[1].get("pos")["value"] == (119.677, 10.544)

        assert sources[2].get("flux")["value"] == 969.539e-08
        assert sources[2].get("index2")["value"] == 1.3477
        assert sources[2].get("index2")["free"] == 1
        assert sources[2].get("pos")["value"] == (263.585, -2.84083)

        assert sources[3].get("flux")["value"] == 35.79e-08
        assert sources[3].get("curvature")["value"] == 0.682363
        assert sources[3].get("curvature")["max"] == 3
        assert sources[3].get("pos")["value"] == (6.16978, -0.0676943)

    def test_parse_source_TXT_format(self):

        sources = []

        with open(self.sourcesTxt, "r") as txtFile:

            for line in txtFile:

                if line == "\n":
                    continue

                source = Source.parseSourceTXTFormat(line)

                sources.append(source)

        assert sources[0].name == "2AGLJ2021+4029"
        assert sources[0].get("flux")["value"] == 119.3e-08
        assert sources[0].get("pos")["value"] == (78.2375, 2.12298)

        assert sources[1].name == "2AGLJ2021+3654"
        assert sources[1].get("flux")["value"] == 70.89e-08
        assert sources[1].get("pos")["value"] == (75.2562, 0.151831)

    def test_init(self):

        source = PointSource(name="test-source")
        source.spectrum = Spectrum.getSpectrum("PowerLaw")
        assert "PointSource" == type(source.spatialModel).__name__

    def test_get(self):

        source = PointSource(name="test-source")
        source.spectrum = Spectrum.getSpectrum("PowerLaw")
        assert "PointSource" == type(source.spatialModel).__name__

        assert len(source.get("flux").keys()) == 6
        assert source.getVal("flux") == None

        with pytest.raises(SourceParameterNotFound):
            source.get("fluxxx")

        with pytest.raises(SourceParameterNotFound):
            source.getVal("fluxxx")

    def test_set(self):

        source = PointSource(name="test-source")
        source.spectrum = Spectrum.getSpectrum("PowerLaw")
        assert "PointSource" == type(source.spatialModel).__name__

        source.set("flux", {"min": 1})
        source.setVal("flux", 10)
        assert source.spectrum.flux["min"] == 1
        assert source.spectrum.flux["value"] == 10

        with pytest.raises(SourceParameterNotFound):
            source.set("fluxxx", {"value": 10})

        with pytest.raises(ValueError):
            source.set("fluxxx", 10)

        with pytest.raises(SourceParameterNotFound):
            source.setVal("fluxxx", 100)

    def test_str(self):
        source = PointSource(name="test-source")
        source.spectrum = Spectrum.getSpectrum("PowerLaw")
        source.setVal("flux", 100)
        source.setVal("index", 1000)
        source.setVal("pos", (30, 15))

        print(source)
        """