def test_function_values_have_not_changed():

    with h5py.File(_get_data_file_path("past_1D_values.h5"), "r") as f:

        eval_x = f["eval_values"][()]

    for key in _known_functions:

        this_function = _known_functions[key]

        # Test only the power law of XSpec, which is the only one we know we can test at 1 keV

        if key.find("XS") == 0 or (key in _multiplicative_models):

            # An XSpec model OR EBLattenuation function. Test it only if it's a power law (the others might need other parameters during
            # initialization)

            continue

        if key.find("TemplateModel") == 0:

            # The TemplateModel function has its own test

            continue

    #        if key.find("Synchrotron")==0:

    # Naima Synchtron function should have its own test

    #            continue

        if this_function._n_dim == 1:

            print("testing %s ..." % key)

            func = this_function()

            new_values = np.atleast_1d(func(eval_x))

            with h5py.File(_get_data_file_path("past_1D_values.h5"), "r") as f:
                if key not in f.keys():

                    print(
                        "the function %s does not exist in the past data. You must run a script to add it"
                        % key)

                else:

                    old_values = f[key][()]

                    npt.assert_almost_equal(new_values, old_values)
Ejemplo n.º 2
0
def test_xspec_table_model():

    test_table = _get_data_file_path("tests/test_xspec_table_model.fits")

    xtm = XSPECTableModel(test_table)

    xtm.to_table_model('xspectm_test', 'xspec model', overwrite=True)
Ejemplo n.º 3
0
def test_table_conversion():

    old_table_file = _get_data_file_path("tests/old_table.h5")

    p = Path.home() / ".astromodels" / "data" / "old_table.h5"

    shutil.copy(old_table_file, p)

    # convert the table

    convert_old_table_model("old_table")

    # now load the old table

    old_table = TemplateModel("old_table")

    # should be the same as in test

    test = TemplateModel("__test")

    xx = np.logspace(1, 3, 50)

    npt.assert_almost_equal(test(xx), old_table(xx))

    p.unlink()
Ejemplo n.º 4
0
def build_tbabs_arg(ene):

    file_name = _get_data_file_path(Path("xsect/xsect_tbabs_wilm.fits"))
    fxs = fits.open(file_name)
    dxs = fxs[1].data
    xsect_ene = dxs["ENERGY"]
    xsect_val = dxs["SIGMA"]

    return np.interp(ene, xsect_ene, xsect_val)
Ejemplo n.º 5
0
def _get_xsect_table(model, abund_table):
    """
    contructs the abundance table from the values given
    """

    assert model in _abs_tables, "the model %s does not exist" % model
    assert abund_table in _abs_tables[model], ("the table %s does not exist" %
                                               abund_table)

    path_to_xsect = _get_data_file_path(
        os.path.join(
            "xsect",
            "xsect_%s_%s.fits" % (model, _abs_tables[model][abund_table])))

    fxs = fits.open(path_to_xsect)
    dxs = fxs[1].data
    xsect_ene = dxs["ENERGY"]
    xsect_val = dxs["SIGMA"]

    return xsect_ene, xsect_val
Ejemplo n.º 6
0
#

import sys

import h5py

from astromodels.functions.function import _known_functions
from astromodels.functions.priors import *
from astromodels.utils.data_files import _get_data_file_path

eval_x = np.logspace(-1,3, 10)
_multiplicative_models = ["PhAbs", "TbAbs", "WAbs", "APEC", "VAPEC"]

input = int(sys.argv[-1])

file_path = _get_data_file_path("past_1D_values.h5")


if input == 0:
    # do not regenerate only add
    with h5py.File(file_path, "r") as f:

        already_known_functions = list(f.keys())

        flag = "a"

elif input == 1:

    already_known_functions = []

    flag = "w"
Ejemplo n.º 7
0
    def _setup(self):

        tablepath = _get_data_file_path("dark_matter/gammamc_dif.dat")

        self._data = np.loadtxt(tablepath)

        """
            Mapping between the channel codes and the rows in the gammamc file

            1 : 8, # ee
            2 : 6, # mumu
            3 : 3, # tautau
            4 : 1, # bb
            5 : 2, # tt
            6 : 7, # gg
            7 : 4, # ww
            8 : 5, # zz
            9 : 0, # cc
            10 : 10, # uu
            11 : 11, # dd
            12 : 9, # ss
        """

        channel_index_mapping = {
            1: 8,  # ee
            2: 6,  # mumu
            3: 3,  # tautau
            4: 1,  # bb
            5: 2,  # tt
            6: 7,  # gg
            7: 4,  # ww
            8: 5,  # zz
            9: 0,  # cc
            10: 10,  # uu
            11: 11,  # dd
            12: 9,  # ss
        }

        # Number of decades in x = log10(E/M)
        ndec = 10.0
        xedge = np.linspace(0, 1.0, 251)
        self._x = 0.5 * (xedge[1:] + xedge[:-1]) * ndec - ndec

        ichan = channel_index_mapping[int(self.channel.value)]

        # These are the mass points
        self._mass = np.array([2.0, 4.0, 6.0, 8.0, 10.0,
                               25.0, 50.0, 80.3, 91.2, 100.0,
                               150.0, 176.0, 200.0, 250.0, 350.0, 500.0, 750.0,
                               1000.0, 1500.0, 2000.0, 3000.0, 5000.0, 7000.0, 1E4])
        self._dn = self._data.reshape((12, 24, 250))

        self._dn_interp = RegularGridInterpolator([self._mass, self._x],
                                                  self._dn[ichan, :, :],
                                                  bounds_error=False,
                                                  fill_value=None)

        if self.mass.value > 10000:

            print("Warning: DMFitFunction only appropriate for masses <= 10 TeV")
            print("To model DM from 2 GeV < mass < 1 PeV use DMSpectra")
Ejemplo n.º 8
0
    def _setup(self):

        # Get and open the two data files

        tablepath_h = _get_data_file_path("dark_matter/dmSpecTab.npy")
        self._data_h = np.load(tablepath_h)

        tablepath_f = _get_data_file_path("dark_matter/gammamc_dif.dat")
        self._data_f = np.loadtxt(tablepath_f)

        """
            Mapping between the channel codes and the rows in the gammamc file
            dmSpecTab.npy created to match this mapping too

            1 : 8, # ee
            2 : 6, # mumu
            3 : 3, # tautau
            4 : 1, # bb
            5 : 2, # tt
            6 : 7, # gg
            7 : 4, # ww
            8 : 5, # zz
            9 : 0, # cc
            10 : 10, # uu
            11 : 11, # dd
            12 : 9, # ss
            """

        channel_index_mapping = {
            1: 8,  # ee
            2: 6,  # mumu
            3: 3,  # tautau
            4: 1,  # bb
            5: 2,  # tt
            6: 7,  # gg
            7: 4,  # ww
            8: 5,  # zz
            9: 0,  # cc
            10: 10,  # uu
            11: 11,  # dd
            12: 9,  # ss
        }

        # Number of decades in x = log10(E/M)
        ndec = 10.0
        xedge = np.linspace(0, 1.0, 251)
        self._x = 0.5 * (xedge[1:] + xedge[:-1]) * ndec - ndec

        ichan = channel_index_mapping[int(self.channel.value)]

        # These are the mass points in GeV
        self._mass_h = np.array([50., 61.2, 74.91, 91.69, 112.22, 137.36, 168.12, 205.78, 251.87, 308.29,
                                 377.34, 461.86, 565.31, 691.93, 846.91, 1036.6, 1268.78, 1552.97, 1900.82,
                                 2326.57, 2847.69, 3485.53, 4266.23, 5221.81, 6391.41, 7823.0, 9575.23,
                                 11719.94, 14345.03, 17558.1, 21490.85, 26304.48, 32196.3, 39407.79, 48234.54,
                                 59038.36, 72262.07, 88447.7, 108258.66, 132506.99, 162186.57, 198513.95,
                                 242978.11, 297401.58, 364015.09, 445549.04, 545345.37, 667494.6, 817003.43, 1000000.])

        # These are the mass points in GeV
        self._mass_f = np.array([2.0, 4.0, 6.0, 8.0, 10.0,
                                 25.0, 50.0, 80.3, 91.2, 100.0,
                                 150.0, 176.0, 200.0, 250.0, 350.0, 500.0, 750.0,
                                 1000.0, 1500.0, 2000.0, 3000.0, 5000.0, 7000.0, 1E4])

        self._mass = np.append(self._mass_f, self._mass_h[27:])

        self._dn_f = self._data_f.reshape((12, 24, 250))

        # Is this really used?
        self._dn_h = self._data_h

        self._dn = np.zeros((12, len(self._mass), 250))
        self._dn[:, 0:24, :] = self._dn_f
        self._dn[:, 24:, :] = self._dn_h[:, 27:, :]

        self._dn_interp = RegularGridInterpolator([self._mass, self._x],
                                                  self._dn[ichan, :, :],
                                                  bounds_error=False,
                                                  fill_value=None)

        if self.channel.value in [1, 6, 7] and self.mass.value > 10000.:
            print("ERROR: currently spectra for selected channel and mass not implemented.")
            print("Spectra for channels ['ee','gg','WW'] currently not available for mass > 10 TeV")