





'''simple docstring'''






import doctest
import glob
import importlib
import inspect
import os
import re
from contextlib import contextmanager
from functools import wraps
from unittest.mock import patch

import numpy as np
import pytest
from absl.testing import parameterized

import datasets
from datasets import load_metric

from .utils import for_all_test_methods, local, slow


# mark all tests as integration
__lowerCamelCase						: Dict		=   pytest.mark.integration


__lowerCamelCase						: int		=   {'comet'}
__lowerCamelCase						: Dict		=   importlib.util.find_spec('fairseq') is not None

__lowerCamelCase						: Tuple		=   {'code_eval'}
__lowerCamelCase						: Optional[Any]		=   os.name == 'nt'

__lowerCamelCase						: Union[str, Any]		=   {'bertscore', 'frugalscore', 'perplexity'}
__lowerCamelCase						: Union[str, Any]		=   importlib.util.find_spec('transformers') is not None


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  @wraps(__A   )
  def wrapper(self  ,      __SCREAMING_SNAKE_CASE   ):
    if not _has_fairseq and metric_name in REQUIRE_FAIRSEQ:
      self.skipTest('''\"test requires Fairseq\"'''   )
    else:
      test_case(self  ,      __A   )

  return wrapper


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  @wraps(__A   )
  def wrapper(self  ,      __SCREAMING_SNAKE_CASE   ):
    if not _has_transformers and metric_name in REQUIRE_TRANSFORMERS:
      self.skipTest('''\"test requires transformers\"'''   )
    else:
      test_case(self  ,      __A   )

  return wrapper


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  @wraps(__A   )
  def wrapper(self  ,      __SCREAMING_SNAKE_CASE   ):
    if _on_windows and metric_name in UNSUPPORTED_ON_WINDOWS:
      self.skipTest('''\"test not supported on Windows\"'''   )
    else:
      test_case(self  ,      __A   )

  return wrapper


def 					_a   ():


  """simple docstring"""

  _UpperCamelCase       			=[metric_dir.split(os.sep   )[-2] for metric_dir in glob.glob('''./metrics/*/'''   )]
  return [{"testcase_name": x, "metric_name": x} for x in metrics if x != "gleu"]  # gleu is unfinished







@parameterized.named_parameters(get_local_metric_names())
@for_all_test_methods(
    _snake_case   ,     _snake_case   ,     _snake_case)
@local
class 		UpperCAmelCase   (				parameterized.TestCase):


  """simple docstring"""







  lowerCAmelCase_					=				{}
  lowerCAmelCase_					=				None


  @pytest.mark.filterwarnings('''ignore:metric_module_factory is deprecated:FutureWarning'''  )
  @pytest.mark.filterwarnings('''ignore:load_metric is deprecated:FutureWarning'''  )
  def   UpperCamelCase__      (				self    :					List[str]							,	UpperCamelCase__    :					Tuple  )     ->						Dict:
    _UpperCamelCase       			='''[...]'''
    _UpperCamelCase       			=importlib.import_module(
        datasets.load.metric_module_factory(os.path.join('''metrics'''							,	lowerCAmelCase__  )  ).module_path  )
    _UpperCamelCase       			=datasets.load.import_main_class(metric_module.__name__							,	dataset=lowerCAmelCase__  )
    # check parameters
    _UpperCamelCase       			=inspect.signature(metric._compute  ).parameters
    self.assertTrue(all(p.kind != p.VAR_KEYWORD for p in parameters.values()  )  )  # no **kwargs
    # run doctest
    with self.patch_intensive_calls(lowerCAmelCase__							,	metric_module.__name__  ):
      with self.use_local_metrics():
        try:
          _UpperCamelCase       			=doctest.testmod(lowerCAmelCase__							,	verbose=lowerCAmelCase__							,	raise_on_error=lowerCAmelCase__  )
        except doctest.UnexpectedException as e:
          raise e.exc_info[1]  # raise the exception that doctest caught
    self.assertEqual(results.failed							,	0  )
    self.assertGreater(results.attempted							,	1  )


  @slow
  def   UpperCamelCase__      (				self    :					str							,	UpperCamelCase__    :					Optional[int]  )     ->						str:
    _UpperCamelCase       			='''[...]'''
    _UpperCamelCase       			=importlib.import_module(
        datasets.load.metric_module_factory(os.path.join('''metrics'''							,	lowerCAmelCase__  )  ).module_path  )
    # run doctest
    with self.use_local_metrics():
      _UpperCamelCase       			=doctest.testmod(lowerCAmelCase__							,	verbose=lowerCAmelCase__							,	raise_on_error=lowerCAmelCase__  )
    self.assertEqual(results.failed							,	0  )
    self.assertGreater(results.attempted							,	1  )


  @contextmanager
  def   UpperCamelCase__      (				self    :					Optional[int]							,	UpperCamelCase__    :					Union[str, Any]							,	UpperCamelCase__    :					Optional[Any]  )     ->						Dict:
    if metric_name in self.INTENSIVE_CALLS_PATCHER:
      with self.INTENSIVE_CALLS_PATCHER[metric_name](lowerCAmelCase__  ):
        yield
    else:
      yield


  @contextmanager
  def   UpperCamelCase__      (				self    :					List[str]  )     ->						int:
    def load_local_metric(UpperCamelCase__    :					Tuple							,	*UpperCamelCase__    :					str							,	**UpperCamelCase__    :					Dict  ):
      return load_metric(os.path.join('''metrics'''							,	lowerCAmelCase__  )							,	*lowerCAmelCase__							,	**lowerCAmelCase__  )

    with patch('''datasets.load_metric'''  ) as mock_load_metric:
      _UpperCamelCase       			=load_local_metric
      yield




  @classmethod
  def   UpperCamelCase__      (				cls    :					List[Any]							,	UpperCamelCase__    :					Any  )     ->						str:
    def wrapper(UpperCamelCase__    :					List[Any]  ):
      _UpperCamelCase       			=contextmanager(lowerCAmelCase__  )
      _UpperCamelCase       			=patcher
      return patcher

    return wrapper




@LocalMetricTest.register_intensive_calls_patcher('''bleurt'''   )
def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  import tensorflow.compat.va as tf
  from bleurt.score import Predictor

  tf.flags.DEFINE_string('''sv'''  ,      ''''''  ,      ''''''   )  # handle pytest cli flags







  class 		UpperCAmelCase   (				_snake_case):


    """simple docstring"""







    def   UpperCamelCase__      (				self    :					Tuple							,	UpperCamelCase__    :					Any  )     ->						int:
      assert len(input_dict['''input_ids''']  ) == 2
      return np.array([1.03, 1.04]  )

     # mock predict_fn which is supposed to do a forward pass with a bleurt model
  with patch('''bleurt.score._create_predictor'''   ) as mock_create_predictor:
    _UpperCamelCase       			=MockedPredictor()
    yield


@LocalMetricTest.register_intensive_calls_patcher('''bertscore'''   )
def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  import torch

  def bert_cos_score_idf(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      *__SCREAMING_SNAKE_CASE  ,      **__SCREAMING_SNAKE_CASE   ):
    return torch.tensor([[1.0, 1.0, 1.0]] * len(__A   )   )

  # mock get_model which is supposed to do download a bert model
  # mock bert_cos_score_idf which is supposed to do a forward pass with a bert model
  with patch('''bert_score.scorer.get_model'''   ), patch(
      '''bert_score.scorer.bert_cos_score_idf'''   ) as mock_bert_cos_score_idf:
    _UpperCamelCase       			=bert_cos_score_idf
    yield


@LocalMetricTest.register_intensive_calls_patcher('''comet'''   )
def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  def load_from_checkpoint(__SCREAMING_SNAKE_CASE   ):







    class 		UpperCAmelCase   :


      """simple docstring"""







      def   UpperCamelCase__      (				self    :					Optional[Any]							,	UpperCamelCase__    :					Optional[int]							,	*UpperCamelCase__    :					Dict							,	**UpperCamelCase__    :					str  )     ->						Dict:
        assert len(lowerCAmelCase__  ) == 2
        _UpperCamelCase       			=[0.19, 0.92]
        return scores, sum(lowerCAmelCase__  ) / len(lowerCAmelCase__  )




    return Model()

  # mock load_from_checkpoint which is supposed to do download a bert model
  # mock load_from_checkpoint which is supposed to do download a bert model
  with patch('''comet.download_model'''   ) as mock_download_model:
    _UpperCamelCase       			=None
    with patch('''comet.load_from_checkpoint'''   ) as mock_load_from_checkpoint:
      _UpperCamelCase       			=load_from_checkpoint
      yield


def 					_a   ():


  """simple docstring"""

  _UpperCamelCase       			=load_metric(os.path.join('''metrics'''  ,      '''seqeval'''   )   )
  _UpperCamelCase       			='''ERROR'''
  _UpperCamelCase       			=f'''Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {wrong_scheme}'''
  with pytest.raises(__A  ,      match=re.escape(__A   )   ):
    metric.compute(predictions=[]  ,      references=[]  ,      scheme=__A   )













'''simple docstring'''






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 PoolFormerImageProcessor







class 		UpperCAmelCase   (				unittest.TestCase):


  """simple docstring"""







  def __init__(				self    :					Optional[int]							,	UpperCamelCase__    :					Union[str, Any]							,	UpperCamelCase__    :					Any=7							,	UpperCamelCase__    :					List[Any]=3							,	UpperCamelCase__    :					Dict=30							,	UpperCamelCase__    :					Union[str, Any]=400							,	UpperCamelCase__    :					List[Any]=True							,	UpperCamelCase__    :					List[str]=None							,	UpperCamelCase__    :					List[str]=0.9							,	UpperCamelCase__    :					Any=None							,	UpperCamelCase__    :					Any=True							,	UpperCamelCase__    :					Optional[Any]=[0.5, 0.5, 0.5]							,	UpperCamelCase__    :					List[str]=[0.5, 0.5, 0.5]							,	)     ->						Dict:
    _UpperCamelCase       			=size if size is not None else {'''shortest_edge''': 30}
    _UpperCamelCase       			=crop_size if crop_size is not None else {'''height''': 30, '''width''': 30}
    _UpperCamelCase       			=parent
    _UpperCamelCase       			=batch_size
    _UpperCamelCase       			=num_channels
    _UpperCamelCase       			=min_resolution
    _UpperCamelCase       			=max_resolution
    _UpperCamelCase       			=do_resize_and_center_crop
    _UpperCamelCase       			=size
    _UpperCamelCase       			=crop_pct
    _UpperCamelCase       			=crop_size
    _UpperCamelCase       			=do_normalize
    _UpperCamelCase       			=image_mean
    _UpperCamelCase       			=image_std




  def   UpperCamelCase__      (				self    :					str  )     ->						Union[str, Any]:
    return {
        "size": self.size,
        "do_resize_and_center_crop": self.do_resize_and_center_crop,
        "crop_pct": self.crop_pct,
        "crop_size": self.crop_size,
        "do_normalize": self.do_normalize,
        "image_mean": self.image_mean,
        "image_std": self.image_std,
    }







@require_torch
@require_vision
class 		UpperCAmelCase   (				lowercase_   ,     unittest.TestCase):


  """simple docstring"""







  lowerCAmelCase_					=				PoolFormerImageProcessor if is_vision_available() else None


  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						int:
    _UpperCamelCase       			=PoolFormerImageProcessingTester(self  )


  @property
  def   UpperCamelCase__      (				self    :					Tuple  )     ->						Optional[int]:
    return self.image_processor_tester.prepare_image_processor_dict()


  def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						int:
    _UpperCamelCase       			=self.image_processing_class(**self.image_processor_dict  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''do_resize_and_center_crop'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''size'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''crop_pct'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''do_normalize'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''image_mean'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''image_std'''  )  )


  def   UpperCamelCase__      (				self    :					Any  )     ->						Dict:
    _UpperCamelCase       			=self.image_processing_class.from_dict(self.image_processor_dict  )
    self.assertEqual(image_processor.size							,	{'''shortest_edge''': 30}  )
    self.assertEqual(image_processor.crop_size							,	{'''height''': 30, '''width''': 30}  )

    _UpperCamelCase       			=self.image_processing_class.from_dict(self.image_processor_dict							,	size=42							,	crop_size=84  )
    self.assertEqual(image_processor.size							,	{'''shortest_edge''': 42}  )
    self.assertEqual(image_processor.crop_size							,	{'''height''': 84, '''width''': 84}  )


  def   UpperCamelCase__      (				self    :					Dict  )     ->						Optional[Any]:
    pass


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

    # Test not batched input
    _UpperCamelCase       			=image_processing(image_inputs[0]							,	return_tensors='''pt'''  ).pixel_values

    self.assertEqual(
        encoded_images.shape							,	(
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)

    # Test batched
    _UpperCamelCase       			=image_processing(UpperCamelCase__							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)


  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						Optional[int]:
    # Initialize image_processing
    _UpperCamelCase       			=self.image_processing_class(**self.image_processor_dict  )
    # create random numpy tensors
    _UpperCamelCase       			=prepare_image_inputs(self.image_processor_tester							,	equal_resolution=UpperCamelCase__							,	numpify=UpperCamelCase__  )
    for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__							,	np.ndarray  )

    # Test not batched input
    _UpperCamelCase       			=image_processing(image_inputs[0]							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)

    # Test batched
    _UpperCamelCase       			=image_processing(UpperCamelCase__							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)




  def   UpperCamelCase__      (				self    :					str  )     ->						List[Any]:
    # Initialize image_processing
    _UpperCamelCase       			=self.image_processing_class(**self.image_processor_dict  )
    # create random PyTorch tensors
    _UpperCamelCase       			=prepare_image_inputs(self.image_processor_tester							,	equal_resolution=UpperCamelCase__							,	torchify=UpperCamelCase__  )
    for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__							,	torch.Tensor  )

    # Test not batched input
    _UpperCamelCase       			=image_processing(image_inputs[0]							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)

    # Test batched
    _UpperCamelCase       			=image_processing(UpperCamelCase__							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)






