예제 #1
0
def test_exponential_temporal_model_integral():
    temporal_model = ExpDecayTemporalModel()
    start = [1, 3, 5] * u.day
    stop = [2, 3.5, 6] * u.day
    t_ref = Time(55555, format="mjd")
    gti = GTI.create(start, stop, reference_time=t_ref)
    val = temporal_model.integral(gti.time_start, gti.time_stop)
    assert len(val) == 3
    assert_allclose(np.sum(val), 0.1024784, rtol=1e-5)
예제 #2
0
def test_exponential_temporal_model_evaluate():
    t = Time(46301, format="mjd")
    t_ref = 46300 * u.d
    t0 = 2.0 * u.d
    temporal_model = ExpDecayTemporalModel(t_ref=t_ref, t0=t0)
    val = temporal_model(t)
    assert_allclose(val, 0.6065306597126334, rtol=1e-5)
예제 #3
0
def fit_lc(datasets):
    spatial_model1 = GaussianSpatialModel(lon_0="0.2 deg",
                                          lat_0="0.1 deg",
                                          sigma="0.3 deg",
                                          frame="galactic")
    spatial_model1.parameters["lon_0"].frozen = False
    spatial_model1.parameters["lat_0"].frozen = False
    spatial_model1.parameters["sigma"].frozen = True
    spectral_model1 = PowerLawSpectralModel(index=3,
                                            amplitude="2e-11 cm-2 s-1 TeV-1",
                                            reference="1 TeV")
    temporal_model1 = ExpDecayTemporalModel(t0="10 h", t_ref=gti_t0.mjd * u.d)
    model_fit = SkyModel(
        spatial_model=spatial_model1,
        spectral_model=spectral_model1,
        temporal_model=temporal_model1,
        name="fit",
    )

    for dataset in datasets:
        dataset.models = [
            model_fit,
            FoVBackgroundModel(dataset_name=dataset.name)
        ]
        dataset.background_model.parameters["norm"].frozen = True

    fit = Fit()
    result = fit.run(datasets=datasets)
    print(result.success)
    print(result.parameters.to_table())
예제 #4
0
def simulate():

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

    # Reconstructed and true energy axis
    center = SkyCoord(0.0, 0.0, unit="deg", frame="galactic")
    energy_axis = MapAxis.from_edges(
        np.logspace(-0.5, 1.0, 10), unit="TeV", name="energy", interp="log",
    )
    energy_axis_true = MapAxis.from_edges(
        np.logspace(-1.2, 2.0, 31), unit="TeV", name="energy_true", interp="log",
    )

    on_region_radius = Angle("0.11 deg")
    on_region = CircleSkyRegion(center=center, radius=on_region_radius)

    pointing = SkyCoord(0.5, 0.5, unit="deg", frame="galactic")

    spectral_model = PowerLawSpectralModel(
        index=3, amplitude="1e-11 cm-2 s-1 TeV-1", reference="1 TeV"
    )
    temporal_model = ExpDecayTemporalModel(t0="6 h", t_ref=gti_t0.mjd * u.d)
    model_simu = SkyModel(
        spectral_model=spectral_model, temporal_model=temporal_model, name="model-simu",
    )

    lvtm = np.ones(N_OBS) * 1.0 * u.hr
    tstart = 1.0 * u.hr

    datasets = []
    for i in range(N_OBS):
        obs = Observation.create(
            pointing=pointing,
            livetime=lvtm[i],
            tstart=tstart,
            irfs=irfs,
            reference_time=gti_t0,
        )
        empty = SpectrumDataset.create(
            e_reco=energy_axis,
            e_true=energy_axis_true,
            region=on_region,
            name=f"dataset_{i}",
        )
        maker = SpectrumDatasetMaker(selection=["aeff", "background", "edisp"])
        dataset = maker.run(empty, obs)
        dataset.models = model_simu
        dataset.fake()
        datasets.append(dataset)
        tstart = tstart + 2.0 * u.hr

    return datasets
예제 #5
0
def fit_lc(datasets):
    spectral_model = PowerLawSpectralModel(
        index=2.0, amplitude="1e-12 cm-2 s-1 TeV-1", reference="1 TeV"
    )
    temporal_model1 = ExpDecayTemporalModel(t0="10 h", t_ref=gti_t0.mjd * u.d)
    model = SkyModel(
        spectral_model=spectral_model,
        temporal_model=temporal_model1,
        name="model-test",
    )
    for dataset in datasets:
        dataset.models = model
    fit = Fit(datasets)
    result = fit.run()
    print(result)
    print(result.parameters.to_table())
예제 #6
0
def simulate():

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

    center = SkyCoord(0.0, 0.0, unit="deg", frame="galactic")
    energy_reco = MapAxis.from_edges(np.logspace(-1.0, 1.0, 10),
                                     unit="TeV",
                                     name="energy",
                                     interp="log")
    pointing = SkyCoord(0.5, 0.5, unit="deg", frame="galactic")
    geom = WcsGeom.create(
        skydir=center,
        binsz=0.02,
        width=(4, 4),
        frame="galactic",
        axes=[energy_reco],
    )
    energy_true = MapAxis.from_edges(np.logspace(-1.5, 1.5, 30),
                                     unit="TeV",
                                     name="energy_true",
                                     interp="log")

    spectral_model = PowerLawSpectralModel(index=3,
                                           amplitude="1e-11 cm-2 s-1 TeV-1",
                                           reference="1 TeV")
    temporal_model = ExpDecayTemporalModel(t0="6 h", t_ref=gti_t0.mjd * u.d)
    spatial_model = GaussianSpatialModel(lon_0="0.2 deg",
                                         lat_0="0.1 deg",
                                         sigma="0.3 deg",
                                         frame="galactic")
    model_simu = SkyModel(
        spectral_model=spectral_model,
        spatial_model=spatial_model,
        temporal_model=temporal_model,
        name="model-simu",
    )

    lvtm = np.ones(N_OBS) * 1.0 * u.hr
    tstart = 1.0 * u.hr

    datasets = []
    for i in range(N_OBS):
        obs = Observation.create(
            pointing=pointing,
            livetime=lvtm[i],
            tstart=tstart,
            irfs=irfs,
            reference_time=gti_t0,
        )
        empty = MapDataset.create(geom,
                                  name=f"dataset_{i}",
                                  energy_axis_true=energy_true)
        maker = MapDatasetMaker(
            selection=["exposure", "background", "psf", "edisp"])
        maker_safe_mask = SafeMaskMaker(methods=["offset-max"],
                                        offset_max=4.0 * u.deg)
        dataset = maker.run(empty, obs)
        dataset = maker_safe_mask.run(dataset, obs)
        dataset.models = [
            model_simu,
            FoVBackgroundModel(dataset_name=dataset.name)
        ]
        dataset.fake()
        datasets.append(dataset)
        tstart = tstart + 2.0 * u.hr

    return datasets
예제 #7
0
"""

# %%
# Example plot
# ------------
# Here is an example plot of the model:

from astropy import units as u
from astropy.time import Time
import matplotlib.pyplot as plt
from gammapy.modeling.models import Models, ExpDecayTemporalModel, SkyModel

t0 = "5 h"
t_ref = Time("2020-10-01")
time_range = [t_ref, t_ref + 1 * u.d]
expdecay_model = ExpDecayTemporalModel(t_ref=t_ref.mjd * u.d, t0=t0)
expdecay_model.plot(time_range)
plt.grid(which="both")

# %%
# YAML representation
# -------------------
# Here is an example YAML file using the model:
from gammapy.modeling.models import PowerLawSpectralModel
model = SkyModel(spectral_model=PowerLawSpectralModel(),
                 temporal_model=expdecay_model,
                 name="expdecay_model")
models = Models([model])

print(models.to_yaml())
예제 #8
0
def test_time_sampling(tmp_path):
    time = np.arange(0, 10, 0.06) * u.hour

    table = Table()
    table["TIME"] = time
    table["NORM"] = rate(time)
    table.meta = dict(MJDREFI=55197.0, MJDREFF=0, TIMEUNIT="hour")
    temporal_model = LightCurveTemplateTemporalModel(table)

    filename = str(make_path(tmp_path / "tmp.fits"))
    temporal_model.write(path=filename)
    model_read = temporal_model.read(filename)
    assert temporal_model.filename == filename
    assert model_read.filename == filename
    assert_allclose(model_read.table["TIME"].quantity.value, time.value)

    t_ref = "2010-01-01T00:00:00"
    t_min = "2010-01-01T00:00:00"
    t_max = "2010-01-01T08:00:00"

    sampler = temporal_model.sample_time(n_events=2,
                                         t_min=t_min,
                                         t_max=t_max,
                                         random_state=0,
                                         t_delta="10 min")
    sampler = u.Quantity((sampler - Time(t_ref)).sec, "s")

    assert len(sampler) == 2
    assert_allclose(sampler.value, [12661.65802564, 7826.92991], rtol=1e-5)

    table = Table()
    table["TIME"] = time
    table["NORM"] = np.ones(len(time))
    table.meta = dict(MJDREFI=55197.0, MJDREFF=0, TIMEUNIT="hour")
    temporal_model_uniform = LightCurveTemplateTemporalModel(table)

    sampler_uniform = temporal_model_uniform.sample_time(n_events=2,
                                                         t_min=t_min,
                                                         t_max=t_max,
                                                         random_state=0,
                                                         t_delta="10 min")
    sampler_uniform = u.Quantity((sampler_uniform - Time(t_ref)).sec, "s")

    assert len(sampler_uniform) == 2
    assert_allclose(sampler_uniform.value, [1261.65802564, 6026.9299098],
                    rtol=1e-5)

    temporal_model = ConstantTemporalModel()
    sampler_costant = temporal_model.sample_time(n_events=2,
                                                 t_min=t_min,
                                                 t_max=t_max,
                                                 random_state=0)
    sampler_costant = u.Quantity((sampler_costant - Time(t_ref)).sec, "s")

    assert len(sampler_costant) == 2
    assert_allclose(sampler_costant.value, [4330.10377559, 3334.04566256],
                    rtol=1e-5)

    temporal_model = ExpDecayTemporalModel(t_ref=Time(t_ref).mjd * u.d)
    sampler_expo = temporal_model.sample_time(n_events=2,
                                              t_min=t_min,
                                              t_max=t_max,
                                              random_state=0)
    sampler_expo = u.Quantity((sampler_expo.mjd - Time(t_ref).mjd), "d")

    assert sampler_expo.unit == u.d
    assert_allclose(sampler_expo.to("s").value, [11824.1055276, 7273.04658336],
                    rtol=1e-8)