Search code examples
pythonarraysnumpyunwrap

Efficiently unwrap in multiple dimensions with numpy


Let's assume I have an array of phases (from complex numbers)

A = np.angle(np.random.uniform(-1,1,[10,10,10]) + 1j*np.random.uniform(-1,1,[10,10,10]))

I would now like to unwrap this array in ALL dimensions. In the above 3D case I would do

A_unwrapped = np.unwrap(np.unwrap(np.unwrap(A,axis=0), axis=1),axis=2)

While this is still feasible in the 3D case, in case of higher dimensionality, this approach seems a little cumbersome to me. Is there a more efficient way to do this with numpy?


Solution

  • You could use np.apply_over_axes, which is supposed to apply a function over each dimension of an array in turn:

    np.apply_over_axes(np.unwrap, A, np.arange(len(A.shape)))
    

    I believe this should do it.