Skip to content

Commit

Permalink
Merge pull request gammapy#5438 from fabiopintore/bottleneck
Browse files Browse the repository at this point in the history
Speed up event sampler
  • Loading branch information
registerrier authored Sep 24, 2024
2 parents 0845cfb + 55e2cb6 commit 71089b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions gammapy/datasets/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,27 +248,34 @@ def _sample_coord_time(self, npred, temporal_model, gti):

return table

def sample_sources(self, dataset):
def sample_sources(self, dataset, psf_update=False):
"""Sample source model components.
Parameters
----------
dataset : `~gammapy.datasets.MapDataset`
Map dataset.
psf_update : bool
Parameter to switch-off (on) the update of the PSF
in the dataset; default is False.
Returns
-------
events : `~gammapy.data.EventList`
Event list.
"""
if psf_update is True:
psf_update = dataset.psf
else:
psf_update = None

events_all = EventList(Table())
for idx, evaluator in enumerate(dataset.evaluators.values()):
log.info(f"Evaluating model: {evaluator.model.name}")
if evaluator.needs_update:
evaluator.update(
dataset.exposure,
dataset.psf,
psf_update,
dataset.edisp,
dataset._geom,
dataset.mask,
Expand Down
8 changes: 8 additions & 0 deletions gammapy/datasets/tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ def test_mde_sample_sources(dataset, models):
assert_allclose(events.table["MC_ID"][0], 1, rtol=1e-5)


@requires_data()
def test_mde_sample_sources_psf_update(dataset, models):
dataset.models = models
sampler = MapDatasetEventSampler(random_state=0)
events = sampler.sample_sources(dataset=dataset, psf_update=False)
assert len(events.table["ENERGY_TRUE"]) == 90


@requires_data()
def test_sample_sources_energy_dependent(dataset, energy_dependent_temporal_sky_model):
dataset.models = energy_dependent_temporal_sky_model
Expand Down

0 comments on commit 71089b6

Please sign in to comment.