MAINT: move common logic to helper function
@@ -349,14 +349,7 @@ def get_ellipse(data, a, b, PA, cy=None, cx=None, mode="ind"):
[mode='bool'] A boolean mask where ``True`` is the inner region.
"""
- if isinstance(data, np.ndarray):
- array = data
- if array.ndim != 2:
- raise TypeError('`data` is not a frame or 2d array')
- elif isinstance(data, tuple):
- array = np.zeros(data)
- else:
- raise TypeError('`data` must be a tuple (shape) or a 2d array')
+ array = _image_or_shape(data)
if cy is None or cx is None:
cy, cx = frame_center(array, verbose=False)
@@ -447,14 +440,7 @@ def get_annulus_segments(data, inner_radius, width, nsegm=1, theta_init=0,
# `in <= ann <= out`. But that should make no difference in practice.
"""
- if isinstance(data, np.ndarray):
- array = data
- if array.ndim != 2:
- raise TypeError('`data` is not a frame or 2d array')
- elif isinstance(data, tuple):
- array = np.zeros(data)
- else:
- raise TypeError('`data` must be a tuple (shape) or a 2d array')
+ array = _image_or_shape(data)
if not isinstance(nsegm, int):
raise TypeError('`nsegm` must be an integer')
@@ -535,14 +521,7 @@ def get_ell_annulus(data, a, b, PA, width, cy=None, cx=None, mode="ind"):
[mode='mask'] Input image where the outer region is masked with ``0``.
"""
- if isinstance(data, np.ndarray):
- array = data
- if array.ndim != 2:
- raise TypeError('`data` is not a frame or 2d array')
- elif isinstance(data, tuple):
- array = np.zeros(data)
- else:
- raise TypeError('`data` must be a tuple (shape) or a 2d array')
+ array = _image_or_shape(data)
hwa = width / 2 # half width for a
hwb = (width * b / a) / 2 # half width for b
@@ -716,3 +695,16 @@ def reshape_matrix(array, y, x):
"""
return array.reshape(array.shape[0], y, x)
+
+
+def _image_or_shape(data):
+ if isinstance(data, np.ndarray):
+ array = data
+ if array.ndim != 2:
+ raise TypeError('`data` is not a frame or 2d array')
+ elif isinstance(data, tuple):
+ array = np.zeros(data)
+ else:
+ raise TypeError('`data` must be a tuple (shape) or a 2d array')
+
+ return array
