a
    ^ŠÝg
  ã                   @   sV   d dl Zd dlZd dlmZ ddd„Zddd„Zddd„Zd	d
„ Zdd„ Z	dd„ Z
dS )é    N)Úget_invlist_sizesc                 C   s~   |j \}}|j |fksJ ‚t| tjƒr.|d9 }|| jks<J ‚|dur^|j |fksTJ ‚t |¡}|  |t |¡|t |¡¡ dS )zP
    Add elements to an IVF index, where the assignment is already computed
    é   N)ÚshapeÚ
isinstanceÚfaissÚIndexBinaryIVFÚdÚswig_ptrZadd_core)Ú	index_ivfÚxÚaZidsÚnr   © r   úe/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/faiss/contrib/ivf_tools.pyÚadd_preassigned   s    

ÿr   c           	      C   sÖ   t | tjƒrJ| j ¡ dks"J dƒ‚t | j d¡¡}| |¡}t | j	¡} |j
\}}t | tjƒrn|d9 }d}nd}|| jks€J ‚|j
|| jfks”J ‚|du r²tj|| jf|d}n|j
|| jfksÆJ ‚|  ||||¡S )	zÙ
    Perform a search in the IVF index, with predefined lists to search into.
    Supports indexes with pretransforms (as opposed to the
    IndexIVF.search_preassigned, that cannot be applied with pretransform).
    é   z"chain must have only one componentr   r   Úint32Úfloat32N©Zdtype)r   r   ZIndexPreTransformÚchainÚsizeZdowncast_VectorTransformÚatÚapplyÚdowncast_indexÚindexr   r   r   ÚnprobeÚnpZzerosÚsearch_preassigned)	r
   ÚxqÚkÚlist_nosÚ
coarse_disZ	transformr   r   Údis_typer   r   r   r      s     

r   c                 C   sü   |j \}}t| tjƒr$|d9 }d}nd}|du rFtj|| jf|d}n|j || jfksZJ ‚|| jkshJ ‚|j || jfks|J ‚t |¡}tj	}	|  
||	|ƒ||	|ƒ|	|ƒ|¡ t |j|d ¡ ¡ }
t|
d ƒ}t |j|¡ ¡ }t |j|¡ ¡ }|
||fS )z[
    Perform a range search in the IVF index, with predefined lists to
    search into
    r   r   r   Nr   r   éÿÿÿÿ)r   r   r   r   r   Úemptyr   r   ZRangeSearchResultr	   Zrange_search_preassigned_cZrev_swig_ptrÚlimsÚcopyÚintZ	distancesÚlabels)r
   r   Zradiusr    r!   r   r   r"   ÚresÚspr%   Znum_resultsÚdistÚindicesr   r   r   Úrange_search_preassigned<   s,    


ýr-   c                 C   sˆ   |j dkr*| j ¡ }| |¡ | |¡ n|j | jks:J ‚| j}d| _t | j¡}|j	 
|¡ || _t| dƒr|| j |¡ n|g| _|S )zQ replace the IVF quantizer with a flat quantizer and return the
    old quantizerr   FÚreferenced_objects)ÚntotalÚ	quantizerZreconstruct_nÚtrainÚaddÚnlistZ
own_fieldsr   r   ÚthisZownÚhasattrr.   Úappend)r
   Znew_quantizerZ	centroidsZold_ownZold_quantizerr   r   r   Úreplace_ivf_quantizerb   s    



r7   c                 C   s”   |j \}| j|ksJ ‚t | j¡}|j| jks2J ‚tj|dd}tj||d}t 	|tj
|tdk¡sjJ ‚| |¡ t | j¡}| t |¡¡ dS )z« Apply some permutation to the inverted lists, and modify the quantizer
    entries accordingly.
    Perm is an array of size nlist, where old_index = perm[new_index]
    Zint64r   )Z	minlengthN)r   r3   r   r   r0   r/   r   ZascontiguousarrayZbincountÚallZonesr'   Zpermute_entriesZdowncast_InvertedListsÚinvlistsÚpermute_invlistsr	   )r
   Úpermr3   r0   Úbcr9   r   r   r   r:   z   s    
r:   c                 C   s"   t | jƒ}t |¡}t| |ƒ d S )N)r   r9   r   Zargsortr:   )r
   Zinvlist_sizesr;   r   r   r   Úsort_invlists_by_size‘   s    

r=   )N)N)N)Únumpyr   r   Zfaiss.contrib.inspect_toolsr   r   r   r-   r7   r:   r=   r   r   r   r   Ú<module>   s   


&