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