U
    ®PÓh	  ã                   @  sV   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d	œd
d„ZdS )é    )Úannotations)ÚCallableÚHashable)ÚAny)ÚKeysCollection©Úensure_tupler   ÚstrzCallable[[Any], Any])ÚkeysÚ
nested_keyÚreturnc                   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                   sn   t ˆ tƒr"t‡ ‡fdd„ˆD ƒƒS t ˆ tƒrjt ˆ d tƒrj‡ ‡fdd„ˆD ƒ}t|ƒdkrbt|ƒS |d S d S )Nc                 3  s   | ]}ˆ | ˆ V  qd S )N© )Ú.0Úk©Údatar   r   úX/home/dell461/cl/sdc2/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)Ú
__future__r   Úcollections.abcr   r   Útypingr   Úmonai.configr   Úmonai.utilsr   r    r   r   r   r   Ú<module>   s
   