U
    Ph,                     @  s   d dl mZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
 e
d\ZZe
d\ZZdd	d
dddZdd	ddddZG dd deZdS )    )annotations)AnyN)ProbNMS)optional_importzskimage.measurezscipy.ndimagez
np.ndarrayfloatr   )mask	thresholdreturnc                 C  s>   d| d  }t j|}||k }t j|}tj|dd}|S )z
    This method computes the segmentation mask according to the binary tumor mask.

    Args:
        mask: the binary mask array
        threshold: the threshold to fill holes
          )connectivity)ndimage
morphologydistance_transform_edtbinary_fill_holesmeasurelabel)r   r   negdistancebinaryfilled_imageZmulti_instance_mask r   O/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/pathology/utils.pycompute_multi_instance_mask   s    	r   z	list[int])
tumor_maskr   r	   c                   s0   t | }t|   fddt|D }|S )aJ  
    This method computes identifies Isolated Tumor Cells (ITC) and return their labels.

    Args:
        tumor_mask: the tumor mask.
        threshold: the threshold (at the mask level) to define an isolated tumor cell (ITC).
            A region with the longest diameter less than this threshold is considered as an ITC.
    c                   s"   g | ]} | j k r|d  qS )   )major_axis_length).0i
propertiesr   r   r   
<listcomp>8   s      z0compute_isolated_tumor_cells.<locals>.<listcomp>)npamaxr   regionpropsrange)r   r   	max_labelZitc_listr   r   r   compute_isolated_tumor_cells-   s    	

r'   c                   @  s$   e Zd ZdZd
ddddddZd	S )PathologyProbNMSzc
    This class extends monai.utils.ProbNMS and add the `resolution` option for
    Pathology.
    r   znp.ndarray | torch.Tensorintz
list[list])	probs_mapresolution_levelr	   c           
      C  sh   t d|}t| |}g }|D ]D}|d }t|dd }|d | t}	||gt|	  q|S )z
        probs_map: the input probabilities map, it must have shape (H[, W, ...]).
        resolution_level: the level at which the probabilities map is made.
        r   r   r   Ng      ?)	powr   __call__r"   asarrayastyper)   appendlist)
selfr*   r+   
resolutionZorg_outputsoutputsZ
org_outputprobcoordZ	coord_wsir   r   r   r-   C   s    
zPathologyProbNMS.__call__N)r   )__name__
__module____qualname____doc__r-   r   r   r   r   r(   =   s   r(   )
__future__r   typingr   numpyr"   torchmonai.transforms.post.arrayr   monai.utilsr   r   _r   r   r'   r(   r   r   r   r   <module>   s   