Performance Tips#

Bottleneck#

The optional Bottleneck package provides fast, NaN-aware replacements for NumPy’s nansum, nanmin, nanmax, nanmean, nanmedian, nanstd, and nanvar functions. When Bottleneck is installed, Photutils will automatically use it for these operations, improving performance for any workflow that computes statistics on arrays containing NaN values (e.g., masked pixels).

Bottleneck acceleration is used internally by the following Photutils packages:

Note

Due to known accuracy issues in Bottleneck with float32 arrays (see bottleneck #379 and bottleneck #462), Photutils uses Bottleneck only for float64 arrays and falls back to NumPy for other dtypes.

To install Bottleneck:

python -m pip install bottleneck

Array Byte Order (Endianness)#

Bottleneck requires that the byte order of the input data array matches the native byte order of the operating system (typically little-endian on modern processors). Arrays loaded by astropy.io.fits are stored as big-endian. If the byte order does not match, Bottleneck will not be used and the code will fall back to NumPy.

You can convert a big-endian FITS array to native byte order in place, without allocating additional memory, using:

>>> data.byteswap(inplace=True)
>>> data.dtype = data.dtype.newbyteorder('=')

Alternatively, you can create a native-endian copy with:

>>> data = data.astype(float)

The first approach is preferred for large arrays because it avoids allocating a temporary copy of the entire array.