o
    1Xxid-                     @   s  d dl Z d dlmZmZ d dlmZmZmZmZm	Z	m
Z
mZ d dlmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ e
d
Zdeee	e f de	e fddZdedefddZdededdfddZdededeed ed ed f ddfddZ dededdfddZ!dededdfddZ"dededdfd d!Z#dededdfd"d#Z$deddfd$d%Z%ej&fd&ej'd'ej'd(eej(ej(ge)f dee	e	e*  e	e	e)  f fd)d*Z+	+	,		d3deded-e*d.e)d/eej, d0ee	e*  ddfd1d2Z-dS )4    N)NormalizedRecordSet	RecordSet)CallableOptionalTupleUnionListTypeVarcast)Literal)types)
Collection)note)InvalidArgument)distance_functionsTvaluereturnc                 C   s$   | du rt dt| tr| S | gS )z*Wrap a value in a list if it is not a listNzvalue cannot be None)r   
isinstancer   )r    r   `/home/air/biblejyuku/back/venv/lib/python3.10/site-packages/chromadb/test/property/invariants.pywrap   s
   
r   
record_setc                 C   s   | d du r	d}nHt | d trM| d dusJ t| d dkrDtdd | d D sDtdd | d D r@ttj| d g}ntdttj| d }ntdt| d	 | d
 durbt| d
 nd| d durrt| d |dS d|dS )z9Ensure that an embedding set has lists for all its values
embeddingsNr   c                 s       | ]}t |tV  qd S Nr   list).0	embeddingr   r   r   	<genexpr>#   s    

zwrap_all.<locals>.<genexpr>c                 s   s    | ]
}t |ttfV  qd S r   )r   intfloatr   er   r   r   r    &   s    z-an embedding must be a list of floats or intsz>embeddings must be a list of lists, a list of numbers, or Noneids	documents	metadatas)r%   r&   r'   r   )	r   r   lenallr
   r   
Embeddingsr   r   )r   embedding_listr   r   r   wrap_all   s2   

r,   
collectionc                 C   s(   |   }t|}|t|d ksJ dS )z?The given collection count is equal to the number of embeddingsr%   N)countr,   r(   )r-   r   r.   normalized_record_setr   r   r   r.   =   s   r.   r/   
field_namer&   r'   r   c                    s   | j |d |gddd t|d D  | }t|d dkr)|g ks'J dS |dus/J tt| fddd	}d
d |D }|| }|du rTdgt|d  }|dkrhtt|t|sfJ dS ||ksnJ dS )zq
    The actual embedding field is equal to the expected field
    field_name: one of [documents, metadatas]
    r%   r%   includec                 S      i | ]\}}||qS r   r   r   iidr   r   r   
<dictcomp>R       z"_field_matches.<locals>.<dictcomp>r   Nc                    s    d | d   S )Nr%   r   r   )index_and_field_valueembedding_id_to_indexresultr   r   <lambda>^   s    z _field_matches.<locals>.<lambda>keyc                 S   s   g | ]\}}|qS r   r   )r   _field_valuer   r   r   
<listcomp>b       z"_field_matches.<locals>.<listcomp>r   )get	enumerater(   sortednpallclosearray)r-   r/   r0   actual_fieldsorted_fieldfield_valuesexpected_fieldr   r:   r   _field_matchesD   s$    rN   c                    s\   t |}| j|d g dd }dd t|d D  t| fddd}||d ks,J dS )	z5The actual embedding ids is equal to the expected idsr%   r1   c                 S   r3   r   r   r4   r   r   r   r7   u   r8   zids_match.<locals>.<dictcomp>c                    s    |  S r   r   )r6   r;   r   r   r=   v   s    zids_match.<locals>.<lambda>r>   N)r,   rD   rE   rF   )r-   r   r/   
actual_idsr   rO   r   	ids_matcho   s
   rQ   c                 C      t |}t| |d dS )z?The actual embedding metadata is equal to the expected metadatar'   Nr,   rN   r-   r   r/   r   r   r   metadatas_matchz      rU   c                 C   rR   )AThe actual embedding documents is equal to the expected documentsr&   NrS   rT   r   r   r   documents_match   rV   rX   c                 C   rR   )rW   r   NrS   rT   r   r   r   embeddings_match   rV   rY   c                 C   s(   |   d }t|tt|ksJ d S )Nr%   )rD   r(   set)r-   r%   r   r   r   no_duplicates   s   r[   querytargetsdistance_fnc                    sB   t | }t |t  fddd|}t | | fS )zGReturn the ordered indices and distances from each query to each targetc                    s   t  d| S )N   )rG   apply_along_axis)r\   r^   
np_targetsr   r   r=      s    z"_exact_distances.<locals>.<lambda>r_   )rG   rI   r`   argsorttolist)r\   r]   r^   np_query	distancesr   ra   r   _exact_distances   s   

rg   r_   Gz?	n_results
min_recallembedding_functionquery_indicesc              	      s  t |}t|d dkrdS |d   duot dk}|s;|dus$J |d dus,J t|d ts5J ||d  tj}d}	| jdusGJ  dusMJ d| jv r| jd }
tdd	  D sbJ t d }|	t	d
t
t| }	|
dkr|tj}|
dkrtj}|du r n fdd|D }|d |durdurfdd|D t| |d\}}| j|r|nd|snd|g dd}|d dusJ |d dusJ |d dusJ |d dusJ dd t|d D }d}tt||D ]\}\}}t|d |d|  }|tt|t|d |  7 }t|d | D ]p\}}||v}|| }tj|| |d | | |	d}|rK|rI|d8 }nq"|sPJ t | |d | | saJ |d dury|d | |d | | ksyJ |d dur|d | |d | | ksJ q"qt|d }|| | }ztd| d| d| d|	  W n
 ty   Y nw ||ksJ |d D ]}tt||sJ qdS )zBValidate that the API performs nearest_neighbor searches correctlyr%   r   Nr   r&   gư>z
hnsw:spacec                 s   r   r   r   r#   r   r   r   r       s    zann_accuracy.<locals>.<genexpr>
   cosineipc                       g | ]} | qS r   r   r   r5   )r   r   r   rB      rC   z ann_accuracy.<locals>.<listcomp>c                    rp   r   r   rq   )query_documentsr   r   rB      rC   )r^   )r   r&   r'   rf   )query_embeddingsquery_textsri   r2   rf   r'   c                 S   r3   r   r   r4   r   r   r   r7      r8   z ann_accuracy.<locals>.<dictcomp>)atolr_   zrecall: z
, missing z out of z, accuracy threshold )r,   r(   r   r   r   l2metadatar)   mathpowr!   log10rn   ro   rg   r\   rE   ziprG   rI   rZ   rH   r   r   sort)r-   r   ri   rj   rk   rl   r/   have_embeddingsdistance_functionaccuracy_thresholdspacedimrs   indicesrf   query_resultsid_to_indexmissingr5   	indices_idistances_iexpected_idsjr6   unexpected_idindexcorrect_distancesizerecalldistance_resultr   )r   rr   r   ann_accuracy   s   	




 

"



 r   )r_   rh   NN).rx   !chromadb.test.property.strategiesr   r   typingr   r   r   r   r   r	   r
   typing_extensionsr   numpyrG   numpy.typingnptchromadb.apir   chromadb.api.models.Collectionr   
hypothesisr   hypothesis.errorsr   chromadb.utilsr   r   r   r,   r.   rN   rQ   rU   rX   rY   r[   rv   r*   	ArrayLiker"   r!   rg   EmbeddingFunctionr   r   r   r   r   <module>   s|    $"
"
+


