Search code examples
pythonnumpypython-3.4shiftindex-error

Using scipy.ndimage.interpolation.shift(), IndexError: only integers, slices (`:`)


My problem is that when I run the code below I get the following puzzling Error

 IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

which appears due to the function: scipy.ndimage.interpolation.shift(input, shift, output=None, order=3, mode='constant', cval=0.0, prefilter=True)

It is puzzling for me due to the definition of the function given on "https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.ndimage.interpolation.shift.html#scipy-ndimage-interpolation-shift"

The definition states:

"shift : float or sequence, optional

The shift along the axes. If a float, shift is the same for each axis. If a sequence, shift should contain one value for each axis."

The Error explicitly states that the values should be int, while definition explicitly asks for floating point values. I tried inserting integer values for 'shift' and the code worked fine. I also tried moving the data into a ndarray, but the error still occurred.

So what I want to do is shift the image according to the float values in the "coords" array, with sub pixel resolution. I either don't understand the definition or error message correctly, and would like to know why my function implementation does not work.

pa_float = [float(x) for x in pa_list]  
dist_float =[float(x) for x in dist_list]  
pa_rad_list = np.radians(pa_float)

x_coord = np.multiply(np.cos(pa_rad_list), dist_float)*(-1)*(512)
y_coord = np.multiply(np.sin(pa_rad_list), dist_float)*(512)
# The first number in x_coord and the first number in y_coord represent the first coordinate pair.... and so on for the second..n th
coords = np.column_stack((x_coord,y_coord)) # shape (72,12)

for data in glob.glob(ImageFolderPath+'/*icn.rest_avg.fits'):
    scidata0 = fits.getdata(data,0)
    scidata0[0,0,:,:] = ndimage.interpolation.shift(scidata0[0,0,:,:], coords[data,:], order=3,mode='nearest',prefilter=True)
    finalarray.append(scidata0)

Solution

  • So I figured out the problem. The 'data' parameter ´does not function like a typical counter in a for loop but rather is a string containing the file name. Adding a counter inside the for loop and changing the 'data' to that counter in the function will fix the problem.

    for data in glob.glob(ImageFolderPath+'/*icn.rest_avg.fits'):
    scidata0 = fits.getdata(data,0)
    scidata0[0,0,:,:] = ndimage.interpolation.shift(scidata0[0,0,:,:], coords[i,:], order=3, mode='nearest', prefilter=True)
    finalarray.append(scidata0)
    i+=1