0

This is an expansion of some previous questions. I will start out with the problem description and then reference what has already been created in response to other questions.

Say you have an array of arbitrary shape and 3 dimensions or greater (e.g. (10, 20, 30)) and you would like to shift the index of the values in the last dimension. To do this, you create a 10x20 map of how each (30,) array should shift.

Here is the way I implemented this with dummy values:

import numba
import numpy as np


@numba.njit
def shift4_numba(arr, num, fill_value=np.nan):
    #print("The num is: ", num)
    if num > 0:
        return np.concatenate((np.full(num, fill_value), arr[:-num]))
    else:
        return np.concatenate((arr[-num:], np.full(-num, fill_value)))


if __name__ == "__main__":
    d = np.random.randn(10, 20, 30)

    new_idx_loc = np.random.randint(0, 10, size=(10, 20))
    nda = np.empty((10, 20, 30))

    for i in range(10):
        for j in range(20):
            a = shift4_numba(d[i, j, :], new_idx_loc[i, j])
            nda[i, j, :] = a
            #print(a, d[i, j, :]) 

Here are some references to what has already been done:

The function shift4_numba in the code above is slightly altered from a response on the question here by np8. It just needed to have num = 0 in the else statement not in the if statement. I selected this function because the actual data I am working with is significantly larger than the example I created.

A similar question was asked here and here. I thought that what TomNorway and Yann Dubois posted would do what I wanted it to do but it doesn't appear to. I am not sure if I am just thinking about this differently, because I am not fully sure in what case you would use those functions. Is there some way to adapt what I have above to work with their function definitions? Is there a way to write this with better performance?

Amanda.py
  • 113
  • 10

0 Answers0