Skip to content

Commit

Permalink
Merge pull request #4458 from MRegeard/weights_fill_events
Browse files Browse the repository at this point in the history
adding weights option to fill_events
  • Loading branch information
registerrier committed May 4, 2023
2 parents 98aec08 + 7ffeb9b commit cf3166f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
15 changes: 12 additions & 3 deletions gammapy/maps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,9 +1088,18 @@ def reproject_to_geom(self, geom, preserve_counts=False, precision_factor=10):
output_map = output_map.resample(geom, preserve_counts=preserve_counts)
return output_map

def fill_events(self, events):
"""Fill event coordinates (`~gammapy.data.EventList`)."""
self.fill_by_coord(events.map_coord(self.geom))
def fill_events(self, events, weights=None):
"""Fill event coordinates (`~gammapy.data.EventList`).
Parameters
----------
events : `~gammapy.data.EventList`
Events to be fill in the map.
weights : `~numpy.ndarray`
Weights vector. Default is weight of one. The weights vector must be of the same length
as the events column length.
"""
self.fill_by_coord(events.map_coord(self.geom), weights=weights)

def fill_by_coord(self, coords, weights=None):
"""Fill pixels at ``coords`` with given ``weights``.
Expand Down
10 changes: 10 additions & 0 deletions gammapy/maps/region/tests/test_ndmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,12 @@ def test_region_nd_map_fill_events(region_map):
region_map = Map.from_geom(region_map.geom)
region_map.fill_events(events)

weights = np.linspace(0, 1, len(events.time))
region_map2 = Map.from_geom(region_map.geom)
region_map2.fill_events(events, weights=weights)

assert_allclose(region_map.data.sum(), 665)
assert_allclose(region_map2.data.sum(), 328.33487)


@requires_data()
Expand All @@ -257,6 +262,11 @@ def test_region_nd_map_fill_events_point_sky_region(point_region_map):

assert_allclose(region_map.data.sum(), 0)

weights = np.linspace(0, 1, len(events.time))
region_map = Map.from_geom(point_region_map.geom)
region_map.fill_events(events, weights=weights)
assert_allclose(region_map.data.sum(), 0)


def test_apply_edisp(point_region_map):
e_true = MapAxis.from_energy_bounds("1 TeV", "10 TeV", nbin=3, name="energy_true")
Expand Down
10 changes: 10 additions & 0 deletions gammapy/maps/tests/test_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def test_map_fill_events_wcs(events):
assert m.data.sum() == 1
assert_allclose(m.data[0, 0, 0], 1)

weights = np.array([0.5, 1])
m = Map.create(npix=(2, 1), binsz=10, axes=[axis])
m.fill_events(events, weights=weights)
assert_allclose(m.data.sum(), 0.5)


@requires_dependency("healpy")
def test_map_fill_events_hpx(events):
Expand All @@ -48,6 +53,11 @@ def test_map_fill_events_hpx(events):
assert m.data[0, 4] == 1
assert m.data[1, 4] == 1

weights = np.array([0.5, 1])
m = Map.from_geom(HpxGeom(1, axes=[axis]))
m.fill_events(events, weights=weights)
assert_allclose(m.data.sum(), 1.5)


def test_map_fill_events_keyerror(events):
axis = MapAxis([0, 1, 2], name="nokey")
Expand Down

0 comments on commit cf3166f

Please sign in to comment.