1

According to Most efficient way to calculate radial profile, I want to calculate the same but instead of mean I'd like to use median for each radial bin. I wrote these functions:

def radial_median(background, rs = None, is_fft_shifted = True):
    if rs is None :
        i = np.fft.fftfreq(background.shape[0]) * background.shape[0]
        j = np.fft.fftfreq(background.shape[1]) * background.shape[1]
        k = np.fft.fftfreq(background.shape[2]) * background.shape[2]
        i, j, k = np.meshgrid(i, j, k, indexing='ij')
        rs      = np.sqrt(i**2 + j**2 + k**2).astype(np.int16)
        
        if is_fft_shifted is False :
            rs = np.fft.fftshift(rs)
        rs = rs.ravel()

    ind = np.argsort(rs.flat) # get sorted indices

    sr = rs.flat[ind] # sorted radii
    
    ri = sr.astype(np.int32) # integer part of radii (bin size = 1)
    ri = np.array(list(set(ri)))
    
    # calculate the median
    f = lambda r: np.nanmedian(background[(rs >= r - .5) & (rs < r + .5)])

    # vectorize median function 
    radial_median_profile = np.vectorize(f)(ri)
    background = radial_median_profile[rs].reshape(background.shape)

    return background, rs, radial_median_provile

def av_radial_median_profile(x, y, data):
    Int, rs, median_profile = radial_median(data, rs=np.rint(np.sqrt(x**2+y**2)).astype(np.int))
    return Int

But I came across this:

Traceback (most recent call last):
  File "maskMakerGUI.py", line 1211, in radial_median_substraction
    m_background = av_radial_median_profile(self.x_map, self.y_map, dist, self.cspad.copy(), self.mask_clicked.copy(), pol_bool)
  File "maskMakerGUI.py", line 317, in av_radial_median_profile
    Int, rs, median_profile = radial_median(Int.reshape(data.shape), rs=np.rint(np.sqrt(x**2+y**2)).astype(np.int))
  File "maskMakerGUI.py", line 284, in radial_median
    background = radial_median_profile[rs].reshape(background.shape)
IndexError: index 1145 is out of bounds for axis 0 with size 1145

I couldn't understand why I got this error.

data.shape, x.shape and y.shape = (1480, 1552) I have to wrok with Python 3.6. I would be very thankful for any advice or corrections.

kitsune_breeze
  • 97
  • 1
  • 11

0 Answers0