a
    ^g                     @   sf   d dl Zd dlZdd Zdd Zdd Zdd	 Zd
d ZdddZdd Z	dd Z
dd Zdd ZdS )    Nc           
   
   C   s6  t | } | |}tj|dd}d }}z| |}|dkrTt t |||j | 	|}| j
t jjkrtj|| j
fdd}n4| j}| j}|| d | }	tj|	|| |fdd}|dkrt t |||j W |dur| || |dur| || n.|dur| || |dur,| || 0 ||fS )zy returns the inverted lists content as a pair of (list_ids, list_codes).
    The codes are reshaped to a proper size
    int64ZdtypeNr   Zuint8   )faissZdowncast_InvertedLists	list_sizenpzerosZget_idsmemcpyswig_ptrnbytes	get_codes	code_sizeZInvertedListsZINVALID_CODE_SIZEZn_per_block
block_sizeZrelease_idsZrelease_codes)
invlistslZlsZlist_idsZidscodesZ
list_codesZnpbbsZls_round r   i/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/faiss/contrib/inspect_tools.pyget_invlist
   s4    





r   c                    s"   t j fddt jD ddS )z1 return the array of sizes of the inverted lists c                    s   g | ]}  |qS r   )r   .0ir   r   r   
<listcomp>+   s   z%get_invlist_sizes.<locals>.<listcomp>r   r   )r   arrayrangeZnlistr   r   r   r   get_invlist_sizes)   s
    r   c                 C   s*   | j jD ]}t| dt| |  qdS )z3 list values all fields of an object known to SWIG z = N)	__class__Z__swig_getmethods__printgetattr)objnamer   r   r   print_object_fields1   s    r#   c                 C   s    t | j}|| j| j| jS )z% return the PQ centroids as an array )r   vector_to_arrayZ	centroidsreshapeMZksubZdsub)ZpqZcenr   r   r   get_pq_centroids8   s    r'   c                 C   s,   t | j}t | j| j| j}||fS )zo extract matrix + bias from the PCA object
    works for any linear transform (OPQ, random rotation, etc.)
    )r   r$   bAr%   d_outd_in)Zpcar(   r)   r   r   r   get_LinearTransform_matrix>   s    r,   c                 C   sn   | j \}}|dur"|j |fks"J t|||du}t|  |j |dur\t||j d|_|  |S )zA make a linear transform from a matrix and a bias term (optional)NT)	shaper   ZLinearTransformZcopy_array_to_vectorZravelr)   r(   Z
is_trainedZset_is_orthonormal)r)   r(   r*   r+   ltr   r   r   make_LinearTransform_matrixG   s    
r/   c                    s<   t | jd| jt | j  fddt| jD S )z. return to codebooks of an additive quantizer c                    s$   g | ]} |  |d    qS )r   r   r   co	codebooksr   r   r   Y   s   z4get_additive_quantizer_codebooks.<locals>.<listcomp>)r   r$   r3   r%   dZcodebook_offsetsr   r&   )Zaqr   r1   r    get_additive_quantizer_codebooksU   s
    r5   c                 C   s"   t | jd}|| j| jS )z1 copy and return the data matrix in an IndexFlat Zfloat32)r   r$   r   viewr%   ntotalr4   )indexxbr   r   r   get_flat_data_   s    r:   c                 C   s   t | j| j| jS )z2 get the codes from an indexFlatCodes as an array )r   r$   r   r%   r7   r   )Z
index_flatr   r   r   get_flat_codese   s    r;   c                 C   s:   |   }tj|j|jfdd}tt||j|j	 |S )zf get the neighbor list for the vectors stored in the NSG structure, as
    a N-by-K matrix of indices Zint32r   )
Zget_final_graphr   r   NKr   r	   r
   datar   )ZnsggraphZ	neighborsr   r   r   get_NSG_neighborsk   s    r@   )N)numpyr   r   r   r   r#   r'   r,   r/   r5   r:   r;   r@   r   r   r   r   <module>   s   	

