
    Ng                     ,    d dl Zd Zd Zd Zd Zd ZdS )    Nc                    t          |t          j                  rt          |j                  dk    rt          d          |j        t          j        k    r$|j        t          j        k    rt          d          t          | t          j                  rt          | j                  dk    rt          d          |t          j	        |          k     rt          d          t          |           t          |          k    rt          d          |dk    rdS t          j
        |           }|d	d	d
         }||         }|dk                        t                    }t          j        |          }t          j        |          }|                    t                    ||z   z  }|                    t                    |z  }	||	fS )ap  Compute precision and recall.
    Args:
        scores: A float numpy array representing detection score
        labels: A float numpy array representing weighted true/false positive labels
        num_gt: Number of ground truth instances
    Raises:
        ValueError: if the input is not of the correct format
    Returns:
        precision: Fraction of positive instances over detected ones. This value is
            None if no ground truth labels are present.
        recall: Fraction of detected positive instance over all positive instances.
            This value is None if no ground truth labels are present.
       z+labels must be single dimension numpy arrayz(labels type must be either bool or floatz+scores must be single dimension numpy arrayz5Number of true positives must be smaller than num_gt.z+scores and labels must be of the same size.r   )NNN)
isinstancenpndarraylenshape
ValueErrordtypefloat_bool_sumargsortastypefloatcumsum)
scoreslabelsnum_gtsorted_indicestrue_positive_labelsfalse_positive_labelscum_true_positivescum_false_positives	precisionrecalls
             U/var/www/html/ai-engine/env/lib/python3.11/site-packages/effdet/evaluation/metrics.pycompute_precision_recallr      s    fbj)) HS->->!-C-CFGGG|ry  V\RX%=%=CDDDfbj)) HS->->!-C-CFGGGvPQQQ
6{{c&kk!!FGGG{{zZ''N#DDbD)N!.11Q6>>uEE#788)$9::"))%004FI\4\]I&&u--6Ff    c                    | t          d          t          j        S t          | t          j                  rt          t          j                  st          d          | j        t          j        k    sj        t          j        k    rt          d          t          |           t                    k    rt          d          | j        sdS t          j	        |           dk     st          j
        |           dk    rt          d	          t          j	                  dk     st          j
                  dk    rt          d
          t          fdt          t                    dz
            D                       st          d          t          j        dgdgg          t          j        dg| dgg          } t          t          |           dz
  dd          D ])}t          j        | |         | |dz                      | |<   *t          j        dd         dd         k              d         dz   }t          j        |         |dz
           z
  | |         z            }|S )a  Compute Average Precision according to the definition in VOCdevkit.
    Precision is modified to ensure that it does not decrease as recall
    decrease.
    Args:
        precision: A float [N, 1] numpy array of precisions
        recall: A float [N, 1] numpy array of recalls
    Raises:
        ValueError: if the input is not of the correct format
    Returns:
        average_precison: The area under the precision recall curve. NaN if
            precision and recall are None.
    Nz.If precision is None, recall must also be Nonez(precision and recall must be numpy arrayz input must be float numpy array.z.precision and recall must be of the same size.g        r   r   z)Precision must be in the range of [0, 1].z&recall must be in the range of [0, 1].c              3   B   K   | ]}|         |d z            k    V  dS )r   N ).0ir   s     r   	<genexpr>z,compute_average_precision.<locals>.<genexpr>M   s4      JJavayF1q5M)JJJJJJr    z%recall must be a non-decreasing array   r   )r   r   NANr   r   r   r   r	   sizeaminamaxallrangeconcatenatemaximumwherer   )r   r   r%   indicesaverage_precisions    `   r   compute_average_precisionr3   /   sV    MNNNvi,, EJvrz4R4R ECDDD")##v|ry'@'@;<<<
9~~V$$IJJJ> s	wyA!3!3a!7!7DEEE	wvbgfoo11ABBBJJJJ5Vq3I3IJJJJJ B@AAA^aS&1#.//FY455I 3y>>A%r2.. B Bz)A,	!a%0@AA	!hvabbzVCRC[011!4q8Gw&12E ESZI[[\\r    c                 P    t          j        | dk    t           j        || z            S )a  Compute CorLoc according to the definition in the following paper.
    https://www.robots.ox.ac.uk/~vgg/rg/papers/deselaers-eccv10.pdf
    Returns nans if there are no ground truth images for a class.
    Args:
        num_gt_imgs_per_class: 1D array, representing number of images containing
            at least one object instance of a particular class
        num_images_correctly_detected_per_class: 1D array, representing number of
            images that are correctly detected at least one object instance of a particular class
    Returns:
        corloc_per_class: A float numpy array represents the corloc score of each class
    r   )r   r0   nan)num_gt_imgs_per_class'num_images_correctly_detected_per_classs     r   compute_cor_locr8   \   s0     8"BF/2GGI I Ir    c                 P   g }t          t          |                     D ]`}|                    t          j        | |         dt          || |         j        d                            dk              d                    at          j        |          }t          j        |          S )ag  Computes MedianRank@k, where k is the top-scoring labels.
    Args:
        tp_fp_list: a list of numpy arrays; each numpy array corresponds to the all
            detection on a single image, where the detections are sorted by score in
            descending order. Further, each numpy array element can have boolean or
            float values. True positive elements have either value >0.0 or True;
            any other value is considered false positive.
        k: number of top-scoring proposals to take.
    Returns:
        median_rank: median rank of all true positive proposals among top k by score.
    r   )	r-   r	   appendr   r0   minr
   r.   median)
tp_fp_listkranksr%   concatenated_rankss        r   compute_median_rank_at_krA   m   s     E3z??## W WRXjmAc!Z]5H5K.L.L,LMPQQRRSTUVVVV..9'(((r    c           
         g }t          t          |                     D ]D}|                    | |         dt          || |         j        d                                       Et          j        |          }t          j        |          |z  S )a  Computes Recall@k, MedianRank@k, where k is the top-scoring labels.
    Args:
        tp_fp_list: a list of numpy arrays; each numpy array corresponds to the all
            detection on a single image, where the detections are sorted by score in
            descending order. Further, each numpy array element can have boolean or
            float values. True positive elements have either value >0.0 or True;
            any other value is considered false positive.
        num_gt: number of groundtruth anotations.
        k: number of top-scoring proposals to take.
    Returns:
        recall: recall evaluated on the top k by score detections.
    r   )r-   r	   r:   r;   r
   r   r.   r   )r=   r   r>   
tp_fp_evalr%   s        r   compute_recall_at_krD      s     J3z??## K K*Q-#aA1DQ1G*H*H(HIJJJJ
++J6*&&r    )numpyr   r   r3   r8   rA   rD   r#   r    r   <module>rF      sk       ( ( (V* * *ZI I I") ) )&' ' ' ' 'r    