Upload metrics.py with huggingface_hub
Browse files- metrics.py +92 -2
 
    	
        metrics.py
    CHANGED
    
    | 
         @@ -1,6 +1,7 @@ 
     | 
|
| 1 | 
         
             
            import uuid
         
     | 
| 2 | 
         
             
            from abc import ABC, abstractmethod
         
     | 
| 3 | 
         
            -
            from  
     | 
| 
         | 
|
| 4 | 
         
             
            from typing import Any, Dict, Generator, List, Optional
         
     | 
| 5 | 
         | 
| 6 | 
         
             
            import evaluate
         
     | 
| 
         @@ -9,7 +10,6 @@ import numpy 
     | 
|
| 9 | 
         | 
| 10 | 
         
             
            from .operator import (
         
     | 
| 11 | 
         
             
                MultiStreamOperator,
         
     | 
| 12 | 
         
            -
                SequntialOperator,
         
     | 
| 13 | 
         
             
                SingleStreamOperator,
         
     | 
| 14 | 
         
             
                StreamingOperator,
         
     | 
| 15 | 
         
             
                StreamInstanceOperator,
         
     | 
| 
         @@ -353,3 +353,93 @@ class Bleu(HuggingfaceMetric): 
     | 
|
| 353 | 
         
             
                metric_name = "bleu"
         
     | 
| 354 | 
         
             
                main_score = "bleu"
         
     | 
| 355 | 
         
             
                scale = 1.0
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
             
            import uuid
         
     | 
| 2 | 
         
             
            from abc import ABC, abstractmethod
         
     | 
| 3 | 
         
            +
            from collections import Counter
         
     | 
| 4 | 
         
            +
            from dataclasses import field
         
     | 
| 5 | 
         
             
            from typing import Any, Dict, Generator, List, Optional
         
     | 
| 6 | 
         | 
| 7 | 
         
             
            import evaluate
         
     | 
| 
         | 
|
| 10 | 
         | 
| 11 | 
         
             
            from .operator import (
         
     | 
| 12 | 
         
             
                MultiStreamOperator,
         
     | 
| 
         | 
|
| 13 | 
         
             
                SingleStreamOperator,
         
     | 
| 14 | 
         
             
                StreamingOperator,
         
     | 
| 15 | 
         
             
                StreamInstanceOperator,
         
     | 
| 
         | 
|
| 353 | 
         
             
                metric_name = "bleu"
         
     | 
| 354 | 
         
             
                main_score = "bleu"
         
     | 
| 355 | 
         
             
                scale = 1.0
         
     | 
| 356 | 
         
            +
             
     | 
| 357 | 
         
            +
             
     | 
| 358 | 
         
            +
            class CustomF1(GlobalMetric):
         
     | 
| 359 | 
         
            +
                main_score = "f1_micro"
         
     | 
| 360 | 
         
            +
             
     | 
| 361 | 
         
            +
                @abstractmethod
         
     | 
| 362 | 
         
            +
                def get_element_group(self, element):
         
     | 
| 363 | 
         
            +
                    pass
         
     | 
| 364 | 
         
            +
             
     | 
| 365 | 
         
            +
                @abstractmethod
         
     | 
| 366 | 
         
            +
                def get_element_representation(self, element):
         
     | 
| 367 | 
         
            +
                    pass
         
     | 
| 368 | 
         
            +
             
     | 
| 369 | 
         
            +
                def group_elements(self, l):
         
     | 
| 370 | 
         
            +
                    return {
         
     | 
| 371 | 
         
            +
                        k: Counter([self.get_element_representation(value) for value in l if self.get_element_group(value) == k])
         
     | 
| 372 | 
         
            +
                        for k in set([self.get_element_group(e) for e in l])
         
     | 
| 373 | 
         
            +
                    }
         
     | 
| 374 | 
         
            +
             
     | 
| 375 | 
         
            +
                def calculate_groups_ratio(self, actual_group, total_group):
         
     | 
| 376 | 
         
            +
                    return sum([min(actual_group[k], total_group[k]) for k in actual_group.keys()]), sum(actual_group.values())
         
     | 
| 377 | 
         
            +
             
     | 
| 378 | 
         
            +
                def f1(self, pn, pd, rn, rd):
         
     | 
| 379 | 
         
            +
                    precision = 1.0 if pn == 0 and pd == 0 else pn / pd
         
     | 
| 380 | 
         
            +
                    recall = 1.0 if rn == 0 and rd == 0 else rn / rd
         
     | 
| 381 | 
         
            +
                    try:
         
     | 
| 382 | 
         
            +
                        return 2 * precision * recall / (precision + recall)
         
     | 
| 383 | 
         
            +
                    except ZeroDivisionError:
         
     | 
| 384 | 
         
            +
                        return 0.0
         
     | 
| 385 | 
         
            +
             
     | 
| 386 | 
         
            +
                def compute(self, references: List[Any], predictions: List[Any]) -> dict:
         
     | 
| 387 | 
         
            +
                    # in case reference are List[List[List[Any]]] and predictions are List[List[Any]]:
         
     | 
| 388 | 
         
            +
                    if isinstance(references[0], list) and isinstance(references[0][0], list):
         
     | 
| 389 | 
         
            +
                        references = [element[0] for element in references]
         
     | 
| 390 | 
         
            +
             
     | 
| 391 | 
         
            +
                    assert len(references) == len(predictions), (
         
     | 
| 392 | 
         
            +
                        f"references size ({len(references)})" f" doesn't mach predictions sise ({len(references)})."
         
     | 
| 393 | 
         
            +
                    )
         
     | 
| 394 | 
         
            +
                    groups_statistics = dict()
         
     | 
| 395 | 
         
            +
                    for references_batch, predictions_batch in zip(references, predictions):
         
     | 
| 396 | 
         
            +
                        grouped_references = self.group_elements(references_batch)
         
     | 
| 397 | 
         
            +
                        grouped_predictions = self.group_elements(predictions_batch)
         
     | 
| 398 | 
         
            +
                        all_groups = set(grouped_references.keys()).union(grouped_predictions.keys())
         
     | 
| 399 | 
         
            +
                        for group in all_groups:
         
     | 
| 400 | 
         
            +
                            if group not in groups_statistics:
         
     | 
| 401 | 
         
            +
                                groups_statistics[group] = {
         
     | 
| 402 | 
         
            +
                                    "precision_numerator": 0,
         
     | 
| 403 | 
         
            +
                                    "precision_denominator": 0,
         
     | 
| 404 | 
         
            +
                                    "recall_numerator": 0,
         
     | 
| 405 | 
         
            +
                                    "recall_denominator": 0,
         
     | 
| 406 | 
         
            +
                                }
         
     | 
| 407 | 
         
            +
                            references_by_group = grouped_references.get(group, Counter([]))
         
     | 
| 408 | 
         
            +
                            predictions_by_group = grouped_predictions.get(group, Counter([]))
         
     | 
| 409 | 
         
            +
                            pn, pd = self.calculate_groups_ratio(
         
     | 
| 410 | 
         
            +
                                actual_group=predictions_by_group, total_group=references_by_group
         
     | 
| 411 | 
         
            +
                            )
         
     | 
| 412 | 
         
            +
                            rn, rd = self.calculate_groups_ratio(
         
     | 
| 413 | 
         
            +
                                actual_group=references_by_group, total_group=predictions_by_group
         
     | 
| 414 | 
         
            +
                            )
         
     | 
| 415 | 
         
            +
                            groups_statistics[group]["precision_numerator"] += pn
         
     | 
| 416 | 
         
            +
                            groups_statistics[group]["precision_denominator"] += pd
         
     | 
| 417 | 
         
            +
                            groups_statistics[group]["recall_numerator"] += rn
         
     | 
| 418 | 
         
            +
                            groups_statistics[group]["recall_denominator"] += rd
         
     | 
| 419 | 
         
            +
             
     | 
| 420 | 
         
            +
                    result = {}
         
     | 
| 421 | 
         
            +
                    pn_total = pd_total = rn_total = rd_total = 0
         
     | 
| 422 | 
         
            +
                    for group in groups_statistics.keys():
         
     | 
| 423 | 
         
            +
                        pn, pd, rn, rd = (
         
     | 
| 424 | 
         
            +
                            groups_statistics[group]["precision_numerator"],
         
     | 
| 425 | 
         
            +
                            groups_statistics[group]["precision_denominator"],
         
     | 
| 426 | 
         
            +
                            groups_statistics[group]["recall_numerator"],
         
     | 
| 427 | 
         
            +
                            groups_statistics[group]["recall_denominator"],
         
     | 
| 428 | 
         
            +
                        )
         
     | 
| 429 | 
         
            +
                        result[f"f1_{group}"] = self.f1(pn, pd, rn, rd)
         
     | 
| 430 | 
         
            +
                        pn_total, pd_total, rn_total, rd_total = pn_total + pn, pd_total + pd, rn_total + rn, rd_total + rd
         
     | 
| 431 | 
         
            +
                    try:
         
     | 
| 432 | 
         
            +
                        result["f1_macro"] = sum(result.values()) / len(result.keys())
         
     | 
| 433 | 
         
            +
                    except ZeroDivisionError:
         
     | 
| 434 | 
         
            +
                        result["f1_macro"] = 1.0
         
     | 
| 435 | 
         
            +
             
     | 
| 436 | 
         
            +
                    result[f"f1_micro"] = self.f1(pn_total, pd_total, rn_total, rd_total)
         
     | 
| 437 | 
         
            +
                    return result
         
     | 
| 438 | 
         
            +
             
     | 
| 439 | 
         
            +
             
     | 
| 440 | 
         
            +
            class NER(CustomF1):
         
     | 
| 441 | 
         
            +
                def get_element_group(self, element):
         
     | 
| 442 | 
         
            +
                    return element[1]
         
     | 
| 443 | 
         
            +
             
     | 
| 444 | 
         
            +
                def get_element_representation(self, element):
         
     | 
| 445 | 
         
            +
                    return str(element)
         
     |