EllipsePixelRegion

class regions.EllipsePixelRegion(center, width, height, angle=<Quantity 0. deg>, meta=None, visual=None)[source]

Bases: regions.core.core.PixelRegion

An ellipse in pixel coordinates.

Parameters
centerPixCoord

The position of the center of the ellipse.

widthfloat

The width of the ellipse (before rotation) in pixels

heightfloat

The height of the ellipse (before rotation) in pixels

angleQuantity, optional

The rotation angle of the ellipse, measured anti-clockwise. If set to zero (the default), the width axis is lined up with the x axis.

metaRegionMeta, optional

A dictionary that stores the meta attributes of this region.

visualRegionVisual, optional

A dictionary that stores the visual meta attributes of this region.

Examples

import numpy as np
from astropy.modeling.models import Ellipse2D
from astropy.coordinates import Angle
from regions import PixCoord, EllipsePixelRegion
import matplotlib.pyplot as plt
x0, y0 = 15, 10
a, b = 8, 5
theta = Angle(30, 'deg')
ell = Ellipse2D(amplitude=100., x_0=x0, y_0=y0, a=a, b=b,
                theta=theta.radian)
y, x = np.mgrid[0:20, 0:30]
fig, ax = plt.subplots(1, 1)
ax.imshow(ell(x, y), origin='lower', interpolation='none',
          cmap='Greys_r')

center = PixCoord(x=x0, y=y0)
reg = EllipsePixelRegion(center=center, width=2*a, height=2*b,
                         angle=theta)
patch = reg.as_artist(facecolor='none', edgecolor='red', lw=2)
ax.add_patch(patch)

(Source code, png, hires.png, pdf, svg)

../_images/regions-EllipsePixelRegion-1.png

Attributes Summary

angle

Descriptor class for SkyRegion, which takes a scalar Quantity object.

area

Region area (float)

bounding_box

The minimal bounding box (BoundingBox) enclosing the exact elliptical region.

center

Descriptor class for PixelRegion, which takes a scalar PixCoord object.

height

Descriptor class for PixelRegion, which takes a scalar python/numpy number.

mpl_artist

width

Descriptor class for PixelRegion, which takes a scalar python/numpy number.

Methods Summary

as_artist([origin])

Return a matplotlib patch object for this region (matplotlib.patches.Ellipse).

as_mpl_selector(ax[, active, sync, callback])

A matplotlib editable widget for this region (matplotlib.widgets.EllipseSelector).

contains(pixcoord)

Check whether a position or positions fall inside the region.

rotate(center, angle)

Rotate the region.

to_mask([mode, subpixels])

Return a mask for the region.

to_sky(wcs)

Return a region defined in sky coordinates.

Attributes Documentation

angle

Descriptor class for SkyRegion, which takes a scalar Quantity object.

area

Region area (float)

bounding_box

The minimal bounding box (BoundingBox) enclosing the exact elliptical region.

center

Descriptor class for PixelRegion, which takes a scalar PixCoord object.

height

Descriptor class for PixelRegion, which takes a scalar python/numpy number.

mpl_artist = 'Patch'
width

Descriptor class for PixelRegion, which takes a scalar python/numpy number.

Methods Documentation

as_artist(origin=(0, 0), **kwargs)[source]

Return a matplotlib patch object for this region (matplotlib.patches.Ellipse).

Parameters
originarray_like, optional

The (x, y) pixel position of the origin of the displayed image.

**kwargsdict

Any keyword arguments accepted by Ellipse. These keywords will override any visual meta attributes of this region.

Returns
artistEllipse

A matplotlib ellipse patch.

as_mpl_selector(ax, active=True, sync=True, callback=None, **kwargs)[source]

A matplotlib editable widget for this region (matplotlib.widgets.EllipseSelector).

Parameters
axAxes

The matplotlib axes to add the selector to.

activebool, optional

Whether the selector should be active by default.

syncbool, optional

If True (the default), the region will be kept in sync with the selector. Otherwise, the selector will be initialized with the values from the region but the two will then be disconnected.

callbackcallable, optional

If specified, this function will be called every time the region is updated. This only has an effect if sync is True. If a callback is set, it is called for the first time once the selector has been created.

**kwargsdict

Additional keyword arguments that are passed to matplotlib.widgets.EllipseSelector.

Returns
selectormatplotlib.widgets.EllipseSelector

The matplotlib selector.

Notes

Once a selector has been created, you will need to keep a reference to it until you no longer need it. In addition, you can enable/disable the selector at any point by calling selector.set_active(True) or selector.set_active(False).

contains(pixcoord)[source]

Check whether a position or positions fall inside the region.

Parameters
pixcoordPixCoord

The position or positions to check.

rotate(center, angle)[source]

Rotate the region.

Positive angle corresponds to counter-clockwise rotation.

Parameters
centerPixCoord

The rotation center point.

angleAngle

The rotation angle.

Returns
regionEllipsePixelRegion

The rotated region (which is an independent copy).

to_mask(mode='center', subpixels=5)[source]

Return a mask for the region.

Parameters
mode{‘center’, ‘exact’, ‘subpixels’}, optional

The method used to determine the overlap of the region on the pixel grid. Not all options are available for all region types. Note that the more precise methods are generally slower. The following methods are available:

  • 'center': A pixel is considered to be entirely in or out of the region depending on whether its center is in or out of the region. The returned mask will contain values only of 0 (out) and 1 (in).

  • 'exact' (default): The exact fractional overlap of the region and each pixel is calculated. The returned mask will contain values between 0 and 1.

  • 'subpixel': A pixel is divided into subpixels (see the subpixels keyword), each of which are considered to be entirely in or out of the region depending on whether its center is in or out of the region. If subpixels=1, this method is equivalent to 'center'. The returned mask will contain values between 0 and 1.

subpixelsint, optional

For the 'subpixel' mode, resample pixels by this factor in each dimension. That is, each pixel is divided into subpixels ** 2 subpixels.

Returns
maskRegionMask

A mask for the region.

to_sky(wcs)[source]

Return a region defined in sky coordinates.

Parameters
wcsWCS

The world coordinate system transformation to use to convert from pixels to sky coordinates.

Returns
sky_regionSkyRegion

The sky region.