


'''simple docstring'''

import re
import string

import numpy as np

import datasets


a_  :				Dict						= "\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n"

a_  :				Any						= "\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results[\"exact_match\"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results[\"exact_match\"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results[\"exact_match\"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results[\"exact_match\"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric(\"exact_match\")\n    >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"]\n    >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results[\"exact_match\"], 1))\n    33.3\n\n"

a_  :				List[Any]						= "\n"





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION			,						_KWARGS_DESCRIPTION			)
class      snake_case    (    datasets.Metric			):





  """simple docstring"""




  def   snake_case		(    self     ):







   """simple docstring"""
   return datasets.MetricInfo(
       description=_DESCRIPTION			,  citation=_CITATION			,  inputs_description=_KWARGS_DESCRIPTION			,  features=datasets.Features(
           {
               "predictions": datasets.Value("string"			,  id="sequence"     ),
               "references": datasets.Value("string"			,  id="sequence"     ),
           }     )			,  reference_urls=[]			,  )







  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase=None			,  UpperCamelCase=False			,  UpperCamelCase=False			,  UpperCamelCase=False			,  ):







   """simple docstring"""
   if regexes_to_ignore is not None:
    for s in regexes_to_ignore:
     lowerCamelCase_      =  np.array([re.sub(_lowerCAmelCase			,  ""			,  _lowerCAmelCase     ) for x in predictions]     )
     lowerCamelCase_      =  np.array([re.sub(_lowerCAmelCase			,  ""			,  _lowerCAmelCase     ) for x in references]     )
   else:
    lowerCamelCase_      =  np.asarray(_lowerCAmelCase     )
    lowerCamelCase_      =  np.asarray(_lowerCAmelCase     )

   if ignore_case:
    lowerCamelCase_      =  np.char.lower(_lowerCAmelCase     )
    lowerCamelCase_      =  np.char.lower(_lowerCAmelCase     )

   if ignore_punctuation:
    lowerCamelCase_      =  string.punctuation.maketrans(""			,  ""			,  string.punctuation     )
    lowerCamelCase_      =  np.char.translate(_lowerCAmelCase			,  table=_lowerCAmelCase     )
    lowerCamelCase_      =  np.char.translate(_lowerCAmelCase			,  table=_lowerCAmelCase     )

   if ignore_numbers:
    lowerCamelCase_      =  string.digits.maketrans(""			,  ""			,  string.digits     )
    lowerCamelCase_      =  np.char.translate(_lowerCAmelCase			,  table=_lowerCAmelCase     )
    lowerCamelCase_      =  np.char.translate(_lowerCAmelCase			,  table=_lowerCAmelCase     )

   lowerCamelCase_      =  predictions == references

   return {"exact_match": np.mean(_lowerCAmelCase     ) * 100}









import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import GLPNImageProcessor




class       __lowerCAmelCase				(							unittest.TestCase):






       def __init__(      self:    List[Any]				,    _lowerCAmelCase:    str				,    _lowerCAmelCase:    Dict=7				,    _lowerCAmelCase:    Optional[Any]=3				,    _lowerCAmelCase:    List[Any]=18				,    _lowerCAmelCase:    int=30				,    _lowerCAmelCase:    Union[str, Any]=4_00				,    _lowerCAmelCase:    Any=True				,    _lowerCAmelCase:    Union[str, Any]=32				,    _lowerCAmelCase:    int=True				,    ):
         lowercase			:Any							=  parent
         lowercase			:List[str]							=  batch_size
         lowercase			:Any							=  num_channels
         lowercase			:Optional[Any]							=  image_size
         lowercase			:Optional[Any]							=  min_resolution
         lowercase			:str							=  max_resolution
         lowercase			:str							=  do_resize
         lowercase			:Optional[int]							=  size_divisor
         lowercase			:Tuple							=  do_rescale






       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         return {
             "do_resize": self.do_resize,
             "size_divisor": self.size_divisor,
             "do_rescale": self.do_rescale,
         }







@require_torch
@require_vision
class       __lowerCAmelCase				(							lowerCAmelCase					,       unittest.TestCase):
       _a           =  GLPNImageProcessor if is_vision_available() else None






       def     SCREAMING_SNAKE_CASE			(      self:    List[str] ):
         lowercase			:int							=  GLPNImageProcessingTester(self )






       @property
       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         return self.image_processor_tester.prepare_image_processor_dict()






       def     SCREAMING_SNAKE_CASE			(      self:    Optional[int] ):
         lowercase			:Optional[Any]							=  self.image_processing_class(**self.image_processor_dict )
         self.assertTrue(hasattr(_lowerCAmelCase				,    "do_resize" ) )
         self.assertTrue(hasattr(_lowerCAmelCase				,    "size_divisor" ) )
         self.assertTrue(hasattr(_lowerCAmelCase				,    "resample" ) )
         self.assertTrue(hasattr(_lowerCAmelCase				,    "do_rescale" ) )






       def     SCREAMING_SNAKE_CASE			(      self:    List[Any] ):
         pass






       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         # Initialize image_processing
         lowercase			:Union[str, Any]							=  self.image_processing_class(**self.image_processor_dict )
         # create random PIL images
         lowercase			:List[str]							=  prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_lowerCAmelCase )
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase				,    Image.Image )

         # Test not batched input (GLPNImageProcessor doesn't support batching)
         lowercase			:str							=  image_processing(image_inputs[0]				,    return_tensors="pt" ).pixel_values
         self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 )
         self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         # Initialize image_processing
         lowercase			:Union[str, Any]							=  self.image_processing_class(**self.image_processor_dict )
         # create random numpy tensors
         lowercase			:Union[str, Any]							=  prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_lowerCAmelCase				,    numpify=_lowerCAmelCase )
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase				,    np.ndarray )

         # Test not batched input (GLPNImageProcessor doesn't support batching)
         lowercase			:Optional[int]							=  image_processing(image_inputs[0]				,    return_tensors="pt" ).pixel_values
         self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 )
         self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )






       def     SCREAMING_SNAKE_CASE			(      self:    Tuple ):
         # Initialize image_processing
         lowercase			:Optional[Any]							=  self.image_processing_class(**self.image_processor_dict )
         # create random PyTorch tensors
         lowercase			:List[str]							=  prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_lowerCAmelCase				,    torchify=_lowerCAmelCase )
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase				,    torch.Tensor )

         # Test not batched input (GLPNImageProcessor doesn't support batching)
         lowercase			:Dict							=  image_processing(image_inputs[0]				,    return_tensors="pt" ).pixel_values
         self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 )
         self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )



