U Php&@sddlmZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZmZmZmZddgZGd d d ZGd d d Zd d d dddZddZddZddZe fddZeedhZdd dddddZdS)) annotationsN)NdarrayOrTensor) AFFINE_TOL)allclose)LazyAttrconvert_to_numpyconvert_to_tensorlook_up_optionresamplecombine_transformsc@s(eZdZdZdZddZeddZdS)Affinez0A class to represent an affine transform matrix.datacCs ||_dSNr selfrrP/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/transforms/lazy/utils.py__init__!szAffine.__init__cCsZt|trdSt|trdSt|dr4t|jdkr8dS|jddkoX|jd|jdkS)z&Check if the data is an affine matrix.TFshape)) isinstancer DisplacementFieldhasattrlenrr rrris_affine_shaped$s  zAffine.is_affine_shapedN)__name__ __module__ __qualname____doc__ __slots__r staticmethodrrrrrr s r c@s(eZdZdZdZddZeddZdS)rz0A class to represent a dense displacement field.r cCs ||_dSrr rrrrr5szDisplacementField.__init__cCsDt|trdSt|trdSt|dr4t|jdkr8dSt| S)zCheck if the data is a DDF.TFrr)rrr rrrrr rrr is_ddf_shaped8s  zDisplacementField.is_ddf_shapedN)r r!r"r#r$rr%r&rrrrr0s rz torch.Tensor)leftrightreturncCst|rXt|rXtt|tr&|jn|dd}tt|trB|jn|dd}t||St|rt|rtt|tr~|jn|dd}tt|tr|jn|dd}||St dS)znGiven transforms A and B to be applied to x, return the combined transform (AB), so that A(B(x)) becomes AB(x)T wrap_sequenceN) r rrrrtorchmatmulrr&NotImplementedError)r'r(rrrr Ds cCs.t|tjtjfr|St|tr*|tjS|S)z8Extract the affine matrix from a pending transform item.)rr,TensornpndarraydictrAFFINE) pending_itemrrraffine_from_pendingSs   r5cCsjt|tsiStj|tjdtj|tjdi}tj|krL|tj|tj<tj|krf|tj|tj<|S)z-Extract kwargs from a pending transform item.N)rr2r INTERP_MODEget PADDING_MODESHAPEDTYPE)r4retrrrkwargs_from_pending\s     r<cCsdS)zGCheck if two sets of kwargs are compatible (to be combined in `apply`).Tr)Zkwargs_1Zkwargs_2rrris_compatible_apply_kwargsksr=c Cst|dd}|dddf}tj|t||ds6dSt|d}gdg}}t|d|d|fD]\}}t|D]v\}} tj| d|dstj| d|dr|d} ||ks| |krdS|||| qvtj| d|dsvdSqvqf|S) a Check whether the transformation matrix suggests voxel-wise interpolation. Returns None if the affine matrix suggests interpolation. Otherwise, the matrix suggests that the resampling could be achieved by simple array operations such as flip/permute/pad_nd/slice; in this case this function returns axes information about simple axes operations. Args: matrix: the affine matrix to check. atol: absolute tolerance for checking if the matrix is close to an integer. Tr*Nratolrg)rr0rroundr enumerateiscloseappend) matrixr?sndimoxoyxrycZ y_channelrrrrequires_interpps"        rNr?rz dict | None)rrEkwargsc Cs,t|std|jdt|tjjr:|jr:t d|p@i}|D]}t |t qF| dt}| tjd}| tj|j| tjdd}t|d}t|tjd }tj||j} | tjd } | d kr|n| } t| d d } | | tj|| d | tj| tjd} t ||d} | d k r|dkr|dst!"t|d d }t!#| $}t!%|t!&t|s|'|d t|j}|jd|d}|dd|dd D|d |<ddt(|d |D}|r8t)j*||d}|D]:}|d}d|||f<||d||df||df<qt!+| d ksXt,d| dt%|t!-t||drt|t| krt%t|d d | r| d|_|.t)j/}|Stj0j1||| | dd}|.t)j/}| d|_|Stj0j2f|}d|_3|4d |fd|i| W5QRSQRXd S)a Resample `data` using the affine transformation defined by ``matrix``. Args: data: input data to be resampled. matrix: affine transformation matrix. kwargs: currently supports (see also: ``monai.utils.enums.LazyAttr``) - "lazy_shape" for output spatial shape - "lazy_padding_mode" - "lazy_interpolation_mode" (this option might be ignored when ``mode="auto"``.) - "lazy_align_corners" - "lazy_dtype" (dtype for resampling computation; this might be ignored when ``mode="auto"``.) - "atol" for tolerance for matrix floating point comparison. - "lazy_resample_mode" for resampling backend, default to `"auto"`. Setting to other values will use the `monai.transforms.SpatialResample` for resampling. See Also: :py:class:`monai.transforms.SpatialResample` z>Calling the dense grid resample API directly not implemented, .zMdata.pending_operations is not empty, the resampling output may be incorrect.r?autoF)dtype align_cornersr@)r track_metaNTr*r) spatial_size dst_affinemode padding_moder>rScSsg|] }|dqS)r@r).0rJrrr szresample..cSs$g|]\}}||dkr|dqS)rr@r)rYidxvalrrrrZs )dimsrz4Resampling out_spatial_size should be positive, got rVrX)rWimg)5r rr.rrmonair MetaTensorpending_operationswarningswarnr __override_lazy_keywordsr7rr RESAMPLE_MODEr:rR ALIGN_CORNERSrrget_track_meta to_affine_ndaffiner9peek_pending_shaperutilsconvert_to_dst_typer6r8rNr0rAargsorttolistrarangepermuterBr,flipall ValueErroreyetofloat32 transformscrop_or_pad_ndSpatialResamplelazytrace_transform)rrErOkr?rWZ init_kwargsrGr^ init_affinerUZout_spatial_sizeZ call_kwargsaxesZ matrix_npfull_transposein_shaperqfZind_fZ resamplerrrrr sr          " $     )N) __future__rrbnumpyr0r,r_ monai.configrmonai.data.utilsrZ0monai.transforms.utils_pytorch_numpy_unificationr monai.utilsrrrr __all__r rr r5r<r=rNlistrdr rrrr s$      !