
    j                     x   d dl mZmZmZmZ 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 er	  ej                  e      Zej"                   G d d             Zej"                   G d	 d
             Zej"                   G d d             Zej"                   G d d             Zej"                   G d d             Zy)    )ListOptionalDictTYPE_CHECKING)defaultdictN   )score_statisticstest_result)score_resultc                   ^    e Zd ZU dZeej                     ed<   ee	e
j                  f   ed<   y)DatasetItemResultsz4Results for a single dataset item across all trials.test_resultsscoresN)__name__
__module____qualname____doc__r   r
   
TestResult__annotations__r   strr	   ScoreStatistics     z/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/evaluation_result.pyr   r      s-    >{--..&66677r   r   c                   d    e Zd ZU dZeed<   eed<   ee   ed<   ee   ed<   eed<   eee	f   ed<   y)	'EvaluationResultGroupByDatasetItemsViewz4View of evaluation results grouped by dataset items.experiment_id
dataset_idexperiment_nameexperiment_urltrial_countdataset_itemsN)
r   r   r   r   r   r   r   intr   r   r   r   r   r   r      s;    >Oc]"SM!//00r   r   c                       e Zd ZU dZeed<   eed<   ee   ed<   ee   ed<   eed<   ee	j                     ed<   eeej                  f   ed<   y	)
$EvaluationResultAggregatedScoresViewaw  
    Represents an aggregated view of evaluation results and scores for an
    experiment.

    This class is designed to encapsulate information about an experiment,
    its related dataset, trial counts, test results, and aggregated score
    statistics. It serves as a comprehensive representation useful in
    evaluation pipelines or result summaries.

    Attributes:
        experiment_id: Unique identifier for the experiment.
        dataset_id: Unique identifier for the associated dataset.
        experiment_name: Human-readable name of the experiment.
        experiment_url: URL link to the experiment for easy access.
        trial_count: Number of trials conducted in the experiment.
        test_results: Collection of test results from the experiment.
        aggregated_scores: Aggregated statistical scores for evaluation
            results keyed by score name.
    r   r   r   r    r!   r   aggregated_scoresN)r   r   r   r   r   r   r   r#   r   r
   r   r   r	   r   r   r   r   r%   r%   $   sV    ( Oc]"SM!{--..C!1!A!AABBr   r%   c                       e Zd ZU eed<   eed<   ee   ed<   eej                     ed<   ee   ed<   e	ed<    e
j                  e      Zeej                     ed<   d	efd
Zd	efdZd	eeef   fdZy)EvaluationResultr   r   r   r   r    r!   )default_factoryexperiment_scoresreturnc           	          t        j                  | j                        }t        | j                  | j
                  | j                  | j                  | j                  | j                  |      S )a  
        Aggregates evaluation scores from test results and returns the aggregated scores view.

        The method calculates aggregated scores from test results and encapsulates the results
        in an EvaluationResultAggregatedScoresView object, which contains information about
        the experiment and computed aggregated scores.

        The aggregated scores dictionary has keys for each found score name and values containing
        the statistics for that score.

        Returns:
            EvaluationResultAggregatedScoresView object containing details about the
            experiment and the aggregated scores calculated from test results.
        )r   r   r   r    r!   r   r&   )	r	   calculate_aggregated_statisticsr   r%   r   r   r   r    r!   )selfr&   s     r   aggregate_evaluation_scoresz,EvaluationResult.aggregate_evaluation_scoresO   sd     -LL
 4,, 00..((**/
 	
r   c                     | j                         }t        | j                  | j                  | j                  | j
                  | j                  |      S )z
        Create a view of evaluation results grouped by dataset items.

        Returns:
            EvaluationResultGroupByDatasetItemsView containing organized results with aggregated score statistics
        )r   r   r   r    r!   r"   )_build_results_per_dataset_itemr   r   r   r   r    r!   )r.   r"   s     r   group_by_dataset_item_viewz+EvaluationResult.group_by_dataset_item_viewk   sM     <<>6,, 00..(('
 	
r   c                    | j                   st        j                  d       i S t        t              }| j                   D ],  }|j
                  j                  }||   j                  |       . i }|j                         D ]=  \  }}|j                  d        t        j                  |      }t        ||      ||<   ? |S )z
        Build dataset item results with aggregated score statistics.

        Returns:
            Dict mapping dataset item IDs to their results and aggregated score statistics
        z)No test results available for aggregationc                     | j                   S )N)trial_id)xs    r   <lambda>zBEvaluationResult._build_results_per_dataset_item.<locals>.<lambda>   s    AJJr   )key)r   r   )r   LOGGERdebugr   list	test_casedataset_item_idappenditemssortr	   r-   r   )r.   results_by_dataset_itemtest_result_r=   dataset_items_resultsdataset_item_resultsr&   s          r   r1   z0EvaluationResult._build_results_per_dataset_item}   s       LLDEI"-d"3 --L*44DDO#O4;;LI . !#5L5R5R5T1O1 %%*>%? 0 P P$! 6H1:K6!/2 6U %$r   N)r   r   r   r   r   r   r   r
   r   r#   dataclassesfieldr;   r*   r   ScoreResultr%   r/   r   r2   r   r   r1   r   r   r   r(   r(   C   s    Oc]"{--..SM!8I8I8I9tL445 
-Q 
8
,S 
$%c;M6M1N %r   r(   c                   `    e Zd ZU dZeej                     ed<   dee	e
j                  f   fdZy)EvaluationResultOnDictItemsa  
    Evaluation result for dict items evaluation without experiment tracking.

    This class provides a similar interface to EvaluationResult but is designed
    for lightweight evaluations that don't require experiment or dataset management.
    It can aggregate scores across test results just like the regular evaluation.

    Attributes:
        test_results: Collection of test results from the evaluation.
    r   r+   c                 @    t        j                  | j                        S )z
        Aggregates evaluation scores from test results.

        Returns:
            Dictionary mapping score names to their aggregated statistics.
        )r	   r-   r   )r.   s    r   r/   z7EvaluationResultOnDictItems.aggregate_evaluation_scores   s      ??@Q@QRRr   N)r   r   r   r   r   r
   r   r   r   r   r	   r   r/   r   r   r   rI   rI      s:    	 {--..	S	c#333	4	Sr   rI   )typingr   r   r   r   collectionsr   loggingrE    r	   r
   metricsr   	getLoggerr   r9   	dataclassr   r   r%   r(   rI   r   r   r   <module>rR      s    6 6 #   + !			8	$ 8 8 8 1 1 1 C C C< T% T% T%n S S Sr   