这是我的解决方案,但效率不高。(顺便说一句,我没有在问题中告诉你数据的另一个维度。有没有人提出更有效的方法来做到这一点?
再次感谢
import numpy as np
from scipy.stats import pearsonr
from bottleneck import nanmean
def calc_corr_of_neighbours(data, win_shape):
rs = np.empty(data.shape[1:])
thisrs = np.empty(win_shape)
win_data = np.empty(win_shape)
dA = int(win_shape[0]/2)
dB = int(win_shape[1]/2)
maxA = data.shape[2]
maxB = data.shape[3]
for i in np.ndindex(rs.shape):
stA = max(i[1]-dA, 0)
endA = min(i[1]+dA, maxA)
stB = max(i[2]-dB, 0)
endB = min(i[2]+dB, maxB)
win_data = data[:, i[0], stA:endA, stB:endB]
thisrs.fill(np.NaN)
for j in np.ndindex(win_data.shape[1:]):
thisrs[j] = pearsonr(data[:, i[0], i[1], i[2]], win_data[:, j[0], j[1]])[0]
rs[i] = nanmean(thisrs)
return rs