o  ip&@sddlmZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZmZmZmZddgZGd d d ZGd d d Zd!ddZddZddZddZe fddZheedZd"d#d dZdS)$) annotationsN)NdarrayOrTensor) AFFINE_TOL)allclose)LazyAttrconvert_to_numpyconvert_to_tensorlook_up_optionresamplecombine_transformsc@(eZdZdZdZddZeddZdS)Affinez0A class to represent an affine transform matrix.datacC ||_dSNrselfrr]/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/lazy/utils.py__init__! zAffine.__init__cCsZt|trdSt|trdSt|drt|jdkrdS|jddvo,|jd|jdkS)z&Check if the data is an affine matrix.TFshape)) isinstancer DisplacementFieldhasattrlenrrrrris_affine_shaped$s  "zAffine.is_affine_shapedN)__name__ __module__ __qualname____doc__ __slots__r staticmethodr"rrrrr  r c@r )rz0A class to represent a dense displacement field.rcCrrrrrrrr5rzDisplacementField.__init__cCsDt|trdSt|trdSt|drt|jdkrdSt| S)zCheck if the data is a DDF.TFrr)rrr r r!rr"rrrr is_ddf_shaped8s   zDisplacementField.is_ddf_shapedN)r#r$r%r&r'rr(r*rrrrr0r)rleft torch.TensorrightreturncCst|r,t|r,tt|tr|jn|dd}tt|tr!|jn|dd}t||St|rVt|rVtt|tr?|jn|dd}tt|trM|jn|dd}||St )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_sequence) r r"rrrtorchmatmulrr*NotImplementedError)r+r-rrrr Ds cCs.t|tjtjfr |St|tr|tjS|S)z8Extract the affine matrix from a pending transform item.)rr1TensornpndarraydictrAFFINE) pending_itemrrraffine_from_pendingSs   r:cCsjt|tsiStj|tjdtj|tjdi}tj|vr&|tj|tj<tj|vr3|tj|tj<|S)z-Extract kwargs from a pending transform item.N)rr7r INTERP_MODEget PADDING_MODESHAPEDTYPE)r9retrrrkwargs_from_pending\s   rAcCsdS)zGCheck if two sets of kwargs are compatible (to be combined in `apply`).Tr)Zkwargs_1Zkwargs_2rrris_compatible_apply_kwargsksrBc Cst|dd}|dddf}tj|t||dsdSt|d}gdg}}t|d|d|fD]D\}}t|D];\}} tj| d|dsOtj| d|drj|d} ||vs[| |vr_dS|||| q;tj| d|dsvdSq;q3|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)rr5rroundr! enumerateiscloseappend) matrixrDsndimoxoyxrycZ y_channelrrrrequires_interpps&        rSrDrrJrkwargs dict | NonecCs:t|std|jdt|tjjr|jrt d|p i}|D]}t |t q#| dt}| tjd}| tj|j| tjdd}t|d}t|tjd }tj||j} | tjd } | d urn|n| } t| d d } | | tj|| d | tj| tjd} t ||d} | d urp|dkrp|dspt!"t|d d }t!#| $}t!%|t!&t|s|'|d t|j}|jd|d}|dd|dd D|d |<ddt(|d |D}|rt)j*||d}|D]}|d}d|||f<||d||df||df<qt!+| d ks*t,d| dt%|t!-t||drWt|t| krWt%t|d d | rW| d|_|.t)j/}|Stj0j1||| | dd}|.t)j/}| d|_|Stj0j2di|}d|_3|4d|dd|i| Wd S1swYd 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.rDautoF)dtype align_cornersrE)r track_metaNTr/r) spatial_size dst_affinemode padding_moderCrYcSsg|]}|dqS)rEr).0rOrrr szresample..cSs$g|]\}}||dkr|dqS)rrEr)r_idxvalrrrr`s$)dimsrz4Resampling out_spatial_size should be positive, got r\r^)r]imgr)5r r"r3rrmonair MetaTensorpending_operationswarningswarnr __override_lazy_keywordsr<rr RESAMPLE_MODEr?rX ALIGN_CORNERSr!rget_track_meta to_affine_ndaffiner>peek_pending_shaperutilsconvert_to_dst_typer;r=rSr5rFargsorttolistrarangepermuterGr1flipall ValueErroreyetofloat32 transformscrop_or_pad_ndSpatialResamplelazytrace_transform)rrJrTkrDr]Z init_kwargsrLrd init_affiner[Zout_spatial_sizeZ call_kwargsaxesZ matrix_npZfull_transposein_shaperwfZind_f resamplerrrrr sr          " "     &)r+r,r-r,r.r,r)rr,rJrrTrU) __future__rrhnumpyr5r1re monai.configrmonai.data.utilsrZ0monai.transforms.utils_pytorch_numpy_unificationr monai.utilsrrrr __all__r rr r:rArBrSlistrjr rrrrs&       !