Sunday, 23 September 2018

numpy - efficiently copy values from matrix to matrix using some precalculated map

I have an input matrix A of size I*J

And an output matrix B of size N*M

And some precalculated map of size N*M*2 that dictates for each coordinate in B, which coordinate in A to take. The map has no specific rule or linearity that I can use. Just a map that seems random.

The matrices are pretty big (~5000*~3000) so creating a mapping matrix is out of the question (5000*3000*5000*3000)

I managed to do it using a simple map and loop:

for i in range(N):
    for j in range(M):
        B[i, j] = A[mapping[i, j, 0], mapping[i, j, 1]]

And I managed to do it using indexing:

B[coords_y, coords_x] = A[some_mapping[:, 0], some_mapping[:, 1]]
# Where coords_x, coords_y are defined as all of the coordinates:
# [[0,0],[0,1]..[0,M-1],[1,0],[1,1]...[N-1,M-1]]

This works much better, but still kind of slow.

I have infinite time in advance to calculate the mapping or any other utility calculation. But after these precalculations, this mapping should happen as fast as possible.

Currently, the only other option that I see is just to reimplement this in C or something faster...

(Just to make it clear if someone is curious, I'm creating an image out of some other, differently shaped and oriented image with some encoding. But its' mapping is very complicated and not something simple or linear that can be used)



from numpy - efficiently copy values from matrix to matrix using some precalculated map

No comments:

Post a Comment