o  ij;@sddlmZddlmZmZddlmZddlZddlm Z ddl m Z ddl m Z ddlmZdd lmZmZmZmZmZdd lmZdd lmZdd lmZmZgd ZhdZddddd.ddZd/d0ddZ  d/d1d%d&Z! d2d3d'd(Z"d4d5d,d-Z#dS)6) annotations)MappingSequence)AnyN) get_logger)NdarrayOrTensor) MetaTensor) to_affine_nd)affine_from_pendingcombine_transformsis_compatible_apply_kwargskwargs_from_pendingresample) LazyTrait) MapTransform)LazyAttrlook_up_option)apply_pending_transforms!apply_pending_transforms_in_order apply_pending> align_cornersdevice resample_mode padding_modemodedtypeF)lazykey logger_name transformrdataactivitystrr bool | Noner str | Noner bool | strc Cs4|durdSt|tr |nd}t|}t|j}t|tr1d|j}|dur0||jkr0|d7}nd}|d|d|d|} t|trx|durL|jn|f} | D]$} | |vrut|| t ret || j nd } | | j | d | d d qQdSt|t rt |j nd } | | j | |durd nd |d d dS)NFrz, transform.lazy: z (overridden)z, transform is not lazyz - lazy: z(, {key_msg}pending: {pcount}, upcoming ''rkey: '', )pcountZkey_msg) isinstancer"rtype__name__rrrkeysrlenpending_operationsinfoformat) rr r!rrrloggerZtcnameZtlazymsgZtransform_keyskr)r6b/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/lazy/functional.py_log_pending_info's,      ,r8cCs^|durdSt|tr |nd}t|}|durdnd|d}|d|dt|jdS)NFrr*r'r(zPending transforms applied: zapplied_operations: )r+r"rr1r/applied_operations)r rrr3Zkey_strr6r6r7_log_applied_infoJs  r:QNdarrayOrTensor | Sequence[Any | NdarrayOrTensor] | Mapping[Any, NdarrayOrTensor]r. tuple | None overrides dict | Nonec sttrfddDSttr"tfddDSttrofddD}fdd|D}t|dkrmt}|D]#}durOdn|d}t||d \||<} t|||d qG|SStt rj rtd \}} t|d |SS) a= apply_pending_transforms is called with either a tensor or a dictionary, some entries of which contain tensors. When operating on a dictionary of tensors, the 'keys' parameter determines what tensors should be checked. If 'keys' is not set, all keys of 'data' are considered. This method optionally takes a set of overrides that can be used to change specific parameters on the transform pipeline. See ``Compose`` for more details. This method takes a logger_name that can be used to override the default logger, to provide telemetry during the execution of pending transforms. This method is intended primarily for use by ``execute_compose`` and other methods that handle the underlying execution of transform pipelines. You should not need to use it in the general case, unless you are developing functionality to perform such operations. Args: data: a ``torch.Tensor`` or ``MetaTensor``, or dictionary of tensors. keys: an optional tuple of keys that filters the keys on 'data' if it is a dict overrides: An optional dictionary that specifies parameters that can be used to override transform arguments when they are called. When 'data' is a dict, this dictionary should contain a dictionary of overrides for each key that needs them logger_name: An optional name for a logger to be used when applying pending transforms. If None, logging is suppressed. Returns: an object of the same type as data if pending transforms were applied, or 'data' if they were not csg|] }t|qSr6r.0dr.rr=r6r7 usz,apply_pending_transforms..c3s|] }t|VqdSNr?r@rCr6r7 wsz+apply_pending_transforms..cs g|] }dus |vr|qSrEr6rAr5)r.r6r7rD|s cs(g|]}t|tr|jr|qSr6)r+rhas_pending_operationsrG)r r6r7rD}s(rN)r=)rr)r) r+listtupledictr.r/getrr:rrH) r r.r=rZ active_keysZkeys_to_updateZrdatar5Z overrides__r6)r r.rr=r7rTs(     rcCsddlm}d}t||r|jnd}t|tr$|js$|dur!|jn| }|dur8t||d||dt||||St||d||d|S)aF This method causes "in order" processing of pending transforms to occur. "in order" processing of pending transforms ensures that all pending transforms have been applied to the tensor before a non-lazy transform (or lazy transform that is executing non-lazily) is carried out. It ensures that no operations will be added to a metatensor's apply_operations while there are outstanding pending_operations. Note that there is only one mechanism for executing lazy resampling at present but this is expected to change in future releases. Evaluation of pending transforms is performed under the following circumstances: * If the transform is a lazy transform and: * The transform checks data as part of its execution, or * the transform is not executing lazily * If the transform is an ApplyPending[d] transform * If the transform is not a lazy transform This method is designed to be used only in the context of implementing lazy resampling functionality. In general you should not need to interact with or use this method directly, and its API may change without warning between releases. See the :ref:`Lazy Resampling topic for more information about lazy resampling. Args: transform: a transform that should be evaluated to determine whether pending transforms should be applied data: a tensor / MetaTensor, or dictionary containing tensors / MetaTensors whose pending transforms may need to be applied lazy: The lazy mode that is being applied (this can be False, True or None) overrides: An optional dictionary containing overrides to be applied to the pending transforms when they are lazily executed. If data is a dict, it should contain a dictionary of overrides for each key that needs them logger_name: An optional name for a logger to be used when applying pending transforms. If None, logging is suppressed. Returns: an object of the same type as data if pending transforms were applied, or 'data' if they were not r) ApplyPendingdTNzApply pending transforms)rrzAccumulate pending transforms) Z monai.transforms.lazy.dictionaryrNr+r.rrequires_current_datarr8r)rr rr=rrNZmust_apply_pendingr.r6r6r7rs $rtorch.Tensor | MetaTensorpending list | Nonec Cs|pi}|D]}t|tqt|tr"|dur"|j}||dur(gn|}|s0|gfSt|d}|jddkrBt d|}t |d}i}d|vrU|d|t j <d|vr`|d|t j <d|vrk|d|t j<d|vrv|d|t j<|dtj}|dur|jn||t j<|d }|d dD]7} t | } t|| s|} | |t|||| }t| } | jddkrt d| } t|| }|| q||t||||}t|tr|D]} || q||fS) a  This method applies pending transforms to `data` tensors. Currently, only 2d and 3d inputs are supported. This method is designed to be called by ``apply_pending_transforms`` and other methods / classes that are part of the implementation of lazy resampling. In general, you should not need to call this method unless you are directly developing custom lazy execution strategies. It works by calculating the overall effect of the accumulated pending transforms. When it runs out of pending transforms or when it finds incompatibilities between the accumulated pending transform and the next pending transform, it then applies the accumulated transform in a call to ``resample``. Pending transforms are incompatible with each other if one or more of the arguments in the pending transforms differ. These are parameters such as 'mode', 'padding_mode', 'dtype' and so forth. If a pending transform doesn't have a given parameter, it is considered compatible with the accumulated transform. If a subsequent transform has a parameter that is incompatible with the accumulated transform (e.g. 'mode' of 'bilinear' vs. 'mode' of 'nearest'), an intermediate resample will be performed and the accumulated transform reset to its starting state. After resampling, the pending transforms are pushed to the ``applied_transforms`` field of the resulting MetaTensor. Note, if a torch.tensor is passed to this method along with a list of pending transforms, the resampled tensor will be wrapped in a MetaTensor before being returned. Args: data: A torch Tensor or a monai MetaTensor. pending: pending transforms. This must be set if data is a Tensor, but is optional if data is a MetaTensor. overrides: a dictionary of overrides for the transform arguments. The keys must be one of: - mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order ``0-5`` (integers). Interpolation mode to calculate output values. Defaults to None. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html When it's `an integer`, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used and the value represents the order of the spline interpolation. See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html - padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``} Padding mode for outside grid values. Defaults to None. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html When `mode` is an integer, using numpy/cupy backends, this argument accepts {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}. See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html - dtype: data type for resampling computation. Defaults to ``float64``. If ``None``, use the data type of input data, this option may not be compatible the resampling backend. - align_corners: Geometrically, we consider the pixels of the input as squares rather than points, when using the PyTorch resampling backend. Defaults to ``False``. See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html - device: device for resampling computation. Defaults to ``None``. - resample_mode: the mode of resampling, currently support ``"auto"``. Setting to other values will use the :py:class:`monai.transforms.SpatialResample` for resampling (instead of potentially crop/pad). Nrrrrrrr)copyr__override_keywordsr+rr0clear_pending_operationsr shaper r r INTERP_MODE PADDING_MODE ALIGN_CORNERS RESAMPLE_MODErLtorchfloat64rDTYPEr updatertor push_applied_operation) r rQr=r5Zcumulative_xformZ cur_kwargsZoverride_kwargsZoverride_dtyperp new_kwargsZ _cur_kwargs next_matrixr6r6r7rsT 3              r) rrr rr!r"rr#rr$rr%)NF)r rrr%)r r;r.r<r=r>rr%)NNF)rr#r=r>rr%)NN)r rPrQrRr=r>)$ __future__rcollections.abcrrtypingrr]monai.apps.utilsr monai.configrmonai.data.meta_tensorrmonai.data.utilsr Zmonai.transforms.lazy.utilsr r r r rmonai.transforms.traitsrmonai.transforms.transformr monai.utilsrr__all__rVr8r:rrrr6r6r6r7s2         #  > 3