
import numpy as np
from scipy.stats import binned_statistic


def pspec(psd2, nbins=None, return_stddev=False, binsize=1.0,
          logspacing=True, max_bin=None, min_bin=None):
    '''
    Calculate the radial profile using scipy.stats.binned_statistic.

    Parameters
    ----------
    psd2 : np.ndarray
        2D Spectral power density.
    nbins : int, optional
        Number of bins to use. If None, it is calculated based on the size
        of the given arrays.
    return_stddev : bool, optional
        Return the standard deviations in each bin.
    binsize : float, optional
        Size of bins to be used. If logspacing is enabled, this will increase
        the number of bins used by the inverse of the given binsize.
    logspacing : bool, optional
        Use logarithmically spaces bins.
    max_bin : float, optional
        Give the maximum value to bin to.
    min_bin : float, optional
        Give the minimum value to bin to.

    Returns
    -------
    bins_cents : np.ndarray
        Centre of the bins.
    ps1D : np.ndarray
        1D binned power spectrum.
    ps1D_stddev : np.ndarray
        Returned when return_stddev is enabled. Standard deviations
        within each of the bins.
    '''

    yfreqs = np.fft.fftshift(np.abs(np.fft.fftfreq(psd2.shape[0])))
    xfreqs = np.fft.fftshift(np.abs(np.fft.fftfreq(psd2.shape[1])))

    yy_freq, xx_freq = np.meshgrid(yfreqs, xfreqs)

    freqs_dist = np.sqrt(yy_freq**2 + xx_freq**2)

    zero_freq_val = freqs_dist[np.nonzero(freqs_dist)].min() / 2.
    freqs_dist[freqs_dist == 0] = zero_freq_val

    y = np.arange(psd2.shape[0] - np.floor(psd2.shape[0]/2.).astype(int))
    x = np.arange(psd2.shape[1] - np.floor(psd2.shape[1]/2.).astype(int))

    yy, xx = np.meshgrid(y, x)

    dists = np.sqrt(yy**2 + xx**2)

    if nbins is None:
        nbins = int(np.round(dists.max() / binsize)+1)

    if max_bin is None:
        max_bin = freqs_dist.flatten()[np.argmax(dists)]

    if min_bin is None:
        min_bin = zero_freq_val

    if logspacing:
        bins = np.logspace(np.log10(min_bin), np.log10(max_bin), nbins+1)
    else:
        bins = np.linspace(min_bin, max_bin, nbins+1)

    ps1D, bin_edge, cts = binned_statistic(freqs_dist.ravel(),
                                           psd2.ravel(),
                                           bins=bins,
                                           statistic=np.nanmean)

    bin_cents = (bin_edge[1:] + bin_edge[:-1]) / 2.

    if not return_stddev:
        return bin_cents, ps1D
    else:
        ps1D_stddev = binned_statistic(freqs_dist.ravel(),
                                       psd2.ravel(),
                                       bins=bins,
                                       statistic=np.nanstd)[0]
        return bin_cents, ps1D, ps1D_stddev

@@ -171,16 +171,35 @@
 bin=None
+, return_freqs=True
 ):%0A    '
@@ -968,16 +968,86 @@
  bin to.
+%0A    return_freqs : bool, optional%0A        Return spatial frequencies.
 %0A%0A    Re
@@ -1315,16 +1315,336 @@
    '''%0A%0A
+    y = np.arange(psd2.shape%5B0%5D - np.floor(psd2.shape%5B0%5D/2.).astype(int))%0A    x = np.arange(psd2.shape%5B1%5D - np.floor(psd2.shape%5B1%5D/2.).astype(int))%0A%0A    yy, xx = np.meshgrid(y, x)%0A%0A    dists = np.sqrt(yy**2 + xx**2)%0A%0A    if nbins is None:%0A        nbins = int(np.round(dists.max() / binsize)+1)%0A%0A    if return_freqs:%0A    
     yfre
@@ -1707,16 +1707,20 @@
 )))%0A    
+ 
+   
 xfreqs =
@@ -1780,16 +1780,20 @@
 ))%0A%0A    
+   
+ 
 yy_freq,
@@ -1832,24 +1832,28 @@
 freqs)%0A%0A    
+  
+  
 freqs_dist =
@@ -1891,16 +1891,20 @@
 2)%0A%0A    
+ 
+   
 zero_fre
@@ -1957,16 +1957,20 @@
 () / 2.%0A
+    
     freq
@@ -2018,240 +2018,125 @@
     
-y = np.arange(psd2.shape%5B0%5D - np.floor(psd2.shape%5B0%5D/2.).astype(int))%0A    x = np.arange(psd2.shape%5B1%5D - np.floor(psd2.shape%5B1%5D/2.).astype(int))%0A%0A    yy, xx = np.meshgrid(y, x)%0A%0A    dists = np.sqrt(yy**2 + xx**2)%0A%0A    if nbins is Non
+if max_bin is None:%0A        if return_freqs:%0A            max_bin = freqs_dist.flatten()%5Bnp.argmax(dists)%5D%0A        els
 e:%0A 
@@ -2146,53 +2146,32 @@
     
+ 
-n
+   max_
 bin
-s
  = 
-int(np.round(dists.max() / binsize)+1
+dists.max(
 )%0A%0A 
@@ -2169,34 +2169,34 @@
 .max()%0A%0A    if m
-ax
+in
 _bin is None:%0A  
@@ -2205,64 +2205,36 @@
     
-max_bin = freqs_dist.flatten()%5Bnp.argmax(dists)%5D%0A%0A
+if return_freqs:%0A       
     
-if
  min
@@ -2234,33 +2234,58 @@
     min_bin 
-is None:%0A
+= zero_freq_val%0A        else:%0A    
         min_
@@ -2290,29 +2290,19 @@
 n_bin = 
-zero_freq_val
+0.5
 %0A%0A    if
@@ -2449,24 +2449,111 @@
 , nbins+1)%0A%0A
+    if return_freqs:%0A        dist_arr = freqs_dist%0A    else:%0A        dist_arr = dists%0A%0A
     ps1D, bi
@@ -2579,34 +2579,32 @@
 d_statistic(
-freqs_
 dist
+_arr
 .ravel(),%0A  
@@ -2937,26 +2937,24 @@
 atistic(
-freqs_
 dist
+_arr
 .ravel()

Return distances instead of frequencies