Skip to content

Commit

Permalink
Merge pull request #286 from cdeil/repr_params
Browse files Browse the repository at this point in the history
Change _repr_params to _fields, including center
  • Loading branch information
larrybradley committed Jun 21, 2019
2 parents 496578c + 49ac1e7 commit e4e7450
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 87 deletions.
2 changes: 1 addition & 1 deletion docs/crtf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ read from a file in addition to doing the region serialisation and parsing.
>>> write_crtf(regions, filename)
>>> regions = read_crtf(filename)
>>> regions
[<CircleSkyRegion(<SkyCoord (FK5: equinox=J2000.000): (ra, dec) in deg
[<CircleSkyRegion(center=<SkyCoord (FK5: equinox=J2000.000): (ra, dec) in deg
(42., 43.)>, radius=3.0 deg)>]
2 changes: 1 addition & 1 deletion docs/ds9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ read from a file in addition to doing the region serialisation and parsing.
>>> write_ds9(regions, filename)
>>> regions = read_ds9(filename)
>>> regions
[<CircleSkyRegion(<SkyCoord (FK5: equinox=J2000.000): (ra, dec) in deg
[<CircleSkyRegion(center=<SkyCoord (FK5: equinox=J2000.000): (ra, dec) in deg
(245.347655, 24.429081)>, radius=3.0 deg)>]
The ``visual`` metadata includes items used for display, e.g.:
Expand Down
6 changes: 2 additions & 4 deletions regions/core/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CompoundPixelRegion(PixelRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""

_params = ('region1', 'region2', 'operator')
region1 = CompoundRegionPix('region1')
region2 = CompoundRegionPix('region2')

Expand All @@ -49,7 +49,6 @@ def __init__(self, region1, region2, operator, meta=None, visual=None):
else:
self.visual = RegionVisual()
self._operator = operator
self._repr_params = ('region1', 'region2', 'operator')

@property
def operator(self):
Expand Down Expand Up @@ -204,6 +203,7 @@ class CompoundSkyRegion(SkyRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""
_params = ('region1', 'region2', 'operator')
region1 = CompoundRegionSky('region1')
region2 = CompoundRegionSky('region2')

Expand All @@ -223,8 +223,6 @@ def __init__(self, region1, region2, operator, meta=None, visual=None):
self.visual = RegionVisual()
self._operator = operator

self._repr_params = ('region1', 'region2', 'operator')

@property
def operator(self):
return self._operator
Expand Down
23 changes: 6 additions & 17 deletions regions/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ class Region(object):

def copy(self, **changes):
"""Make an independent (deep) copy."""
fields = []
if hasattr(self, "center"):
fields.append("center")
if hasattr(self, "vertices"):
fields.append("vertices")
fields.extend(self._repr_params)
fields.extend(["meta", "visual"])
fields = list(self._params) + ["meta", "visual"]

for field in fields:
if field not in changes:
Expand All @@ -84,12 +78,9 @@ def copy(self, **changes):
return self.__class__(**changes)

def __repr__(self):
if hasattr(self, 'center'):
params = [repr(self.center)]
else:
params = []
if self._repr_params is not None:
for key in self._repr_params:
params = []
if self._params is not None:
for key in self._params:
params.append('{0}={1}'.format(key.replace("_", " "),
getattr(self, key)))
params = ', '.join(params)
Expand All @@ -98,11 +89,9 @@ def __repr__(self):

def __str__(self):
cls_info = [('Region', self.__class__.__name__)]
if hasattr(self, 'center'):
cls_info.append(('center', self.center))
if self._repr_params is not None:
if self._params is not None:
params_value = [(x.replace("_", " "), getattr(self, x))
for x in self._repr_params]
for x in self._params]
cls_info += params_value
fmt = ['{0}: {1}'.format(key, val) for key, val in cls_info]

Expand Down
2 changes: 1 addition & 1 deletion regions/io/fits/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FITSRegionParser(object):
>>> shapes = parser.shapes
>>> regions = shapes.to_regions()
>>> regions[5]
<PointPixelRegion(PixCoord(x=341.0, y=345.0))>
<PointPixelRegion(center=PixCoord(x=341.0, y=345.0))>
"""

valid_columns = ['X', 'Y', 'SHAPE', 'COMPONENT', 'R', 'ROTANG']
Expand Down
37 changes: 18 additions & 19 deletions regions/shapes/annulus.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class CircleAnnulusPixelRegion(AnnulusPixelRegion):
"""
_component_class = CirclePixelRegion

_params = ("center", "inner_radius", "outer_radius")
center = ScalarPix("center")
inner_radius = ScalarLength("inner_radius")
outer_radius = ScalarLength("outer_radius")
Expand All @@ -134,7 +135,6 @@ def __init__(self, center, inner_radius, outer_radius, meta=None, visual=None):
self.outer_radius = outer_radius
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ("inner_radius", "outer_radius")

@property
def _inner_region(self):
Expand Down Expand Up @@ -171,7 +171,7 @@ class CircleAnnulusSkyRegion(SkyRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""

_params = ("center", "inner_radius", "outer_radius")
center = ScalarSky("center")
inner_radius = QuantityLength("inner_radius")
outer_radius = QuantityLength("outer_radius")
Expand All @@ -182,7 +182,6 @@ def __init__(self, center, inner_radius, outer_radius, meta=None, visual=None):
self.outer_radius = outer_radius
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ("inner_radius", "outer_radius")

def to_pixel(self, wcs):
center, scale, _ = skycoord_to_pixel_scale_angle(self.center, wcs)
Expand All @@ -201,7 +200,14 @@ class AsymmetricAnnulusPixelRegion(AnnulusPixelRegion):
Used for ellipse and rectangle annuli below.
"""

_params = (
"center",
"inner_width",
"inner_height",
"outer_width",
"outer_height",
"angle",
)
center = ScalarPix("center")
inner_width = ScalarLength("inner_width")
outer_width = ScalarLength("outer_width")
Expand All @@ -228,13 +234,6 @@ def __init__(
self.angle = angle
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = (
"inner_width",
"inner_height",
"outer_width",
"outer_height",
"angle",
)

@property
def _inner_region(self):
Expand Down Expand Up @@ -277,7 +276,14 @@ class AsymmetricAnnulusSkyRegion(SkyRegion):
Used for ellipse and rectangle annuli below.
"""

_params = (
"center",
"inner_width",
"inner_height",
"outer_width",
"outer_height",
"angle",
)
center = ScalarSky("center")
inner_width = QuantityLength("inner_width")
outer_width = QuantityLength("outer_width")
Expand All @@ -304,13 +310,6 @@ def __init__(
self.angle = angle
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = (
"inner_width",
"inner_height",
"outer_width",
"outer_height",
"angle",
)

def to_pixel_args(self, wcs):
center, scale, north_angle = skycoord_to_pixel_scale_angle(self.center, wcs)
Expand Down
6 changes: 2 additions & 4 deletions regions/shapes/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CirclePixelRegion(PixelRegion):
plt.ylim(0, 15)
ax.set_aspect('equal')
"""

_params = ('center', 'radius')
center = ScalarPix('center')
radius = ScalarLength('radius')

Expand All @@ -62,7 +62,6 @@ def __init__(self, center, radius, meta=None, visual=None):
self.radius = radius
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ('radius',)

@property
def area(self):
Expand Down Expand Up @@ -183,7 +182,7 @@ class CircleSkyRegion(SkyRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""

_params = ('center', 'radius')
center = ScalarSky('center')
radius = QuantityLength("radius")

Expand All @@ -192,7 +191,6 @@ def __init__(self, center, radius, meta=None, visual=None):
self.radius = radius
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ('radius',)

def to_pixel(self, wcs):
center, scale, _ = skycoord_to_pixel_scale_angle(self.center, wcs)
Expand Down
6 changes: 2 additions & 4 deletions regions/shapes/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class EllipsePixelRegion(PixelRegion):
ax.add_patch(patch)
"""

_params = ('center', 'width', 'height', 'angle')
center = ScalarPix('center')
width = ScalarLength('width')
height = ScalarLength('height')
Expand All @@ -77,7 +77,6 @@ def __init__(self, center, width, height, angle=0. * u.deg, meta=None,
self.angle = angle
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ('width', 'height', 'angle')

@property
def area(self):
Expand Down Expand Up @@ -255,7 +254,7 @@ class EllipseSkyRegion(SkyRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""

_params = ('center', 'width', 'height', 'angle')
center = ScalarSky('center')
width = QuantityLength('width')
height = QuantityLength('height')
Expand All @@ -268,7 +267,6 @@ def __init__(self, center, width, height, angle=0. * u.deg, meta=None, visual=No
self.angle = angle
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ('width', 'height', 'angle')

def to_pixel(self, wcs):
center, scale, north_angle = skycoord_to_pixel_scale_angle(self.center, wcs)
Expand Down
6 changes: 2 additions & 4 deletions regions/shapes/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LinePixelRegion(PixelRegion):
plt.ylim(0, 30)
ax.set_aspect('equal')
"""

_params = ('start', 'end')
start = ScalarPix('start')
end = ScalarPix('end')

Expand All @@ -61,7 +61,6 @@ def __init__(self, start, end, meta=None, visual=None):
self.end = end
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ('start', 'end')

@property
def area(self):
Expand Down Expand Up @@ -170,7 +169,7 @@ class LineSkyRegion(SkyRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""

_params = ('start', 'end')
start = ScalarSky('start')
end = ScalarSky('end')

Expand All @@ -179,7 +178,6 @@ def __init__(self, start, end, meta=None, visual=None):
self.end = end
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ('start', 'end')

def contains(self, skycoord, wcs):
if self.meta.get('include', True):
Expand Down
6 changes: 2 additions & 4 deletions regions/shapes/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ class PointPixelRegion(PixelRegion):
ax.set_aspect('equal')
"""

_params = ('center',)
center = ScalarPix('center')

def __init__(self, center, meta=None, visual=None):
self.center = center
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = tuple()

@property
def area(self):
Expand Down Expand Up @@ -149,14 +148,13 @@ class PointSkyRegion(SkyRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""

_params = ('center',)
center = ScalarSky('center')

def __init__(self, center, meta=None, visual=None):
self.center = center
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = tuple()

def contains(self, skycoord, wcs):
if self.meta.get('include', True):
Expand Down
6 changes: 2 additions & 4 deletions regions/shapes/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ class PolygonPixelRegion(PixelRegion):
plt.ylim(50, 80)
ax.set_aspect('equal')
"""

_params = ('vertices',)
vertices = OneDPix('vertices')

def __init__(self, vertices, meta=None, visual=None):
self.vertices = vertices
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ('vertices',)

@property
def area(self):
Expand Down Expand Up @@ -182,14 +181,13 @@ class PolygonSkyRegion(SkyRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""

_params = ('vertices',)
vertices = OneDSky('vertices')

def __init__(self, vertices, meta=None, visual=None):
self.vertices = vertices
self.meta = meta or RegionMeta()
self.visual = visual or RegionVisual()
self._repr_params = ('vertices',)

def to_pixel(self, wcs):
x, y = skycoord_to_pixel(self.vertices, wcs)
Expand Down
6 changes: 2 additions & 4 deletions regions/shapes/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class RectanglePixelRegion(PixelRegion):
plt.ylim(0, 20)
ax.set_aspect('equal')
"""

_params = ('center', 'width', 'height', 'angle')
center = ScalarPix('center')
width = ScalarLength('width')
height = ScalarLength('height')
Expand All @@ -75,7 +75,6 @@ def __init__(self, center, width, height, angle=0 * u.deg, meta=None, visual=Non
self.angle = angle
self.meta = meta or {}
self.visual = visual or {}
self._repr_params = ('width', 'height', 'angle')

@property
def area(self):
Expand Down Expand Up @@ -282,7 +281,7 @@ class RectangleSkyRegion(SkyRegion):
visual : `~regions.RegionVisual` object, optional
A dictionary which stores the visual meta attributes of this region.
"""

_params = ('center', 'width', 'height', 'angle')
center = ScalarSky('center')
width = QuantityLength('width')
height = QuantityLength('height')
Expand All @@ -295,7 +294,6 @@ def __init__(self, center, width, height, angle=0 * u.deg, meta=None, visual=Non
self.angle = angle
self.meta = meta or {}
self.visual = visual or {}
self._repr_params = ('width', 'height', 'angle')

def to_pixel(self, wcs):
center, scale, north_angle = skycoord_to_pixel_scale_angle(self.center, wcs)
Expand Down
Loading

0 comments on commit e4e7450

Please sign in to comment.