Search code examples
python-3.xnumpyjitnumbanumpy-ndarray

numba\jit doesn't allow the use of np argsort in nopython mode


Receiving this error message:

Failed at nopython (nopython frontend)

[1m[1m[1mInvalid usage of Function(<function argsort at 0x0000000002A67840>) with parameters (array(float64, 2d, C), axis=int64)

 * parameterized

In definition 0:


While using this code

def rankbids(bids, shifts, groupPeriod, period):
    rowsSize = bids.shape[0];

    finaltable = np.zeros((rowsSize, groupPeriod), dtype=np.float64)
    for i in range(0, period):
        #for 0 to 99
        #CONSTANT 4 UPDATE WHEN NEEDED


        for worker in range(rowsSize):
            shiftNum = int(shifts[worker,i]);

            finaltable[worker, (shiftNum+i*10)] = bids[worker,i];

            if shiftNum == 1:
                finaltable[worker, (shiftNum+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+1+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+2+i*10)] = bids[worker,i];
            elif shiftNum == 2:
                finaltable[worker, (shiftNum+2+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+3+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+4+i*10)] = bids[worker,i];
            elif shiftNum == 3:
                finaltable[worker, (shiftNum+4+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+5+i*10)] = bids[worker,i];
                finaltable[worker, (shiftNum+6+i*10)] = bids[worker,i];


    indexTable = np.argsort(finaltable, axis=0)
    print(finaltable);




    return finaltable;

rank_bids = numba.jit('float64[:,:](float64[:,:], float64[:,:], int64, float64)', nopython=True)(rankbids);

It seems that numpy does't allow argsort with Nd arrays in numba functions? My question is if anyone has been able to use it within a jit function and perhaps show me what I could possible do to be able to use it!


Solution

  • np.argsort works in numba, but not the axis keyword. You could write your code indexTable = np.argsort(finaltable, axis=0) like this:

    indexTable = np.empty_like(finaltable)
    for j in range(indexTable.shape[1]):
        indexTable[:, j] = np.argsort(finaltable[:, j])