astropy / regions

Astropy package for region handling
https://astropy-regions.readthedocs.io
BSD 3-Clause "New" or "Revised" License
61 stars 54 forks source link

Support for regions has been added in ipyaladin and mocpy libraries #554

Open ManonMarchand opened 3 months ago

ManonMarchand commented 3 months ago

Hi there,

Not really an issue, but I wanted to inform you that support for regions has been added in ipyaladin>=0.4.0 and mocpy>=0.16.0 .

In ipyaladin

This can be installed with pip install ipyaladin. You can now do (in a notebook)

Cell 1: load the widget

from ipyaladin import Aladin
from regions import (
    CircleSkyRegion,
    EllipseSkyRegion,
    LineSkyRegion,
    PolygonSkyRegion,
    RectangleSkyRegion,
    RegionVisual,
)
aladin = Aladin(target="m31", fov=10)
aladin

Cell 2: define regions

center = SkyCoord.from_name("M31")
circle = CircleSkyRegion(
    center=center, radius=Angle(0.5, "deg"), visual={"edgecolor": "yellow"}
)
ellipse = EllipseSkyRegion(
    center=center,
    height=Angle(0.5, "deg"),
    width=Angle(1, "deg"),
    angle=Angle(45, "deg"),
    visual=RegionVisual(edgecolor="red"),
)
line = LineSkyRegion(
    start=center,
    end=center.directional_offset_by(Angle(0, "deg"), Angle(0.5, "deg")),
    visual=RegionVisual(edgecolor="green"),
)
polygon = PolygonSkyRegion(
    vertices=SkyCoord(
        [
            center.directional_offset_by(Angle(0, "deg"), Angle(0.5, "deg")),
            center.directional_offset_by(Angle(90, "deg"), Angle(0.5, "deg")),
            center.directional_offset_by(Angle(-90, "deg"), Angle(0.5, "deg")),
        ],
        frame="icrs",
        unit="deg",
    ),
    visual={"edgecolor": "blue"},
)
rectangle = RectangleSkyRegion(
    center=center,
    width=Angle(1, "deg"),
    height=Angle(1, "deg"),
    angle=Angle(45, "deg"),
    visual=RegionVisual(color="purple"),
)

Cell 3: add the regions to the widget

aladin.add_graphic_overlay_from_region([circle, ellipse, line, polygon, rectangle])

If you scroll up to the widget, you should see image

This is exctracted from the notebook 9 in ipyaladin's examples, see https://cds-astro.github.io/ipyaladin/_collections/notebooks/09_Displaying_Shapes.html .

In mocpy

It is installed with pip install mocpy. You can now build a MOC (an ensemble of healpix cells that represent an approximated region) from a regions

import matplotlib.pyplot as plt
import regions
from astropy import units as u
from astropy.coordinates import SkyCoord
from mocpy import MOC

center = SkyCoord(42, 43, unit="deg", frame="fk5")
ring = regions.CircleAnnulusSkyRegion(center, 3 * u.deg, 4 * u.deg)
moc_ring = MOC.from_astropy_regions(ring, max_depth=9)

fig = plt.figure(figsize=(5, 5))
wcs = moc_ring.wcs(fig)
ax = fig.add_subplot(projection=wcs)
moc_ring.fill(ax, wcs, color="pink", alpha=0.8)
moc_ring.border(ax, wcs)
ring.to_pixel(wcs).plot(color="r");

image

This is exctracted from notebook 02 in mocpy's repository, see https://cds-astro.github.io/mocpy/_collections/notebooks/02-Creating_MOCs_from_astropy_regions.html

The supported astropy regions are:

larrybradley commented 3 months ago

Thanks, @ManonMarchand. This is great!