Skip to content

Commit

Permalink
Merge pull request #4102 from QRemy/fix_resample
Browse files Browse the repository at this point in the history
Fix weights in resample
  • Loading branch information
adonath committed Sep 21, 2022
2 parents 82b1bb3 + 3d757b3 commit d384e20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions gammapy/maps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,11 @@ def resample(self, geom, weights=None, preserve_counts=True):
coords = self.geom.get_coord()
idx = geom.coord_to_idx(coords)

resampled = self.from_geom(geom=geom)
weights = 1 if weights is None else weights

resampled = self.from_geom(geom=geom)
resampled._resample_by_idx(
idx, weights=self.data, preserve_counts=preserve_counts
idx, weights=self.data * weights, preserve_counts=preserve_counts
)
return resampled

Expand Down
11 changes: 11 additions & 0 deletions gammapy/maps/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,17 @@ def test_resample_wcs_Wcs():
rtol=1e-3,
)

def test_resample_weights():
npix1 = 3
geom1 = WcsGeom.create(npix=npix1, frame="icrs")
map1 = Map.from_geom(geom1, data=np.eye(npix1))

geom2 = WcsGeom.create(
skydir=SkyCoord(0.0, 0.0, unit=u.deg), binsz=0.5, npix=7, frame="galactic"
)

map2 = map1.resample(geom2, weights=np.zeros(npix1), preserve_counts=False)
assert np.sum(map2.data) == 0.

def test_resample_downsample_wcs():
geom_fine = WcsGeom.create(npix=(15, 15), frame="icrs")
Expand Down

0 comments on commit d384e20

Please sign in to comment.