o
    i	                     @  sN   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 dddZdS )    )annotations)CallableHashable)Any)KeysCollectionensure_tuplekeysr   
nested_keystrreturnCallable[[Any], Any]c                   s   t |   fdd}|S )a  
    Since the output of HoVerNet is a dictionary, this function is to extend `monai.handlers.from_engine`
    to work with HoVerNet.

    If data is a list of nested dictionaries after decollating, extract nested value with expected keys and
    construct lists respectively, for example,
    if data is `[{"A": {"C": 1, "D": 2}, "B": {"C": 2, "D": 2}}, {"A":  {"C": 3, "D": 2}, "B":  {"C": 4, "D": 2}}]`,
    from_engine_hovernet(["A", "B"], "C"): `([1, 3], [2, 4])`.

    Here is a simple example::

        from monai.handlers import MeanDice, from_engine_hovernet

        metric = MeanDice(
            include_background=False,
            output_transform=from_engine_hovernet(keys=["pred", "label"], nested_key=HoVerNetBranch.NP.value)
        )

    Args:
        keys: specified keys to extract data from dictionary or decollated list of dictionaries.
        nested_key: specified key to extract nested data from dictionary or decollated list of dictionaries.

    c                   sr   t  trt fddD S t  tr5t  d tr7 fddD }t|dkr1t|S |d S d S d S )Nc                 3  s    | ]	} |  V  qd S )N ).0kdatar
   r   e/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/pathology/handlers/utils.py	<genexpr>1   s    z9from_engine_hovernet.<locals>._wrapper.<locals>.<genexpr>r   c                   s    g | ]  fd dD qS )c                   s   g | ]}|   qS r   r   )r   i)r   r
   r   r   
<listcomp>4   s    zEfrom_engine_hovernet.<locals>._wrapper.<locals>.<listcomp>.<listcomp>r   )r   r   )r   r   r   4   s     z:from_engine_hovernet.<locals>._wrapper.<locals>.<listcomp>   )
isinstancedicttuplelistlen)r   ret_keysr
   )r   r   _wrapper/   s   
z&from_engine_hovernet.<locals>._wrapperr   )r	   r
   r    r   r   r   from_engine_hovernet   s   r!   N)r	   r   r
   r   r   r   )
__future__r   collections.abcr   r   typingr   monai.configr   monai.utilsr   r!   r   r   r   r   <module>   s   