<jupyter_start><jupyter_text>Not Correct Cython : wrong order of returned values<jupyter_code>%%cython
import numpy as np
from tqdm import tqdm_notebook as tqdm

def extract_values(list idxs, int k, float[:] data, int[:] indices, int[:] indptr):
    
    cdef Py_ssize_t x_idxs = len(idxs)
    cdef int x
    cdef int t
    cdef int indptr_start
    cdef int indptr_end
    cdef float[:] row_data
    cdef long[:] available_idxs
    
    res = np.zeros((x_idxs, k), dtype=float)
    cdef double[:, :] res_view = res
    
    for x in tqdm(range(x_idxs)):
        indptr_start = indptr[x]
        indptr_end = indptr[x+1]
        #available_idxs = list(set(idxs[x]).intersection(set(indices[indptr_start: indptr_end])))
        available_idxs = np.where(np.isin(indices[indptr_start : indptr_end], idxs[x]))[0]
        row_data = data[indptr_start:indptr_end]
        for t in range(len(available_idxs)):
            res_view[x, t] = row_data[available_idxs[t]] 
        
    return res
a = extract_values(linid_idx, 50, sim_name.data, sim_name.indices, sim_name.indptr)<jupyter_output><empty_output><jupyter_text>Correct cython code!!<jupyter_code>%%cython
import numpy as np
from tqdm import tqdm_notebook as tqdm

cdef list get_inorder_sliced(int[:] tmp_idxs, int[:] sliced, long[:] available_idxs):
    cdef dict idxs_dict
    cdef list tuple_sliced
    
    idxs_dict = {k:v for v,k in enumerate(tmp_idxs)}
    tuple_sliced = [( idxs_dict[sliced[i]] , i)   for i in available_idxs]
    tuple_sliced.sort(key=lambda tup: tup[0])
    tuple_sliced = [x[1] for x in tuple_sliced]
    return tuple_sliced
    

cpdef extract_values_2(list idxs, int k, float[:] data, int[:] indices, int[:] indptr):
    
    cdef Py_ssize_t x_idxs = len(idxs)
    cdef int x
    cdef int t
    cdef int indptr_start
    cdef int indptr_end
    cdef float[:] row_data
    
    cdef long[:] available_idxs
    cdef list ordered_available
            
    res = np.zeros((x_idxs, k), dtype=float)
    cdef double[:, :] res_view = res
    
    for x in tqdm(range(x_idxs)):
        indptr_start = indptr[x]
        indptr_end = indptr[x+1]
        
    
        available_idxs = np.where(np.isin(indices[indptr_start : indptr_end], idxs[x]))[0]
    
        ordered_available = get_inorder_sliced(idxs[x], indices[indptr_start : indptr_end], available_idxs)
    
        row_data = data[indptr_start:indptr_end]
        for t in range(len(ordered_available)):
            res_view[x, t] = row_data[ordered_available[t]] 
    
    return res
a = extract_values_2(linid_idx, 50, sim_name.data, sim_name.indices, sim_name.indptr)
a[2]
linid_ = []
linid_idx = []
linid_score = []
linid_name_cosine = []
linid_email_cosine = []
linid_phone_cosine = []
linid_address_cosine = []
linid_record_id = []
k=50

tr = df_train[['record_id', 'linked_id']]
for x in tqdm(range(df_test.shape[0])):
    #df = df_train.loc[hybrid[x].nonzero()[1][hybrid[x].data.argsort()[::-1]],:][:k]
    indices = hybrid[x].nonzero()[1][hybrid[x].data.argsort()[::-1]][:k]
    df = tr.loc[indices, :][:k]
    linid_.append(df['linked_id'].values)
    linid_idx.append(indices)    # TODO CAMBIATO QUI
    linid_record_id.append(df.record_id.values)
    linid_score.append(np.sort(hybrid[x].data)[::-1][:k]) # Questo ha senso perché tanto gli indices sono sortati in base allo scores di hybrid
    linid_name_cosine.append([sim_name[x, t] for t in indices])
    #linid_email_cosine.append([sim_email[x, t] for t in indices])
    #linid_phone_cosine.append([sim_phone[x, t] for t in indices])
    #linid_address_cosine.append([sim_address[x, t] for t in indices])
    
extract_values_2(linid_idx, 50, sim_name.data, sim_name.indices, sim_name.indptr)
extract_values_2(linid_idx, 50, sim_email.data, sim_email.indices, sim_email.indptr)
extract_values_2(linid_idx, 50, sim_phone.data, sim_phone.indices, sim_phone.indptr)
extract_values_2(linid_idx, 50, sim_address.data, sim_address.indices, sim_address.indptr)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12452924930956566, 'fraction_numerical': 0.008285212151644489, 'mean_word_length': 3.955223880597015, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '373137', 'license': 'Apache-2.0', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1539, 'n_words': 309, 'path': 'notebooks/SpeedUp creation.ipynb', 'repo_name': 'teomores/Oracle_HPC_contest', 'size': '3983'}