U PhO;@s,ddlmZddlmZmZmZddlZddlmZddl m Z ddl m Z ddl mZddlmZmZmZmZmZdd lmZdd lmZdd lmZmZd d dgZddddddhZdddddddddddddZd-dddd d!Zd.d"d#d$dd%d&d Z d/dd$dd'd(d Z!d0d)d*d$d+d,dZ"dS)1) annotations)AnyMappingSequenceN) get_logger)NdarrayOrTensor) MetaTensor) to_affine_nd)affine_from_pendingcombine_transformsis_compatible_apply_kwargskwargs_from_pendingresample) LazyTrait) MapTransform)LazyAttrlook_up_optionapply_pending_transforms!apply_pending_transforms_in_order apply_pendingmode padding_modedtype align_corners resample_modedeviceF)lazykey logger_namerstrz bool | Nonez str | Nonez bool | str) transformdataactivityrrrc Cs6|dkr dSt|tr|nd}t|}t|j}t|trbd|j}|dk rf||jkrf|d7}nd}|d|d|d|} t|tr|dkr|jn|f} | D]H} | |krt|| t rt || j nd } | | j | d | d d qnDt|t rt |j nd } | | j | |dkr d n d |d d dS)NFrz, transform.lazy: z (overridden)z, transform is not lazyz - lazy: z(, {key_msg}pending: {pcount}, upcoming ''rkey: '', )pcountZkey_msg) isinstancerrtype__name__rrrkeysrlenpending_operationsinfoformat) r r!r"rrrloggerZtcnameZtlazymsgZtransform_keyskr&r3U/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/transforms/lazy/functional.py_log_pending_info&s&        r5)r!rcCs^|dkr dSt|tr|nd}t|}|dkr2dn d|d}|d|dt|jdS)NFrr'r$r%zPending transforms applied: zapplied_operations: )r(rrr.r,applied_operations)r!rrr0Zkey_strr3r3r4_log_applied_infoIs r7zQNdarrayOrTensor | Sequence[Any | NdarrayOrTensor] | Mapping[Any, NdarrayOrTensor]z tuple | Nonez dict | None)r!r+ overridesrc sttr fddDSttrDtfddDSttr܇fddD}fdd|D}t|dkrt}|D]F}dkrdn |d}t||d \||<} t|||d q|Sn4tt 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|qSr3r.0dr+rr8r3r4 tsz,apply_pending_transforms..c3s|]}t|VqdSNr9r:r=r3r4 vsz+apply_pending_transforms..cs g|]}dks|kr|qSr?r3r;r2)r+r3r4r>{scs(g|] }t|tr|jr|qSr3)r(rhas_pending_operationsrA)r!r3r4r>|s rN)r8)rr)r) r(listtupledictr+r,getrr7rrB) r!r+r8rZ active_keysZkeys_to_updateZrdatar2Z overrides__r3)r!r+rr8r4rSs&     )rr8rcCsddlm}d}t||r |jnd}t|trH|jsH|dkrB|jn| }|dkrpt||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.dictionaryrHr(r+rrequires_current_datarr5r)r r!rr8rrHZmust_apply_pendingr+r3r3r4rs$ ztorch.Tensor | MetaTensorz list | None)r!pendingr8c Cs|pi}|D]}t|tqt|trD|dkrD|j}||dkrPgn|}|s`|gfSt|d}|jddkrt d|}t |d}i}d|kr|d|t j <d|kr|d|t j <d|kr|d|t j<d|kr|d|t j<|dtj}|dkr |jn||t j<|d }|d dD]t} t | } t|| sf|} | |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(rr-clear_pending_operationsr shaper r r INTERP_MODE PADDING_MODE ALIGN_CORNERS RESAMPLE_MODErFtorchfloat64rDTYPEr updatertor push_applied_operation) r!rJr8r2Zcumulative_xformZ cur_kwargsZoverride_kwargsZoverride_dtyperp new_kwargsZ _cur_kwargs next_matrixr3r3r4rsT3             )NF)NF)NNF)NN)# __future__rtypingrrrrUmonai.apps.utilsr monai.configrmonai.data.meta_tensorrmonai.data.utilsr Zmonai.transforms.lazy.utilsr r r r rmonai.transforms.traitsrmonai.transforms.transformr monai.utilsrr__all__rNr5r7rrrr3r3r3r4 s2        # >3