U Ph`"@sddlmZddlZddlZddlmZddlmZmZddl Z ddl Z ddl m Z mZmZdaddd gZd d d d dZd dddZGdd d ZdS)) annotationsN)deepcopy)AnyIterable) TraceKeysfirst is_immutableTget_track_metaset_track_metaMetaObjboolNonevalreturncCs|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)rrH/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/meta_obj.pyr srcCstS)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 c@sZeZdZdZddddZedddd Zed d Zd5ddZeddddZ eddddZ ddddZ e ddddZ e jddddZ e ddddZejddddZd dd!d"d#Zd dd$d%Ze ddd&d'Ze d(dd)d*Zd dd!d+d,Zd dd-d.Zd dd/d0Ze d(dd1d2Zejd(dd3d4d2Zd 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 rcCs(t|_t|_t|_d|_dS)NF)r get_default_meta_metaget_default_applied_operations_applied_operations_pending_operations _is_batchselfrrr__init__Qs   zMetaObj.__init__r)argscgsDtj|D]4}t|ttfr.t|EdHq t|tr |Vq dS)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%Ws  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_itemsjs  zMetaObj.copy_itemsTNcszt|tr|n t||dtds(|Sj|dkr>n|}|s\fdd|D|_n|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|]}|kr||qSrr.0r&Z first_metarr sz*MetaObj.copy_meta_from..cs$i|]}|kr|t|qSr)r r0r3r5rrr6s)r"r rhasattrr2keysupdate)rZ input_objsZ copy_attrr8rr5rcopy_meta_fromus  zMetaObj.copy_meta_fromr'cCsiS)zNGet the default meta. Returns: default metadata. rrrrrrszMetaObj.get_default_metar#cCsgS)zfGet the default applied operations. Returns: default applied operations. rrrrrrsz&MetaObj.get_default_applied_operationsstrcCs~d}|jdk r.|ddd|jD7}n|d7}|d7}|jdk rb|tj|jdd d d 7}n|d7}|d |j7}|S) zString representation of class.z Metadata Ncss$|]\}}d|d|dVqdS) z:  Nr)r4kvrrr sz#MetaObj.__repr__..r z Applied operations Tx)indentcompactwidthz Is batch?: )metajoinitemsapplied_operationspprintpformatis_batch)routrrr__repr__s   zMetaObj.__repr__cCst|dr|jStS)z!Get the meta. Defaults to ``{}``.r)r7rr rrrrrrGsz MetaObj.metacCs |tjkrt|_n||_dS)z Set the meta.N)rNONEr rr)rdrrrrGs  z list[dict]cCst|dr|jStS)z/Get the applied operations. Defaults to ``[]``.r)r7rr rrrrrrJs zMetaObj.applied_operationscCs"|tjkrt|_dS||_dS)zSet the applied operations.N)rrPr rrrtrrrrJs  r)rSrcCs|j|dSN)rappendrRrrrpush_applied_operationszMetaObj.push_applied_operationcCs |jSrT)rpoprrrrpop_applied_operationszMetaObj.pop_applied_operationcCst|dr|jStS)z/Get the pending operations. Defaults to ``[]``.r)r7rr rrrrrpending_operationss zMetaObj.pending_operationsr cCs|jdk ot|jdkS)z Determine whether there are pending operations. Returns: True if there are pending operations; False if not Nr)rYlenrrrrhas_pending_operationsszMetaObj.has_pending_operationscCs|j|dSrT)rrUrRrrrpush_pending_operationszMetaObj.push_pending_operationcCs |jSrT)rrWrrrrpop_pending_operationszMetaObj.pop_pending_operationcCst|_dSrT)r rrrrrrclear_pending_operationssz MetaObj.clear_pending_operationscCst|dr|jSdS)z.Return whether object is part of batch or not.rF)r7rrrrrrMszMetaObj.is_batchrcCs ||_dS)z+Set whether object is part of batch or not.N)r)rrrrrrMs)TN)__name__ __module__ __qualname____doc__r staticmethodr%r0r:rrrOpropertyrGsetterrJrVrXrYr[r\r]r^rMrrrrr >sB ) __future__rr rKr*rtypingrrnumpyr(r+ monai.utilsrrrr__all__r r r rrrr s