o
    &jIH                     @   st   d dl Z d dlZd dlZddddddddddd
Zd	d
 ZG dd deZG dd deZG dd deZe Z	dS )    Ni  
   d   ii,  i'  )
scalarimage	histogram
embeddingsaudiopr_curve	roc_curve	meta_datatexthyper_parametersc                 C   s   |  ddd | S )N%_)replace)tagZsub_tag r   ]/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/visualdl/server/data_manager.pyadd_sub_tag!   s   r   c                   @   s   e Zd ZdZd"ddZedd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!S )#	ReservoirzA map-to-arrays dict, with deterministic Reservoir Sampling.

    Store each reservoir bucket by key, and each bucket is a list sampling
    with reservoir algorithm.
    r   c                    sF   |dk s
|t |krtd|_t fdd_t _dS )a  Creates a new reservoir.

        Args:
            max_size: The number of values to keep in the reservoir for each tag,
                if max_size is zero, all values will be kept in bucket.
            seed: The seed to initialize a random.Random().
            num_item_index: The index of data to add.

        Raises:
          ValueError: If max_size is not a nonnegative integer.
        r   %Max_size must be nonnegative integer.c                      s   t jt dS )N)max_sizerandom_instance)_ReservoirBucket	_max_sizerandomRandomr   seedselfr   r   <lambda>;   s    z$Reservoir.__init__.<locals>.<lambda>N)	round
ValueErrorr   collectionsdefaultdict_buckets	threadingLock_mutex)r   r   r   r   r   r   __init__,   s
   zReservoir.__init__c                 C   s8   | j  t| j W  d   S 1 sw   Y  dS )zxReturn all keys in self._buckets.

        Returns:
            All keys in reservoir buckets.
        :return:
        N)r(   listr%   keysr   r   r   r   r+   ?   s   $zReservoir.keysc                 C   s   || j  v r	dS dS )zDetermine if key exists.

        Args:
            key: Key to determine if exists.

        Returns:
            True if key exists in buckets.keys, otherwise False.
        TF)r%   r+   r   keyr   r   r   _exist_in_keysJ   s   	zReservoir._exist_in_keysc                 C      |d | }|  |S )a2  Determine if run_tag exists.

        For usage habits of VisualDL, actually call self._exist_in_keys()

        Args:
            run: Identity of one tablet.
            tag: Identity of one record in tablet.

        Returns:
            True if run_tag exists in buckets.keys, otherwise False.
        /)r/   r   runr   r.   r   r   r   exist_in_keysU      
zReservoir.exist_in_keysc                 C   s&   | j }||vrtd| | j| jS )NKey %s not in buckets.keys())r+   KeyErrorr%   num_items_indexr   r.   r+   r   r   r   _get_num_items_indexd   s   zReservoir._get_num_items_indexc                 C   r0   )Nr1   )r:   r2   r   r   r   get_num_items_indexj   s   
zReservoir.get_num_items_indexc                 C   sP   | j }| j ||vrtd| | j| jW  d   S 1 s!w   Y  dS )zGet items with tag "key"

        Args:
            key: Key to finding bucket in reservoir buckets.

        Returns:
            One bucket in reservoir buckets by key.
        r6   N)r+   r(   r7   r%   itemsr9   r   r   r   
_get_itemsn   s   	
$zReservoir._get_itemsc                 C   r0   )a%  Get items with tag 'run_tag'

        For usage habits of VisualDL, actually call self._get_items()

        Args:
            run: Identity of one tablet.
            tag: Identity of one record in tablet.

        Returns:
            One bucket in reservoir buckets by run and tag.
        r1   )r=   r2   r   r   r   	get_items}   r5   zReservoir.get_itemsc                 C   <   | j  | j| | W d   dS 1 sw   Y  dS )a  Add a new item to reservoir buckets with given tag as key.

        If bucket with key has not yet reached full size, each item will be
        added.

        If bucket with key is full, each item will be added with same
        probability.

        Add new item to buckets will always valid because self._buckets is a
        collection.defaultdict.

        Args:
            key: Tag of one bucket to add new item.
            item: New item to add to bucket.
        N)r(   r%   add_itemr   r.   itemr   r   r   	_add_item      "zReservoir._add_itemc                 C   r?   a  Add a new scalar item to reservoir buckets with given tag as key.

        If bucket with key has not yet reached full size, each item will be
        added.

        If bucket with key is full, each item will be added with same
        probability.

        Add new item to buckets will always valid because self._buckets is a
        collection.defaultdict.

        Args:
            key: Tag of one bucket to add new item.
            item: New item to add to bucket.
        N)r(   r%   add_scalar_itemrA   r   r   r   _add_scalar_item   rD   zReservoir._add_scalar_itemc                 C   r?   rE   )r(   r%   add_scalars_itemrA   r   r   r   _add_scalars_item   rD   zReservoir._add_scalars_itemc                 C   s   |d | }|  || dS )a"  Add a new item to reservoir buckets with given tag as key.

        For usage habits of VisualDL, actually call self._add_items()

        Args:
            run: Identity of one tablet.
            tag: Identity of one record in tablet.
            item: New item to add to bucket.
        r1   N)rC   r   r3   r   rB   r.   r   r   r   r@      s   
zReservoir.add_itemc                 C   sx   | ddkr|d | }| || dS | ddkr3|d t||jj d | }| || dS td| d )a)  Add a new scalar item to reservoir buckets with given tag as key.

        For usage habits of VisualDL, actually call self._add_items()

        Args:
            run: Identity of one tablet.
            tag: Identity of one record in tablet.
            item: New item to add to bucket.
        	one_valuevaluer1   	tag_valuezNot scalar type:N)Z
WhichOneofrG   r   rM   r   rI   r"   rJ   r   r   r   rF      s   
zReservoir.add_scalar_itemc                 C   s:   | j  | j|   W d    d S 1 sw   Y  d S N)r(   r%   cut_tailr-   r   r   r   	_cut_tail   s   "zReservoir._cut_tailc                 C   s   |d | }|  | dS )a  Pop the last item in reservoir buckets.

        Sometimes the tail of the retrieved data is abnormal 0. This
        method is used to handle this problem.

        Args:
            run: Identity of one tablet.
            tag: Identity of one record in tablet.
        r1   N)rP   r2   r   r   r   rO      s   
zReservoir.cut_tailN)r   )__name__
__module____qualname____doc__r)   propertyr+   r/   r4   r:   r;   r=   r>   rC   rG   rI   r@   rF   rP   rO   r   r   r   r   r   %   s$    


r   c                   @   sR   e Zd ZdZdddZdd Zdd Zd	d
 Zedd Z	edd Z
dd ZdS )r   z<Data manager for sampling data, use reservoir sampling.
    Nc                 C   sf   |dk s
|t |krtd|| _|dur|ntd| _g | _t | _	d| _
d| _d| _d| _dS )a  Create a _ReservoirBucket instance.

        Args:
            max_size: The maximum size of reservoir bucket. If max_size is
                zero, the bucket has unbounded size.
            random_instance: The random number generator. If not specified,
                default to random.Random(0)
            num_item_index: The index of data to add.

        Raises:
            ValueError: If args max_size is not a nonnegative integer.
        r   r   NF)r!   r"   r   r   r   _random_itemsr&   r'   r(   _num_items_index
max_scalar
min_scalar_last_special)r   r   r   r   r   r   r)      s   

z_ReservoirBucket.__init__c                 C   s   | j C t| j| jk s| jdkr| j| n| jd| j}|| jk r2| j| | j| n|| jd< |  jd7  _W d   dS 1 sIw   Y  dS )a   Add an item to bucket, replacing an old item with probability.

        Use reservoir sampling to add a new item to sampling bucket,
        each item in a steam has same probability stay in the bucket.

        Args:
            item: The item to add to reservoir bucket.
        r      N)	r(   lenrW   r   appendrV   randintrX   popr   rB   rr   r   r   r@     s   	

"z_ReservoirBucket.add_itemc                 C   s  | j  | jr| jj|jk r|| _| jr| jj|jkr|| _t| j| jk s+| jdkr2| j| n| jrv| jd j	| jj	ksI| jd j	| jj	krMd| _n)| j
d| j}|| jkrv| jd | j| |  jd7  _	 W d   dS |j	| jj	ks|j	| jj	kr| j
d| jd }d| _n| j
d| j}d| _|| jk r| j| j	| jj	ks| j| j	| jj	kr|d dkr|d }n|d | jk r|d }| j| | j| |  jd7  _W d   dS 1 sw   Y  dS )ap   Add an scalar item to bucket, replacing an old item with probability.

        Use reservoir sampling to add a new item to sampling bucket,
        each item in a steam has same probability stay in the bucket.

        use _last_special mark to improve performance when data is monotonous

        Args:
            item: The item to add to reservoir bucket.
        r   r]   Fr\   NT)r(   rY   rL   rZ   r^   rW   r   r_   r[   idrV   r`   rX   ra   rb   r   r   r   rF   ,  sP   


"z _ReservoirBucket.add_scalar_itemc                 C   s  | j  | jr| jjj|jjk r|| _| jr| jjj|jjkr"|| _t| j| jk s/| jdkr6| j| n| j	rz| jd j
| jj
ksM| jd j
| jj
krQd| _	n)| jd| j}|| jkrz| jd | j| |  jd7  _	 W d   dS |j
| jj
ks|j
| jj
kr| jd| jd }d| _	n| jd| j}d| _	|| jk r| j| j
| jj
ks| j| j
| jj
kr|d dkr|d }n|d | jk r|d }| j| | j| n|| jd< |  jd7  _W d   dS 1 sw   Y  dS )a!   Add an scalar item to bucket, replacing an old item with probability.

        Use reservoir sampling to add a new item to sampling bucket,
        each item in a steam has same probability stay in the bucket.

        Args:
            item: The item to add to reservoir bucket.
        r   r]   Fr\   NT)r(   rY   rM   rL   rZ   r^   rW   r   r_   r[   rd   rV   r`   rX   ra   rb   r   r   r   rH   d  sR   	



"z!_ReservoirBucket.add_scalars_itemc                 C   s0   | j  | jW  d   S 1 sw   Y  dS )zAGet self._items

        Returns:
            All items.
        N)r(   rW   r,   r   r   r   r<     s   $z_ReservoirBucket.itemsc                 C   s0   | j  | jW  d    S 1 sw   Y  d S rN   )r(   rX   r,   r   r   r   r8     s   $z _ReservoirBucket.num_items_indexc                 C   sD   | j  | j  |  jd8  _W d   dS 1 sw   Y  dS )zPop the last item in reservoir buckets.

        Sometimes the tail of the retrieved data is abnormal 0. This
        method is used to handle this problem.
        r\   N)r(   rW   ra   rX   r,   r   r   r   rO     s   
"z_ReservoirBucket.cut_tailrN   )rQ   rR   rS   rT   r)   r@   rF   rH   rU   r<   r8   rO   r   r   r   r   r      s    
88
	
r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )DataManagerz!Data manager for all plugin.
    c                 C   s   t td dt td dt td dt td dt td dt td dt td dt td	 dt td
 dt td dd
| _t | _dS )zCreate a data manager for all plugin.

        All kinds of plugin has own reservoir, stored in a dict with plugin
        name as key.

        r   r   r   r   r   r   r	   r
   r   r   r   )
r   r   r   r   r   r	   r
   r   r   r   N)r   DEFAULT_PLUGIN_MAXSIZE_reservoirsr&   r'   r(   r,   r   r   r   r)     s   	zDataManager.__init__c                 C   sd   | j % || j vr | j|tt| di W d   dS W d   dS 1 s+w   Y  dS )zAdd reservoir to reservoirs.

        Every reservoir is attached to one plugin.

        Args:
            plugin: Key to get one reservoir bucket for one specified plugin.
        rf   N)r(   rh   r+   updater   rg   r   pluginr   r   r   add_reservoir  s   "zDataManager.add_reservoirc                 C   sN   | j  || j vrtd| | j| W  d   S 1 s w   Y  dS )zGet reservoir by plugin as key.

        Args:
            plugin: Key to get one reservoir bucket for one specified plugin.

        Returns:
            Reservoir bucket for plugin.
        zKey %s not in reservoirs.N)r(   rh   r+   r7   rj   r   r   r   get_reservoir  s
   	$zDataManager.get_reservoirc                 C   sv   | j . d|ksd|kr| jd ||| n| j| ||| W d   dS W d   dS 1 s4w   Y  dS )aV  Add item to one plugin reservoir bucket.

        Use 'run', 'tag' for usage habits of VisualDL.

        Args:
            plugin: Key to get one reservoir bucket.
            run: Each tablet has different 'run'.
            tag: Tag will be used to generate paths of tablets.
            item: The item to add to reservoir bucket.
        r   ZscalarsN)r(   rh   rF   r@   )r   rk   r3   r   rB   r   r   r   r@     s   "zDataManager.add_itemc                 C   s4   | j  | j W  d   S 1 sw   Y  dS )zTGet all plugin buckets name.

        Returns:
            All plugin keys.
        N)r(   rh   r+   r,   r   r   r   get_keys  s   $zDataManager.get_keysN)	rQ   rR   rS   rT   r)   rl   rm   r@   rn   r   r   r   r   re     s    re   )
r#   r   r&   rg   r   objectr   r   re   Zdefault_data_managerr   r   r   r   <module>   s*    S @
[