11# Licensed under a 3-clause BSD style license - see LICENSE.rst
22
3+ import functools
34import warnings
45
56import numpy as np
1213from astropy .utils .compat .optional_deps import HAS_BOTTLENECK
1314from astropy .utils .exceptions import AstropyUserWarning
1415
15- if HAS_BOTTLENECK :
16- import bottleneck
17-
1816if NUMPY_LT_2_0 :
1917 from numpy .core .multiarray import normalize_axis_index
2018else :
2321__all__ = ["SigmaClip" , "sigma_clip" , "sigma_clipped_stats" ]
2422
2523
26- def _move_tuple_axes_first (array , axis ):
27- """
28- Bottleneck can only take integer axis, not tuple, so this function
29- takes all the axes to be operated on and combines them into the
30- first dimension of the array so that we can then use axis=0.
31- """
32- # Figure out how many axes we are operating over
33- naxis = len (axis )
34-
35- # Add remaining axes to the axis tuple
36- axis += tuple (i for i in range (array .ndim ) if i not in axis )
37-
38- # The new position of each axis is just in order
39- destination = tuple (range (array .ndim ))
40-
41- # Reorder the array so that the axes being operated on are at the
42- # beginning
43- array_new = np .moveaxis (array , axis , destination )
44-
45- # Collapse the dimensions being operated on into a single dimension
46- # so that we can then use axis=0 with the bottleneck functions
47- array_new = array_new .reshape ((- 1 ,) + array_new .shape [naxis :])
24+ if HAS_BOTTLENECK :
25+ import bottleneck
4826
49- return array_new
27+ def _move_tuple_axes_first (array , axis ):
28+ """
29+ Bottleneck can only take integer axis, not tuple, so this function
30+ takes all the axes to be operated on and combines them into the
31+ first dimension of the array so that we can then use axis=0.
32+ """
33+ # Figure out how many axes we are operating over
34+ naxis = len (axis )
5035
36+ # Add remaining axes to the axis tuple
37+ axis += tuple (i for i in range (array .ndim ) if i not in axis )
5138
52- def _nanmean (array , axis = None ):
53- """Bottleneck nanmean function that handle tuple axis."""
54- if isinstance (axis , tuple ):
55- array = _move_tuple_axes_first (array , axis = axis )
56- axis = 0
39+ # The new position of each axis is just in order
40+ destination = tuple (range (array .ndim ))
5741
58- if isinstance (array , Quantity ):
59- return array .__array_wrap__ (bottleneck .nanmean (array , axis = axis ))
60- else :
61- return bottleneck .nanmean (array , axis = axis )
42+ # Reorder the array so that the axes being operated on are at the
43+ # beginning
44+ array_new = np .moveaxis (array , axis , destination )
6245
46+ # Collapse the dimensions being operated on into a single dimension
47+ # so that we can then use axis=0 with the bottleneck functions
48+ array_new = array_new .reshape ((- 1 ,) + array_new .shape [naxis :])
6349
64- def _nanmedian (array , axis = None ):
65- """Bottleneck nanmedian function that handle tuple axis."""
66- if isinstance (axis , tuple ):
67- array = _move_tuple_axes_first (array , axis = axis )
68- axis = 0
50+ return array_new
6951
70- if isinstance (array , Quantity ):
71- return array .__array_wrap__ (bottleneck .nanmedian (array , axis = axis ))
72- else :
73- return bottleneck .nanmedian (array , axis = axis )
52+ def _apply_bottleneck (function , array , axis = None , ** kwargs ):
53+ """Wrap bottleneck function to handle tuple axis.
7454
55+ Also takes care to ensure the output is of the expected type,
56+ i.e., a quantity, numpy array, or numpy scalar.
57+ """
58+ if isinstance (axis , tuple ):
59+ array = _move_tuple_axes_first (array , axis = axis )
60+ axis = 0
61+
62+ result = function (array , axis = axis , ** kwargs )
63+ if isinstance (array , Quantity ):
64+ return array .__array_wrap__ (result )
65+ elif isinstance (result , float ):
66+ # For compatibility with numpy, always return a numpy scalar.
67+ return np .float64 (result )
68+ else :
69+ return result
7570
76- def _nanstd (array , axis = None , ddof = 0 ):
77- """Bottleneck nanstd function that handle tuple axis."""
78- if isinstance (axis , tuple ):
79- array = _move_tuple_axes_first (array , axis = axis )
80- axis = 0
71+ _nanmean = functools .partial (_apply_bottleneck , bottleneck .nanmean )
72+ _nanmedian = functools .partial (_apply_bottleneck , bottleneck .nanmedian )
73+ _nanstd = functools .partial (_apply_bottleneck , bottleneck .nanstd )
8174
82- if isinstance ( array , Quantity ) :
83- return array . __array_wrap__ ( bottleneck . nanstd ( array , axis = axis , ddof = ddof ))
84- else :
85- return bottleneck .nanstd ( array , axis = axis , ddof = ddof )
75+ else :
76+ _nanmean = np . nanmean
77+ _nanmedian = np . nanmedian
78+ _nanstd = np .nanstd
8679
8780
8881def _nanmadstd (array , axis = None ):
@@ -279,16 +272,10 @@ def __str__(self):
279272 def _parse_cenfunc (cenfunc ):
280273 if isinstance (cenfunc , str ):
281274 if cenfunc == "median" :
282- if HAS_BOTTLENECK :
283- cenfunc = _nanmedian
284- else :
285- cenfunc = np .nanmedian # pragma: no cover
275+ cenfunc = _nanmedian
286276
287277 elif cenfunc == "mean" :
288- if HAS_BOTTLENECK :
289- cenfunc = _nanmean
290- else :
291- cenfunc = np .nanmean # pragma: no cover
278+ cenfunc = _nanmean
292279
293280 else :
294281 raise ValueError (f"{ cenfunc } is an invalid cenfunc." )
@@ -299,10 +286,7 @@ def _parse_cenfunc(cenfunc):
299286 def _parse_stdfunc (stdfunc ):
300287 if isinstance (stdfunc , str ):
301288 if stdfunc == "std" :
302- if HAS_BOTTLENECK :
303- stdfunc = _nanstd
304- else :
305- stdfunc = np .nanstd # pragma: no cover
289+ stdfunc = _nanstd
306290 elif stdfunc == "mad_std" :
307291 stdfunc = _nanmadstd
308292 else :
@@ -316,15 +300,7 @@ def _compute_bounds(self, data, axis=None):
316300 with warnings .catch_warnings ():
317301 warnings .simplefilter ("ignore" , category = RuntimeWarning )
318302 cen = self ._cenfunc_parsed (data , axis = axis )
319- if np .isscalar (cen ) and not isinstance (cen , np .float64 ):
320- # change bottleneck float scalar to np.float64
321- cen = np .float64 (cen )
322-
323303 std = self ._stdfunc_parsed (data , axis = axis )
324- if np .isscalar (std ) and not isinstance (std , np .float64 ):
325- # change bottleneck float scalar to np.float64
326- std = np .float64 (std )
327-
328304 self ._min_value = cen - (std * self .sigma_lower )
329305 self ._max_value = cen + (std * self .sigma_upper )
330306
@@ -1030,13 +1006,8 @@ def sigma_clipped_stats(
10301006 data , axis = axis , masked = False , return_bounds = False , copy = True
10311007 )
10321008
1033- if HAS_BOTTLENECK :
1034- mean = _nanmean (data_clipped , axis = axis )
1035- median = _nanmedian (data_clipped , axis = axis )
1036- std = _nanstd (data_clipped , ddof = std_ddof , axis = axis )
1037- else : # pragma: no cover
1038- mean = np .nanmean (data_clipped , axis = axis )
1039- median = np .nanmedian (data_clipped , axis = axis )
1040- std = np .nanstd (data_clipped , ddof = std_ddof , axis = axis )
1009+ mean = _nanmean (data_clipped , axis = axis )
1010+ median = _nanmedian (data_clipped , axis = axis )
1011+ std = _nanstd (data_clipped , ddof = std_ddof , axis = axis )
10411012
10421013 return mean , median , std
0 commit comments