Ejemplo n.º 1
0
def get_npred_map():
    position = SkyCoord(0.0, 0.0, frame="galactic", unit="deg")
    energy_axis = MapAxis.from_bounds(1,
                                      100,
                                      nbin=30,
                                      unit="TeV",
                                      name="energy",
                                      interp="log")

    exposure = Map.create(
        binsz=0.02,
        map_type="wcs",
        skydir=position,
        width="5 deg",
        axes=[energy_axis],
        coordsys="GAL",
        unit="cm2 s",
    )

    spatial_model = SkyGaussian("0 deg", "0 deg", sigma="0.2 deg")
    spectral_model = PowerLaw(amplitude="1e-11 cm-2 s-1 TeV-1")
    skymodel = SkyModel(spatial_model=spatial_model,
                        spectral_model=spectral_model)

    exposure.data = 1e14 * np.ones(exposure.data.shape)
    evaluator = MapEvaluator(model=skymodel, exposure=exposure)

    npred = evaluator.compute_npred()
    return npred
Ejemplo n.º 2
0
def simulate_map_dataset(random_state=0):
    irfs = load_cta_irfs(
        "$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
    )

    skydir = SkyCoord("0 deg", "0 deg", frame="galactic")
    edges = np.logspace(-1, 2, 15) * u.TeV
    energy_axis = MapAxis.from_edges(edges=edges, name="energy")

    geom = WcsGeom.create(skydir=skydir,
                          width=(4, 4),
                          binsz=0.1,
                          axes=[energy_axis],
                          coordsys="GAL")

    gauss = SkyGaussian("0 deg", "0 deg", "0.4 deg", frame="galactic")
    pwl = PowerLaw(amplitude="1e-11 cm-2 s-1 TeV-1")
    skymodel = SkyModel(spatial_model=gauss, spectral_model=pwl, name="source")
    dataset = simulate_dataset(
        skymodel=skymodel,
        geom=geom,
        pointing=skydir,
        irfs=irfs,
        random_state=random_state,
    )
    return dataset
Ejemplo n.º 3
0
def sky_model():
    spatial_model = SkyGaussian(lon_0="0.2 deg",
                                lat_0="0.1 deg",
                                sigma="0.2 deg")
    spectral_model = PowerLaw(index=3,
                              amplitude="1e-11 cm-2 s-1 TeV-1",
                              reference="1 TeV")
    return SkyModel(spatial_model=spatial_model, spectral_model=spectral_model)
Ejemplo n.º 4
0
def test_sky_model_init():
    with pytest.raises(ValueError) as excinfo:
        spatial_model = SkyGaussian("0 deg", "0 deg", "0.1 deg")
        _ = SkyModel(spectral_model=1234, spatial_model=spatial_model)

    assert "Spectral model" in str(excinfo.value)

    with pytest.raises(ValueError) as excinfo:
        _ = SkyModel(spectral_model=PowerLaw(), spatial_model=1234)

    assert "Spatial model" in str(excinfo.value)
Ejemplo n.º 5
0
def test_sky_gaussian():
    sigma = 1 * u.deg
    model = SkyGaussian(lon_0="5 deg", lat_0="15 deg", sigma=sigma)
    assert model.parameters["sigma"].min == 0
    val_0 = model(5 * u.deg, 15 * u.deg)
    val_sigma = model(5 * u.deg, 16 * u.deg)
    assert val_0.unit == "sr-1"
    ratio = val_0 / val_sigma
    assert_allclose(ratio, np.exp(0.5))
    radius = model.evaluation_radius
    assert radius.unit == "deg"
    assert_allclose(radius.value, 5 * sigma.value)
Ejemplo n.º 6
0
    def calc_bk(self, lon0, lat0, sig, amp):
        """
            returns the computed b_k and the diffuse model template.
        """
        # Define sky model to fit the data
        ind = 2.0

        spatial_model = SkyGaussian(lon_0=lon0, lat_0=lat0, sigma=sig)
        spectral_model = PowerLaw(index=ind, amplitude=amp, reference="1 TeV")
        model = SkyModel(spatial_model=spatial_model,
                         spectral_model=spectral_model)

        # For simulations, we can have the same npred map

        b_k = []
        Sk_list = []

        for count, bkg, exp in zip(self.count_list, self.background_list,
                                   self.exposure_list):

            evaluator = MapEvaluator(model=model, exposure=exp)
            npred = evaluator.compute_npred()
            geom = exp.geom
            diffuse_map = WcsNDMap(geom, npred)  #This is Sk
            Bk = bkg.data
            Sk = diffuse_map.data
            Nk = count.data

            not_has_exposure = ~(exp.data > 0)
            not_has_bkg = ~(Bk > 0)

            S_B = np.divide(Sk, Bk)
            S_B[not_has_exposure] = 0.0
            S_B[not_has_bkg] = 0.0

            #Sk is nan for large sep.. to be investigated. temp soln
            #if np.isnan(np.sum(S_B)):
            #    S_B=np.zeros(S_B.shape)

            delta = np.power(np.sum(Nk) / np.sum(Bk),
                             2.0) - 4.0 * np.sum(S_B) / np.sum(Bk)
            #print(np.sum(Nk),np.sum(Bk),np.sum(Sk),np.sum(S_B), delta)
            #print("delta is %f for obs no %s",delta,k)
            #bk1=(np.sum(Nk)/np.sum(Bk) - np.sqrt(delta))/2.0
            bk2 = (np.sum(Nk) / np.sum(Bk) + np.sqrt(delta)) / 2.0
            b_k.append(bk2)
            Sk_list.append(diffuse_map)

        return Sk_list, b_k
Ejemplo n.º 7
0
def test_simulate():
    irfs = load_cta_irfs(
        "$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
    )

    # Define sky model to simulate the data
    spatial_model = SkyGaussian(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg")
    spectral_model = PowerLaw(index=2,
                              amplitude="1e-11 cm-2 s-1 TeV-1",
                              reference="1 TeV")
    sky_model_simu = SkyModel(spatial_model=spatial_model,
                              spectral_model=spectral_model)

    # Define map geometry
    axis = MapAxis.from_edges(np.logspace(-1, 1.0, 20),
                              unit="TeV",
                              name="energy",
                              interp="log")
    geom = WcsGeom.create(skydir=(0, 0),
                          binsz=0.025,
                          width=(1, 1),
                          coordsys="GAL",
                          axes=[axis])

    # Define some observation parameters
    pointing = SkyCoord(0 * u.deg, 0 * u.deg, frame="galactic")

    dataset = simulate_dataset(sky_model_simu,
                               geom,
                               pointing,
                               irfs,
                               livetime=10 * u.h,
                               random_state=42)

    assert isinstance(dataset, MapDataset)
    assert isinstance(dataset.model, SkyModels)

    assert dataset.counts.data.dtype is np.dtype("int")
    assert_allclose(dataset.counts.data[5, 20, 20], 5)
    assert_allclose(dataset.exposure.data[5, 20, 20], 16122681639.856329)
    assert_allclose(dataset.background_model.map.data[5, 20, 20],
                    0.976554,
                    rtol=1e-5)
    assert_allclose(dataset.psf.data[5, 32, 32], 0.044402823)
    assert_allclose(dataset.edisp.data.data[10, 10], 0.662208, rtol=1e-5)
Ejemplo n.º 8
0
def make_example_2():
    spatial = SkyGaussian("0 deg", "0 deg", "1 deg")
    model = SkyModel(spatial, PowerLaw())
    models = SkyModels([model])
    models.to_yaml(DATA_PATH / "example2.yaml")
Ejemplo n.º 9
0
import emcee
import corner

# ## Simulate an observation
#
# Here we will start by simulating an observation using the `simulate_dataset` method.

# In[ ]:

irfs = load_cta_irfs(
    "$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits")

# In[ ]:

# Define sky model to simulate the data
spatial_model = SkyGaussian(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg")

spectral_model = ExponentialCutoffPowerLaw(
    index=2,
    amplitude="3e-12 cm-2 s-1 TeV-1",
    reference="1 TeV",
    lambda_="0.05 TeV-1",
)

sky_model_simu = SkyModel(spatial_model=spatial_model,
                          spectral_model=spectral_model)
print(sky_model_simu)

# In[ ]:

# Define map geometry
Ejemplo n.º 10
0
def sky_model():
    spatial_model = SkyGaussian(lon_0="3 deg", lat_0="4 deg", sigma="3 deg")
    spectral_model = PowerLaw(index=2,
                              amplitude="1e-11 cm-2 s-1 TeV-1",
                              reference="1 TeV")
    return SkyModel(spatial_model, spectral_model, name="source-1")
Ejemplo n.º 11
0
from gammapy.image.models import SkyGaussian
from gammapy.utils.random import get_random_state
from gammapy.cube import make_map_exposure_true_energy, MapFit, MapEvaluator
from gammapy.cube.models import SkyModel

filename = "$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
aeff = EffectiveAreaTable2D.read(filename, hdu="EFFECTIVE AREA")

# Define sky model to simulate the data
lon_0_1 = 0.2
lon_0_2 = 0.4
lat_0_1 = 0.1
lat_0_2 = 0.6

spatial_model_1 = SkyGaussian(
    lon_0=lon_0_1 * u.deg, lat_0=lat_0_1 * u.deg, sigma="0.3 deg"
)
spatial_model_2 = SkyGaussian(
    lon_0=lon_0_2 * u.deg, lat_0=lat_0_2 * u.deg, sigma="0.2 deg"
)

spectral_model_1 = PowerLaw(
    index=3, amplitude="1e-11 cm-2 s-1 TeV-1", reference="1 TeV"
)

spectral_model_2 = PowerLaw(
    index=3, amplitude="1e-11 cm-2 s-1 TeV-1", reference="1 TeV"
)

sky_model_1 = SkyModel(spatial_model=spatial_model_1, spectral_model=spectral_model_1)
Ejemplo n.º 12
0
def test_sky_gaussian_elongated():
    # test the normalization for an elongated Gaussian near the Galactic Plane
    m_geom_1 = WcsGeom.create(binsz=0.05,
                              width=(20, 20),
                              skydir=(2, 2),
                              coordsys="GAL",
                              proj="AIT")
    coords = m_geom_1.get_coord()
    solid_angle = m_geom_1.solid_angle()
    lon = coords.lon * u.deg
    lat = coords.lat * u.deg
    semi_major = 3 * u.deg
    model_1 = SkyGaussianElongated(2 * u.deg, 2 * u.deg, semi_major, 0.8,
                                   30 * u.deg)
    vals_1 = model_1(lon, lat)
    assert vals_1.unit == "sr-1"
    assert_allclose(np.sum(vals_1 * solid_angle), 1, rtol=1.0e-3)

    radius = model_1.evaluation_radius
    assert radius.unit == "deg"
    assert_allclose(radius.value, 5 * semi_major.value)

    # check the ratio between the value at the peak and on the 1-sigma isocontour
    semi_major = 4 * u.deg
    semi_minor = 2 * u.deg
    e = np.sqrt(1 - (semi_minor / semi_major)**2)
    model_2 = SkyGaussianElongated(0 * u.deg, 0 * u.deg, semi_major, e,
                                   0 * u.deg)
    val_0 = model_2(0 * u.deg, 0 * u.deg)
    val_major = model_2(0 * u.deg, 4 * u.deg)
    val_minor = model_2(2 * u.deg, 0 * u.deg)
    assert val_0.unit == "sr-1"
    ratio_major = val_0 / val_major
    ratio_minor = val_0 / val_minor

    assert_allclose(ratio_major, np.exp(0.5))
    assert_allclose(ratio_minor, np.exp(0.5))

    # check the rotation
    model_3 = SkyGaussianElongated(0 * u.deg, 0 * u.deg, semi_major, e,
                                   90 * u.deg)
    val_minor_rotated = model_3(0 * u.deg, 2 * u.deg)
    ratio_minor_rotated = val_0 / val_minor_rotated
    assert_allclose(ratio_minor_rotated, np.exp(0.5))

    # compare the normalization of a symmetric Gaussian (ellipse with e=0) and an
    # elongated Gaussian with null eccentricity, both defined at the Galactic Pole
    m_geom_4 = WcsGeom.create(binsz=0.05,
                              width=(25, 25),
                              skydir=(0, 90),
                              coordsys="GAL",
                              proj="AIT")
    coords = m_geom_4.get_coord()
    solid_angle = m_geom_4.solid_angle()
    lon = coords.lon * u.deg
    lat = coords.lat * u.deg

    semi_major = 5 * u.deg
    model_4_el = SkyGaussianElongated(0 * u.deg, 90 * u.deg, semi_major, 0.0,
                                      0.0 * u.deg)
    model_4_sym = SkyGaussian(0 * u.deg, 90 * u.deg, semi_major)

    vals_4_el = model_4_el(lon, lat)
    vals_4_sym = model_4_sym(lon, lat)

    int_elongated = np.sum(vals_4_el * solid_angle)
    int_symmetric = np.sum(vals_4_sym * solid_angle)

    assert_allclose(int_symmetric, int_elongated, rtol=1e-3)