Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- egion strings
- --------------
- We could consider adding a helper function to ``astropy-regions`` or Gammapy
- that we call everywhere in Gammapy at the start of functions or methods that
- have a ``region`` argument, like this::
- >>> from regions import DS9Parser
- >>> def make_region(region):
- ... if isinstance(region, str):
- ... return DS9Parser(region).shapes[0].to_region()
- ... else:
- ... return region
- ...
- >>> make_region("image;circle(42,43,3)")
- <CirclePixelRegion(PixCoord(x=41.0, y=42.0), radius=3.0)>
- >>> make_region("galactic;circle(42,43,3)")
- <CircleSkyRegion(<SkyCoord (Galactic): (l, b) in deg
- (42., 43.)>, radius=3.0 deg)>
- This is convenient, a simple ``region="galactic;circle(42,43,3)"`` is much
- shorter than having to remember and type this equivalent code::
- >>> from astropy.coordinates import SkyCoord, Angle
- >>> from regions import CircleSkyRegion
- >>> center = SkyCoord(42, 43, unit="deg", frame="galactic")
- >>> radius = Angle(3, "deg")
- >>> region = CircleSkyRegion(center, radius)
- >>> region
- <CircleSkyRegion(<SkyCoord (Galactic): (l, b) in deg
- (42., 43.)>, radius=3.0 deg)>
- There is precendence for this pattern in Gammapy, we usually pass ``Quantity``
- and ``Angle`` arguments through via e.g. ``angle = Angle(angle)`` in Gammapy
- functions, and often call them with strings ``angle="3 deg"``.
- This ``make_region`` function could also be the place where we type checks and
- do the ``SkyRegion`` to ``PixelRegion`` conversion, avoiding duplicated code for
- this in each Gammapy function that has a ``region`` argument, like this::
- from regions import DS9Parser, SkyRegion, PixelRegion
- def make_pixel_region(region, wcs=None):
- if isinstance(region, str):
- region = DS9Parser(region).shapes[0].to_region()
- if isinstance(region, SkyRegion):
- return region.to_pixel(wcs)
- elif isinstance(region, PixelRegion):
- return region
- else:
- raise TypeError("...")
- The drawback of adding this now is that everyone will start using it and we
- should after Gammapy v1.0 keep supporting it, i.e. we are committing to the DS9
- region format in Gammapy, without having considered other possible formats in
- detail, or having an equivalent solution in place for ``SkyCoord``.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement