PolygonAperture#
- class photutils.aperture.PolygonAperture(positions, vertex_offsets)[source]#
Bases:
PixelApertureA polygon aperture defined in pixel coordinates.
The aperture has a single fixed shape, defined by
vertex_offsetsrelative to eachpositionsentry, but it can have multiple positions.- Parameters:
- positionsarray_like
The pixel coordinates of the aperture center(s) in one of the following formats:
- vertex_offsetsarray_like
Shape
(n_vertices, 2). The pixel-space offsets(dx, dy)of each polygon vertex relative topositions. The polygon must be simple (non-self-intersecting) with at least 3 vertices. The polygon may be convex or non-convex. Either clockwise or counter-clockwise vertex orderings are accepted; the input is normalized to counter-clockwise internally.
- Raises:
- ValueError
ValueError If
vertex_offsetsis not a valid simple polygon with at least 3 vertices.
- ValueError
Examples
>>> import numpy as np >>> from photutils.aperture import PolygonAperture >>> # A regular hexagon centered on (10, 20) with circumradius 5. >>> theta = np.linspace(0.0, 2 * np.pi, 6, endpoint=False) >>> offsets = np.column_stack([5.0 * np.cos(theta), ... 5.0 * np.sin(theta)]) >>> aper = PolygonAperture((10.0, 20.0), offsets)
Construct a single polygon directly from absolute vertex coordinates:
>>> verts = [(0.0, 0.0), (4.0, 0.0), (4.0, 3.0)] >>> aper = PolygonAperture.from_vertices(verts)
Attributes Summary
The exact geometric area of the aperture shape.
The minimal bounding box for the aperture.
The exterior angle of the regular polygon as an angular
Quantityin degrees.The inner (inscribed-circle) radius of the regular polygon in pixels, i.e., the distance from
positionsto the midpoint of each side.The interior angle of the regular polygon as an angular
Quantityin degrees.Trueif the polygon is regular (equal-length sides and equal interior angles, with all vertices at the same distance frompositions).Whether the instance is scalar (i.e., a single position).
The number of vertices of the polygon.
The outer (circumscribed-circle) radius of the regular polygon in pixels, i.e., the distance from
positionsto each vertex.The perimeter of the polygon in pixels.
The center pixel position(s).
The shape of the instance.
The side length of the regular polygon, in pixels.
The rotation angle of the regular polygon as an angular
Quantityin degrees.The (n_vertices, 2) array of pixel offsets of the polygon vertices, measured relative to
positions.The absolute pixel
(x, y)vertices of the polygon at each aperture position.Methods Summary
area_overlap(data, *[, mask, method, subpixels])Return the area of overlap between the data and the aperture.
copy()Make a deep copy of this object.
do_photometry(data[, error, mask, method, ...])Perform aperture photometry on the input data.
from_regular_polygon(positions, n_vertices, ...)Construct a regular
PolygonAperturefrom a center, vertex count, circumradius, and rotation angle.from_vertices(vertices)Construct a
PolygonAperturefrom a single set of absolute pixel vertices.plot([ax, origin])Plot the aperture on a matplotlib
Axesinstance.to_mask([method, subpixels])Return a mask for the aperture.
to_sky(wcs)Convert the aperture to a
SkyPolygonApertureobject defined in celestial coordinates.Attributes Documentation
- area#
The exact geometric area of the aperture shape.
- bbox#
The minimal bounding box for the aperture.
If the aperture is scalar then a single
BoundingBoxis returned, otherwise a list ofBoundingBoxis returned.
- exterior_angle#
The exterior angle of the regular polygon as an angular
Quantityin degrees.This attribute is only defined for regular polygons. Accessing it for a non-regular polygon raises
ValueError.
- inner_radius#
The inner (inscribed-circle) radius of the regular polygon in pixels, i.e., the distance from
positionsto the midpoint of each side.This is also called the inradius
This attribute is only defined for regular polygons. Accessing it for a non-regular polygon raises
ValueError.
- interior_angle#
The interior angle of the regular polygon as an angular
Quantityin degrees.This attribute is only defined for regular polygons. Accessing it for a non-regular polygon raises
ValueError.
- is_regular#
Trueif the polygon is regular (equal-length sides and equal interior angles, with all vertices at the same distance frompositions).
- isscalar#
Whether the instance is scalar (i.e., a single position).
- n_vertices#
The number of vertices of the polygon.
- outer_radius#
The outer (circumscribed-circle) radius of the regular polygon in pixels, i.e., the distance from
positionsto each vertex.This is also called the circumradius.
This attribute is only defined for regular polygons. Accessing it for a non-regular polygon raises
ValueError.
- perimeter#
The perimeter of the polygon in pixels.
The perimeter is computed as the sum of the Euclidean distances between consecutive vertices.
- positions#
The center pixel position(s).
- shape#
The shape of the instance.
- side_length#
The side length of the regular polygon, in pixels.
This attribute is only defined for regular polygons. Accessing it for a non-regular polygon raises
ValueError.
- theta#
The rotation angle of the regular polygon as an angular
Quantityin degrees.The angle is measured counter-clockwise from the
+yaxis to the first vertex, in the range[0, 360) deg. This convention matches thethetaparameter offrom_regular_polygon.This attribute is only defined for regular polygons. Accessing it for a non-regular polygon raises
ValueError.
- vertex_offsets#
The (n_vertices, 2) array of pixel offsets of the polygon vertices, measured relative to
positions.
- vertices#
The absolute pixel
(x, y)vertices of the polygon at each aperture position.For a scalar aperture, the returned array has shape
(n_vertices, 2). For an aperture withn_positionspositions, the returned array has shape(n_positions, n_vertices, 2).
Methods Documentation
- area_overlap(data, *, mask=None, method='exact', subpixels=5)#
Return the area of overlap between the data and the aperture.
This method takes into account the aperture mask method, masked data pixels (
maskkeyword), and partial/no overlap of the aperture with the data. In other words, it returns the area that used to compute the aperture sum (assuming identical inputs).Use the
areamethod to calculate the exact analytical area of the aperture shape.- Parameters:
- dataarray_like or
Quantity A 2D array.
- maskarray_like (bool), optional
A boolean mask with the same shape as
datawhere aTruevalue indicates the corresponding element ofdatais masked. Masked data are excluded from the area overlap.- method{‘exact’, ‘center’, ‘subpixel’}, optional
The method used to determine the pixel weights (the fraction of the pixel area covered by the aperture):
'exact'(default): Calculates the exact geometric overlap area. Weights are continuous in the range [0, 1].'center': Binary weighting based on the pixel center. Weights are either 0 or 1. A pixel is included only if its center lies strictly inside the aperture; pixel centers lying exactly on the aperture boundary are excluded (weight 0).'subpixel': Approximates the overlap by averaging binary samples on a subgrid. The number of samples is set by thesubpixelsparameter. Weights are discrete in the range [0, 1]. A subpixel is included only if its center lies strictly inside the aperture; subpixel centers lying exactly on the aperture boundary are excluded (weight 0).
- subpixelsint, optional
The subsampling factor per axis used when
method='subpixel'. Each pixel is divided into a grid ofsubpixels**2subpixels to approximate the overlap. This parameter is ignored for other methods.
- dataarray_like or
- Returns:
- areasfloat or array_like
The area (in pixels**2) of overlap between the data and the aperture.
See also
- do_photometry(data, error=None, mask=None, method='exact', subpixels=5)#
Perform aperture photometry on the input data.
- Parameters:
- dataarray_like or
Quantityinstance The 2D array on which to perform photometry.
datashould be background subtracted.- errorarray_like or
Quantity, optional The pixel-wise Gaussian 1-sigma errors of the input
data.erroris assumed to include all sources of error, including the Poisson error of the sources (seecalc_total_error).errormust have the same shape as the inputdata.- maskarray_like (bool), optional
A boolean mask with the same shape as
datawhere aTruevalue indicates the corresponding element ofdatais masked. Masked data are excluded from all calculations.- method{‘exact’, ‘center’, ‘subpixel’}, optional
The method used to determine the pixel weights (the fraction of the pixel area covered by the aperture):
'exact'(default): Calculates the exact geometric overlap area. Weights are continuous in the range [0, 1].'center': Binary weighting based on the pixel center. Weights are either 0 or 1. A pixel is included only if its center lies strictly inside the aperture; pixel centers lying exactly on the aperture boundary are excluded (weight 0).'subpixel': Approximates the overlap by averaging binary samples on a subgrid. The number of samples is set by thesubpixelsparameter. Weights are discrete in the range [0, 1]. A subpixel is included only if its center lies strictly inside the aperture; subpixel centers lying exactly on the aperture boundary are excluded (weight 0).
- subpixelsint, optional
The subsampling factor per axis used when
method='subpixel'. Each pixel is divided into a grid ofsubpixels**2subpixels to approximate the overlap. This parameter is ignored for other methods.
- dataarray_like or
- Returns:
- classmethod from_regular_polygon(positions, n_vertices, radius, *, theta=0.0)[source]#
Construct a regular
PolygonAperturefrom a center, vertex count, circumradius, and rotation angle.The first vertex is placed directly above the center (in the
+ydirection) fortheta == 0and is rotated counter-clockwise bytheta. This convention matchesregions.RegularPolygonPixelRegion.- Parameters:
- positionsarray_like
The pixel coordinates of the aperture center(s) (see
PolygonAperturefor the accepted formats).- n_verticesint
The number of vertices (must be at least 3).
- radiusfloat
The circumradius (outer radius) of the polygon in pixels (must be positive).
- thetafloat or
Quantity, optional The rotation angle of the polygon, measured counter- clockwise from the
+yaxis. A scalar float is interpreted in radians.
- Returns:
- aperture
PolygonAperture The regular polygon aperture.
- aperture
- classmethod from_vertices(vertices)[source]#
Construct a
PolygonAperturefrom a single set of absolute pixel vertices.The aperture’s
positionsis set to the area-weighted centroid of the input vertices, andvertex_offsetsis set to the vertices minus the centroid.- Parameters:
- verticesarray_like
Shape
(n_vertices, 2)array of absolute pixel(x, y)vertex coordinates.
- Returns:
- aperture
PolygonAperture A scalar polygon aperture whose
verticesproperty reproduces the input.
- aperture
- plot(ax=None, origin=(0, 0), **kwargs)#
Plot the aperture on a matplotlib
Axesinstance.- Parameters:
- ax
matplotlib.axes.AxesorNone, optional The matplotlib axes on which to plot. If
None, then the currentAxesinstance is used.- originarray_like, optional
The
(x, y)position of the origin of the displayed image.- **kwargsdict, optional
Any keyword arguments accepted by
matplotlib.patches.Patch.
- ax
- Returns:
- patchlist of
Patch A list of matplotlib patches for the plotted aperture. The patches can be used, for example, when adding a plot legend.
- patchlist of
- to_mask(method='exact', subpixels=5)#
Return a mask for the aperture.
- Parameters:
- method{‘exact’, ‘center’, ‘subpixel’}, optional
The method used to determine the pixel weights (the fraction of the pixel area covered by the aperture):
'exact'(default): Calculates the exact geometric overlap area. Weights are continuous in the range [0, 1].'center': Binary weighting based on the pixel center. Weights are either 0 or 1. A pixel is included only if its center lies strictly inside the aperture; pixel centers lying exactly on the aperture boundary are excluded (weight 0).'subpixel': Approximates the overlap by averaging binary samples on a subgrid. The number of samples is set by thesubpixelsparameter. Weights are discrete in the range [0, 1]. A subpixel is included only if its center lies strictly inside the aperture; subpixel centers lying exactly on the aperture boundary are excluded (weight 0).
- subpixelsint, optional
The subsampling factor per axis used when
method='subpixel'. Each pixel is divided into a grid ofsubpixels**2subpixels to approximate the overlap. This parameter is ignored for other methods.
- Returns:
- mask
ApertureMaskor list ofApertureMask A mask for the aperture. If the aperture is scalar then a single
ApertureMaskis returned, otherwise a list ofApertureMaskis returned.
- mask
- to_sky(wcs)[source]#
Convert the aperture to a
SkyPolygonApertureobject defined in celestial coordinates.The conversion uses
wcs.pixel_to_worlddirectly on the absolute pixel vertex coordinates, evaluated at the first aperture position.- Parameters:
- wcsWCS object
A world coordinate system (WCS) transformation that supports the astropy shared interface for WCS (e.g.,
astropy.wcs.WCS,gwcs.wcs.WCS).
- Returns:
- aperture
SkyPolygonAperture The equivalent sky-coordinate polygon aperture.
- aperture