o
    #i{"                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
Zd dlZd dlmZmZmZ dag dZdddZdddZG dd dZdS )    )annotationsN)Iterable)deepcopy)Any)	TraceKeysfirstis_immutableT)get_track_metaset_track_metaMetaObjvalboolreturnNonec                 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   U/home/dell461/cl/sdc2/last_ska_mid/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   r	   c                   @  s  e Zd ZdZd5ddZed6dd	Zed
d Zd7ddZed8ddZ	ed9ddZ
d:ddZed8ddZejd5ddZed;ddZejd5ddZd<d"d#Zd=d$d%Zed;d&d'Zed>d)d*Zd<d+d,Zd=d-d.Zd=d/d0Zed>d1d2Zejd?d4d2ZdS )@r   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__R   s   



zMetaObj.__init__argsr   c                  g  sF    t j|  D ]}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#   X   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_itemsk   s   zMetaObj.copy_itemsTNc                   s|   t |tr|nt|| d t ds| S  j |du r  n|}|s/ fdd|D | _| S | 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 | ]}| v r| | qS r   r   .0r$   Z
first_metar   r   
<dictcomp>   s    z*MetaObj.copy_meta_from.<locals>.<dictcomp>c                   s$   i | ]}| v r|t  | qS r   )r   r.   r1   r3   r   r   r4      s   $ )r    r   r   hasattrr0   keysupdate)r   Z
input_objsZ	copy_attrr6   r   r3   r   copy_meta_fromv   s   
zMetaObj.copy_meta_fromr%   c                   C  s   i S )zNGet the default meta.

        Returns:
            default metadata.
        r   r   r   r   r   r         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      r9   z&MetaObj.get_default_applied_operationsstrc                 C  s~   d}| j dur|ddd | j  D 7 }n|d7 }|d7 }| jdur1|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   )r2   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     t | dr| jS t S )z!Get the meta. Defaults to ``{}``.r   )r5   r   r   r   r   r   r   r   rF      s   zMetaObj.metac                 C  "   |t jkrt | _dS || _dS )zSet the meta.N)r   NONEr   r   r   )r   dr   r   r   rF      s   


list[dict]c                 C  rO   )z/Get the applied operations. Defaults to ``[]``.r   )r5   r   r   r   r   r   r   r   rI         
zMetaObj.applied_operationsc                 C  rP   )zSet the applied operations.N)r   rQ   r   r   r   r   tr   r   r   rI      s   


rV   r   c                 C     | j | d S N)r   appendrU   r   r   r   push_applied_operation      zMetaObj.push_applied_operationc                 C  
   | j  S rX   )r   popr   r   r   r   pop_applied_operation      
zMetaObj.pop_applied_operationc                 C  rO   )z/Get the pending operations. Defaults to ``[]``.r   )r5   r   r   r   r   r   r   r   pending_operations   rT   zMetaObj.pending_operationsr   c                 C  s   | j duot| j dkS )z
        Determine whether there are pending operations.
        Returns:
            True if there are pending operations; False if not
        Nr   )r`   lenr   r   r   r   has_pending_operations   s   zMetaObj.has_pending_operationsc                 C  rW   rX   )r   rY   rU   r   r   r   push_pending_operation   r[   zMetaObj.push_pending_operationc                 C  r\   rX   )r   r]   r   r   r   r   pop_pending_operation   r_   zMetaObj.pop_pending_operationc                 C  s   t  | _d S rX   )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)r5   r   r   r   r   r   rL      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   rL      s   
)r   r   )r   r   )TN)r   r%   )r   r!   )r   r:   )r   rS   )rV   r   r   r   )r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   staticmethodr#   r.   r8   r   r   rN   propertyrF   setterrI   rZ   r^   r`   rb   rc   rd   re   rL   r   r   r   r   r   ?   sD    









r   rg   rf   )
__future__r   r   rJ   collections.abcr   r(   r   typingr   numpyr&   r)   monai.utilsr   r   r   r   __all__r
   r	   r   r   r   r   r   <module>   s   

