Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angle wrapping fails for PiecewiseNormSpatialModel #5084

Closed
StFroese opened this issue Feb 7, 2024 · 0 comments · Fixed by #5300
Closed

Angle wrapping fails for PiecewiseNormSpatialModel #5084

StFroese opened this issue Feb 7, 2024 · 0 comments · Fixed by #5300
Assignees
Labels
bug coding sprint To be tackled during coding sprint

Comments

@StFroese
Copy link
Member

StFroese commented Feb 7, 2024

I came across this problem in #5082

Example

Create PiecewiseNormSpatialModel

from gammapy.maps import WcsGeom, MapCoord
import astropy.units as u

geom = WcsGeom.create(skydir=(0, 0), npix=(2, 2), binsz=0.3, frame="galactic")
coords = MapCoord.create(geom.footprint)
coords["lon"] *= u.deg
coords["lat"] *= u.deg

model = PiecewiseNormSpatialModel(coords, frame="galactic")
model.to_dict()
Out[104]: 
{'spatial': {'type': 'PiecewiseNormSpatialModel',
  'frame': 'galactic',
  'parameters': [{'name': 'norm_0', 'value': 1.0},
   {'name': 'norm_1', 'value': 1.0},
   {'name': 'norm_2', 'value': 1.0},
   {'name': 'norm_3', 'value': 1.0}],
  'lon': {'data': [0.15, 0.15, -0.14999999999997726, -0.14999999999997726],
   'unit': 'deg'},
  'lat': {'data': [-0.15, 0.15, 0.15, -0.15], 'unit': 'deg'}}}

Creating a new instance from the dict changes the lon values:

PiecewiseNormSpatialModel.from_dict(model.to_dict()).to_dict()
{'spatial': {'type': 'PiecewiseNormSpatialModel',
  'frame': 'galactic',
  'parameters': [{'name': 'norm_0', 'value': 1.0},
   {'name': 'norm_1', 'value': 1.0},
   {'name': 'norm_2', 'value': 1.0},
   {'name': 'norm_3', 'value': 1.0}],
  'lon': {'data': [-359.85,
    -359.85,
    -0.14999999999997726,
    -0.14999999999997726],
   'unit': 'deg'},
  'lat': {'data': [-0.15, 0.15, 0.15, -0.15], 'unit': 'deg'}}}

This is because of the wrapping of the Angle in the init of the class

def __init__(self, coords, norms=None, interp="lin", **kwargs):
self._coords = coords.copy()
self._coords["lon"] = Angle(coords.lon).wrap_at(0 * u.deg)
self._wrap_angle = (coords.lon.max() - coords.lon.min()) / 2
self._coords["lon"] = Angle(coords.lon).wrap_at(self._wrap_angle)
self._interp = interp

But: Why are the lon values not getting wrapped in the creation of the first model above?

@StFroese StFroese added the bug label Feb 7, 2024
@QRemy QRemy self-assigned this Feb 28, 2024
@QRemy QRemy added the coding sprint To be tackled during coding sprint label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug coding sprint To be tackled during coding sprint
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants