o # i{"@sddlmZddlZddlZddlmZddlmZddlm Z ddl Z ddl Z ddl mZmZmZdagdZdd dZdddZGdddZdS)) annotationsN)Iterable)deepcopy)Any) TraceKeysfirst is_immutableT)get_track_metaset_track_metaMetaObjvalboolreturnNonecCs|adS)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 rU/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/data/meta_obj.pyr sr cCstS)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. rrrrrr /s r c@s eZdZdZd5ddZed6dd Zed d Zd7ddZed8ddZ ed9ddZ d:ddZ e d8ddZ e jd5ddZ e d;ddZejd5ddZdd)d*Zdd1d2Zejd?d4d2Zd 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). rrcCs(t|_t|_t|_d|_dS)NF)r get_default_meta_metaget_default_applied_operations_applied_operations_pending_operations _is_batchselfrrr__init__Rs    zMetaObj.__init__argsrcgsFtj|D]}t|ttfrt|EdHqt|tr |VqdS)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) itertoolschain isinstancelisttupler flatten_meta_objs)rarrrr#Xs  zMetaObj.flatten_meta_objscCsFt|r|St|tttjfr|St|tjr| St |S)zUreturns a copy of the data. list and dict are shallow copied for efficiency purposes.) rr r!dictnpndarraycopytorchTensordetachcloner)datarrr copy_itemsks  zMetaObj.copy_itemsTNcs|t|tr|nt||dtds|Sj|durn|}|s/fdd|D|_|S|jfdd|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__Ncsi|] }|vr||qSrr.0r$Z first_metarr sz*MetaObj.copy_meta_from..cs$i|]}|vr|t|qSr)r r.r1r3rrr4s$)r r rhasattrr0keysupdate)rZ input_objsZ copy_attrr6rr3rcopy_meta_fromvs zMetaObj.copy_meta_fromr%cCsiS)zNGet the default meta. Returns: default metadata. rrrrrrzMetaObj.get_default_metar!cCsgS)zfGet the default applied operations. Returns: default applied operations. rrrrrrr9z&MetaObj.get_default_applied_operationsstrcCs~d}|jdur|ddd|jD7}n|d7}|d7}|jdur1|tj|jdd d d 7}n|d7}|d |j7}|S) zString representation of class.z Metadata Ncss&|]\}}d|d|dVqdS) z:  Nr)r2kvrrr s$z#MetaObj.__repr__..rz Applied operations Tx)indentcompactwidthz Is batch?: )metajoinitemsapplied_operationspprintpformatis_batch)routrrr__repr__s   zMetaObj.__repr__cCt|dr|jStS)z!Get the meta. Defaults to ``{}``.r)r5rr rrrrrrFsz MetaObj.metacC"|tjkr t|_dS||_dS)z Set the meta.N)rNONEr rr)rdrrrrFs   list[dict]cCrO)z/Get the applied operations. Defaults to ``[]``.r)r5rr rrrrrrI zMetaObj.applied_operationscCrP)zSet the applied operations.N)rrQr rrrtrrrrIs   rVrcC|j|dSN)rappendrUrrrpush_applied_operationzMetaObj.push_applied_operationcC |jSrX)rpoprrrrpop_applied_operation zMetaObj.pop_applied_operationcCrO)z/Get the pending operations. Defaults to ``[]``.r)r5rr rrrrrpending_operationsrTzMetaObj.pending_operationsr cCs|jduo t|jdkS)z Determine whether there are pending operations. Returns: True if there are pending operations; False if not Nr)r`lenrrrrhas_pending_operationsszMetaObj.has_pending_operationscCrWrX)rrYrUrrrpush_pending_operationr[zMetaObj.push_pending_operationcCr\rX)rr]rrrrpop_pending_operationr_zMetaObj.pop_pending_operationcCst|_dSrX)r rrrrrrclear_pending_operationssz MetaObj.clear_pending_operationscCst|dr|jSdS)z.Return whether object is part of batch or not.rF)r5rrrrrrLszMetaObj.is_batchr cCs ||_dS)z+Set whether object is part of batch or not.N)r)rr rrrrLs )rr)rr)TN)rr%)rr!)rr:)rrS)rVrrr)rrrr r r rr)__name__ __module__ __qualname____doc__r staticmethodr#r.r8rrrNpropertyrFsetterrIrZr^r`rbrcrdrerLrrrrr ?sD                   r rgrf) __future__rrrJcollections.abcrr(rtypingrnumpyr&r) monai.utilsrrrr__all__r r r rrrrs