Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
statistics.cpython-313.pyc161 linesDownload Raw Back to __pycache__
1�

2��j&���SrSSKJrJrJrJrJrJr SSKJ	r	 SSK3JrJrJ
r
 SSKJr \(aSSKJr SSKJr SS	S4\4SjrSS	S\S5\S
\44SjrSSjrSSS	S\S6\4SjjrSSS	S\S\\\S7\\\44Sjjrg)a�Utility functions for managing collection statistics.8 9This module provides standalone functions for enabling, disabling, and retrieving10statistics for ChromaDB collections. These functions work with the attached function11system to automatically compute metadata value frequencies.12 13Example:14    >>> from chromadb.utils.statistics import attach_statistics_function, get_statistics15    >>> import chromadb16    >>>17    >>> client = chromadb.Client()18    >>> collection = client.get_or_create_collection("my_collection")19    >>>20    >>> # Attach statistics function with output collection name21    >>> attach_statistics_function(collection, "my_collection_statistics")22    >>>23    >>> # Add some data24    >>> collection.add(25    ...     ids=["id1", "id2"],26    ...     documents=["doc1", "doc2"],27    ...     metadatas=[{"category": "A"}, {"category": "B"}]28    ... )29    >>>30    >>> # Get statistics from the named output collection31    >>> stats = get_statistics(collection, "my_collection_statistics")32    >>> print(stats)33�)�
TYPE_CHECKING�Optional�Dict�Any�cast�Tuple)�defaultdict)�	OneOrMany�Where�maybe_cast_one_to_many)�STATISTICS_FUNCTION��34Collection)�AttachedFunction�35collectionr�returnc� �URS3$)z�Generate the default name for the statistics attached function.36 37Args:38    collection: The collection to generate the name for39 40Returns:41    str: The statistics function name42�_stats)�name)rs �_D:\code\apps\devtools\python\user_packages\Python313\site-packages\chromadb/utils/statistics.py�get_statistics_fn_namer's���o�o�
�f�%�%��stats_collection_namerc�@�UR[[U5USS9$)azAttach statistics collection function to a collection.43 44This attaches the statistics function which will automatically compute45and update metadata value frequencies whenever records are added, updated,46or deleted.47 48Args:49    collection: The collection to enable statistics for50    stats_collection_name: Name of the collection where statistics will be stored.51 52Returns:53    Tuple of (AttachedFunction, created) where created is True if newly created,54    False if already existed (idempotent request)55 56Example:57    >>> attached_fn, created = attach_statistics_function(collection, "my_collection_statistics")58    >>> if created:59    ...     print("Statistics function newly attached")60    >>> collection.add(ids=["id1"], documents=["doc1"], metadatas=[{"key": "value"}])61    >>> # Statistics are automatically computed62    >>> stats = get_statistics(collection, "my_collection_statistics")63N)�functionr�output_collection�params)�attach_functionr
r)rrs  r�attach_statistics_functionr3s-��2�%�%�$�
#�J�
/�/��	&��rc�h�UR[U55nURS:XdS5eU$)a<Get the statistics attached function for a collection.64 65Args:66    collection: The collection to get the statistics function for67 68Returns:69    AttachedFunction: The statistics function70 71Raises:72    NotFoundError: If statistics are not enabled73    AssertionError: If the attached function is not a statistics function74�75statisticsz.Attached function is not a statistics function)�get_attached_functionr�
function_name)r�afs  r�get_statistics_fnr%Ts=��76�	)�	)�*@��*L�	M�B�77���L�(�8�7�8�(�
�Ir�delete_stats_collectionc�L�[U5nURURUS9$)a|Detach statistics collection function from a collection.78 79Args:80    collection: The collection to disable statistics for81    delete_stats_collection: If True, also delete the statistics output collection.82                              Defaults to False.83 84Returns:85    bool: True if successful86 87Example:88    >>> detach_statistics_function(collection, delete_stats_collection=True)89)�delete_output_collection)r%�detach_functionr)rr&�attached_fns   r�detach_statistics_functionr+hs2�� $�J�/�K��%�%����3J�&��rN�keysc���[U5nSnUb*[U5U:�a[S[U5SUS35eSSKJn UR90R
UURURS9nU"UR91USSS	9n[S925n0n	Sn93Ub[[SSUS
/-005OSnURS/X�S9nURS5=(d /n
U
(dO�U
H�nUcMURS5nURS5nURS5nURS5nURS5nUcMbUcMgUcMlUcMqUS
:XaUS:XaUU	S'M�M�UbUOUn[U[5(de[U[5(de[U[5(deUX�US'M� U94[U
5-
n95GM&S[!U50nU	(aU	US
'U$)a�Get the current statistics for a collection.96 97Statistics include frequency counts for all metadata key-value pairs,98as well as a summary with the total record count.99 100Args:101    collection: The collection to get statistics for102    stats_collection_name: Name of the statistics collection to read from.103    keys: Optional metadata key(s) to filter statistics for. Can be a single key104          string or a list of keys. If provided, only returns statistics for105          those specific keys.106 107Returns:108    Dict[str, Any]: A dictionary with the structure:109        {110            "statistics": {111                "key1": {112                    "value1": {"count": count, ...},113                    "value2": {"count": count, ...}114                },115                "key2": {...},116                ...117            },118            "summary": {119                "total_count": count120            }121        }122 123Example:124    >>> attach_statistics_function(collection, "my_collection_statistics")125    >>> collection.add(126    ...     ids=["id1", "id2"],127    ...     documents=["doc1", "doc2"],128    ...     metadatas=[{"category": "A", "score": 10}, {"category": "B", "score": 10}]129    ... )130    >>> # Wait for statistics to be computed131    >>> stats = get_statistics(collection, "my_collection_statistics")132    >>> print(stats)133    {134        "statistics": {135            "category": {136                "A": {"count": 1},137                "B": {"count": 1}138            },139            "score": {140                "10": {"count": 2}141            }142        },143        "summary": {144            "total_count": 2145        }146    }147 148Raises:149    ValueError: If more than 30 keys are provided in the keys filter.150�NzToo many keys provided: z. Maximum allowed is z[ keys per request. Consider calling get_statistics multiple times with smaller key batches.rr)r�tenant�database)�client�model�embedding_function�data_loaderc� �[[5$�N)r	�dict�rr�<lambda>� get_statistics.<locals>.<lambda>�s151��k�RV�FWr�keyz$in�summary�	metadatas)�include�offset�where�value�value_label�type�count�total_countr!)r�len�152ValueError�chromadb.api.models.Collectionr�_client�get_collectionr/r0r	rr�get�153isinstance�str�intr7)rrr,�	keys_list�MAX_KEYSr�stats_collection_model�stats_collection�statsr<r?�where_filter�pager=�metadata�meta_keyrArB�154value_typerD�	stats_key�results                      r�get_statisticsr[~sP��|'�t�,�I��H����Y��(�!:��&�s�9�~�&6�7"�"*��,W�
W�155�	156�:�(�/�/�>�>�
"�� � ��$�$�?���"��!�!�$���	��3>�>W�2X�E� �G�
�F�� �	
�U�U�U�I���$;�<�=�>�
����#�#� �M�&�$�157���H�H�[�)�/�R�	���!�H�����|�|�E�*�H��L�L��)�E�"�,�,�}�5�K�!���f�-�J��L�L��)�E��$��%��*��%��y�(��
�-�16��
�.�.�0;�/F��E�I�%�h��4�4�4�4�%�i��5�5�5�5�%�e�S�1�1�1�1�:?�E�O�I�.�w�7�3"�8	�#�i�.� ��K�N�D��K�
(�F��#��y���Mr)rrrr)Fr6)�__doc__�typingrrrrrr�collectionsr	�chromadb.api.typesr158rr�chromadb.api.functionsr
rHr�$chromadb.api.models.AttachedFunctionrrMr�boolrr%r+r[r8rr�<module>rcs����8C�B�#�G�G�6��9�E�	&�|�	&��	&����58��159��t�#�$��B�*?D����7;��	��2&*�R��R��R��9�S�>�160"�R�161�#�s�(�^�	Rr
codekingpro/portable-devtools · Team Ai