Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _convert_pix_coords(self):
"""
Convert to pixel coordinates, `regions.PixCoord`
"""
if self.region_type in ['polygon', 'line']:
# have to special-case polygon in the phys coord case
# b/c can't typecheck when iterating as in sky coord case
coords = [PixCoord(self.coord[0::2], self.coord[1::2])]
else:
temp = [_.value for _ in self.coord]
coord = PixCoord(temp[0], temp[1])
coords = [coord] + temp[2:]
# The angle remains as a quantity object.
# Modulus check makes sure that it works for ellipse/rectangle annulus
if self.region_type in ['ellipse', 'rectangle'] and len(coords) % 2 == 0:
coords[-1] = self.coord[-1]
return coords
def to_pixel(self, wcs):
center, scale, _ = skycoord_to_pixel_scale_angle(self.center, wcs)
# FIXME: The following line is needed to get a scalar PixCoord
center = PixCoord(float(center.x), float(center.y))
inner_radius = self.inner_radius.to("deg").value * scale
outer_radius = self.outer_radius.to("deg").value * scale
return CircleAnnulusPixelRegion(
center, inner_radius, outer_radius, self.meta, self.visual
)
def to_pixel(self, wcs):
start_x, start_y = skycoord_to_pixel(self.start, wcs=wcs)
start = PixCoord(start_x, start_y)
end_x, end_y = skycoord_to_pixel(self.end, wcs=wcs)
end = PixCoord(end_x, end_y)
return LinePixelRegion(start, end)
reg = ellipse.EllipseSkyRegion(coord_list[0], coord_list[1], coord_list[2], coord_list[3])
elif isinstance(coord_list[0], PixCoord):
reg = ellipse.EllipsePixelRegion(coord_list[0], coord_list[1], coord_list[2], coord_list[3])
else:
raise DS9RegionParserError("No central coordinate")
elif region_type == 'polygon':
if isinstance(coord_list[0], BaseCoordinateFrame):
reg = polygon.PolygonSkyRegion(coord_list[0])
elif isinstance(coord_list[0], PixCoord):
reg = polygon.PolygonPixelRegion(coord_list[0])
else:
raise DS9RegionParserError("No central coordinate")
elif region_type in ('rectangle', 'box'):
if isinstance(coord_list[0], BaseCoordinateFrame):
reg = rectangle.RectangleSkyRegion(coord_list[0], coord_list[1], coord_list[2], coord_list[3])
elif isinstance(coord_list[0], PixCoord):
reg = rectangle.RectanglePixelRegion(coord_list[0], coord_list[1], coord_list[2], coord_list[3])
else:
raise DS9RegionParserError("No central coordinate")
elif region_type == 'point':
if isinstance(coord_list[0], BaseCoordinateFrame):
reg = point.PointSkyRegion(coord_list[0])
elif isinstance(coord_list[0], PixCoord):
reg = point.PointPixelRegion(coord_list[0])
else:
raise DS9RegionParserError("No central coordinate")
else:
# Note: this should effectively never happen, because it would
# imply that the line_parser found a region that didn't match the
# above region types. However, this can help with development,
# since we could in theory implement more region types in the line
# parser and forget to add them here.
def _convert_pix_coords(self):
"""
Convert to pixel coordinates, `regions.PixCoord`
"""
if self.region_type in ['polygon', 'line']:
# have to special-case polygon in the phys coord case
# b/c can't typecheck when iterating as in sky coord case
coords = [PixCoord(self.coord[0::2], self.coord[1::2])]
else:
temp = [_.value for _ in self.coord]
coord = PixCoord(temp[0], temp[1])
coords = [coord] + temp[2:]
# The angle remains as a quantity object.
# Modulus check makes sure that it works for ellipse/rectangle annulus
if self.region_type in ['ellipse', 'rectangle'] and len(coords) % 2 == 0:
coords[-1] = self.coord[-1]
return coords
def to_pixel_args(self, wcs):
center, scale, north_angle = skycoord_to_pixel_scale_angle(self.center, wcs)
center = PixCoord(center.x, center.y)
inner_width = self.inner_width.to("deg").value * scale
inner_height = self.inner_height.to("deg").value * scale
outer_width = self.outer_width.to("deg").value * scale
outer_height = self.outer_height.to("deg").value * scale
angle = self.angle + (north_angle - 90 * u.deg)
return center, inner_width, inner_height, outer_width, outer_height, angle
def contains(self, pixcoord):
pixcoord = PixCoord._validate(pixcoord, 'pixcoord')
x = np.atleast_1d(np.asarray(pixcoord.x, dtype=float))
y = np.atleast_1d(np.asarray(pixcoord.y, dtype=float))
vx = np.asarray(self.vertices.x, dtype=float)
vy = np.asarray(self.vertices.y, dtype=float)
shape = x.shape
mask = points_in_polygon(x.flatten(), y.flatten(), vx, vy).astype(bool)
in_poly = mask.reshape(shape)
if self.meta.get('include', True):
return in_poly
else:
return np.logical_not(in_poly)
# force entries to be scalar if they are length-1
lon, lat = u.Quantity(lon[0]), u.Quantity(lat[0])
else:
# otherwise, they are vector quantitites
lon, lat = u.Quantity(lon), u.Quantity(lat)
sphcoords = coordinates.UnitSphericalRepresentation(lon, lat)
coords = frame(sphcoords)
return region_type, [coords] + parsed[len(coords) * 2:], parsed_meta, composite, include
else:
parsed = type_parser(coords_etc, language_spec[region_type],
coordsys)
if region_type == 'polygon':
# have to special-case polygon in the phys coord case
# b/c can't typecheck when iterating as in sky coord case
coord = PixCoord(parsed[0::2], parsed[1::2])
parsed_return = [coord]
else:
parsed = [_.value for _ in parsed]
coord = PixCoord(parsed[0], parsed[1])
parsed_return = [coord] + parsed[2:]
# Reset iterator for ellipse annulus
if region_type == 'ellipse':
language_spec[region_type] = itertools.chain((coordinate, coordinate), itertools.cycle((radius,)))
return region_type, parsed_return, parsed_meta, composite, include
else:
# This will raise warnings even if the first line is acceptable,
# e.g. something like:
# # Region file format: DS9 version 4.1
# That behavior is unfortunate, but there's not a great workaround