def test_lightcurve_temporal_model_integral(): time = np.arange(0, 10, 0.06) * u.hour table = Table() table["TIME"] = time table["NORM"] = np.ones(len(time)) table.meta = dict(MJDREFI=55197.0, MJDREFF=0, TIMEUNIT="hour") temporal_model = LightCurveTemplateTemporalModel(table) start = [1, 3, 5] * u.hour stop = [2, 3.5, 6] * u.hour gti = GTI.create(start, stop, reference_time=Time("2010-01-01T00:00:00")) val = temporal_model.integral(gti.time_start, gti.time_stop) assert len(val) == 3 assert_allclose(np.sum(val), 1.0, rtol=1e-5)
def models(): spatial_model = GaussianSpatialModel(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg", frame="galactic") spectral_model = PowerLawSpectralModel(amplitude="1e-11 cm-2 s-1 TeV-1") t_max = 1000 * u.s time = np.arange(t_max.value) * u.s tau = u.Quantity("2e2 s") norm = np.exp(-time / tau) table = Table() table["TIME"] = time table["NORM"] = norm / norm.max() t_ref = Time("2000-01-01") table.meta = dict(MJDREFI=t_ref.mjd, MJDREFF=0, TIMEUNIT="s") temporal_model = LightCurveTemplateTemporalModel(table) model = SkyModel( spatial_model=spatial_model, spectral_model=spectral_model, temporal_model=temporal_model, name="test-source", ) bkg_model = FoVBackgroundModel(dataset_name="test") return [model, bkg_model]
def dataset(): position = SkyCoord(0.0, 0.0, frame="galactic", unit="deg") energy_axis = MapAxis.from_bounds(1, 10, nbin=3, unit="TeV", name="energy", interp="log") spatial_model = GaussianSpatialModel(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg", frame="galactic") spectral_model = PowerLawSpectralModel(amplitude="1e-11 cm-2 s-1 TeV-1") t_min = 0 * u.s t_max = 1000 * u.s time = np.arange(t_max.value) * u.s tau = u.Quantity("2e2 s") norm = np.exp(-time / tau) table = Table() table["TIME"] = time table["NORM"] = norm / norm.max() t_ref = Time("2000-01-01") table.meta = dict(MJDREFI=t_ref.mjd, MJDREFF=0, TIMEUNIT="s") temporal_model = LightCurveTemplateTemporalModel(table) skymodel = SkyModel( spatial_model=spatial_model, spectral_model=spectral_model, temporal_model=temporal_model, ) geom = WcsGeom.create(skydir=position, binsz=1, width="5 deg", frame="galactic", axes=[energy_axis]) gti = GTI.create(start=t_min, stop=t_max, reference_time=t_ref) geom_true = geom.copy() geom_true.axes[0].name = "energy_true" dataset = get_map_dataset(sky_model=skymodel, geom=geom, geom_etrue=geom_true, edisp="edispmap") dataset.gti = gti return dataset
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)
def test_time_sampling(): time = np.arange(0, 10, 0.06) * u.hour table = Table() table["TIME"] = time table["NORM"] = rate(time) temporal_model = LightCurveTemplateTemporalModel(table) 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") table = Table() table["TIME"] = time table["NORM"] = np.ones(len(time)) 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) == 2 assert len(sampler_uniform) == 2 assert_allclose(sampler.value, [12661.65802564, 26.9299098], rtol=1e-5) assert_allclose(sampler_uniform.value, [1261.65802564, 6026.9299098], rtol=1e-5)
def light_curve(): path = "$GAMMAPY_DATA/tests/models/light_curve/lightcrv_PKSB1222+216.fits" return LightCurveTemplateTemporalModel.read(path)
This model parametrises a lightCurve time model. """ from astropy.time import Time from gammapy.modeling.models import ( LightCurveTemplateTemporalModel, Models, PowerLawSpectralModel, SkyModel, ) time_range = [Time("59100", format="mjd"), Time("59365", format="mjd")] path = "$GAMMAPY_DATA/tests/models/light_curve/lightcrv_PKSB1222+216.fits" light_curve_model = LightCurveTemplateTemporalModel.read(path) light_curve_model.plot(time_range) # %% # YAML representation # ------------------- # Here is an example YAML file using the model: model = SkyModel( spectral_model=PowerLawSpectralModel(), temporal_model=light_curve_model, name="light_curve_model", ) models = Models([model]) print(models.to_yaml())
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)