Example #1
0
    def test_energybins(self):

        self.config = AgilepyConfig()

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

        self.config.loadConfigurations(conf1Path)

        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]"])
    def test_add_source(self):

        self.config = AgilepyConfig()

        self.config.loadConfigurations(self.agilepyconfPath, validate=True)

        self.sl.loadSourcesFromFile(self.xmlsourcesconfPath)

        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)

        newSourceObj = self.sl.addSource("newsource", newSourceDict)
        self.assertEqual(True, isinstance(newSourceObj, Source))

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

        self.assertEqual(0, newSource.spectrum.get("flux"))
        self.assertEqual(0, newSource.spectrum.get("curvature"))
        self.assertEqual("newsource", newSource.name)
        self.assertEqual(148.52505082279242,
                         newSource.spatialModel.get("dist"))

        newSourceDict = {
            "glon": 250,
            "glat": 30,
            "spectrumType": "LogParabola",
            "flux": 1,
            "curvature": 2
        }

        newSourceObj = self.sl.addSource("newsource2", newSourceDict)
        self.assertEqual(True, isinstance(newSourceObj, Source))

        newSource = self.sl.selectSources('name == "newsource2"').pop()
        self.assertEqual(1, newSource.spectrum.get("flux"))
        self.assertEqual(2, newSource.spectrum.get("curvature"))
        self.assertEqual(250, newSource.spatialModel.get("pos")[0])
        self.assertEqual(30, newSource.spatialModel.get("pos")[1])
        self.assertEqual("newsource2", newSource.name)

        self.assertEqual(None, self.sl.addSource("newsource2", newSourceDict))
    def test_backup_restore(self):
        self.config = AgilepyConfig()

        self.config.loadConfigurations(self.agilepyconfPath, validate=True)

        self.sl.loadSourcesFromFile(self.xmlsourcesconfPath)
        """
        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))
    def test_write_to_file_txt(self):

        self.config = AgilepyConfig()

        self.config.loadConfigurations(self.agilepyconfPath, validate=True)

        sourcesFile = os.path.join(
            self.currentDirPath, "conf/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 setUp(self):
        self.currentDirPath = Path(__file__).parent.absolute()
        self.agilepyconfPath = os.path.join(self.currentDirPath,
                                            "conf/agilepyconf.yaml")
        self.xmlsourcesconfPath = os.path.join(self.currentDirPath,
                                               "conf/sourceconf.xml")
        self.agsourcesconfPath = os.path.join(self.currentDirPath,
                                              "conf/sourceconf.txt")

        self.config = AgilepyConfig()
        self.config.loadConfigurations(self.agilepyconfPath, validate=True)

        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/api"))

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

        self.sl = SourcesLibrary(self.config, self.logger)
Example #6
0
    def __init__(self, configurationFilePath):
        """AGEng constructor.

        Args:
            configurationFilePath (str): the relative or absolute path to the yaml configuration file.

        Example:
            >>> from agilepy.api import AGEng
            >>> ageng = AGEng('agconfig.yaml')

        """

        self.config = AgilepyConfig()

        self.config.loadConfigurations(configurationFilePath, validate=True)

        self.outdir = join(self.config.getConf("output", "outdir"), "eng_data")

        Path(self.outdir).mkdir(parents=True, exist_ok=True)

        self.logger = AgilepyLogger()

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

        self.plottingUtils = PlottingUtils(self.config, self.logger)
Example #7
0
    def setUp(self):
        self.currentDirPath = Path(__file__).parent.absolute()
        self.agilepyconfPath = os.path.join(self.currentDirPath,
                                            "conf/agilepyconf.yaml")

        self.config = AgilepyConfig()
        self.config.loadBaseConfigurations(self.agilepyconfPath)

        self.agilepyLogger = AgilepyLogger()

        self.agilepyLogger.initialize(
            self.config.getConf("output", "outdir"),
            self.config.getConf("output", "logfilenameprefix"),
            self.config.getConf("output", "verboselvl"))

        self.datadir = os.path.join(self.currentDirPath, "data")

        self.outDir = Path(self.config.getOptionValue("outdir"))

        if self.outDir.exists() and self.outDir.is_dir():
            self.agilepyLogger.reset()
            shutil.rmtree(self.outDir)

        self.tmpDir = Path("./tmp")
        self.tmpDir.mkdir(exist_ok=True)
Example #8
0
    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))
Example #9
0
    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)
Example #10
0
    def __init__(self, configurationFilePath):

        self.config = AgilepyConfig()

        # load only "input" and "output" sections
        self.config.loadBaseConfigurations(
            Utils._expandEnvVar(configurationFilePath))

        # Creating output directory

        self.outdir = self.config.getConf("output", "outdir")

        Path(self.outdir).mkdir(parents=True, exist_ok=True)

        self.logger = AgilepyLogger()

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

        self.plottingUtils = PlottingUtils(self.config, self.logger)

        if "AGILE" not in os.environ:
            raise AGILENotFoundError("$AGILE is not set.")

        if "PFILES" not in os.environ:
            raise PFILESNotFoundError("$PFILES is not set.")
Example #11
0
    def test_validation_tmax_not_in_index(self):

        self.config = AgilepyConfig()
        self.config.loadConfigurations(self.agilepyconfPath, validate=False)

        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          tmax=456537946,
                          timetype="TT")
Example #12
0
    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"))
Example #13
0
    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)

        self.config = AgilepyConfig()
        self.config.loadConfigurations(self.agilepyconfPath, validate=False)
Example #14
0
    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"))
Example #15
0
    def test_validation_min_max(self):

        self.config = AgilepyConfig()
        self.config.loadConfigurations(self.agilepyconfPath, validate=False)

        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          fovradmin=10,
                          fovradmax=0)
        self.assertRaises(ConfigurationsNotValidError,
                          self.config.setOptions,
                          emin=10,
                          emax=0)
Example #16
0
    def test_set_options(self):

        self.config = AgilepyConfig()
        self.config.loadConfigurations(self.agilepyconfPath, validate=False)

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

        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])
    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_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)
Example #19
0
    def __init__(self, configurationFilePath, sourcesFilePath):
        """AGAnalysis constructor.

        Args:
            configurationFilePath (str): the relative or absolute path to the yaml configuration file.
            sourcesFilePath (str): the relative or absolute path to the xml sources descriptor file.

        Raises:
            FileExistsError: if the output directory already exists.
            AGILENotFoundError: if the AGILE environment variable is not set.
            PFILESNotFoundError: if the PFILES environment variable is not set.

        Example:
            >>> from agilepy.api import AGAnalysis
            >>> aganalysis = AGAnalysis('agconfig.yaml', 'sources.xml')

        """

        self.config = AgilepyConfig()

        self.config.loadConfigurations(configurationFilePath, validate=True)

        self.config.printOptions("output")

        self.outdir = self.config.getConf("output", "outdir")

        if exists(self.outdir):
            raise FileExistsError(
                "The output directory %s already exists! Please, delete it or specify another output directory!"
                % (self.outdir))

        self.logger = agilepyLogger

        self.logger.initialize(self.outdir,
                               self.config.getConf("output", "logfilename"),
                               self.config.getConf("output", "debuglvl"))

        self.sourcesLibrary = SourcesLibrary()

        self.sourcesLibrary.loadSources(sourcesFilePath)

        if "AGILE" not in os.environ:
            raise AGILENotFoundError("$AGILE is not set.")

        if "PFILES" not in os.environ:
            raise PFILESNotFoundError("$PFILES is not set.")
    def test_write_to_file_xml(self):

        self.config = AgilepyConfig()

        self.config.loadConfigurations(self.agilepyconfPath, validate=True)

        self.sl.loadSourcesFromFile(self.xmlsourcesconfPath)

        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))
Example #21
0
    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))
Example #22
0
    def test_plot_data_availability(self):

        outputDir = self.currentDirPath.joinpath(
            "output", "test_plot_data_availability")
        outputDir.mkdir(parents=True, exist_ok=True)
        os.environ["TEST_LOGS_DIR"] = str(outputDir)

        config = AgilepyConfig()
        config.loadBaseConfigurations(
            self.currentDirPath.joinpath("conf",
                                         "test_plot_data_availability.yaml"))

        pu = PlottingUtils(config, self.agilepyLogger)

        dataDir = Path(self.datadir).joinpath("test_plot_data_availability")

        pu.plotDataAvailability(dataDir.joinpath("EVT.qfile"),
                                dataDir.joinpath("EVT.index"),
                                saveImage=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()
    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"))
Example #25
0
    def test_complete_configuration(self):

        self.config = AgilepyConfig()

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

        self.config.loadConfigurations(conf1Path, validate=False)

        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.loadConfigurations(conf2Path, validate=False)

        self.assertEqual(9.21034, self.config.getOptionValue("loccl"))
Example #26
0
class AgilepyConfigUnittesting(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)

        self.config = AgilepyConfig()
        self.config.loadConfigurations(self.agilepyconfPath, validate=False)

    def test_validation_tmin_not_in_index(self):

        self.config.setOptions(tmin=456361777)

        error_dict = self.config._validateConfiguration(self.config.conf)

        self.assertEqual(True, "input/tmin" in error_dict)
        self.assertEqual(1, len(error_dict))

    def test_validation_tmax_not_in_index(self):

        self.config.setOptions(tmax=456537946)

        error_dict = self.config._validateConfiguration(self.config.conf)

        self.assertEqual(True, "input/tmax" in error_dict)
        self.assertEqual(1, len(error_dict))

    def test_set_options(self):

        # float instead of int is ok.
        self.assertEqual(None, self.config.setOptions(tmin=456361777))

        """
Example #27
0
class AGBaseAnalysis:
    def __init__(self, configurationFilePath):

        self.config = AgilepyConfig()

        # load only "input" and "output" sections
        self.config.loadBaseConfigurations(
            Utils._expandEnvVar(configurationFilePath))

        # Creating output directory

        self.outdir = self.config.getConf("output", "outdir")

        Path(self.outdir).mkdir(parents=True, exist_ok=True)

        self.logger = AgilepyLogger()

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

        self.plottingUtils = PlottingUtils(self.config, self.logger)

        if "AGILE" not in os.environ:
            raise AGILENotFoundError("$AGILE is not set.")

        if "PFILES" not in os.environ:
            raise PFILESNotFoundError("$PFILES is not set.")

    def getAnalysisDir(self):
        """It returns the path of the output directory

        Returns:
            path (str) : the path of the output directory
        """

        if self.outdir.exists() and self.outdir.is_dir():
            return str(self.outdir)

        else:
            self.logger.warning(self, "OutputDirectory not found")

    def deleteAnalysisDir(self):
        """It deletes the output directory where all the products of the analysis are written.

        Args:

        Returns:
            True if the directory is succesfully deleted, False otherwise.

        """
        outDir = Path(self.config.getConf("output", "outdir"))

        if outDir.exists() and outDir.is_dir():
            self.logger.info(self, "Removing directory %s", str(outDir))
            self.logger.reset()
            rmtree(outDir)
        else:
            self.logger.warning(self,
                                "Output directory %s exists? %r is dir? %r",
                                str(outDir), outDir.exists(), outDir.is_dir())
            return False

        return True

    def setOptions(self, **kwargs):
        """It updates configuration options specifying one or more key=value pairs at once.

        Args:
            kwargs: key-values pairs, separated by a comma.

        Returns:
            None

        Raises:
            ConfigFileOptionTypeError: if the type of the option value is not wrong.
            ConfigurationsNotValidError: if the values are not coherent with the configuration.
            CannotSetHiddenOptionError: if the option is hidden.
            OptionNotFoundInConfigFileError: if the option is not found.

        Note:
            The ``config`` attribute is initialized by reading the corresponding
            yaml configuration file, loading its contents in memory. Updating the values
            held by this object will not affect the original values written on disk.

        Example:

            >>> aganalysis.setOptions(mapsize=60, binsize=0.5)
            True

        """
        return self.config.setOptions(**kwargs)

    def getOption(self, optionName):
        """It reads an option value from the configuration.

        Args:
            optionName (str): the name of the option.

        Returns:
            The option value

        Raises:
            OptionNotFoundInConfigFileError: if the optionName is not found in the configuration.
        """

        return self.config.getOptionValue(optionName)

    def printOptions(self, section=None):
        """It prints the configuration options in the console.

        Args:
            section (str): you can specify a configuration file section to be printed out.

        Returns:
            None
        """
        return self.config.printOptions(section)
Example #28
0
class AgilepyUtilsUT(unittest.TestCase):
    def setUp(self):
        self.currentDirPath = Path(__file__).parent.absolute()
        self.agilepyconfPath = os.path.join(self.currentDirPath,
                                            "conf/agilepyconf.yaml")

        self.config = AgilepyConfig()
        self.config.loadBaseConfigurations(self.agilepyconfPath)

        self.agilepyLogger = AgilepyLogger()

        self.agilepyLogger.initialize(
            self.config.getConf("output", "outdir"),
            self.config.getConf("output", "logfilenameprefix"),
            self.config.getConf("output", "verboselvl"))

        self.datadir = os.path.join(self.currentDirPath, "data")

        self.outDir = Path(self.config.getOptionValue("outdir"))

        if self.outDir.exists() and self.outDir.is_dir():
            self.agilepyLogger.reset()
            shutil.rmtree(self.outDir)

        self.tmpDir = Path("./tmp")
        self.tmpDir.mkdir(exist_ok=True)

    def tearDown(self):
        self.agilepyLogger.reset()
        if self.tmpDir.exists() and self.tmpDir.is_dir():
            shutil.rmtree(self.tmpDir)

    def test_display_sky_map(self):

        pu = PlottingUtils(self.config, self.agilepyLogger)

        smooth = 4
        fileFormat = ".png"
        title = "testcase"
        cmap = "CMRmap"
        regFiles = [
            Utils._expandEnvVar("$AGILE/catalogs/2AGL_2.reg"),
            Utils._expandEnvVar("$AGILE/catalogs/2AGL_2.reg")
        ]
        regFileColors = ["yellow", "blue"]


        file = pu.displaySkyMap(
                    self.datadir+"/testcase_EMIN00100_EMAX00300_01.cts.gz", \
                    smooth = smooth,
                    fileFormat = fileFormat,
                    title = title,
                    cmap = cmap,
                    regFiles = regFiles,
                    regFileColors=regFileColors,
                    catalogRegions = "2AGL",
                    catalogRegionsColor = "red",
                    saveImage=True,
                    normType="linear")

        assert True == os.path.isfile(file)

    def test_display_sky_map_single_mode_3_imgs(self):

        pu = PlottingUtils(self.config, self.agilepyLogger)

        smooth = 4
        fileFormat = ".png"
        title = "testcase"
        cmap = "CMRmap"
        regFiles = [
            Utils._expandEnvVar("$AGILE/catalogs/2AGL_2.reg"),
            Utils._expandEnvVar("$AGILE/catalogs/2AGL_2.reg")
        ]
        regFileColors = ["yellow", "blue"]
        img = self.datadir + "/testcase_EMIN00100_EMAX00300_01.cts.gz"

        file = pu.displaySkyMapsSingleMode(
                    [img, img, img], \
                    smooth = smooth,
                    fileFormat = fileFormat,
                    titles = [title+"_1", title+"_2", title+"_3"],
                    cmap = cmap,
                    regFiles = regFiles,
                    regFileColors=regFileColors,
                    catalogRegions = "2AGL",
                    catalogRegionsColor = "red",
                    saveImage=True,
                    normType="linear")

        assert True == os.path.isfile(file)

    def test_display_sky_map_single_mode_2_imgs(self):

        pu = PlottingUtils(self.config, self.agilepyLogger)

        smooth = 4
        fileFormat = ".png"
        title = "testcase"
        cmap = "CMRmap"
        regFiles = [
            Utils._expandEnvVar("$AGILE/catalogs/2AGL_2.reg"),
            Utils._expandEnvVar("$AGILE/catalogs/2AGL_2.reg")
        ]
        regFileColors = ["yellow", "blue"]
        img = self.datadir + "/testcase_EMIN00100_EMAX00300_01.cts.gz"

        file = pu.displaySkyMapsSingleMode(
                    [img, img], \
                    smooth = smooth,
                    fileFormat = fileFormat,
                    titles = [title+"_1", title+"_2", title+"_3"],
                    cmap = cmap,
                    regFiles = regFiles,
                    regFileColors=regFileColors,
                    catalogRegions = "2AGL",
                    catalogRegionsColor = "red",
                    saveImage=True,
                    normType="linear")

        assert True == os.path.isfile(file)

    def test_plot_data_availability(self):

        outputDir = self.currentDirPath.joinpath(
            "output", "test_plot_data_availability")
        outputDir.mkdir(parents=True, exist_ok=True)
        os.environ["TEST_LOGS_DIR"] = str(outputDir)

        config = AgilepyConfig()
        config.loadBaseConfigurations(
            self.currentDirPath.joinpath("conf",
                                         "test_plot_data_availability.yaml"))

        pu = PlottingUtils(config, self.agilepyLogger)

        dataDir = Path(self.datadir).joinpath("test_plot_data_availability")

        pu.plotDataAvailability(dataDir.joinpath("EVT.qfile"),
                                dataDir.joinpath("EVT.index"),
                                saveImage=True)

        #pu.plotDataAvailability(dataDir.joinpath("LOG.qfile"), dataDir.joinpath("LOG.index"), saveImage=True)

    def test_initialize_logger_verboselvl_2(self):
        sleep(1.0)
        self.agilepyLogger.reset()

        self.config.loadBaseConfigurations(
            os.path.join(self.currentDirPath,
                         "conf/agilepyconf_verbose_2.yaml"))

        logfilePath = self.agilepyLogger.initialize(
            self.config.getOptionValue("outdir"),
            self.config.getOptionValue("logfilenameprefix"),
            self.config.getOptionValue("verboselvl"))

        assert True == logfilePath.is_file()

        with open(logfilePath, "r") as f:
            linesNumber = len(f.readlines())
            assert 1 == linesNumber

        self.agilepyLogger.debug(self, "%s %s", "Debug", "message")
        self.agilepyLogger.info(self, "%s %s", "Info", "message")
        self.agilepyLogger.warning(self, "%s %s", "Warning", "message")
        self.agilepyLogger.critical(self, "%s %s", "Critical", "message")

        with open(logfilePath, "r") as f:
            linesNumber = len(f.readlines())
            assert 5 == linesNumber

    def test_initialize_logger_verboselvl_1(self):
        sleep(1.0)
        self.agilepyLogger.reset()

        self.config.loadBaseConfigurations(
            os.path.join(self.currentDirPath,
                         "conf/agilepyconf_verbose_1.yaml"))

        logfilePath = self.agilepyLogger.initialize(
            self.config.getOptionValue("outdir"),
            self.config.getOptionValue("logfilenameprefix"),
            self.config.getOptionValue("verboselvl"))

        assert True == logfilePath.is_file()

        with open(logfilePath, "r") as f:
            linesNumber = len(f.readlines())
            assert 1 == linesNumber

        self.agilepyLogger.debug(self, "%s %s", "Debug", "message")
        self.agilepyLogger.info(self, "%s %s", "Info", "message")
        self.agilepyLogger.warning(self, "%s %s", "Warning", "message")
        self.agilepyLogger.critical(self, "%s %s", "Critical", "message")

        with open(logfilePath, "r") as f:
            linesNumber = len(f.readlines())
            assert 5 == linesNumber

    def test_initialize_logger_verboselvl_0(self):
        sleep(1.0)
        self.agilepyLogger.reset()

        self.config.loadBaseConfigurations(
            os.path.join(self.currentDirPath,
                         "conf/agilepyconf_verbose_0.yaml"))

        logfilePath = self.agilepyLogger.initialize(
            self.config.getOptionValue("outdir"),
            self.config.getOptionValue("logfilenameprefix"),
            self.config.getOptionValue("verboselvl"))

        assert True == logfilePath.is_file()

        with open(logfilePath, "r") as f:
            linesNumber = len(f.readlines())
            assert 1 == linesNumber

        self.agilepyLogger.debug(self, "%s %s", "Debug", "message")
        self.agilepyLogger.info(self, "%s %s", "Info", "message")
        self.agilepyLogger.warning(self, "%s %s", "Warning", "message")
        self.agilepyLogger.critical(self, "%s %s", "Critical", "message")

        with open(logfilePath, "r") as f:
            linesNumber = len(f.readlines())
            assert 5 == linesNumber

    def test_filterAP(self):

        print(self.datadir + "/E1q1_604800s_emin100_emax10000_r2.ap")
        print(self.currentDirPath)
        product = AstroUtils.AP_filter(
            self.datadir + "/E1q1_604800s_emin100_emax10000_r2.ap", 1,
            174142800, 447490800, self.currentDirPath)
        with open(product, "r") as f:
            linesNumber = len(f.readlines())
            assert 4 == linesNumber

        os.remove(os.path.join(self.currentDirPath, "result.txt"))
        os.remove(os.path.join(self.currentDirPath, product))

    """
    Time conversions
        # https://tools.ssdc.asi.it/conversionTools
        # https://heasarc.gsfc.nasa.gov/cgi-bin/Tools/xTime/xTime.pl?time_in_i=&time_in_c=&time_in_d=&time_in_j=&time_in_m=58871.45616898&time_in_sf=&time_in_wf=&time_in_sl=&time_in_sni=&time_in_snu=&time_in_s=&time_in_h=&time_in_sz=&time_in_ss=&time_in_sn=&timesys_in=u&timesys_out=u&apply_clock_offset=yes
    """

    def test_astro_utils_time_mjd_to_agile_seconds(self):
        sec_tolerance = 0.001
        tt = AstroUtils.time_mjd_to_agile_seconds(
            58871.45616898)  # 506861812.99987227
        assert abs(506861813 - tt) <= sec_tolerance

    def test_astro_utils_time_agile_seconds_to_mjd(self):
        sec_tolerance = 0.0000001
        mjd = AstroUtils.time_agile_seconds_to_mjd(507391426.9447)
        assert abs(58877.58595999 - mjd) <= sec_tolerance

    def test_astro_utils_time_utc_to_jd(self):

        tol = 0.00000001

        dt = "2020-01-23T10:56:53.000"

        jd = AstroUtils.time_fits_to_jd(dt)

        assert abs(2458871.95616898 - jd) <= tol

    def test_astro_utils_time_utc_to_mjd(self):

        tol = 0.00000001

        dt = "2020-01-23T10:56:53.000"

        mjd = AstroUtils.time_fits_to_mjd(dt)

        assert abs(58871.45616898 - mjd) <= tol

    def test_astro_utils_time_utc_to_tt(self):

        tol = 0.0001

        agileseconds = AstroUtils.time_fits_to_agile_seconds(
            "2020-01-23T10:56:53.000")

        assert abs(506861813 - agileseconds) <= tol

    def test_astro_utils_time_agile_seconds_to_jd(self):
        jd = AstroUtils.time_agile_seconds_to_jd(449582332)
        assert jd == pytest.approx(2458208.99921296, 0.00001)

    def test_astro_utils_time_agile_seconds_to_utc(self):

        sec_tol = 1
        """
        utc = AstroUtils.time_tt_to_utc(506861813)
        dt = datetime.strptime(utc, '%Y-%m-%dT%H:%M:%S.%f')

        assert dt.year == 2020
        assert dt.month == 1
        assert dt.day == 23
        assert dt.hour == 10
        assert dt.minute == 56
        assert abs(53 - dt.second) <= sec_tol
        """

        # This date would result in "0 days"
        sec_tolerance = 0.0000001
        fitstime = AstroUtils.time_agile_seconds_to_fits(449582332)
        dt = datetime.strptime(fitstime, '%Y-%m-%dT%H:%M:%S.%f')

        assert dt.year == 2018
        assert dt.month == 3
        assert dt.day == 31
        assert dt.hour == 11
        assert dt.minute == 58
        assert abs(52 - dt.second) <= sec_tol

    def test_astro_utils_time_mjd_to_fits(self):

        sec_tol = 1

        fitstime = AstroUtils.time_mjd_to_fits(58871.45616898)

        dt = datetime.strptime(fitstime, '%Y-%m-%dT%H:%M:%S.%f')

        assert dt.year == 2020
        assert dt.month == 1
        assert dt.day == 23
        assert dt.hour == 10
        assert dt.minute == 56
        assert abs(53 - dt.second) <= sec_tol

    def test_astro_utils_time_fits_to_mjd_2(self):

        sec_tol = 0.00000001

        mjd = AstroUtils.time_fits_to_mjd("2020-01-23T10:56:53.000")

        assert abs(58871.45616898 - mjd) <= sec_tol

    def test_get_first_and_last_line_in_file(self):

        line1 = '/ASDC_PROC2/FM3.119_2/EVT/agql2004151750_2004151850.EVT__FM.gz 514057762.000000 514061362.000000 EVT\n'
        line2 = '/ASDC_PROC2/FM3.119_2/EVT/agql2004152249_2004160008.EVT__FM.gz 514075704.000000 514080437.000000 EVT\n'
        line3 = '/ASDC_PROC2/FM3.119_2/EVT/agql2004160008_2004160045.EVT__FM.gz 514080437.000000 514082644.000000 EVT\n'

        # I test: 1 line
        test_file = self.tmpDir.joinpath("test_file1.txt")
        with open(test_file, "w") as f:
            f.write(line1)
        (first, last) = Utils._getFirstAndLastLineInFile(test_file)
        assert first == line1
        assert last == line1

        # II test: 2 lines
        test_file = self.tmpDir.joinpath("test_file2.txt")
        with open(test_file, "w") as f:
            f.write(line1)
            f.write(line2)
        (first, last) = Utils._getFirstAndLastLineInFile(test_file)
        assert first == line1
        assert last == line2

        # III test: 3 lines
        test_file = self.tmpDir.joinpath("test_file3.txt")
        with open(test_file, "w") as f:
            f.write(line1)
            f.write(line2)
            f.write(line3)
        (first, last) = Utils._getFirstAndLastLineInFile(test_file)
        assert first == line1
        assert last == line3

    """
    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)
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
    """