U
    Ph`"                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZmZ d dl	Z
d dlZd dlmZmZmZ daddd	gZd
ddddZd
dddZG dd	 d	ZdS )    )annotationsN)deepcopy)AnyIterable)	TraceKeysfirstis_immutableTget_track_metaset_track_metaMetaObjboolNonevalreturnc                 C  s   | a dS )aQ  
    Boolean to set whether metadata is tracked. If `True`, metadata will be associated
    its data by using subclasses of `MetaObj`. If `False`, then data will be returned
    with empty metadata.

    If `set_track_meta` is `False`, then standard data objects will be returned (e.g.,
    `torch.Tensor` and `np.ndarray`) as opposed to MONAI's enhanced objects.

    By default, this is `True`, and most users will want to leave it this way. However,
    if you are experiencing any problems regarding metadata, and aren't interested in
    preserving metadata, then you can disable it.
    N_TRACK_META)r    r   H/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/meta_obj.pyr
      s    r   c                   C  s   t S )a[  
    Return the boolean as to whether metadata is tracked. If `True`, metadata will be
    associated its data by using subclasses of `MetaObj`. If `False`, then data will be
    returned with empty metadata.

    If `set_track_meta` is `False`, then standard data objects will be returned (e.g.,
    `torch.Tensor` and `np.ndarray`) as opposed to MONAI's enhanced objects.

    By default, this is `True`, and most users will want to leave it this way. However,
    if you are experiencing any problems regarding metadata, and aren't interested in
    preserving metadata, then you can disable it.
    r   r   r   r   r   r	   .   s    c                   @  sZ  e Zd ZdZddddZedddd	Zed
d Zd5ddZeddddZ	eddddZ
ddddZeddddZejddddZeddddZejddddZd dd!d"d#Zd dd$d%Zeddd&d'Zed(dd)d*Zd dd!d+d,Zd dd-d.Zd dd/d0Zed(dd1d2Zejd(dd3d4d2ZdS )6r   a|  
    Abstract base class that stores data as well as any extra metadata.

    This allows for subclassing `torch.Tensor` and `np.ndarray` through multiple inheritance.

    Metadata is stored in the form of a dictionary.

    Behavior should be the same as extended class (e.g., `torch.Tensor` or `np.ndarray`)
    aside from the extended meta functionality.

    Copying of information:

        * For `c = a + b`, then auxiliary data (e.g., metadata) will be copied from the
          first instance of `MetaObj` if `a.is_batch` is False
          (For batched data, the metadata will be shallow copied for efficiency purposes).

    r   r   c                 C  s(   t  | _t  | _t  | _d| _d S )NF)r   get_default_meta_metaget_default_applied_operations_applied_operations_pending_operations	_is_batchselfr   r   r   __init__Q   s    


zMetaObj.__init__r   )argsc                  g  sD   t j|  D ]4}t|ttfr.t|E dH  q
t|tr
|V  q
dS )a  
        Recursively flatten input and yield all instances of `MetaObj`.
        This means that for both `torch.add(a, b)`, `torch.stack([a, b])` (and
        their numpy equivalents), we return `[a, b]` if both `a` and `b` are of type
        `MetaObj`.

        Args:
            args: Iterables of inputs to be flattened.
        Returns:
            list of nested `MetaObj` from input.
        N)	itertoolschain
isinstancelisttupler   flatten_meta_objs)r   ar   r   r   r%   W   s
    
zMetaObj.flatten_meta_objsc                 C  sF   t | r| S t| tttjfr&|  S t| tjr>| 	 
 S t| S )zUreturns a copy of the data. list and dict are shallow copied for efficiency purposes.)r   r"   r#   dictnpndarraycopytorchTensordetachcloner   )datar   r   r   
copy_itemsj   s    zMetaObj.copy_itemsTNc                   sz   t |tr|n
t|| d t ds(| S  j |dkr>  n|}|s\ fdd|D | _n| j fdd|D  | S )a  
        Copy metadata from a `MetaObj` or an iterable of `MetaObj` instances.

        Args:
            input_objs: list of `MetaObj` to copy data from.
            copy_attr: whether to copy each attribute with `MetaObj.copy_item`.
                note that if the attribute is a nested list or dict, only a shallow copy will be done.
            keys: the keys of attributes to copy from the ``input_objs``.
                If None, all keys from the input_objs will be copied.
        )default__dict__Nc                   s   i | ]}| kr| | qS r   r   .0r&   Z
first_metar   r   
<dictcomp>   s       z*MetaObj.copy_meta_from.<locals>.<dictcomp>c                   s$   i | ]}| kr|t  | qS r   )r   r0   r3   r5   r   r   r6      s       )r"   r   r   hasattrr2   keysupdate)r   Z
input_objsZ	copy_attrr8   r   r5   r   copy_meta_fromu   s    
zMetaObj.copy_meta_fromr'   c                   C  s   i S )zNGet the default meta.

        Returns:
            default metadata.
        r   r   r   r   r   r      s    zMetaObj.get_default_metar#   c                   C  s   g S )zfGet the default applied operations.

        Returns:
            default applied operations.
        r   r   r   r   r   r      s    z&MetaObj.get_default_applied_operationsstrc                 C  s~   d}| j dk	r.|ddd | j  D 7 }n|d7 }|d7 }| jdk	rb|tj| jdd	d
d7 }n|d7 }|d| j 7 }|S )zString representation of class.z

Metadata
N c                 s  s$   | ]\}}d | d| dV  qdS )	z: 
Nr   )r4   kvr   r   r   	<genexpr>   s     z#MetaObj.__repr__.<locals>.<genexpr>r   z
Applied operations
   Tx   )indentcompactwidthz
Is batch?: )metajoinitemsapplied_operationspprintpformatis_batch)r   outr   r   r   __repr__   s    
 
zMetaObj.__repr__c                 C  s   t | dr| jS t S )z!Get the meta. Defaults to ``{}``.r   )r7   r   r   r   r   r   r   r   rG      s    zMetaObj.metac                 C  s    |t jkrt | _n|| _dS )zSet the meta.N)r   NONEr   r   r   )r   dr   r   r   rG      s    
z
list[dict]c                 C  s   t | dr| jS t S )z/Get the applied operations. Defaults to ``[]``.r   )r7   r   r   r   r   r   r   r   rJ      s    
zMetaObj.applied_operationsc                 C  s"   |t jkrt | _dS || _dS )zSet the applied operations.N)r   rP   r   r   r   r   tr   r   r   rJ      s    

r   )rS   r   c                 C  s   | j | d S N)r   appendrR   r   r   r   push_applied_operation   s    zMetaObj.push_applied_operationc                 C  s
   | j  S rT   )r   popr   r   r   r   pop_applied_operation   s    zMetaObj.pop_applied_operationc                 C  s   t | dr| jS t S )z/Get the pending operations. Defaults to ``[]``.r   )r7   r   r   r   r   r   r   r   pending_operations   s    
zMetaObj.pending_operationsr   c                 C  s   | j dk	ot| j dkS )z
        Determine whether there are pending operations.
        Returns:
            True if there are pending operations; False if not
        Nr   )rY   lenr   r   r   r   has_pending_operations   s    zMetaObj.has_pending_operationsc                 C  s   | j | d S rT   )r   rU   rR   r   r   r   push_pending_operation   s    zMetaObj.push_pending_operationc                 C  s
   | j  S rT   )r   rW   r   r   r   r   pop_pending_operation   s    zMetaObj.pop_pending_operationc                 C  s   t  | _d S rT   )r   r   r   r   r   r   r   clear_pending_operations   s    z MetaObj.clear_pending_operationsc                 C  s   t | dr| jS dS )z.Return whether object is part of batch or not.r   F)r7   r   r   r   r   r   rM      s    zMetaObj.is_batchr   c                 C  s
   || _ dS )z+Set whether object is part of batch or not.N)r   )r   r   r   r   r   rM      s    )TN)__name__
__module____qualname____doc__r   staticmethodr%   r0   r:   r   r   rO   propertyrG   setterrJ   rV   rX   rY   r[   r\   r]   r^   rM   r   r   r   r   r   >   sB   


)
__future__r   r    rK   r*   r   typingr   r   numpyr(   r+   monai.utilsr   r   r   r   __all__r
   r	   r   r   r   r   r   <module>   s   
