U ¸PÓh‘3ã@sŠddlmZddlZddlmZddlmZmZdgZ edƒ\Z Z edded ƒ\Z Z ed ded ƒ\Z Z ed ded ƒ\ZZ Gdd„dƒZdS)é)Ú annotationsN)ÚNDArray)Ú min_versionÚoptional_importÚUltrasoundConfidenceMapÚcv2z scipy.sparsez1.7.1Ú csc_matrixzscipy.sparse.linalgÚspsolvez scipy.signalÚhilbertc@s¨eZdZdZd'ddddœd d „Zd d d d d œdd„Zd(d ddddœdd„Zd d dœdd„Zd dd dœdd„Zd d dddœdd„Z d d!„Z d"d#„Z d)d dd d$œd%d&„Z dS)*raCompute confidence map from an ultrasound image. This transform uses the method introduced by Karamalis et al. in https://doi.org/10.1016/j.media.2012.07.005. It generates a confidence map by setting source and sink points in the image and computing the probability for random walks to reach the source for each pixel. Args: alpha (float, optional): Alpha parameter. Defaults to 2.0. beta (float, optional): Beta parameter. Defaults to 90.0. gamma (float, optional): Gamma parameter. Defaults to 0.05. mode (str, optional): 'RF' or 'B' mode data. Defaults to 'B'. sink_mode (str, optional): Sink mode. Defaults to 'all'. If 'mask' is selected, a mask must be when calling the transform. Can be 'all', 'mid', 'min', or 'mask'. ç@ç€V@çš™™™™™©?ÚBÚallÚfloat)ÚalphaÚbetaÚgammacCs@||_||_||_||_||_t d¡j|_tjgdd|_ dS)NÚfloat64©Údtype) rrrÚmodeÚ sink_modeÚnpÚfinfoÚepsÚarrayÚ _sink_indices)Úselfrrrrr©rúY/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/ultrasound_confidence_map.pyÚ__init__*sz UltrasoundConfidenceMap.__init__ztuple[int, ...]r)ÚsizeÚrowsÚcolsÚreturncCs|||d}|S)aàConverts row and column subscripts into linear indices, basically the copy of the MATLAB function of the same name. https://www.mathworks.com/help/matlab/ref/sub2ind.html This function is Pythonic so the indices start at 0. Args: size Tuple[int]: Size of the matrix rows (NDArray): Row indices cols (NDArray): Column indices Returns: indices (NDArray): 1-D array of linear indices rr)rr"r#r$Úindicesrrr Úsub2ind8szUltrasoundConfidenceMap.sub2indNÚstrzNDArray | Noneztuple[NDArray, NDArray])ÚdatarÚ sink_maskr%cCsbtjgdd}tjgdd}tj|jddd}t dg¡}| |j||¡ d¡}t |¡}t ||f¡}t |¡} t || f¡}|dkrÐt |¡|jdd} tj| |gdd|_ | |j| |¡ d¡}nV|dkr4t |jddg¡} t | ¡|jdd} tj| | gdd|_ | |j| | ¡ d¡}nò|d krÔt |jdd ƒ} t  |d | | …f¡} t  |d | | …f| k¡d| }|} t | ¡|jdd} tj| | gdd|_ | |j| | ¡ d¡}nR|d kr&t  |dk¡}|d} |d} tj| | gdd|_ | |j| | ¡ d¡}t |¡}t ||f¡}t |¡d} t || f¡}||fS) aHGet the seed and label arrays for the max-flow algorithm Args: data: Input array sink_mode (str, optional): Sink mode. Defaults to 'all'. sink_mask (NDArray, optional): Sink mask. Defaults to None. Returns: Tuple[NDArray, NDArray]: Seed and label arrays rrérrÚint32ÚmidéÚmingš™™™™™¹?éÿÿÿÿÚmask) rrÚarangeÚshaper'ÚastypeÚuniqueÚ concatenateÚ ones_likerÚintr/Úwhere)rr)rr*ÚseedsÚlabelsÚscZsr_upÚseedÚlabelZsr_downZsc_downZ ten_percentÚmin_valZmin_idxsÚcoordsrrr Úget_seed_and_labelsJsJ     $  z+UltrasoundConfidenceMap.get_seed_and_labels)Úinpr%cCs"|t |¡t |¡|j}|S)zNormalize an array to [0, 1])rr/Úptpr)rrBZnormalized_arrayrrr Ú normalize”sz!UltrasoundConfidenceMap.normalize)Úimgrr%cCsNtjdd|jddd}t | dd¡d|jdf¡}dt | |¡}|S)zïCompute attenuation weighting Args: img (NDArray): Image alpha: Attenuation coefficient (see publication) Returns: w (NDArray): Weighting expressing depth-dependent attenuation rr+rrr0çð?)rÚlinspacer3ÚtileÚreshapeÚexp)rrErÚdwÚwrrr Úattenuation_weighting™s z-UltrasoundConfidenceMap.attenuation_weighting)Ú padded_indexÚ padded_imagerrcCsÂ|j\}}|j ¡}|j ¡}t |dk¡d}||d}||d} tj|dd} dd|d|d| d| d|| g} d} t| ƒD]²\} }|||}t |dk¡d}|||d}t ||f¡}||d}t | |f¡} t ||||||¡}t | |f¡} | dkr,| jd} qŒ| dkrŒ| jdqŒ|  | ¡} | d| …|7<|  | ¡} tj | | ddd } t | || ffƒ}|  d¡t |j dd j¡d}|  |¡|S) a>Compute 6-Connected Laplacian for confidence estimation problem Args: padded_index (NDArray): The index matrix of the image with boundary padding. padded_image (NDArray): The padded image. beta (float): Random walks parameter that defines the sensitivity of the Gaussian weighting function. gamma (float): Horizontal penalty factor that adjusts the weight of horizontal edges in the Laplacian. Returns: L (csc_matrix): The 6-connected Laplacian matrix used for confidence map estimation. rr+rrr0Négíµ ÷ư>©Úaxis)r3ÚTÚflattenrr9Ú zeros_likeÚ enumerater6ÚabsrDrJrÚsetdiagÚsumÚA)rrNrOrrÚmÚ_ÚpÚiÚjÚsZedge_templatesZ vertical_endZiter_idxÚkZ neigh_idxsÚqÚiiÚjjrLÚlapÚdiagrrr Úconfidence_laplacian¬sP     ø        ÿ  z,UltrasoundConfidenceMap.confidence_laplaciancCst||ƒ}|S)N)r )rreÚrhsÚxrrr Ú_solve_linear_systems z,UltrasoundConfidenceMap._solve_linear_systemcCsˆt d|jd|jdd¡ |jd|jd¡j}d}tj|||fddd}tj|||fddd} | || ||¡} | dd…|f} t |dk¡ ¡} t  t | ¡|  t ¡¡} | | dd…f} t  t | jd¡|¡}t | |dd…fdd…|fƒ} tj |jddfdd}|dk|dd…df<| |}| | |¡}tj | fdd}||| <d |||dk  t ¡<| |jd|jdf¡j}|S) ažCompute confidence map Args: img (NDArray): Processed image. seeds (NDArray): Seeds for the random walks framework. These are indices of the source and sink nodes. labels (NDArray): Labels for the random walks framework. These represent the classes or groups of the seeds. beta: Random walks parameter that defines the sensitivity of the Gaussian weighting function. gamma: Horizontal penalty factor that adjusts the weight of horizontal edges in the Laplacian. Returns: map: Confidence map which shows the probability of each pixel belonging to the source or sink group. r+rÚconstant)rr)Úconstant_valuesNrrrF)rr2r3rIrSÚpadrgrYÚitemÚ setdiff1dr4r8rÚzerosrj)rrEr:r;rrÚidxrmZ padded_idxZ padded_imgreÚbÚnZi_uZ keep_indicesr[rhriZ probabilitiesrrr Úconfidence_estimations(6   z-UltrasoundConfidenceMap.confidence_estimation)r)r*r%cCsz| d¡}| |¡}|jdkr6t t|dd¡ d¡}| ||j|¡\}}| ||j ¡}||}|  ||||j |j ¡}|S)zÙCompute the confidence map Args: data (NDArray): RF ultrasound data (one scanline per column) [H x W] 2D array Returns: map (NDArray): Confidence map [H x W] 2D array rÚRFrrQ) r4rDrrrWr rArrMrrtrr)rr)r*r:r;rLÚmap_rrr Ú__call__As   z UltrasoundConfidenceMap.__call__)r r r rr)rN)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__r!r'rArDrMrgrjrtrwrrrr rsÿJV:)Ú __future__rÚnumpyrÚ numpy.typingrÚ monai.utilsrrÚ__all__rr\rr r rrrrr Ú s