o
    i                     @  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ZdddZG dd deZdS )    )annotations)AnyN)ProbNMS)optional_importzskimage.measurezscipy.ndimagemask
np.ndarray	thresholdfloatreturnr   c                 C  s:   d| d  }t |}||k }t |}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distance_transform_edtbinary_fill_holesmeasurelabel)r   r   negdistancebinaryZfilled_imageZmulti_instance_mask r   \/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/pathology/utils.pycompute_multi_instance_mask   s   	

r   
tumor_mask	list[int]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 )   )Z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
ZdS )PathologyProbNMSzc
    This class extends monai.utils.ProbNMS and add the `resolution` option for
    Pathology.
    r   	probs_mapnp.ndarray | torch.Tensorresolution_levelintr
   
list[list]c           
      C  sh   t d|}t| |}g }|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   )r(   r)   r*   r+   r
   r,   )__name__
__module____qualname____doc__r.   r   r   r   r   r'   =   s    r'   )r   r   r   r	   r
   r   )r   r   r   r	   r
   r   )
__future__r   typingr   numpyr!   torchmonai.transforms.post.arrayr   monai.utilsr   r   _r   r   r&   r'   r   r   r   r   <module>   s   

