o
    \h                     @   s   d dl mZmZmZmZmZ d dlZd dlZ	d dl
mZmZ deeej  deeeee  eej f  fddZdeeef d	ed
eddfddZdeeef deddfddZd	eded fddZdeddfddZdS )    )ListDictAnyOptionalUnionN)QueryResult	GetResult
embeddingsreturnc                 C   s.   | du rdS t | d tjrdd | D S | S )z
    Transform embeddings from numpy arrays to lists of floats.
    This is a shared helper function to avoid duplicating the transformation logic.
    Nr   c                 S   s   g | ]}|  qS  )tolist).0embr   r   Y/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/chromadb/utils/results.py
<listcomp>   s    z)_transform_embeddings.<locals>.<listcomp>)
isinstancenpndarray)r	   r   r   r   _transform_embeddings   s   r   	data_dictquery_result	query_idxc                 C   sd   |d D ]+}| |}|dur/|d}|dkrt|}t|tr+t|dkr+|| }|| |< qdS )a@  
    Helper function to add fields from a query result to a dictionary.
    Handles the nested array structure specific to query results.

    Args:
        data_dict: Dictionary to add the fields to
        query_result: QueryResult containing the data
        query_idx: Index of the current query being processed
    includedNsr	   r   )getrstripr   r   listlen)r   r   r   fieldvaluekeyr   r   r   _add_query_fields   s   

r!   
get_resultc                 C   sF   |d D ]}| |}|dur |d}|dkrt|}|| |< qdS )z
    Helper function to add fields from a get result to a dictionary.
    Handles the flat array structure specific to get results.

    Args:
        data_dict: Dictionary to add the fields to
        get_result: GetResult containing the data
    r   Nr   r	   )r   r   r   )r   r"   r   r   r    r   r   r   _add_get_fields0   s   

r#   zpd.DataFramec                 C   s   zddl }W n ty   tdw g }t| d }t|D ]#}i }| d | |d< t|| | ||}|jddd || q|S )a>  
    Function to convert QueryResult to list of DataFrames.
    Handles the nested array structure specific to query results.
    Column order is defined by the order of the fields in the QueryResult.

    Args:
        query_result: QueryResult to convert to DataFrames.

    Returns:
        List of DataFrames.
    r   Nz:pandas is required to convert query results to DataFrames.idsidTinplace)pandasImportErrorr   ranger!   	DataFrame	set_indexappend)r   pddfsnum_queriesidata_for_dfdfr   r   r   query_result_to_dfsE   s   
r4   c                 C   sZ   zddl }W n ty   tdw i }| d |d< t||  ||}|jddd |S )a#  
    Function to convert GetResult to a DataFrame.
    Handles the flat array structure specific to get results.
    Column order is defined by the order of the fields in the GetResult.

    Args:
        get_result: GetResult to convert to a DataFrame.

    Returns:
        DataFrame.
    r   Nz9pandas is required to convert get results to a DataFrame.r$   r%   Tr&   )r(   r)   r#   r+   r,   )r"   r.   r2   r3   r   r   r   get_result_to_dfe   s   

r5   )typingr   r   r   r   r   numpyr   r(   r.   chromadb.api.typesr   r   r   floatr   strintr!   r#   r4   r5   r   r   r   r   <module>   s6    




 