diff --git a/gammapy/maps/core.py b/gammapy/maps/core.py index 5687246cdf..51a6fa7461 100644 --- a/gammapy/maps/core.py +++ b/gammapy/maps/core.py @@ -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``. diff --git a/gammapy/maps/region/tests/test_ndmap.py b/gammapy/maps/region/tests/test_ndmap.py index 6f4c027692..84ad7a8211 100644 --- a/gammapy/maps/region/tests/test_ndmap.py +++ b/gammapy/maps/region/tests/test_ndmap.py @@ -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() @@ -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") diff --git a/gammapy/maps/tests/test_counts.py b/gammapy/maps/tests/test_counts.py index 4bbb0d2b87..c5e5de2b3e 100644 --- a/gammapy/maps/tests/test_counts.py +++ b/gammapy/maps/tests/test_counts.py @@ -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): @@ -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")