o
    iR   ã                   @  s    d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dl	Z
d dlZd dlmZ dZdZdd	gd
gdgdœZdgZd,dd„Z								d-d.d+d„ZdS )/é    )ÚannotationsN)ÚCallableÚSequence)ÚAny)ÚTensorT)é   é   é   é   é   é9   é€   r	   r
   r   r   )é   é   é„   Úsample_prompt_pairsÚidÚintÚreturnútuple[int, int]c                 C  s   | t v rtrdS dS )N)é   é   )r   r   )ÚSPECIAL_INDEXÚENABLE_SPECIAL)r   © r   ú\/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/vista3d/sampler.pyÚ_get_point_label"   s   r   r   é   Fçš™™™™™É?Úlabelsr   Ú	label_setúSequence[int]Ú
max_promptú
int | NoneÚmax_forepromptÚmax_backpromptÚ	max_pointÚinclude_backgroundÚboolÚdrop_label_probÚfloatÚdrop_point_probÚpoint_samplerúCallable | NoneÚpoint_sampler_kwargsr   úAtuple[Tensor | None, Tensor | None, Tensor | None, Tensor | None]c
           !   
   K  s  | j d dkstdƒ‚| d } | j}|  ¡  ¡  ¡  ¡ }|r-tt|ƒt|ƒt|ƒ  ƒ}ntt|ƒt|ƒt|ƒ  dh ƒ}tt|ƒt|ƒ ƒ}|dur]t	|ƒ|kr]t
 |¡ |d|… }|durrt	|ƒ|krrt
 |¡ |d|… }|dur™t	|ƒt	|ƒ |kr™t	|ƒ|krt
 ||¡}g }n
t
 ||t	|ƒ ¡}g }g }|	du rZt|tt t
jd|d d¡ƒd ƒ}t|tt t
jd|d d¡ƒƒ}|D ]m}t|ƒ\}}| t|ƒk}| }t |¡}t |¡}tt	|ƒ|ƒ}tt	|ƒ|ƒ}| t t
j||dt
j||d tjg d	¢|d
g|| | |   ¡¡ | t |g| |g|  dg|| | |   ¡ |¡¡ qÈ|D ] }| t || d¡ |¡¡ | t || ¡ |¡d ¡ q8n1|	|fi |
¤Ž\}}|D ]$}| t t	|d ƒd¡ |¡¡ | t t	|d ƒ¡ |¡d ¡ qft	|ƒdkr t	|ƒdkr d\}}}}n`t || ¡ d¡ |¡ ¡ }t |¡}t |¡}t |¡}t
 dd¡|k rót	|ƒdkród}t	|ƒ} |dt	|ƒ|  … }|dt	|ƒ|  … }|dt	|ƒ|  … }nt
 dd¡|k r d}d}||||fS )aÈ  
    Sample training pairs for VISTA3D training.

    Args:
        labels: [1, 1, H, W, D], ground truth labels.
        label_set: the label list for the specific dataset. Note if 0 is included in label_set,
            it will be added into automatic branch training. Recommend removing 0 from label_set
            for multi-partially-labeled-dataset training, and adding 0 for finetuning specific dataset.
            The reason is region with 0 in one partially labeled dataset may contain foregrounds in
            another dataset.
        max_prompt: int, max number of total prompt, including foreground and background.
        max_foreprompt: int, max number of prompt from foreground.
        max_backprompt: int, max number of prompt from background.
        max_point: maximum number of points for each object.
        include_background: if include 0 into training prompt. If included, background 0 is treated
            the same as foreground and points will be sampled. Can be true only if user want to segment
            background 0 with point clicks, otherwise always be false.
        drop_label_prob: probability to drop label prompt.
        drop_point_prob: probability to drop point prompt.
        point_sampler: sampler to augment masks with supervoxel.
        point_sampler_kwargs: arguments for point_sampler.

    Returns:
        tuple:
            - label_prompt (Tensor | None): Tensor of shape [B, 1] containing the classes used for
              training automatic segmentation.
            - point (Tensor | None): Tensor of shape [B, N, 3] representing the corresponding points
              for each class. Note that background label prompts require matching points as well
              (e.g., [0, 0, 0] is used).
            - point_label (Tensor | None): Tensor of shape [B, N] representing the corresponding point
              labels for each point (negative or positive). -1 is used for padding the background
              label prompt and will be ignored.
            - prompt_class (Tensor | None): Tensor of shape [B, 1], exactly the same as label_prompt
              for label indexing during training. If label_prompt is None, prompt_class is used to
              identify point classes.

    r   r   zonly support batch size 1)r   r   Nr   )ÚmuÚsigma)Úk)r   r   r   )Údeviceéÿÿÿÿr   )NNNN) ÚshapeÚ
ValueErrorr3   ÚuniqueÚcpuÚnumpyÚtolistÚlistÚsetÚlenÚrandomÚshuffleÚsampleÚminr   ÚnpÚabsÚgaussr   ÚtorchÚnonzeroÚappendÚstackÚchoicesÚtensorÚtoÚzerosÚ	unsqueezeÚlongÚcopyÚdeepcopyÚuniform)!r   r    r"   r$   r%   r&   r'   r)   r+   r,   r.   r3   Úunique_labelsZbackground_labelsÚ_pointÚ_point_labelÚnum_pÚnum_nr   Zneg_idZpos_idÚplabelsÚnlabelsÚplabelpointsÚnlabelpointsZnum_paZnum_naÚ_Zlabel_promptÚpointÚpoint_labelÚprompt_classÚpadr   r   r   r   )   s”   4"


&"

ÿ"þÿÿ0ÿÿ"ý &


 )r   r   r   r   )NNr   r   Fr   r   N)r   r   r    r!   r"   r#   r$   r#   r%   r   r&   r   r'   r(   r)   r*   r+   r*   r,   r-   r.   r   r   r/   )Ú
__future__r   rO   r>   Úcollections.abcr   r   Útypingr   r9   rB   rE   r   r   r   Z
MERGE_LISTÚ__all__r   r   r   r   r   r   Ú<module>   s2   ý

ö