Overtime4728
Overtime4728

Reputation: 144

Python correlation coefficient for 3D array

Is there a way to compute a correlation coefficient for a moving window over a 3D array? When I run:

Import numpy as np
from scipy.ndimage.filters import generic_filter

arr_3d = np.dstack((data1,data2))

def function(arr_3d):
    return np.corrcoef(arr_3d[:,:,0],arr_3d[:,:,1])

gen_filt = generic_filter(arr_3d, function, footprint=np.ones((7,7,2)), mode='nearest')

The error I keep getting is.

Traceback (most recent call last):
  File "correlate.py", line 251, in <module>
    plot_data()
  File "correlate.py", line 219, in plot_data
    gen_filt = generic_filter(arr_3d,function,footprint=np.ones((35,35,2)),mode='nearest')
  File "/apps/python/3.5.1/lib/python3.5/site-packages/scipy/ndimage/filters.py", line 1245, in generic_filter
    cval, origins, extra_arguments, extra_keywords)
  File "correlate.py", line 217, in function
    return np.corrcoef(arr_3d[:,:,0],arr_3d[:,:,1])
IndexError: too many indices for array

Here's an example of what some of the data looks like. The dstack function seems to successfully create the 3D array.

data1 = np.random.randint(1,100,size=(10,10))
data2 = np.random.randint(1,100,size=(10,10))

arr_3d = np.dstack((x,y))
print(arr_3d.shape)

(10, 10, 2)

I didn't include the arr_3d here but after looking at it it appeared to be correctly formatted.

Upvotes: 0

Views: 1609

Answers (1)

Ehsan
Ehsan

Reputation: 12397

Here is a moving window correlation using a different method:

from skimage.util import view_as_windows

#this is your window shape
window = (7,7,2)
windows = view_as_windows(arr_3d, window).reshape(-1, *window)
window_corr = np.array([np.corrcoef(item[:,:,0], item[:,:,1]) for item in windows])

You can also avoid for loop at a cost of calculating the correlation of all windows, but I think looping is faster in this case (unless there is a better vectorized way of correlation between elements of a 3D array)

Upvotes: 1

Related Questions