Shift interpolation does not give expected behavior

When using scipy.ndimage.interpolation.shift to transfer a numpy dataset along the same axis with periodic border processing ( mode = 'wrap'), I get unexpected behavior. The routine tries to make the first pixel ( index 0) be identical to the last ( index N-1) instead of "last plus one ( index N)".

Minimal example:

# module import
import numpy as np
from scipy.ndimage.interpolation import shift
import matplotlib.pyplot as plt

# print scipy.__version__
# 0.18.1

a = range(10)

plt.figure(figsize=(16,12))

for i, shift_pix in enumerate(range(10)):
    # shift the data via spline interpolation
    b = shift(a, shift=shift_pix, mode='wrap')

    # plotting the data
    plt.subplot(5,2,i+1)
    plt.plot(a, marker='o', label='data')
    plt.plot(np.roll(a, shift_pix), marker='o', label='data, roll')
    plt.plot(b, marker='o',label='shifted data')
    if i == 0:
        plt.legend(loc=4,fontsize=12)
    plt.ylim(-1,10)
    ax = plt.gca()
    ax.text(0.10,0.80,'shift %d pix' % i, transform=ax.transAxes)

Blue line: data before the shift.
Green line: expected shift behavior.
Red line: actual output shift. Scipy.ndimage.interpolation.shift

, mode = 'wrap'? scipy tutorial page qaru.site/questions/412305/.... ?

Scipy 0.18.1, anaconda-2.2.0

enter image description here

+6
1

, , , .

C map_coordinate, :

map_coordinate(double in, npy_intp len, int mode)

NI_ZoomShift, . :

enter image description here

. , output = shift(np.arange(10), shift=4, mode='wrap') ( ).

NI_ZoomShift output[0] output[9] - , output[1] ( ):

# input  =         [0,1,2,3,4,5,6,7,8,9]
# output = [ ,?, , , , , , , , ]          '?' == computed position
# shift  = 4
output_index = 1

in  = output_index - shift    # -3
sz  = 10 - 1                  # 9
in += sz * ((-5 / 9) + 1)
#  +=  9 * ((     0) + 1) == 9
# in == 6

return input[in]  # 6 

, sz = len - 1 , . sz = len , 2007 : ndimage. .

, . , , :

'shift' . [0, k] [0,1,2,...,k]. , , 0 k, , :

0--1--2--3-...-k              0--1--2--3-...-k              0--1-- ...
               0--1--2--3-...-k              0--1--2--3-...-k      ...

, shift -?

0

All Articles