I am trying to calculate correlation coefficients at each grid point in python. The dimension of two variables(variable1[t,y,x], variable2[t,y,x]) are same and I would like to get a correlation coeffients as [y,x].
Two variables have irregular missing values, so it seems that specific "if statement" is needed.
def corr_grid3D(array1,array2):
dims = array1.shape
corr = np.empty([dims[1],dims[2]]); p = np.empty([dims[1],dims[2]])
for i in range(dims[1]):
for j in range(dims[2]):
logic1 = ~np.isnan(array1[:,i,j]); logic2 = ~np.isnan(array2[:,i,j])
if sum(logic1) == sum(logic2) and sum(logic1) > 5 and sum(logic2) > 5:
corr[i,j], p[i,j] = stats.pearsonr(array1[logic1,i,j],array2[logic2,i,j])
else:
corr[i,j], p[i,j] = np.nan, np.nan
return corr, p
It works but takes too long because of many "if statement". How do I make this code fancy?