Skip to content

Commit

Permalink
Merge pull request #305 from cdeil/cleanup2
Browse files Browse the repository at this point in the history
Clean up pix region bounding_box and area
  • Loading branch information
cdeil committed Oct 23, 2019
2 parents cf6e089 + c85a8c4 commit 55cb07f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
8 changes: 1 addition & 7 deletions regions/core/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def from_float(cls, xmin, xmax, ymin, ymax):
>>> BoundingBox.from_float(xmin=1.4, xmax=10.4, ymin=1.6, ymax=10.6)
BoundingBox(ixmin=1, ixmax=11, iymin=2, iymax=12)
"""

ixmin = int(np.floor(xmin + 0.5))
ixmax = int(np.ceil(xmax + 0.5))
iymin = int(np.floor(ymin + 0.5))
Expand Down Expand Up @@ -147,7 +146,6 @@ def shape(self):
"""
The ``(ny, nx)`` shape of the bounding box.
"""

return self.iymax - self.iymin, self.ixmax - self.ixmin

@property
Expand All @@ -158,8 +156,7 @@ def slices(self):
The slice tuple is in numpy axis order (i.e. ``(y, x)``) and
therefore can be used to slice numpy arrays.
"""

return (slice(self.iymin, self.iymax), slice(self.ixmin, self.ixmax))
return slice(self.iymin, self.iymax), slice(self.ixmin, self.ixmax)

@property
def extent(self):
Expand All @@ -174,7 +171,6 @@ def extent(self):
indexing. This is useful for plotting the bounding box using
Matplotlib.
"""

return (
self.ixmin - 0.5,
self.ixmax - 0.5,
Expand Down Expand Up @@ -275,7 +271,6 @@ def union(self, other):
A `BoundingBox` representing the union of the input
`BoundingBox` with this one.
"""

if not isinstance(other, BoundingBox):
raise TypeError('BoundingBox can be joined only with another '
'BoundingBox.')
Expand Down Expand Up @@ -303,7 +298,6 @@ def intersection(self, other):
A `BoundingBox` representing the intersection of the input
`BoundingBox` with this one.
"""

if not isinstance(other, BoundingBox):
raise TypeError('BoundingBox can be intersected only with '
'another BoundingBox.')
Expand Down
17 changes: 5 additions & 12 deletions regions/core/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np

from . import PixelRegion, SkyRegion, BoundingBox, RegionMask
from . import PixelRegion, SkyRegion, RegionMask
from ..core.attributes import (CompoundRegionPix, CompoundRegionSky,
RegionMeta, RegionVisual)

Expand Down Expand Up @@ -61,20 +61,14 @@ def contains(self, pixcoord):
return np.logical_not(in_reg)

def to_mask(self, mode='center', subpixels=1):

if mode != 'center':
raise NotImplementedError

mask1 = self.region1.to_mask(mode=mode, subpixels=subpixels)
mask2 = self.region2.to_mask(mode=mode, subpixels=subpixels)

# Common bounding box
bbox = BoundingBox(
ixmin=min(mask1.bbox.ixmin, mask2.bbox.ixmin),
ixmax=max(mask1.bbox.ixmax, mask2.bbox.ixmax),
iymin=min(mask1.bbox.iymin, mask2.bbox.iymin),
iymax=max(mask1.bbox.iymax, mask2.bbox.iymax)
)
bbox = self.bounding_box

# Pad mask1.data and mask2.data to get the same shape
padded_data = list()
Expand Down Expand Up @@ -107,7 +101,6 @@ def _make_annulus_path(patch_inner, patch_outer):
# This is borrowed from photutils aperture.
"""

import matplotlib.path as mpath

path_inner = patch_inner.get_path()
Expand Down Expand Up @@ -143,7 +136,6 @@ def as_artist(self, origin=(0, 0), **kwargs):
patch : `~matplotlib.patches.PathPatch`
Matplotlib patch object
"""

if self.region1.center == self.region2.center and self.operator == op.xor:
import matplotlib.patches as mpatches

Expand All @@ -155,8 +147,9 @@ def as_artist(self, origin=(0, 0), **kwargs):
else:
raise NotImplementedError

def bounding_box(self, **kwargs):
raise NotImplementedError
@property
def bounding_box(self):
return self.region1.bounding_box | self.region2.bounding_box

@property
def area(self):
Expand Down
6 changes: 6 additions & 0 deletions regions/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ def to_sky(self, wcs):
"""
raise NotImplementedError

@property
@abc.abstractmethod
def area(self):
"""Region area (float)"""
raise NotImplementedError

@property
@abc.abstractmethod
def bounding_box(self):
Expand Down
6 changes: 4 additions & 2 deletions regions/core/tests/test_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from astropy.coordinates import SkyCoord

from ...shapes import CircleSkyRegion, CirclePixelRegion
from ...core import PixCoord, CompoundPixelRegion
from ...core import PixCoord, BoundingBox, CompoundPixelRegion
from ...tests.helpers import make_simple_wcs


Expand Down Expand Up @@ -100,9 +100,11 @@ def test_to_mask(self):
)
assert_allclose(mask1.data, ref_data)

def test_bounding_box(self):
bbox = (self.c1 | self.c2).bounding_box
assert bbox == BoundingBox(1, 16, 1, 10)

def test_compound_sky():

skycoord1 = SkyCoord(0 * u.deg, 0 * u.deg, frame='galactic')
c1 = CircleSkyRegion(skycoord1, 1 * u.deg)

Expand Down
2 changes: 1 addition & 1 deletion regions/shapes/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(self, center, width, height, angle=0. * u.deg, meta=None,
@property
def area(self):
"""Region area (float)"""
return math.pi * self.width * self.height * 0.25
return math.pi / 4 * self.width * self.height

def contains(self, pixcoord):
pixcoord = PixCoord._validate(pixcoord, name='pixcoord')
Expand Down
2 changes: 1 addition & 1 deletion regions/shapes/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, start, end, meta=None, visual=None):
@property
def area(self):
"""Region area (float)."""
return 0 * u.sr
return 0

def contains(self, pixcoord):
if pixcoord.isscalar:
Expand Down

0 comments on commit 55cb07f

Please sign in to comment.