Skip to content

Instantly share code, notes, and snippets.

@cdeil
Created February 13, 2014 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdeil/8974062 to your computer and use it in GitHub Desktop.
Save cdeil/8974062 to your computer and use it in GitHub Desktop.
==================================================================== test session starts =====================================================================
platform darwin -- Python 2.7.6 -- pytest-2.5.1
Running tests with Astropy version 0.4.dev7301.
Running tests in photutils /Users/deil/code/photutils/docs.
Platform: Darwin-13.0.0-x86_64-i386-64bit
Executable: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Full Python Version:
2.7.6 (default, Nov 18 2013, 15:12:51)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)]
encodings: sys: ascii, locale: UTF-8, filesystem: utf-8, unicode bits: 15
byteorder: little
float info: dig: 15, mant_dig: 15
Numpy: 1.8.0
Scipy: 0.13.3
Matplotlib: 1.3.1
h5py: 2.2.1
collected 61 items
photutils/tests/test_aperture_broadcasting.py ..F.F...F.F...F.F...F.F.
photutils/tests/test_aperture_photometry.py ................................
photutils/tests/test_apertures.py .....
========================================================================== FAILURES ==========================================================================
____________________________________________ TestBroadcastingCircular.test_multiple_objects_single_same_aperture _____________________________________________
self = <photutils.tests.test_aperture_broadcasting.TestBroadcastingCircular object at 0x10fc26b10>
def test_multiple_objects_single_same_aperture(self):
# Multiple objects, single aperture (same for each object)
> flux = aperture_circular(self.data, [2., 3., 4.], [5., 6., 7.], 5.)
photutils/tests/test_aperture_broadcasting.py:32:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = [2.0, 3.0, 4.0], yc = [5.0, 6.0, 7.0], r = array(5.0), error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_circular(data, xc, yc, r, error=None, gain=None, mask=None,
method='exact', subpixels=5, pixelwise_errors=True):
r"""Sum flux within circular apertures.
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
r : float or array_like
Radius of the aperture(s). If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options are ['center', 'subpixel', 'exact']. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
See Also
--------
aperture_photometry
"""
r = np.asarray(r)
if r.ndim > 0:
apertures = np.empty(r.shape, dtype=object)
for index in np.ndindex(*r.shape):
apertures[index] = CircularAperture(r[index])
else:
apertures = CircularAperture(r)
return aperture_photometry(data, xc, yc, apertures, error=error,
gain=gain, mask=mask, subpixels=subpixels,
> pixelwise_errors=pixelwise_errors)
photutils/aperture.py:792:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = array([ 2., 3., 4.]), yc = array([ 5., 6., 7.]), apertures = array([[<photutils.aperture.CircularAperture object at 0x10fc26a90>]], dtype=object)
error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_photometry(data, xc, yc, apertures, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within aperture(s).
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
apertures : `Aperture` object or array of `Aperture` objects
The apertures to use for photometry. If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
Note that for subpixel sampling, the input array is only
resampled once for each object.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options include ['center', 'subpixel', 'exact'], but not all options
are available for all types of apertures. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
"""
# Check input array type and dimension.
data = np.asarray(data)
if np.iscomplexobj(data):
raise TypeError('Complex type not supported')
if data.ndim != 2:
raise ValueError('{0}-d array not supported. '
'Only 2-d arrays supported.'.format(data.ndim))
# Note whether xc, yc are scalars so we can try to return scalars later.
scalar_obj_centers = np.isscalar(xc) and np.isscalar(yc)
# Check shapes of xc, yc
xc = np.atleast_1d(xc)
yc = np.atleast_1d(yc)
if xc.ndim > 1 or yc.ndim > 1:
raise ValueError('Only 1-d arrays supported for object centers.')
if xc.shape[0] != yc.shape[0]:
raise ValueError('length of xc and yc must match')
n_obj = xc.shape[0]
# Check 'apertures' dimensions and type
apertures = np.atleast_2d(apertures)
if apertures.ndim > 2:
raise ValueError('{0}-d aperture array not supported. '
'Only 2-d arrays supported.'.format(apertures.ndim))
for aperture in apertures.ravel():
if not isinstance(aperture, Aperture):
raise TypeError("'aperture' must be an instance of Aperture.")
n_aper = apertures.shape[0]
# Check 'apertures' shape and expand trailing dimension to match N_obj
# if necessary.
if apertures.shape[1] not in [1, n_obj]:
raise ValueError("trailing dimension of 'apertures' must be 1 or "
"match length of xc, yc")
if apertures.shape[1] != n_obj:
# We will not use xc2d. This is just for broadcasting 'apertures':
> apertures, xc2d = np.broadcast_arrays(apertures, xc)
photutils/aperture.py:519:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = [array([[<photutils.aperture.CircularAperture object at 0x10fc26a90>]], dtype=object), array([ 2., 3., 4.])], _m = array([ 2., 3., 4.])
x = array([[<photutils.aperture.CircularAperture object at 0x10fc26a90>]], dtype=object), shapes = [[1, 3], [1, 3]]
def broadcast_arrays(*args):
"""
Broadcast any number of arrays against each other.
Parameters
----------
`*args` : array_likes
The arrays to broadcast.
Returns
-------
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
broadcasted array may refer to a single memory location. If you
need to write to the arrays, make copies first.
Examples
--------
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>> np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of
non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
"""
args = [np.asarray(_m) for _m in args]
shapes = [x.shape for x in args]
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return args
shapes = [list(s) for s in shapes]
strides = [list(x.strides) for x in args]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(args)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
strides[i] = [0] * diff + strides[i]
# Chech each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
common_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length. The common shape will take this
# value.
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
# For each array, if this axis is being broadcasted from a length of
# 1, then set its stride to 0 so that it repeats its data.
for i in range(len(args)):
if shapes[i][axis] == 1:
shapes[i][axis] = new_length
strides[i][axis] = 0
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
common_shape.append(1)
# Construct the new arrays.
broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
> zip(args, shapes, strides)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:119:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[<photutils.aperture.CircularAperture object at 0x10fc26a90>]], dtype=object), shape = [1, 3], strides = [8, 0]
def as_strided(x, shape=None, strides=None):
""" Make an ndarray from the given array with the given shape and strides.
"""
interface = dict(x.__array_interface__)
if shape is not None:
interface['shape'] = tuple(shape)
if strides is not None:
interface['strides'] = tuple(strides)
array = np.asarray(DummyArray(interface, base=x))
# Make sure dtype is correct in case of custom dtype
> array.dtype = x.dtype
E TypeError: Cannot change data-type for object array.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:32: TypeError
________________________________________ TestBroadcastingCircular.test_multiple_objects_multiple_aperture_per_object _________________________________________
self = <photutils.tests.test_aperture_broadcasting.TestBroadcastingCircular object at 0x10fc00d90>
def test_multiple_objects_multiple_aperture_per_object(self):
# Multiple objects, multiple apertures per object (same for each object)
flux = aperture_circular(self.data, [2., 3., 4.], [5., 6., 7.],
> [[2.], [5.]])
photutils/tests/test_aperture_broadcasting.py:44:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = [2.0, 3.0, 4.0], yc = [5.0, 6.0, 7.0], r = array([[ 2.],
[ 5.]]), error = None, gain = None, mask = None, method = 'exact', subpixels = 5
pixelwise_errors = True
def aperture_circular(data, xc, yc, r, error=None, gain=None, mask=None,
method='exact', subpixels=5, pixelwise_errors=True):
r"""Sum flux within circular apertures.
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
r : float or array_like
Radius of the aperture(s). If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options are ['center', 'subpixel', 'exact']. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
See Also
--------
aperture_photometry
"""
r = np.asarray(r)
if r.ndim > 0:
apertures = np.empty(r.shape, dtype=object)
for index in np.ndindex(*r.shape):
apertures[index] = CircularAperture(r[index])
else:
apertures = CircularAperture(r)
return aperture_photometry(data, xc, yc, apertures, error=error,
gain=gain, mask=mask, subpixels=subpixels,
> pixelwise_errors=pixelwise_errors)
photutils/aperture.py:792:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = array([ 2., 3., 4.]), yc = array([ 5., 6., 7.])
apertures = array([[<photutils.aperture.CircularAperture object at 0x10fc00d50>],
[<photutils.aperture.CircularAperture object at 0x10fc00e50>]], dtype=object)
error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_photometry(data, xc, yc, apertures, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within aperture(s).
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
apertures : `Aperture` object or array of `Aperture` objects
The apertures to use for photometry. If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
Note that for subpixel sampling, the input array is only
resampled once for each object.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options include ['center', 'subpixel', 'exact'], but not all options
are available for all types of apertures. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
"""
# Check input array type and dimension.
data = np.asarray(data)
if np.iscomplexobj(data):
raise TypeError('Complex type not supported')
if data.ndim != 2:
raise ValueError('{0}-d array not supported. '
'Only 2-d arrays supported.'.format(data.ndim))
# Note whether xc, yc are scalars so we can try to return scalars later.
scalar_obj_centers = np.isscalar(xc) and np.isscalar(yc)
# Check shapes of xc, yc
xc = np.atleast_1d(xc)
yc = np.atleast_1d(yc)
if xc.ndim > 1 or yc.ndim > 1:
raise ValueError('Only 1-d arrays supported for object centers.')
if xc.shape[0] != yc.shape[0]:
raise ValueError('length of xc and yc must match')
n_obj = xc.shape[0]
# Check 'apertures' dimensions and type
apertures = np.atleast_2d(apertures)
if apertures.ndim > 2:
raise ValueError('{0}-d aperture array not supported. '
'Only 2-d arrays supported.'.format(apertures.ndim))
for aperture in apertures.ravel():
if not isinstance(aperture, Aperture):
raise TypeError("'aperture' must be an instance of Aperture.")
n_aper = apertures.shape[0]
# Check 'apertures' shape and expand trailing dimension to match N_obj
# if necessary.
if apertures.shape[1] not in [1, n_obj]:
raise ValueError("trailing dimension of 'apertures' must be 1 or "
"match length of xc, yc")
if apertures.shape[1] != n_obj:
# We will not use xc2d. This is just for broadcasting 'apertures':
> apertures, xc2d = np.broadcast_arrays(apertures, xc)
photutils/aperture.py:519:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = [array([[<photutils.aperture.CircularAperture object at 0x10fc00d50>],
[<photutils.aperture.CircularAperture object at 0x10fc00e50>]], dtype=object), array([ 2., 3., 4.])]
_m = array([ 2., 3., 4.])
x = array([[<photutils.aperture.CircularAperture object at 0x10fc00d50>],
[<photutils.aperture.CircularAperture object at 0x10fc00e50>]], dtype=object)
shapes = [[2, 3], [2, 3]]
def broadcast_arrays(*args):
"""
Broadcast any number of arrays against each other.
Parameters
----------
`*args` : array_likes
The arrays to broadcast.
Returns
-------
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
broadcasted array may refer to a single memory location. If you
need to write to the arrays, make copies first.
Examples
--------
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>> np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of
non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
"""
args = [np.asarray(_m) for _m in args]
shapes = [x.shape for x in args]
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return args
shapes = [list(s) for s in shapes]
strides = [list(x.strides) for x in args]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(args)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
strides[i] = [0] * diff + strides[i]
# Chech each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
common_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length. The common shape will take this
# value.
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
# For each array, if this axis is being broadcasted from a length of
# 1, then set its stride to 0 so that it repeats its data.
for i in range(len(args)):
if shapes[i][axis] == 1:
shapes[i][axis] = new_length
strides[i][axis] = 0
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
common_shape.append(1)
# Construct the new arrays.
broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
> zip(args, shapes, strides)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:119:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[<photutils.aperture.CircularAperture object at 0x10fc00d50>],
[<photutils.aperture.CircularAperture object at 0x10fc00e50>]], dtype=object)
shape = [2, 3], strides = [8, 0]
def as_strided(x, shape=None, strides=None):
""" Make an ndarray from the given array with the given shape and strides.
"""
interface = dict(x.__array_interface__)
if shape is not None:
interface['shape'] = tuple(shape)
if strides is not None:
interface['strides'] = tuple(strides)
array = np.asarray(DummyArray(interface, base=x))
# Make sure dtype is correct in case of custom dtype
> array.dtype = x.dtype
E TypeError: Cannot change data-type for object array.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:32: TypeError
_________________________________________ TestBroadcastingCircularAnnulus.test_multiple_objects_single_same_aperture _________________________________________
self = <photutils.tests.test_aperture_broadcasting.TestBroadcastingCircularAnnulus object at 0x10fbf74d0>
def test_multiple_objects_single_same_aperture(self):
# Multiple objects, single aperture (same for each object)
flux = annulus_circular(self.data, [2., 3., 4.], [5., 6., 7.],
> 2., 5.)
photutils/tests/test_aperture_broadcasting.py:74:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = [2.0, 3.0, 4.0], yc = [5.0, 6.0, 7.0], r_in = array(2.0), r_out = array(5.0), error = None, gain = None, mask = None, method = 'exact', subpixels = 5
pixelwise_errors = True
def annulus_circular(data, xc, yc, r_in, r_out, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within circular annuli.
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
r_in, r_out : float or array_like
The parameters of the annuli: respectively, the inner and
outer radii. If an array (of at most 2 dimensions), the
trailing dimension of the array must be broadcastable to
N_objects (= `len(xc)`). In other words, the trailing
dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options are ['center', 'subpixel', 'exact']. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
See Also
--------
aperture_photometry
"""
r_in, r_out = np.broadcast_arrays(r_in, r_out)
if r_in.ndim > 0:
apertures = np.empty(r_in.shape, dtype=object)
for index in np.ndindex(*r_in.shape):
apertures[index] = CircularAnnulus(r_in[index], r_out[index])
else:
apertures = CircularAnnulus(r_in, r_out)
return aperture_photometry(data, xc, yc, apertures, error=error,
gain=gain, mask=mask, subpixels=subpixels,
> pixelwise_errors=pixelwise_errors)
photutils/aperture.py:991:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = array([ 2., 3., 4.]), yc = array([ 5., 6., 7.]), apertures = array([[<photutils.aperture.CircularAnnulus object at 0x10fbf7650>]], dtype=object)
error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_photometry(data, xc, yc, apertures, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within aperture(s).
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
apertures : `Aperture` object or array of `Aperture` objects
The apertures to use for photometry. If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
Note that for subpixel sampling, the input array is only
resampled once for each object.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options include ['center', 'subpixel', 'exact'], but not all options
are available for all types of apertures. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
"""
# Check input array type and dimension.
data = np.asarray(data)
if np.iscomplexobj(data):
raise TypeError('Complex type not supported')
if data.ndim != 2:
raise ValueError('{0}-d array not supported. '
'Only 2-d arrays supported.'.format(data.ndim))
# Note whether xc, yc are scalars so we can try to return scalars later.
scalar_obj_centers = np.isscalar(xc) and np.isscalar(yc)
# Check shapes of xc, yc
xc = np.atleast_1d(xc)
yc = np.atleast_1d(yc)
if xc.ndim > 1 or yc.ndim > 1:
raise ValueError('Only 1-d arrays supported for object centers.')
if xc.shape[0] != yc.shape[0]:
raise ValueError('length of xc and yc must match')
n_obj = xc.shape[0]
# Check 'apertures' dimensions and type
apertures = np.atleast_2d(apertures)
if apertures.ndim > 2:
raise ValueError('{0}-d aperture array not supported. '
'Only 2-d arrays supported.'.format(apertures.ndim))
for aperture in apertures.ravel():
if not isinstance(aperture, Aperture):
raise TypeError("'aperture' must be an instance of Aperture.")
n_aper = apertures.shape[0]
# Check 'apertures' shape and expand trailing dimension to match N_obj
# if necessary.
if apertures.shape[1] not in [1, n_obj]:
raise ValueError("trailing dimension of 'apertures' must be 1 or "
"match length of xc, yc")
if apertures.shape[1] != n_obj:
# We will not use xc2d. This is just for broadcasting 'apertures':
> apertures, xc2d = np.broadcast_arrays(apertures, xc)
photutils/aperture.py:519:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = [array([[<photutils.aperture.CircularAnnulus object at 0x10fbf7650>]], dtype=object), array([ 2., 3., 4.])], _m = array([ 2., 3., 4.])
x = array([[<photutils.aperture.CircularAnnulus object at 0x10fbf7650>]], dtype=object), shapes = [[1, 3], [1, 3]]
def broadcast_arrays(*args):
"""
Broadcast any number of arrays against each other.
Parameters
----------
`*args` : array_likes
The arrays to broadcast.
Returns
-------
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
broadcasted array may refer to a single memory location. If you
need to write to the arrays, make copies first.
Examples
--------
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>> np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of
non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
"""
args = [np.asarray(_m) for _m in args]
shapes = [x.shape for x in args]
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return args
shapes = [list(s) for s in shapes]
strides = [list(x.strides) for x in args]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(args)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
strides[i] = [0] * diff + strides[i]
# Chech each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
common_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length. The common shape will take this
# value.
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
# For each array, if this axis is being broadcasted from a length of
# 1, then set its stride to 0 so that it repeats its data.
for i in range(len(args)):
if shapes[i][axis] == 1:
shapes[i][axis] = new_length
strides[i][axis] = 0
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
common_shape.append(1)
# Construct the new arrays.
broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
> zip(args, shapes, strides)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:119:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[<photutils.aperture.CircularAnnulus object at 0x10fbf7650>]], dtype=object), shape = [1, 3], strides = [8, 0]
def as_strided(x, shape=None, strides=None):
""" Make an ndarray from the given array with the given shape and strides.
"""
interface = dict(x.__array_interface__)
if shape is not None:
interface['shape'] = tuple(shape)
if strides is not None:
interface['strides'] = tuple(strides)
array = np.asarray(DummyArray(interface, base=x))
# Make sure dtype is correct in case of custom dtype
> array.dtype = x.dtype
E TypeError: Cannot change data-type for object array.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:32: TypeError
_____________________________________ TestBroadcastingCircularAnnulus.test_multiple_objects_multiple_aperture_per_object _____________________________________
self = <photutils.tests.test_aperture_broadcasting.TestBroadcastingCircularAnnulus object at 0x10fc10150>
def test_multiple_objects_multiple_aperture_per_object(self):
# Multiple objects, multiple apertures per object (same for each object)
flux = annulus_circular(self.data, [2., 3., 4.], [5., 6., 7.],
> [[2.], [5.]], [[5.], [8.]])
photutils/tests/test_aperture_broadcasting.py:86:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = [2.0, 3.0, 4.0], yc = [5.0, 6.0, 7.0], r_in = array([[ 2.],
[ 5.]]), r_out = array([[ 5.],
[ 8.]]), error = None, gain = None, mask = None
method = 'exact', subpixels = 5, pixelwise_errors = True
def annulus_circular(data, xc, yc, r_in, r_out, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within circular annuli.
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
r_in, r_out : float or array_like
The parameters of the annuli: respectively, the inner and
outer radii. If an array (of at most 2 dimensions), the
trailing dimension of the array must be broadcastable to
N_objects (= `len(xc)`). In other words, the trailing
dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options are ['center', 'subpixel', 'exact']. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
See Also
--------
aperture_photometry
"""
r_in, r_out = np.broadcast_arrays(r_in, r_out)
if r_in.ndim > 0:
apertures = np.empty(r_in.shape, dtype=object)
for index in np.ndindex(*r_in.shape):
apertures[index] = CircularAnnulus(r_in[index], r_out[index])
else:
apertures = CircularAnnulus(r_in, r_out)
return aperture_photometry(data, xc, yc, apertures, error=error,
gain=gain, mask=mask, subpixels=subpixels,
> pixelwise_errors=pixelwise_errors)
photutils/aperture.py:991:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = array([ 2., 3., 4.]), yc = array([ 5., 6., 7.])
apertures = array([[<photutils.aperture.CircularAnnulus object at 0x10fc10d10>],
[<photutils.aperture.CircularAnnulus object at 0x10fc10c90>]], dtype=object)
error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_photometry(data, xc, yc, apertures, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within aperture(s).
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
apertures : `Aperture` object or array of `Aperture` objects
The apertures to use for photometry. If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
Note that for subpixel sampling, the input array is only
resampled once for each object.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options include ['center', 'subpixel', 'exact'], but not all options
are available for all types of apertures. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
"""
# Check input array type and dimension.
data = np.asarray(data)
if np.iscomplexobj(data):
raise TypeError('Complex type not supported')
if data.ndim != 2:
raise ValueError('{0}-d array not supported. '
'Only 2-d arrays supported.'.format(data.ndim))
# Note whether xc, yc are scalars so we can try to return scalars later.
scalar_obj_centers = np.isscalar(xc) and np.isscalar(yc)
# Check shapes of xc, yc
xc = np.atleast_1d(xc)
yc = np.atleast_1d(yc)
if xc.ndim > 1 or yc.ndim > 1:
raise ValueError('Only 1-d arrays supported for object centers.')
if xc.shape[0] != yc.shape[0]:
raise ValueError('length of xc and yc must match')
n_obj = xc.shape[0]
# Check 'apertures' dimensions and type
apertures = np.atleast_2d(apertures)
if apertures.ndim > 2:
raise ValueError('{0}-d aperture array not supported. '
'Only 2-d arrays supported.'.format(apertures.ndim))
for aperture in apertures.ravel():
if not isinstance(aperture, Aperture):
raise TypeError("'aperture' must be an instance of Aperture.")
n_aper = apertures.shape[0]
# Check 'apertures' shape and expand trailing dimension to match N_obj
# if necessary.
if apertures.shape[1] not in [1, n_obj]:
raise ValueError("trailing dimension of 'apertures' must be 1 or "
"match length of xc, yc")
if apertures.shape[1] != n_obj:
# We will not use xc2d. This is just for broadcasting 'apertures':
> apertures, xc2d = np.broadcast_arrays(apertures, xc)
photutils/aperture.py:519:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = [array([[<photutils.aperture.CircularAnnulus object at 0x10fc10d10>],
[<photutils.aperture.CircularAnnulus object at 0x10fc10c90>]], dtype=object), array([ 2., 3., 4.])]
_m = array([ 2., 3., 4.])
x = array([[<photutils.aperture.CircularAnnulus object at 0x10fc10d10>],
[<photutils.aperture.CircularAnnulus object at 0x10fc10c90>]], dtype=object)
shapes = [[2, 3], [2, 3]]
def broadcast_arrays(*args):
"""
Broadcast any number of arrays against each other.
Parameters
----------
`*args` : array_likes
The arrays to broadcast.
Returns
-------
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
broadcasted array may refer to a single memory location. If you
need to write to the arrays, make copies first.
Examples
--------
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>> np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of
non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
"""
args = [np.asarray(_m) for _m in args]
shapes = [x.shape for x in args]
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return args
shapes = [list(s) for s in shapes]
strides = [list(x.strides) for x in args]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(args)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
strides[i] = [0] * diff + strides[i]
# Chech each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
common_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length. The common shape will take this
# value.
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
# For each array, if this axis is being broadcasted from a length of
# 1, then set its stride to 0 so that it repeats its data.
for i in range(len(args)):
if shapes[i][axis] == 1:
shapes[i][axis] = new_length
strides[i][axis] = 0
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
common_shape.append(1)
# Construct the new arrays.
broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
> zip(args, shapes, strides)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:119:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[<photutils.aperture.CircularAnnulus object at 0x10fc10d10>],
[<photutils.aperture.CircularAnnulus object at 0x10fc10c90>]], dtype=object)
shape = [2, 3], strides = [8, 0]
def as_strided(x, shape=None, strides=None):
""" Make an ndarray from the given array with the given shape and strides.
"""
interface = dict(x.__array_interface__)
if shape is not None:
interface['shape'] = tuple(shape)
if strides is not None:
interface['strides'] = tuple(strides)
array = np.asarray(DummyArray(interface, base=x))
# Make sure dtype is correct in case of custom dtype
> array.dtype = x.dtype
E TypeError: Cannot change data-type for object array.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:32: TypeError
___________________________________________ TestBroadcastingElliptical.test_multiple_objects_single_same_aperture ____________________________________________
self = <photutils.tests.test_aperture_broadcasting.TestBroadcastingElliptical object at 0x10fc276d0>
def test_multiple_objects_single_same_aperture(self):
# Multiple objects, single aperture (same for each object)
flux = aperture_elliptical(self.data, [2., 3., 4.], [5., 6., 7.],
> 2., 5., np.pi / 2.)
photutils/tests/test_aperture_broadcasting.py:116:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = [2.0, 3.0, 4.0], yc = [5.0, 6.0, 7.0], a = array(2.0), b = array(5.0), theta = array(1.5707963267948966), error = None, gain = None, mask = None
method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_elliptical(data, xc, yc, a, b, theta, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within elliptical apertures.
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
a, b, theta : float or array_like
The parameters of the aperture(s): respectively, the semimajor,
semiminor axes in pixels, and the position angle in radians
(measured counterclockwise). If an array (of at most 2 dimensions),
the trailing dimension of the array must be broadcastable to
N_objects (= `len(xc)`). In other words, the trailing dimension must
be equal to either 1 or N_objects. The following shapes are thus
allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options are ['center', 'subpixel', 'exact']. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
See Also
--------
aperture_photometry
"""
a, b, theta = np.broadcast_arrays(a, b, theta)
if a.ndim > 0:
apertures = np.empty(a.shape, dtype=object)
for index in np.ndindex(*a.shape):
apertures[index] = EllipticalAperture(a[index], b[index], theta[index])
else:
apertures = EllipticalAperture(a, b, theta)
return aperture_photometry(data, xc, yc, apertures, error=error,
gain=gain, mask=mask, subpixels=subpixels,
> pixelwise_errors=pixelwise_errors)
photutils/aperture.py:892:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = array([ 2., 3., 4.]), yc = array([ 5., 6., 7.]), apertures = array([[<photutils.aperture.EllipticalAperture object at 0x10fc27650>]], dtype=object)
error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_photometry(data, xc, yc, apertures, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within aperture(s).
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
apertures : `Aperture` object or array of `Aperture` objects
The apertures to use for photometry. If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
Note that for subpixel sampling, the input array is only
resampled once for each object.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options include ['center', 'subpixel', 'exact'], but not all options
are available for all types of apertures. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
"""
# Check input array type and dimension.
data = np.asarray(data)
if np.iscomplexobj(data):
raise TypeError('Complex type not supported')
if data.ndim != 2:
raise ValueError('{0}-d array not supported. '
'Only 2-d arrays supported.'.format(data.ndim))
# Note whether xc, yc are scalars so we can try to return scalars later.
scalar_obj_centers = np.isscalar(xc) and np.isscalar(yc)
# Check shapes of xc, yc
xc = np.atleast_1d(xc)
yc = np.atleast_1d(yc)
if xc.ndim > 1 or yc.ndim > 1:
raise ValueError('Only 1-d arrays supported for object centers.')
if xc.shape[0] != yc.shape[0]:
raise ValueError('length of xc and yc must match')
n_obj = xc.shape[0]
# Check 'apertures' dimensions and type
apertures = np.atleast_2d(apertures)
if apertures.ndim > 2:
raise ValueError('{0}-d aperture array not supported. '
'Only 2-d arrays supported.'.format(apertures.ndim))
for aperture in apertures.ravel():
if not isinstance(aperture, Aperture):
raise TypeError("'aperture' must be an instance of Aperture.")
n_aper = apertures.shape[0]
# Check 'apertures' shape and expand trailing dimension to match N_obj
# if necessary.
if apertures.shape[1] not in [1, n_obj]:
raise ValueError("trailing dimension of 'apertures' must be 1 or "
"match length of xc, yc")
if apertures.shape[1] != n_obj:
# We will not use xc2d. This is just for broadcasting 'apertures':
> apertures, xc2d = np.broadcast_arrays(apertures, xc)
photutils/aperture.py:519:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = [array([[<photutils.aperture.EllipticalAperture object at 0x10fc27650>]], dtype=object), array([ 2., 3., 4.])], _m = array([ 2., 3., 4.])
x = array([[<photutils.aperture.EllipticalAperture object at 0x10fc27650>]], dtype=object), shapes = [[1, 3], [1, 3]]
def broadcast_arrays(*args):
"""
Broadcast any number of arrays against each other.
Parameters
----------
`*args` : array_likes
The arrays to broadcast.
Returns
-------
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
broadcasted array may refer to a single memory location. If you
need to write to the arrays, make copies first.
Examples
--------
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>> np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of
non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
"""
args = [np.asarray(_m) for _m in args]
shapes = [x.shape for x in args]
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return args
shapes = [list(s) for s in shapes]
strides = [list(x.strides) for x in args]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(args)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
strides[i] = [0] * diff + strides[i]
# Chech each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
common_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length. The common shape will take this
# value.
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
# For each array, if this axis is being broadcasted from a length of
# 1, then set its stride to 0 so that it repeats its data.
for i in range(len(args)):
if shapes[i][axis] == 1:
shapes[i][axis] = new_length
strides[i][axis] = 0
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
common_shape.append(1)
# Construct the new arrays.
broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
> zip(args, shapes, strides)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:119:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[<photutils.aperture.EllipticalAperture object at 0x10fc27650>]], dtype=object), shape = [1, 3], strides = [8, 0]
def as_strided(x, shape=None, strides=None):
""" Make an ndarray from the given array with the given shape and strides.
"""
interface = dict(x.__array_interface__)
if shape is not None:
interface['shape'] = tuple(shape)
if strides is not None:
interface['strides'] = tuple(strides)
array = np.asarray(DummyArray(interface, base=x))
# Make sure dtype is correct in case of custom dtype
> array.dtype = x.dtype
E TypeError: Cannot change data-type for object array.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:32: TypeError
_______________________________________ TestBroadcastingElliptical.test_multiple_objects_multiple_aperture_per_object ________________________________________
self = <photutils.tests.test_aperture_broadcasting.TestBroadcastingElliptical object at 0x10fd62650>
def test_multiple_objects_multiple_aperture_per_object(self):
# Multiple objects, multiple apertures per object (same for each object)
flux = aperture_elliptical(self.data, [2., 3., 4.], [5., 6., 7.],
> [[2.], [5.]], [[5.], [8.]], np.pi / 2.)
photutils/tests/test_aperture_broadcasting.py:128:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = [2.0, 3.0, 4.0], yc = [5.0, 6.0, 7.0], a = array([[ 2.],
[ 5.]]), b = array([[ 5.],
[ 8.]])
theta = array([[ 1.57079633],
[ 1.57079633]]), error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_elliptical(data, xc, yc, a, b, theta, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within elliptical apertures.
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
a, b, theta : float or array_like
The parameters of the aperture(s): respectively, the semimajor,
semiminor axes in pixels, and the position angle in radians
(measured counterclockwise). If an array (of at most 2 dimensions),
the trailing dimension of the array must be broadcastable to
N_objects (= `len(xc)`). In other words, the trailing dimension must
be equal to either 1 or N_objects. The following shapes are thus
allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options are ['center', 'subpixel', 'exact']. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
See Also
--------
aperture_photometry
"""
a, b, theta = np.broadcast_arrays(a, b, theta)
if a.ndim > 0:
apertures = np.empty(a.shape, dtype=object)
for index in np.ndindex(*a.shape):
apertures[index] = EllipticalAperture(a[index], b[index], theta[index])
else:
apertures = EllipticalAperture(a, b, theta)
return aperture_photometry(data, xc, yc, apertures, error=error,
gain=gain, mask=mask, subpixels=subpixels,
> pixelwise_errors=pixelwise_errors)
photutils/aperture.py:892:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = array([ 2., 3., 4.]), yc = array([ 5., 6., 7.])
apertures = array([[<photutils.aperture.EllipticalAperture object at 0x10fd62750>],
[<photutils.aperture.EllipticalAperture object at 0x10fd62790>]], dtype=object)
error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_photometry(data, xc, yc, apertures, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within aperture(s).
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
apertures : `Aperture` object or array of `Aperture` objects
The apertures to use for photometry. If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
Note that for subpixel sampling, the input array is only
resampled once for each object.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options include ['center', 'subpixel', 'exact'], but not all options
are available for all types of apertures. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
"""
# Check input array type and dimension.
data = np.asarray(data)
if np.iscomplexobj(data):
raise TypeError('Complex type not supported')
if data.ndim != 2:
raise ValueError('{0}-d array not supported. '
'Only 2-d arrays supported.'.format(data.ndim))
# Note whether xc, yc are scalars so we can try to return scalars later.
scalar_obj_centers = np.isscalar(xc) and np.isscalar(yc)
# Check shapes of xc, yc
xc = np.atleast_1d(xc)
yc = np.atleast_1d(yc)
if xc.ndim > 1 or yc.ndim > 1:
raise ValueError('Only 1-d arrays supported for object centers.')
if xc.shape[0] != yc.shape[0]:
raise ValueError('length of xc and yc must match')
n_obj = xc.shape[0]
# Check 'apertures' dimensions and type
apertures = np.atleast_2d(apertures)
if apertures.ndim > 2:
raise ValueError('{0}-d aperture array not supported. '
'Only 2-d arrays supported.'.format(apertures.ndim))
for aperture in apertures.ravel():
if not isinstance(aperture, Aperture):
raise TypeError("'aperture' must be an instance of Aperture.")
n_aper = apertures.shape[0]
# Check 'apertures' shape and expand trailing dimension to match N_obj
# if necessary.
if apertures.shape[1] not in [1, n_obj]:
raise ValueError("trailing dimension of 'apertures' must be 1 or "
"match length of xc, yc")
if apertures.shape[1] != n_obj:
# We will not use xc2d. This is just for broadcasting 'apertures':
> apertures, xc2d = np.broadcast_arrays(apertures, xc)
photutils/aperture.py:519:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = [array([[<photutils.aperture.EllipticalAperture object at 0x10fd62750>],
[<photutils.aperture.EllipticalAperture object at 0x10fd62790>]], dtype=object), array([ 2., 3., 4.])]
_m = array([ 2., 3., 4.])
x = array([[<photutils.aperture.EllipticalAperture object at 0x10fd62750>],
[<photutils.aperture.EllipticalAperture object at 0x10fd62790>]], dtype=object)
shapes = [[2, 3], [2, 3]]
def broadcast_arrays(*args):
"""
Broadcast any number of arrays against each other.
Parameters
----------
`*args` : array_likes
The arrays to broadcast.
Returns
-------
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
broadcasted array may refer to a single memory location. If you
need to write to the arrays, make copies first.
Examples
--------
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>> np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of
non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
"""
args = [np.asarray(_m) for _m in args]
shapes = [x.shape for x in args]
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return args
shapes = [list(s) for s in shapes]
strides = [list(x.strides) for x in args]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(args)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
strides[i] = [0] * diff + strides[i]
# Chech each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
common_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length. The common shape will take this
# value.
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
# For each array, if this axis is being broadcasted from a length of
# 1, then set its stride to 0 so that it repeats its data.
for i in range(len(args)):
if shapes[i][axis] == 1:
shapes[i][axis] = new_length
strides[i][axis] = 0
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
common_shape.append(1)
# Construct the new arrays.
broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
> zip(args, shapes, strides)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:119:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[<photutils.aperture.EllipticalAperture object at 0x10fd62750>],
[<photutils.aperture.EllipticalAperture object at 0x10fd62790>]], dtype=object)
shape = [2, 3], strides = [8, 0]
def as_strided(x, shape=None, strides=None):
""" Make an ndarray from the given array with the given shape and strides.
"""
interface = dict(x.__array_interface__)
if shape is not None:
interface['shape'] = tuple(shape)
if strides is not None:
interface['strides'] = tuple(strides)
array = np.asarray(DummyArray(interface, base=x))
# Make sure dtype is correct in case of custom dtype
> array.dtype = x.dtype
E TypeError: Cannot change data-type for object array.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:32: TypeError
________________________________________ TestBroadcastingEllipticalAnnulus.test_multiple_objects_single_same_aperture ________________________________________
self = <photutils.tests.test_aperture_broadcasting.TestBroadcastingEllipticalAnnulus object at 0x10fc26490>
def test_multiple_objects_single_same_aperture(self):
# Multiple objects, single aperture (same for each object)
flux = annulus_elliptical(self.data, [2., 3., 4.], [5., 6., 7.],
> 2., 5., 5., np.pi / 2.)
photutils/tests/test_aperture_broadcasting.py:158:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = [2.0, 3.0, 4.0], yc = [5.0, 6.0, 7.0], a_in = array(2.0), a_out = array(5.0), b_out = array(5.0), theta = array(1.5707963267948966), error = None
gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def annulus_elliptical(data, xc, yc, a_in, a_out, b_out, theta,
error=None, gain=None, mask=None, method='exact',
subpixels=5, pixelwise_errors=True):
r"""Sum flux within elliptical annuli.
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
a_in, a_out, b_out, theta : float or array_like
The parameters of the annuli: respectively, the inner and outer
semimajor axis in pixels, the outer semiminor axis in pixels, and
the position angle in radians (measured counterclockwise). If an
array (of at most 2 dimensions), the trailing dimension of the array
must be broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options are ['center', 'subpixel', 'exact']. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
See Also
--------
aperture_photometry
"""
a_in, a_out, b_out, theta = \
np.broadcast_arrays(a_in, a_out, b_out, theta)
if a_in.ndim > 0:
apertures = np.empty(a_in.shape, dtype=object)
for index in np.ndindex(*a_in.shape):
apertures[index] = EllipticalAnnulus(a_in[index], a_out[index],
b_out[index], theta[index])
else:
apertures = EllipticalAnnulus(a_in, a_out, b_out, theta)
return aperture_photometry(data, xc, yc, apertures, error=error,
gain=gain, mask=mask, subpixels=subpixels,
> pixelwise_errors=pixelwise_errors)
photutils/aperture.py:1093:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = array([ 2., 3., 4.]), yc = array([ 5., 6., 7.]), apertures = array([[<photutils.aperture.EllipticalAnnulus object at 0x10fc26410>]], dtype=object)
error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_photometry(data, xc, yc, apertures, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within aperture(s).
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
apertures : `Aperture` object or array of `Aperture` objects
The apertures to use for photometry. If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
Note that for subpixel sampling, the input array is only
resampled once for each object.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options include ['center', 'subpixel', 'exact'], but not all options
are available for all types of apertures. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
"""
# Check input array type and dimension.
data = np.asarray(data)
if np.iscomplexobj(data):
raise TypeError('Complex type not supported')
if data.ndim != 2:
raise ValueError('{0}-d array not supported. '
'Only 2-d arrays supported.'.format(data.ndim))
# Note whether xc, yc are scalars so we can try to return scalars later.
scalar_obj_centers = np.isscalar(xc) and np.isscalar(yc)
# Check shapes of xc, yc
xc = np.atleast_1d(xc)
yc = np.atleast_1d(yc)
if xc.ndim > 1 or yc.ndim > 1:
raise ValueError('Only 1-d arrays supported for object centers.')
if xc.shape[0] != yc.shape[0]:
raise ValueError('length of xc and yc must match')
n_obj = xc.shape[0]
# Check 'apertures' dimensions and type
apertures = np.atleast_2d(apertures)
if apertures.ndim > 2:
raise ValueError('{0}-d aperture array not supported. '
'Only 2-d arrays supported.'.format(apertures.ndim))
for aperture in apertures.ravel():
if not isinstance(aperture, Aperture):
raise TypeError("'aperture' must be an instance of Aperture.")
n_aper = apertures.shape[0]
# Check 'apertures' shape and expand trailing dimension to match N_obj
# if necessary.
if apertures.shape[1] not in [1, n_obj]:
raise ValueError("trailing dimension of 'apertures' must be 1 or "
"match length of xc, yc")
if apertures.shape[1] != n_obj:
# We will not use xc2d. This is just for broadcasting 'apertures':
> apertures, xc2d = np.broadcast_arrays(apertures, xc)
photutils/aperture.py:519:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = [array([[<photutils.aperture.EllipticalAnnulus object at 0x10fc26410>]], dtype=object), array([ 2., 3., 4.])], _m = array([ 2., 3., 4.])
x = array([[<photutils.aperture.EllipticalAnnulus object at 0x10fc26410>]], dtype=object), shapes = [[1, 3], [1, 3]]
def broadcast_arrays(*args):
"""
Broadcast any number of arrays against each other.
Parameters
----------
`*args` : array_likes
The arrays to broadcast.
Returns
-------
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
broadcasted array may refer to a single memory location. If you
need to write to the arrays, make copies first.
Examples
--------
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>> np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of
non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
"""
args = [np.asarray(_m) for _m in args]
shapes = [x.shape for x in args]
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return args
shapes = [list(s) for s in shapes]
strides = [list(x.strides) for x in args]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(args)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
strides[i] = [0] * diff + strides[i]
# Chech each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
common_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length. The common shape will take this
# value.
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
# For each array, if this axis is being broadcasted from a length of
# 1, then set its stride to 0 so that it repeats its data.
for i in range(len(args)):
if shapes[i][axis] == 1:
shapes[i][axis] = new_length
strides[i][axis] = 0
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
common_shape.append(1)
# Construct the new arrays.
broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
> zip(args, shapes, strides)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:119:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[<photutils.aperture.EllipticalAnnulus object at 0x10fc26410>]], dtype=object), shape = [1, 3], strides = [8, 0]
def as_strided(x, shape=None, strides=None):
""" Make an ndarray from the given array with the given shape and strides.
"""
interface = dict(x.__array_interface__)
if shape is not None:
interface['shape'] = tuple(shape)
if strides is not None:
interface['strides'] = tuple(strides)
array = np.asarray(DummyArray(interface, base=x))
# Make sure dtype is correct in case of custom dtype
> array.dtype = x.dtype
E TypeError: Cannot change data-type for object array.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:32: TypeError
____________________________________ TestBroadcastingEllipticalAnnulus.test_multiple_objects_multiple_aperture_per_object ____________________________________
self = <photutils.tests.test_aperture_broadcasting.TestBroadcastingEllipticalAnnulus object at 0x10f6e69d0>
def test_multiple_objects_multiple_aperture_per_object(self):
# Multiple objects, multiple apertures per object (same for each object)
flux = annulus_elliptical(self.data, [2., 3., 4.], [5., 6., 7.],
> [[2.], [5.]], [[5.], [8.]], [[5.], [8.]], np.pi / 2.)
photutils/tests/test_aperture_broadcasting.py:170:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = [2.0, 3.0, 4.0], yc = [5.0, 6.0, 7.0], a_in = array([[ 2.],
[ 5.]]), a_out = array([[ 5.],
[ 8.]]), b_out = array([[ 5.],
[ 8.]])
theta = array([[ 1.57079633],
[ 1.57079633]]), error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def annulus_elliptical(data, xc, yc, a_in, a_out, b_out, theta,
error=None, gain=None, mask=None, method='exact',
subpixels=5, pixelwise_errors=True):
r"""Sum flux within elliptical annuli.
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
a_in, a_out, b_out, theta : float or array_like
The parameters of the annuli: respectively, the inner and outer
semimajor axis in pixels, the outer semiminor axis in pixels, and
the position angle in radians (measured counterclockwise). If an
array (of at most 2 dimensions), the trailing dimension of the array
must be broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options are ['center', 'subpixel', 'exact']. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
See Also
--------
aperture_photometry
"""
a_in, a_out, b_out, theta = \
np.broadcast_arrays(a_in, a_out, b_out, theta)
if a_in.ndim > 0:
apertures = np.empty(a_in.shape, dtype=object)
for index in np.ndindex(*a_in.shape):
apertures[index] = EllipticalAnnulus(a_in[index], a_out[index],
b_out[index], theta[index])
else:
apertures = EllipticalAnnulus(a_in, a_out, b_out, theta)
return aperture_photometry(data, xc, yc, apertures, error=error,
gain=gain, mask=mask, subpixels=subpixels,
> pixelwise_errors=pixelwise_errors)
photutils/aperture.py:1093:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
data = array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 1., ...1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
xc = array([ 2., 3., 4.]), yc = array([ 5., 6., 7.])
apertures = array([[<photutils.aperture.EllipticalAnnulus object at 0x10fd98f50>],
[<photutils.aperture.EllipticalAnnulus object at 0x10fd98f90>]], dtype=object)
error = None, gain = None, mask = None, method = 'exact', subpixels = 5, pixelwise_errors = True
def aperture_photometry(data, xc, yc, apertures, error=None, gain=None,
mask=None, method='exact', subpixels=5,
pixelwise_errors=True):
r"""Sum flux within aperture(s).
Multiple objects and multiple apertures per object can be specified.
Parameters
----------
data : array_like
The 2-d array on which to perform photometry.
xc, yc : float or list_like
The x and y coordinates of the object center(s). If list_like,
the lengths must match.
apertures : `Aperture` object or array of `Aperture` objects
The apertures to use for photometry. If an array (of at most 2
dimensions), the trailing dimension of the array must be
broadcastable to N_objects (= `len(xc)`). In other words,
the trailing dimension must be equal to either 1 or N_objects. The
following shapes are thus allowed:
`()` or `(1,)` or `(1, 1)`
The same single aperture is applied to all objects.
`(N_objects,)` or `(1, N_objects)`
Each object gets its own single aperture.
`(N_apertures, 1)`
The same `N_aper` apertures are applied to all objects.
`(N_apertures, N_objects)`
Each object gets its own set of N_apertures apertures.
Note that for subpixel sampling, the input array is only
resampled once for each object.
error : float or array_like, optional
Error in each pixel, interpreted as Gaussian 1-sigma uncertainty.
gain : float or array_like, optional
Ratio of counts (e.g., electrons or photons) to units of the data
(e.g., ADU), for the purpose of calculating Poisson error from the
object itself. If `gain` is `None` (default), `error` is assumed to
include all uncertainty in each pixel. If `gain` is given, `error`
is assumed to be the "background error" only (not accounting for
Poisson error in the flux in the apertures).
mask : array_like (bool), optional
Mask to apply to the data. The value of masked pixels are replaced
by the value of the pixel mirrored across the center of the object,
if available. If unavailable, the value is set to zero.
method : str, optional
Method to use for determining overlap between the aperture and pixels.
Options include ['center', 'subpixel', 'exact'], but not all options
are available for all types of apertures. More precise methods will
generally be slower.
'center'
A pixel is considered to be entirely in or out of the aperture
depending on whether its center is in or out of the aperture.
'subpixel'
A pixel is divided into subpixels and the center of each subpixel
is tested (as above). With `subpixels` set to 1, this method is
equivalent to 'center'.
'exact'
The exact overlap between the aperture and each pixel is
calculated.
subpixels : int, optional
For the 'subpixel' method, resample pixels by this factor (in
each dimension). That is, each pixel is divided into
`subpixels ** 2` subpixels.
pixelwise_errors : bool, optional
For error and/or gain arrays. If True, assume error and/or gain
vary significantly within an aperture: sum contribution from each
pixel. If False, assume error and gain do not vary significantly
within an aperture. Use the single value of error and/or gain at
the center of each aperture as the value for the entire aperture.
Default is True.
Returns
-------
flux : float or `~numpy.ndarray`
Enclosed flux in aperture(s). If `xc` and `yc` are floats and
there is a single aperture, a float is returned. If xc, yc are
list_like and there is a single aperture per object, a 1-d
array is returned. If there are multiple apertures per object,
a 2-d array is returned.
fluxerr : float or `~numpy.ndarray`
Uncertainty in flux values. Only returned if error is not `None`.
"""
# Check input array type and dimension.
data = np.asarray(data)
if np.iscomplexobj(data):
raise TypeError('Complex type not supported')
if data.ndim != 2:
raise ValueError('{0}-d array not supported. '
'Only 2-d arrays supported.'.format(data.ndim))
# Note whether xc, yc are scalars so we can try to return scalars later.
scalar_obj_centers = np.isscalar(xc) and np.isscalar(yc)
# Check shapes of xc, yc
xc = np.atleast_1d(xc)
yc = np.atleast_1d(yc)
if xc.ndim > 1 or yc.ndim > 1:
raise ValueError('Only 1-d arrays supported for object centers.')
if xc.shape[0] != yc.shape[0]:
raise ValueError('length of xc and yc must match')
n_obj = xc.shape[0]
# Check 'apertures' dimensions and type
apertures = np.atleast_2d(apertures)
if apertures.ndim > 2:
raise ValueError('{0}-d aperture array not supported. '
'Only 2-d arrays supported.'.format(apertures.ndim))
for aperture in apertures.ravel():
if not isinstance(aperture, Aperture):
raise TypeError("'aperture' must be an instance of Aperture.")
n_aper = apertures.shape[0]
# Check 'apertures' shape and expand trailing dimension to match N_obj
# if necessary.
if apertures.shape[1] not in [1, n_obj]:
raise ValueError("trailing dimension of 'apertures' must be 1 or "
"match length of xc, yc")
if apertures.shape[1] != n_obj:
# We will not use xc2d. This is just for broadcasting 'apertures':
> apertures, xc2d = np.broadcast_arrays(apertures, xc)
photutils/aperture.py:519:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = [array([[<photutils.aperture.EllipticalAnnulus object at 0x10fd98f50>],
[<photutils.aperture.EllipticalAnnulus object at 0x10fd98f90>]], dtype=object), array([ 2., 3., 4.])]
_m = array([ 2., 3., 4.])
x = array([[<photutils.aperture.EllipticalAnnulus object at 0x10fd98f50>],
[<photutils.aperture.EllipticalAnnulus object at 0x10fd98f90>]], dtype=object)
shapes = [[2, 3], [2, 3]]
def broadcast_arrays(*args):
"""
Broadcast any number of arrays against each other.
Parameters
----------
`*args` : array_likes
The arrays to broadcast.
Returns
-------
broadcasted : list of arrays
These arrays are views on the original arrays. They are typically
not contiguous. Furthermore, more than one element of a
broadcasted array may refer to a single memory location. If you
need to write to the arrays, make copies first.
Examples
--------
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>> np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of
non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
"""
args = [np.asarray(_m) for _m in args]
shapes = [x.shape for x in args]
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return args
shapes = [list(s) for s in shapes]
strides = [list(x.strides) for x in args]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(args)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
strides[i] = [0] * diff + strides[i]
# Chech each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
common_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length. The common shape will take this
# value.
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
# For each array, if this axis is being broadcasted from a length of
# 1, then set its stride to 0 so that it repeats its data.
for i in range(len(args)):
if shapes[i][axis] == 1:
shapes[i][axis] = new_length
strides[i][axis] = 0
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
common_shape.append(1)
# Construct the new arrays.
broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
> zip(args, shapes, strides)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:119:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[<photutils.aperture.EllipticalAnnulus object at 0x10fd98f50>],
[<photutils.aperture.EllipticalAnnulus object at 0x10fd98f90>]], dtype=object)
shape = [2, 3], strides = [8, 0]
def as_strided(x, shape=None, strides=None):
""" Make an ndarray from the given array with the given shape and strides.
"""
interface = dict(x.__array_interface__)
if shape is not None:
interface['shape'] = tuple(shape)
if strides is not None:
interface['strides'] = tuple(strides)
array = np.asarray(DummyArray(interface, base=x))
# Make sure dtype is correct in case of custom dtype
> array.dtype = x.dtype
E TypeError: Cannot change data-type for object array.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py:32: TypeError
============================================================ 8 failed, 53 passed in 10.68 seconds ============================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment