





'''simple docstring'''






from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
      import PIL

A__				:   List[str]      =logging.get_logger(__name__)
def    UpperCamelCase__      (		lowerCAmelCase  ):





      """simple docstring"""


      if isinstance(lowerCAmelCase							,	(list, tuple)  ) and isinstance(videos[0]							,	(list, tuple)  ) and is_valid_image(videos[0][0]  ):
            return videos

      elif isinstance(lowerCAmelCase							,	(list, tuple)  ) and is_valid_image(videos[0]  ):
            return [videos]

      elif is_valid_image(lowerCAmelCase  ):
            return [[videos]]

      raise ValueError(f"Could not make batched video from {videos}"  )





class  UpperCAmelCase     (			snake_case_   ):
      _lowercase:			Any						     =   ['''pixel_values''']




      def __init__(			self			: Tuple			,			__snake_case			: bool = True			,			__snake_case			: Dict[str, int] = None			,			__snake_case			: PILImageResampling = PILImageResampling.BILINEAR			,			__snake_case			: bool = True			,			__snake_case			: Dict[str, int] = None			,			__snake_case			: bool = True			,			__snake_case			: Union[int, float] = 1 / 2_55			,			__snake_case			: bool = True			,			__snake_case			: bool = True			,			__snake_case			: Optional[Union[float, List[float]]] = None			,			__snake_case			: Optional[Union[float, List[float]]] = None			,			**__snake_case			: str			,			)				->      None:
            super().__init__(**__snake_case  )
            _lowerCAmelCase							     =							size if size is not None else {"""shortest_edge""": 2_56}
            _lowerCAmelCase							     =							get_size_dict(__snake_case			,			default_to_square=__snake_case  )
            _lowerCAmelCase							     =							crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24}
            _lowerCAmelCase							     =							get_size_dict(__snake_case			,			param_name="""crop_size"""  )

            _lowerCAmelCase							     =							do_resize
            _lowerCAmelCase							     =							size
            _lowerCAmelCase							     =							do_center_crop
            _lowerCAmelCase							     =							crop_size
            _lowerCAmelCase							     =							resample
            _lowerCAmelCase							     =							do_rescale
            _lowerCAmelCase							     =							rescale_factor
            _lowerCAmelCase							     =							offset
            _lowerCAmelCase							     =							do_normalize
            _lowerCAmelCase							     =							image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
            _lowerCAmelCase							     =							image_std if image_std is not None else IMAGENET_STANDARD_STD




      def 				lowercase__    (			self			: int			,			__snake_case			: np.ndarray			,			__snake_case			: Dict[str, int]			,			__snake_case			: PILImageResampling = PILImageResampling.BILINEAR			,			__snake_case			: Optional[Union[str, ChannelDimension]] = None			,			**__snake_case			: Optional[Any]			,			)				->      np.ndarray:
            _lowerCAmelCase							     =							get_size_dict(__snake_case			,			default_to_square=__snake_case  )
            if "shortest_edge" in size:
                  _lowerCAmelCase							     =							get_resize_output_image_size(__snake_case			,			size["""shortest_edge"""]			,			default_to_square=__snake_case  )
            elif "height" in size and "width" in size:
                  _lowerCAmelCase							     =							(size["""height"""], size["""width"""])
            else:
                  raise ValueError(f"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"  )
            return resize(__snake_case			,			size=__snake_case			,			resample=__snake_case			,			data_format=__snake_case			,			**__snake_case  )




      def 				lowercase__    (			self			: Union[str, Any]			,			__snake_case			: np.ndarray			,			__snake_case			: Dict[str, int]			,			__snake_case			: Optional[Union[str, ChannelDimension]] = None			,			**__snake_case			: List[Any]			,			)				->      np.ndarray:
            _lowerCAmelCase							     =							get_size_dict(__snake_case  )
            if "height" not in size or "width" not in size:
                  raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}"  )
            return center_crop(__snake_case			,			size=(size["""height"""], size["""width"""])			,			data_format=__snake_case			,			**__snake_case  )




      def 				lowercase__    (			self			: Union[str, Any]			,			__snake_case			: np.ndarray			,			__snake_case			: Union[int, float]			,			__snake_case			: bool = True			,			__snake_case			: Optional[Union[str, ChannelDimension]] = None			,			**__snake_case			: Optional[Any]			,			)				->      Dict:
            _lowerCAmelCase							     =							image.astype(np.floataa  )
            if offset:
                  _lowerCAmelCase							     =							image - (scale / 2)
            return rescale(__snake_case			,			scale=__snake_case			,			data_format=__snake_case			,			**__snake_case  )




      def 				lowercase__    (			self			: Optional[int]			,			__snake_case			: np.ndarray			,			__snake_case			: Union[float, List[float]]			,			__snake_case			: Union[float, List[float]]			,			__snake_case			: Optional[Union[str, ChannelDimension]] = None			,			**__snake_case			: Tuple			,			)				->      np.ndarray:
            return normalize(__snake_case			,			mean=__snake_case			,			std=__snake_case			,			data_format=__snake_case			,			**__snake_case  )




      def 				lowercase__    (			self			: List[Any]			,			__snake_case			: ImageInput			,			__snake_case			: bool = None			,			__snake_case			: Dict[str, int] = None			,			__snake_case			: PILImageResampling = None			,			__snake_case			: bool = None			,			__snake_case			: Dict[str, int] = None			,			__snake_case			: bool = None			,			__snake_case			: float = None			,			__snake_case			: bool = None			,			__snake_case			: bool = None			,			__snake_case			: Optional[Union[float, List[float]]] = None			,			__snake_case			: Optional[Union[float, List[float]]] = None			,			__snake_case			: Optional[ChannelDimension] = ChannelDimension.FIRST			,			)				->      np.ndarray:
            if do_resize and size is None or resample is None:
                  raise ValueError("""Size and resample must be specified if do_resize is True."""  )

            if do_center_crop and crop_size is None:
                  raise ValueError("""Crop size must be specified if do_center_crop is True."""  )

            if do_rescale and rescale_factor is None:
                  raise ValueError("""Rescale factor must be specified if do_rescale is True."""  )

            if do_normalize and (image_mean is None or image_std is None):
                  raise ValueError("""Image mean and std must be specified if do_normalize is True."""  )

            if offset and not do_rescale:
                  raise ValueError("""For offset, do_rescale must also be set to True."""  )

            # All transformations expect numpy arrays.
            _lowerCAmelCase							     =							to_numpy_array(__snake_case  )

            if do_resize:
                  _lowerCAmelCase							     =							self.resize(image=__snake_case			,			size=__snake_case			,			resample=__snake_case  )

            if do_center_crop:
                  _lowerCAmelCase							     =							self.center_crop(__snake_case			,			size=__snake_case  )

            if do_rescale:
                  _lowerCAmelCase							     =							self.rescale(image=__snake_case			,			scale=__snake_case			,			offset=__snake_case  )

            if do_normalize:
                  _lowerCAmelCase							     =							self.normalize(image=__snake_case			,			mean=__snake_case			,			std=__snake_case  )

            _lowerCAmelCase							     =							to_channel_dimension_format(__snake_case			,			__snake_case  )
            return image




      def 				lowercase__    (			self			: List[Any]			,			__snake_case			: ImageInput			,			__snake_case			: bool = None			,			__snake_case			: Dict[str, int] = None			,			__snake_case			: PILImageResampling = None			,			__snake_case			: bool = None			,			__snake_case			: Dict[str, int] = None			,			__snake_case			: bool = None			,			__snake_case			: float = None			,			__snake_case			: bool = None			,			__snake_case			: bool = None			,			__snake_case			: Optional[Union[float, List[float]]] = None			,			__snake_case			: Optional[Union[float, List[float]]] = None			,			__snake_case			: Optional[Union[str, TensorType]] = None			,			__snake_case			: ChannelDimension = ChannelDimension.FIRST			,			**__snake_case			: List[str]			,			)				->      PIL.Image.Image:
            _lowerCAmelCase							     =							do_resize if do_resize is not None else self.do_resize
            _lowerCAmelCase							     =							resample if resample is not None else self.resample
            _lowerCAmelCase							     =							do_center_crop if do_center_crop is not None else self.do_center_crop
            _lowerCAmelCase							     =							do_rescale if do_rescale is not None else self.do_rescale
            _lowerCAmelCase							     =							rescale_factor if rescale_factor is not None else self.rescale_factor
            _lowerCAmelCase							     =							offset if offset is not None else self.offset
            _lowerCAmelCase							     =							do_normalize if do_normalize is not None else self.do_normalize
            _lowerCAmelCase							     =							image_mean if image_mean is not None else self.image_mean
            _lowerCAmelCase							     =							image_std if image_std is not None else self.image_std

            _lowerCAmelCase							     =							size if size is not None else self.size
            _lowerCAmelCase							     =							get_size_dict(__snake_case			,			default_to_square=__snake_case  )
            _lowerCAmelCase							     =							crop_size if crop_size is not None else self.crop_size
            _lowerCAmelCase							     =							get_size_dict(__snake_case			,			param_name="""crop_size"""  )

            if not valid_images(__snake_case  ):
                  raise ValueError(
                      """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                      """torch.Tensor, tf.Tensor or jax.ndarray."""  )

            _lowerCAmelCase							     =							make_batched(__snake_case  )

            _lowerCAmelCase							     =							[
                [
                    self._preprocess_image(
                        image=__snake_case			,			do_resize=__snake_case			,			size=__snake_case			,			resample=__snake_case			,			do_center_crop=__snake_case			,			crop_size=__snake_case			,			do_rescale=__snake_case			,			rescale_factor=__snake_case			,			offset=__snake_case			,			do_normalize=__snake_case			,			image_mean=__snake_case			,			image_std=__snake_case			,			data_format=__snake_case			,			)
                    for img in video
                ]
                for video in videos
            ]

            _lowerCAmelCase							     =							{"""pixel_values""": videos}
            return BatchFeature(data=__snake_case			,			tensor_type=__snake_case  )










'''simple docstring'''




from sklearn.metrics import fa_score, matthews_corrcoef

import datasets

from .record_evaluation import evaluate as evaluate_record


__snake_case    						="""\
@article{wang2019superglue,
  title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},
  author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},
  journal={arXiv preprint arXiv:1905.00537},
  year={2019}
}
"""

__snake_case    						="""\
SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after
GLUE with a new set of more difficult language understanding tasks, improved
resources, and a new public leaderboard.
"""

__snake_case    						="""
Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset.
Args:
    predictions: list of predictions to score. Depending on the SuperGlUE subset:
        - for 'record': list of question-answer dictionaries with the following keys:
            - 'idx': index of the question as specified by the dataset
            - 'prediction_text': the predicted answer text
        - for 'multirc': list of question-answer dictionaries with the following keys:
            - 'idx': index of the question-answer pair as specified by the dataset
            - 'prediction': the predicted answer label
        - otherwise: list of predicted labels
    references: list of reference labels. Depending on the SuperGLUE subset:
        - for 'record': list of question-answers dictionaries with the following keys:
            - 'idx': index of the question as specified by the dataset
            - 'answers': list of possible answers
        - otherwise: list of reference labels
Returns: depending on the SuperGLUE subset:
    - for 'record':
        - 'exact_match': Exact match between answer and gold answer
        - 'f1': F1 score
    - for 'multirc':
        - 'exact_match': Exact match between answer and gold answer
        - 'f1_m': Per-question macro-F1 score
        - 'f1_a': Average F1 score over all answers
    - for 'axb':
        'matthews_correlation': Matthew Correlation
    - for 'cb':
        - 'accuracy': Accuracy
        - 'f1': F1 score
    - for all others:
        - 'accuracy': Accuracy
Examples:

    >>> super_glue_metric = datasets.load_metric('super_glue', 'copa')  # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"]
    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}

    >>> super_glue_metric = datasets.load_metric('super_glue', 'cb')
    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0, 'f1': 1.0}

    >>> super_glue_metric = datasets.load_metric('super_glue', 'record')
    >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}]
    >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'exact_match': 1.0, 'f1': 1.0}

    >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc')
    >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0}

    >>> super_glue_metric = datasets.load_metric('super_glue', 'axb')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'matthews_correlation': 1.0}
"""





def 	a_					( lowerCamelCase	:     str	, lowerCamelCase	:     Union[str, Any]				):
   return float((preds == labels).mean()				)





def 	a_					( lowerCamelCase	:     Union[str, Any]	, lowerCamelCase	:     Dict	, lowerCamelCase	:     str="binary"				):
   lowerCAmelCase     =			simple_accuracy(lowerCamelCase	, lowerCamelCase				)
   lowerCAmelCase     =			float(fa_score(y_true=lowerCamelCase	, y_pred=lowerCamelCase	, average=lowerCamelCase				)				)
   return {
       "accuracy": acc,
       "f1": fa,
   }





def 	a_					( lowerCamelCase	:     List[Any]	, lowerCamelCase	:     List[Any]				):
   lowerCAmelCase     =			{}
   for id_pred, label in zip(lowerCamelCase	, lowerCamelCase				):
      lowerCAmelCase     =			f'''{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}'''
      lowerCAmelCase     =			id_pred['prediction']
      if question_id in question_map:
         question_map[question_id].append((pred, label)				)
      else:
         lowerCAmelCase     =			[(pred, label)]
   lowerCAmelCase     ,  lowerCAmelCase     =			[], []
   for question, preds_labels in question_map.items():
      lowerCAmelCase     ,  lowerCAmelCase     =			zip(*lowerCamelCase				)
      lowerCAmelCase     =			fa_score(y_true=lowerCamelCase	, y_pred=lowerCamelCase	, average='macro'				)
      fas.append(lowerCamelCase				)
      lowerCAmelCase     =			int(sum(pred == label for pred, label in preds_labels				) == len(lowerCamelCase				)				)
      ems.append(lowerCamelCase				)
   lowerCAmelCase     =			float(sum(lowerCamelCase				) / len(lowerCamelCase				)				)
   lowerCAmelCase     =			sum(lowerCamelCase				) / len(lowerCamelCase				)
   lowerCAmelCase     =			float(fa_score(y_true=lowerCamelCase	, y_pred=[id_pred['prediction'] for id_pred in ids_preds]				)				)
   return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a}




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





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  List[Any]:
        if self.config_name not in [
            "boolq",
            "cb",
            "copa",
            "multirc",
            "record",
            "rte",
            "wic",
            "wsc",
            "wsc.fixed",
            "axb",
            "axg",
        ]:
           raise KeyError(
               'You should supply a configuration name selected in '
               '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]'    )
        return datasets.MetricInfo(
            description=_DESCRIPTION    ,      citation=_CITATION    ,      inputs_description=_KWARGS_DESCRIPTION    ,      features=datasets.Features(self._get_feature_types()    )    ,      codebase_urls=[]    ,      reference_urls=[]    ,      format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None    ,      )





     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  str:
        if self.config_name == "record":
           return {
               "predictions": {
                   "idx": {
                       "passage": datasets.Value('int64'    ),
                       "query": datasets.Value('int64'    ),
                   },
                   "prediction_text": datasets.Value('string'    ),
               },
               "references": {
                   "idx": {
                       "passage": datasets.Value('int64'    ),
                       "query": datasets.Value('int64'    ),
                   },
                   "answers": datasets.Sequence(datasets.Value('string'    )    ),
               },
           }
        elif self.config_name == "multirc":
           return {
               "predictions": {
                   "idx": {
                       "answer": datasets.Value('int64'    ),
                       "paragraph": datasets.Value('int64'    ),
                       "question": datasets.Value('int64'    ),
                   },
                   "prediction": datasets.Value('int64'    ),
               },
               "references": datasets.Value('int64'    ),
           }
        else:
           return {
               "predictions": datasets.Value('int64'    ),
               "references": datasets.Value('int64'    ),
           }





     def 							__UpperCAmelCase      ( self       :      List[str]    ,      UpperCAmelCase__       :      Any    ,      UpperCAmelCase__       :      Optional[Any]    )				->  Any:
        if self.config_name == "axb":
           return {"matthews_correlation": matthews_corrcoef(UpperCAmelCase__    ,      UpperCAmelCase__    )}
        elif self.config_name == "cb":
           return acc_and_fa(UpperCAmelCase__    ,      UpperCAmelCase__    ,      fa_avg='macro'    )
        elif self.config_name == "record":
           lowerCAmelCase     =			[
               {
                   'qas': [
                       {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]}
                       for ref in references
                   ]
               }
           ]
           lowerCAmelCase     =			{pred['idx']['query']: pred['prediction_text'] for pred in predictions}
           return evaluate_record(UpperCAmelCase__    ,      UpperCAmelCase__    )[0]
        elif self.config_name == "multirc":
           return evaluate_multirc(UpperCAmelCase__    ,      UpperCAmelCase__    )
        elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]:
           return {"accuracy": simple_accuracy(UpperCAmelCase__    ,      UpperCAmelCase__    )}
        else:
           raise KeyError(
               'You should supply a configuration name selected in '
               '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]'    )





