
    ^i
                        d Z ddlZddlmZmZmZ ddlmZm	Z	m
Z
mZ ddlZ ej                  e      Zdeeef   deeef   defdZdeeef   deeef   defd	Zdeeef   deeef   defd
Zdeeef   deeef   defdZdee   defdZy)z+Evaluation metrics for preference learning.    N)ListDictAny)accuracy_scoreprecision_scorerecall_scoref1_scorey_truey_predreturnc                     t        t        | j                               t        |j                               z        }|D cg c]  }| |   	 }}|D cg c]  }||   	 }}t        ||      S c c}w c c}w )zCompute accuracy.
    
    Args:
        y_true: Ground truth labels (dict mapping pattern_id to label)
        y_pred: Predicted labels (dict mapping pattern_id to label)
        
    Returns:
        Accuracy score
    )sortedsetkeysr   r
   r   pattern_idspidtrue_labelspred_labelss         I   /home/ubuntu/codebase/yexijia/保研/colocation_mvp/experiment/metrics.pyaccuracyr      sn     V[[]+c&++-.@@AK*56+36#;+K6*56+36#;+K6+{33 76s   A-A2c                     t        t        | j                               t        |j                               z        }|D cg c]  }| |   	 }}|D cg c]  }||   	 }}t        ||d      S c c}w c c}w )zCompute precision.
    
    Args:
        y_true: Ground truth labels
        y_pred: Predicted labels
        
    Returns:
        Precision score
            zero_division)r   r   r   r   r   s         r   	precisionr      sp     V[[]+c&++-.@@AK*56+36#;+K6*56+36#;+K6;3GG 76   A/A4c                     t        t        | j                               t        |j                               z        }|D cg c]  }| |   	 }}|D cg c]  }||   	 }}t        ||d      S c c}w c c}w )zCompute recall.
    
    Args:
        y_true: Ground truth labels
        y_pred: Predicted labels
        
    Returns:
        Recall score
    r   r   )r   r   r   r   r   s         r   recallr   .   sp     V[[]+c&++-.@@AK*56+36#;+K6*56+36#;+K6[DD 76r   c                     t        t        | j                               t        |j                               z        }|D cg c]  }| |   	 }}|D cg c]  }||   	 }}t        ||d      S c c}w c c}w )zCompute F1 score.
    
    Args:
        y_true: Ground truth labels
        y_pred: Predicted labels
        
    Returns:
        F1 score
    r   r   )r   r   r   r	   r   s         r   f1r!   ?   sp     V[[]+c&++-.@@AK*56+36#;+K6*56+36#;+K6KC@@ 76r   scoresc                 \    | syd}t         j                  d|ddt        |        d       |S )zCompute dynamic threshold using median.
    
    IMPORTANT: Do NOT use fixed threshold.
    
    Args:
        scores: List of prediction scores
        
    Returns:
        Threshold value (median of scores)
    r   gffffff?zComputed threshold: z.4fz (median of z scores))loggerdebuglen)r"   	thresholds     r   compute_thresholdr(   P   s9      I
LL'	#l3v;-xXY    )__doc__numpynptypingr   r   r   sklearn.metricsr   r   r   r	   logging	getLogger__name__r$   intfloatr   r   r   r!   r(    r)   r   <module>r5      s    1  " " S S 			8	$4T#s(^ 4T#s(^ 4 4$Hd38n Hd38n H H"E4S> E4S> Ee E"AtCH~ AtCH~ A% A"d5k e r)   