Skip to content

Commit

Permalink
implement comments
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Pintore <fabio.pintore@inaf.it>
  • Loading branch information
fabiopintore committed Oct 24, 2023
1 parent c54ea51 commit 9f6dab0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
26 changes: 16 additions & 10 deletions gammapy/datasets/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ class MapDatasetEventSampler:
an energy-dependent time-varying source
t_delta : `~astropy.units.Quantity`
Time interval used to sample the time-dependent source
mc_id : `~bool`
Flag to tag sampled events from a given model with a Montecarlo identifier. If set to "False", no
identifier will be assigned
keep_mc_id : bool
Flag to tag sampled events from a given model with a Montecarlo identifier.
Default is True. If set to False, no identifier will be assigned
"""

def __init__(
self,
random_state="random-seed",
oversample_energy_factor=10,
t_delta=0.5 * u.s,
mc_id=True,
keep_mc_id=True,
):
self.random_state = get_random_state(random_state)
self.oversample_energy_factor = oversample_energy_factor
self.t_delta = t_delta
self.mc_id = mc_id
self.keep_mc_id = keep_mc_id

def _repr_html_(self):
try:
Expand Down Expand Up @@ -272,7 +272,7 @@ def sample_sources(self, dataset):
npred = evaluator.apply_exposure(flux)
table = self._sample_coord_time(npred, temporal_model, dataset.gti)

if self.mc_id:
if self.keep_mc_id:
if len(table) == 0:
mcid = table.Column(name="MC_ID", length=0, dtype=int)
table.add_column(mcid)
Expand Down Expand Up @@ -308,7 +308,7 @@ def sample_background(self, dataset):
table["RA"] = table["RA_TRUE"]
table["DEC"] = table["DEC_TRUE"]

if self.mc_id:
if self.keep_mc_id:
table["MC_ID"] = 0
table.meta["MID{:05d}".format(0)] = 0
table.meta["MMN{:05d}".format(0)] = dataset.background_model.name
Expand Down Expand Up @@ -397,15 +397,19 @@ def event_det_coords(observation, events):
return events

@staticmethod
def event_list_meta(dataset, observation, mc_id=True):
def event_list_meta(dataset, observation, keep_mc_id=True):
"""Event list meta info.
Please, note that this function will be updated in the future.
Parameters
----------
dataset : `~gammapy.datasets.MapDataset`
Map dataset
observation : `~gammapy.data.Observation`
In memory observation
keep_mc_id : bool
Flag to tag sampled events from a given model with a Montecarlo identifier.
Default is True. If set to False, no identifier will be assigned
Returns
-------
Expand Down Expand Up @@ -486,7 +490,7 @@ def event_list_meta(dataset, observation, mc_id=True):
meta["CONV_RA"] = 0
meta["CONV_DEC"] = 0

if mc_id:
if keep_mc_id:
meta["NMCIDS"] = len(dataset.models)

# Necessary for DataStore, but they should be ALT and AZ instead!
Expand Down Expand Up @@ -566,7 +570,9 @@ def run(self, dataset, observation=None):

events = self.event_det_coords(observation, events)
events.table["EVENT_ID"] = np.arange(len(events.table))
events.table.meta.update(self.event_list_meta(dataset, observation, self.mc_id))
events.table.meta.update(
self.event_list_meta(dataset, observation, self.keep_mc_id)
)

geom = dataset._geom
selection = geom.contains(events.map_coord(geom))
Expand Down
2 changes: 1 addition & 1 deletion gammapy/datasets/tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ def test_MC_ID_flag(model_alternative):

model_alternative[0].spectral_model.parameters["amplitude"].value = 1e-16
dataset.models = model_alternative
sampler = MapDatasetEventSampler(random_state=0, mc_id=False)
sampler = MapDatasetEventSampler(random_state=0, keep_mc_id=False)
events = sampler.run(dataset=dataset, observation=obs)

meta = events.table.meta
Expand Down

0 comments on commit 9f6dab0

Please sign in to comment.