0

I am attempting to transform a numpy ndarray in the following manner.

Here is the array I have currently given by rot_F:

# F is laid out to be human readable here
F = np.array(
    [ 
        [# Filter 0
             [ # Depth 0
             [1, -1],
             [2,  0]
             ],
             [ # Depth 1
             [ 0,  0],
             [-1, -1]
             ]
        ],
        [# Filter 1
             [ # Depth 0
             [0, -1],
             [3,  0]
             ],
             [ # Depth 1
             [ 1,  2],
             [-1, -1]
             ]
        ]
    ]
    )

F = np.moveaxis(F,1,3)

# rotate F by 180 degrees along axes (1,2)
rot_F = np.rot90(F,2,(1,2))

print(rot_F)

OUTPUT FROM print(rot_F):
[[[[ 0 -1]
   [ 2 -1]]

  [[-1  0]
   [ 1  0]]]

[[[ 0 -1]
  [ 3 -1]]

  [[-1  2]
   [ 0  1]]]]

Now I want to turn rot_F into the following:

desired_filters = np.zeros_like(rot_F)
desired_filters[0,:,:,0] = np.array([[0,2],[-1,1]])
desired_filters[0,:,:,1] = np.array([[0,3],[-1,0]])
desired_filters[1,:,:,0] = np.array([[-1,-1],[0,0]])
desired_filters[1,:,:,1] = np.array([[-1,-1],[2,1]])


print(desired_filters)

OUTPUT FROM print(desired_filters):
[[[[ 0  0]
   [ 2  3]]

  [[-1 -1]
   [ 1  0]]]


  [[[-1 -1]
    [-1 -1]]

   [[ 0  2]
    [ 0  1]]]]

Basically I am trying stack all of the arrays on 0th depth dimension in rot_F along their own depth dimension and all of the arrays on the 1st depth dimension in rot_F on their own depth dimension while preserving the original shape.

3
  • Make sure you understand np.rot90. In its code I see the use of transpose and flip. flip is [::-1] indexing. Commented May 12, 2018 at 17:03
  • With those initial F values, identifying what has changed in rot_F or the desired output is tedious. There's no obvious pattern. Commented May 12, 2018 at 17:11
  • In rot_f each 2d array is rotated by 180 degrees. In the desired output all 2d arrays at depth 0 are now stacked along their own depth axis and likewise for depth 1. Commented May 12, 2018 at 17:29

1 Answer 1

1

After a lot of trial and error with transpose:

In [157]: rotF = np.rot90(np.moveaxis(F,1,3),2,(1,2))
In [158]: rotF
Out[158]: 
array([[[[ 0, -1],
         [ 2, -1]],

        [[-1,  0],
         [ 1,  0]]],


       [[[ 0, -1],
         [ 3, -1]],

        [[-1,  2],
         [ 0,  1]]]])
In [159]: F1 = rotF.transpose(3,0,1,2)
In [160]: F1
Out[160]: 
array([[[[ 0,  2],
         [-1,  1]],

        [[ 0,  3],
         [-1,  0]]],


       [[[-1, -1],
         [ 0,  0]],

        [[-1, -1],
         [ 2,  1]]]])
In [161]: F1.transpose(0,2,3,1)
Out[161]: 
array([[[[ 0,  0],
         [ 2,  3]],

        [[-1, -1],
         [ 1,  0]]],


       [[[-1, -1],
         [-1, -1]],

        [[ 0,  2],
         [ 0,  1]]]])

That F1 intermediate stage was

In [110]: G =np.array([np.array([[0,2],[-1,1]]),
     ...: np.array([[0,3],[-1,0]]),
     ...: np.array([[-1,-1],[0,0]]),
     ...: np.array([[-1,-1],[2,1]])]).reshape(2,2,2,2)
In [111]: G
Out[111]: 
array([[[[ 0,  2],
         [-1,  1]],

        [[ 0,  3],
         [-1,  0]]],


       [[[-1, -1],
         [ 0,  0]],

        [[-1, -1],
         [ 2,  1]]]])

Or in short:

In [163]: rotF.transpose(3,1,2,0)
Out[163]: 
array([[[[ 0,  0],
         [ 2,  3]],

        [[-1, -1],
         [ 1,  0]]],


       [[[-1, -1],
         [-1, -1]],

        [[ 0,  2],
         [ 0,  1]]]])

It's hard to picture, but apparently you just wanted to swap the first and last axes.


Looks like rot_F is also

np.rot90(F,2,(2,3)).transpose(0,2,3,1)
F[:,:,::-1,::-1].transpose(0,2,3,1)
F.transpose(0,2,3,1)[:,::-1,::-1,:]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.